settings.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. ########################################################################
  2. # Searx-Qt - Lightweight desktop application for Searx.
  3. # Copyright (C) 2020-2022 CYBERDEViL
  4. #
  5. # This file is part of Searx-Qt.
  6. #
  7. # Searx-Qt is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # Searx-Qt is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. #
  20. ########################################################################
  21. import random
  22. from PyQt5.QtCore import QObject, pyqtSignal
  23. from searxqt.themes import Themes
  24. from searxqt.core.instances import Stats2
  25. from searxqt.core import log
  26. class General:
  27. def serialize():
  28. return {
  29. 'logLevel': log.LogLevel,
  30. 'themes': Themes.serialize()
  31. }
  32. def deserialize(data):
  33. # Populate / re-populate user and system themes
  34. Themes.populate()
  35. themesData = data.get('themes', {})
  36. if themesData:
  37. Themes.deserialize(themesData)
  38. # Log level
  39. logLevel = data.get('logLevel', None)
  40. if logLevel is not None:
  41. log.LogLevel = logLevel
  42. else:
  43. log.LogLevel = log.LogLevels.QUIET
  44. class SettingsModel(QObject):
  45. changed = pyqtSignal()
  46. def __init__(self, parent=None):
  47. QObject.__init__(self, parent=parent)
  48. self._requests = RequestSettingsModel()
  49. self._stats2 = None
  50. self._requests.changed.connect(self.changed)
  51. def loadSettings(self, data, stats2=True):
  52. self.requests.data = data.get('requests', {})
  53. if self._stats2:
  54. self._stats2.deleteLater()
  55. self._stats2 = None
  56. if stats2:
  57. self._stats2 = SearxStats2Model()
  58. stats2Data = data.get('stats2', {})
  59. self.stats2.loadSettings(stats2Data)
  60. General.deserialize(data.get('general', {}))
  61. def saveSettings(self):
  62. if self._stats2:
  63. return {
  64. 'general': General.serialize(),
  65. 'requests': self.requests.data,
  66. 'stats2': self.stats2.saveSettings()
  67. }
  68. return {
  69. 'general': General.serialize(),
  70. 'requests': self.requests.data,
  71. }
  72. @property
  73. def requests(self): return self._requests
  74. @property
  75. def stats2(self): return self._stats2
  76. class SearxStats2Model(QObject):
  77. changed = pyqtSignal()
  78. def __init__(self, parent=None):
  79. QObject.__init__(self, parent=parent)
  80. self._backupUrl = Stats2.URL
  81. def loadSettings(self, data):
  82. self.url = data.get('url', self._backupUrl)
  83. def saveSettings(self):
  84. return {
  85. 'url': self.url
  86. }
  87. def reset(self):
  88. self.url = self._backupUrl
  89. @property
  90. def url(self):
  91. return Stats2.URL
  92. @url.setter
  93. def url(self, url):
  94. Stats2.URL = url
  95. self.changed.emit()
  96. class RequestSettingsModel(QObject):
  97. changed = pyqtSignal()
  98. def __init__(self, parent=None):
  99. QObject.__init__(self, parent=parent)
  100. self._kwargs = {
  101. 'verify': True,
  102. 'timeout': 10,
  103. }
  104. self._randomUserAgent = True
  105. self._useragents = ['searx-qt']
  106. def __changed(self): self.changed.emit()
  107. @property
  108. def data(self):
  109. return {
  110. 'useragents': self._useragents,
  111. 'randomUserAgent': self._randomUserAgent,
  112. 'kwargs': self._kwargs
  113. }
  114. @data.setter
  115. def data(self, data):
  116. self._useragents = data.get('useragents', ['searx-qt'])
  117. self._randomUserAgent = data.get('randomUserAgent', True)
  118. kwargs = data.get('kwargs', {})
  119. self._kwargs.update({'verify': kwargs.get('verify', True)})
  120. self._kwargs.update({'timeout': kwargs.get('timeout', 10)})
  121. self._kwargs.update({'proxies': kwargs.get('proxies', {})})
  122. @property
  123. def kwargs(self):
  124. """ Returns kwargs to be passed to requests.get
  125. """
  126. kwargs = self._kwargs.copy()
  127. kwargs.update({'headers': self.headers})
  128. return kwargs
  129. @kwargs.setter
  130. def kwargs(self, kwargs):
  131. self._kwargs = kwargs
  132. @property
  133. def verify(self): return self._kwargs['verify']
  134. @verify.setter
  135. def verify(self, state):
  136. self._kwargs['verify'] = state
  137. self.__changed()
  138. @property
  139. def timeout(self): return self._kwargs['timeout']
  140. @timeout.setter
  141. def timeout(self, state):
  142. self._kwargs['timeout'] = state
  143. self.__changed()
  144. @property
  145. def proxies(self): return self._kwargs.get('proxies', {})
  146. @proxies.setter
  147. def proxies(self, data):
  148. self._kwargs.update({'proxies': data})
  149. self.__changed()
  150. @property
  151. def headers(self):
  152. headers = {}
  153. if self.useragent:
  154. headers.update({'user-agent': self.useragent})
  155. return headers
  156. @property
  157. def useragent(self):
  158. if not self._useragents:
  159. return ""
  160. if len(self._useragents) == 1 or not self._randomUserAgent:
  161. return self._useragents[0]
  162. return random.choice(self._useragents)
  163. @property
  164. def useragents(self):
  165. return self._useragents
  166. @useragents.setter
  167. def useragents(self, strList):
  168. """
  169. @param strList: A string with user-agents, separated by a new line.
  170. May be empty.
  171. @type strList: str
  172. """
  173. self._useragents = strList
  174. self.__changed()
  175. @property
  176. def randomUserAgent(self): return self._randomUserAgent
  177. @randomUserAgent.setter
  178. def randomUserAgent(self, state):
  179. """
  180. @param state: True to select random user-agent for each request
  181. If False it will select the first user-agent. When
  182. the user-agents list is empty the send string will
  183. be also empty.
  184. @type state: bool
  185. """
  186. self._randomUserAgent = state
  187. self.__changed()