models.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # pylint: disable=missing-module-docstring
  3. import typing
  4. import babel
  5. class EngineRef:
  6. """Reference by names to an engine and category"""
  7. __slots__ = 'name', 'category'
  8. def __init__(self, name: str, category: str):
  9. self.name = name
  10. self.category = category
  11. def __repr__(self):
  12. return "EngineRef({!r}, {!r})".format(self.name, self.category)
  13. def __eq__(self, other):
  14. return self.name == other.name and self.category == other.category
  15. def __hash__(self):
  16. return hash((self.name, self.category))
  17. class SearchQuery:
  18. """container for all the search parameters (query, language, etc...)"""
  19. __slots__ = (
  20. 'query',
  21. 'engineref_list',
  22. 'lang',
  23. 'locale',
  24. 'safesearch',
  25. 'pageno',
  26. 'time_range',
  27. 'timeout_limit',
  28. 'external_bang',
  29. 'engine_data',
  30. 'redirect_to_first_result',
  31. )
  32. def __init__(
  33. self,
  34. query: str,
  35. engineref_list: typing.List[EngineRef],
  36. lang: str = 'all',
  37. safesearch: int = 0,
  38. pageno: int = 1,
  39. time_range: typing.Optional[str] = None,
  40. timeout_limit: typing.Optional[float] = None,
  41. external_bang: typing.Optional[str] = None,
  42. engine_data: typing.Optional[typing.Dict[str, str]] = None,
  43. redirect_to_first_result: typing.Optional[bool] = None,
  44. ): # pylint:disable=too-many-arguments
  45. self.query = query
  46. self.engineref_list = engineref_list
  47. self.lang = lang
  48. self.safesearch = safesearch
  49. self.pageno = pageno
  50. self.time_range = time_range
  51. self.timeout_limit = timeout_limit
  52. self.external_bang = external_bang
  53. self.engine_data = engine_data or {}
  54. self.redirect_to_first_result = redirect_to_first_result
  55. self.locale = None
  56. if self.lang:
  57. try:
  58. self.locale = babel.Locale.parse(self.lang, sep='-')
  59. except babel.core.UnknownLocaleError:
  60. pass
  61. @property
  62. def categories(self):
  63. return list(set(map(lambda engineref: engineref.category, self.engineref_list)))
  64. def __repr__(self):
  65. return "SearchQuery({!r}, {!r}, {!r}, {!r}, {!r}, {!r}, {!r}, {!r}, {!r})".format(
  66. self.query,
  67. self.engineref_list,
  68. self.lang,
  69. self.safesearch,
  70. self.pageno,
  71. self.time_range,
  72. self.timeout_limit,
  73. self.external_bang,
  74. self.redirect_to_first_result,
  75. )
  76. def __eq__(self, other):
  77. return (
  78. self.query == other.query
  79. and self.engineref_list == other.engineref_list
  80. and self.lang == other.lang
  81. and self.safesearch == other.safesearch
  82. and self.pageno == other.pageno
  83. and self.time_range == other.time_range
  84. and self.timeout_limit == other.timeout_limit
  85. and self.external_bang == other.external_bang
  86. and self.redirect_to_first_result == other.redirect_to_first_result
  87. )
  88. def __hash__(self):
  89. return hash(
  90. (
  91. self.query,
  92. tuple(self.engineref_list),
  93. self.lang,
  94. self.safesearch,
  95. self.pageno,
  96. self.time_range,
  97. self.timeout_limit,
  98. self.external_bang,
  99. self.redirect_to_first_result,
  100. )
  101. )
  102. def __copy__(self):
  103. return SearchQuery(
  104. self.query,
  105. self.engineref_list,
  106. self.lang,
  107. self.safesearch,
  108. self.pageno,
  109. self.time_range,
  110. self.timeout_limit,
  111. self.external_bang,
  112. self.engine_data,
  113. self.redirect_to_first_result,
  114. )