config.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. ########################################################################
  2. # Searx-qt - Lightweight desktop application for SearX.
  3. # Copyright (C) 2020 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. from searxqt.core.handler import NetworkTypes
  22. from searxqt.core.instanceVersions import (
  23. parseVersionString,
  24. InstanceVersionGtEqCmp,
  25. InstanceVersionTypes
  26. )
  27. def upgradeProfileConfig(profileSettings):
  28. """
  29. @param profileSettings: Profile settings to possible upgrade.
  30. @type profileSettings: QSettings
  31. """
  32. # Update from 0.2 to 0.3 (0.2 doesn't have the version defined in the
  33. # config)
  34. confVersion = profileSettings.value('version', "", str)
  35. if not confVersion:
  36. upgrade_from_0_2_to_0_3(profileSettings)
  37. return profileSettings
  38. def upgrade_from_0_2_to_0_3(profileSettings):
  39. """ Migration from version 0.2 to 0.3
  40. """
  41. """ Instance filter changes
  42. """
  43. instanceFilterConf = profileSettings.value('instanceFilter', dict(), dict)
  44. if instanceFilterConf:
  45. """ Version filter changes
  46. 'instanceFilter' 'version' changed from a list with version strings to:
  47. {
  48. 'min': '',
  49. 'git': True,
  50. 'unknown': False
  51. }
  52. """
  53. # Get old minimum set version (if set)
  54. oldVersionFilter = instanceFilterConf.get('version', [])
  55. newMinString = ''
  56. if oldVersionFilter:
  57. oldLowestVersion = parseVersionString('100.0.0')
  58. for oldVersion in oldVersionFilter:
  59. versionObj = parseVersionString(oldVersion)
  60. if versionObj.type == InstanceVersionTypes.No:
  61. continue
  62. if InstanceVersionGtEqCmp(oldLowestVersion, versionObj):
  63. oldLowestVersion = versionObj
  64. newMinString = str(oldLowestVersion)
  65. # Set new config
  66. instanceFilterConf.update({
  67. 'version': {
  68. 'min': newMinString,
  69. 'git': True,
  70. 'unknown': False
  71. }
  72. })
  73. """ Blacklist structure changes
  74. From a list with url's to a dict with as key the url and as value a
  75. tuple with as first value the date (uint) and second value the reason
  76. (str).
  77. """
  78. oldBlacklist = instanceFilterConf.get('skipUrl', [])
  79. newBlacklist = {}
  80. for instanceUrl in oldBlacklist:
  81. newBlacklist.update({instanceUrl: (0, '')})
  82. instanceFilterConf.update({'skipUrl': newBlacklist})
  83. """ Network filter changes
  84. """
  85. web = False
  86. tor = False
  87. i2p = False
  88. skipExtList = instanceFilterConf.get('skipExt', [])
  89. extList = instanceFilterConf.get('ext', [])
  90. # Nothing is checked
  91. if not extList and not skipExtList:
  92. web = True
  93. tor = True
  94. i2p = True
  95. # Only web checked
  96. elif 'onion' in skipExtList and 'i2p' in skipExtList:
  97. web = True
  98. # Web + Tor
  99. elif 'i2p' in skipExtList and not extList:
  100. web = True
  101. tor = True
  102. # Web + i2p
  103. elif 'onion' in skipExtList and not extList:
  104. web = True
  105. i2p = True
  106. # Tor + i2p
  107. elif 'onion' in extList and 'i2p' in extList:
  108. tor = True
  109. i2p = True
  110. # Tor
  111. elif 'onion' in extList:
  112. tor = True
  113. # i2p
  114. elif 'i2p' in extList:
  115. i2p = True
  116. # Remove old keys
  117. if 'skipExt' in instanceFilterConf:
  118. del instanceFilterConf['skipExt']
  119. if 'ext' in instanceFilterConf:
  120. del instanceFilterConf['ext']
  121. newValue = []
  122. if web:
  123. newValue.append(NetworkTypes.Web)
  124. if tor:
  125. newValue.append(NetworkTypes.Tor)
  126. if i2p:
  127. newValue.append(NetworkTypes.I2P)
  128. # Set new config
  129. instanceFilterConf.update({
  130. 'networkTypes': newValue
  131. })
  132. profileSettings.setValue('instanceFilter', instanceFilterConf)
  133. """ Instances data
  134. """
  135. handlerData = profileSettings.value('data', dict(), dict)
  136. instancesData = handlerData.get('instances', {})
  137. if instancesData:
  138. """ Network type changes
  139. """
  140. for url, instanceData in instancesData.items():
  141. instanceData['network_type'] = NetworkTypes.netTypeFromUrl(url)
  142. profileSettings.setValue('data', handlerData)
  143. profileSettings.setValue('version', "0.3")