__init__.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. '''
  2. searx is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU Affero General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or
  5. (at your option) any later version.
  6. searx is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU Affero General Public License for more details.
  10. You should have received a copy of the GNU Affero General Public License
  11. along with searx. If not, see < http://www.gnu.org/licenses/ >.
  12. (C) 2013- by Adam Tauber, <asciimoo@gmail.com>
  13. '''
  14. import logging
  15. import searx.settings_loader
  16. from os import environ
  17. from os.path import realpath, dirname, join, abspath, isfile
  18. from sys import exit
  19. from searx.exceptions import SearxSettingsException
  20. searx_dir = abspath(dirname(__file__))
  21. engine_dir = dirname(realpath(__file__))
  22. static_path = abspath(join(dirname(__file__), 'static'))
  23. settings, settings_outgoing = {}, ''
  24. try:
  25. settings, settings_load_message = searx.settings_loader.load_settings()
  26. except SearxSettingsException as e:
  27. logger = logging.getLogger('searx')
  28. logger.error('Failed to load settings file: {}'.format(str(e)))
  29. exit(1)
  30. if settings['ui']['static_path']:
  31. static_path = settings['ui']['static_path']
  32. '''
  33. enable debug if
  34. the environment variable SEARX_DEBUG is 1 or true
  35. (whatever the value in settings.yml)
  36. or general.debug=True in settings.yml
  37. disable debug if
  38. the environment variable SEARX_DEBUG is 0 or false
  39. (whatever the value in settings.yml)
  40. or general.debug=False in settings.yml
  41. '''
  42. searx_debug_env = environ.get('SEARX_DEBUG', '').lower()
  43. if searx_debug_env == 'true' or searx_debug_env == '1':
  44. searx_debug = True
  45. elif searx_debug_env == 'false' or searx_debug_env == '0':
  46. searx_debug = False
  47. else:
  48. searx_debug = settings.get('general', {}).get('debug')
  49. if searx_debug:
  50. logging.basicConfig(level=logging.DEBUG)
  51. else:
  52. logging.basicConfig(level=logging.WARNING)
  53. logger = logging.getLogger('searx')
  54. logger.info(settings_load_message)
  55. logger.info('Initialisation done')
  56. if 'SEARX_SECRET' in environ:
  57. settings['server']['secret_key'] = environ['SEARX_SECRET']
  58. if 'SEARX_BIND_ADDRESS' in environ:
  59. settings['server']['bind_address'] = environ['SEARX_BIND_ADDRESS']
  60. class _brand_namespace:
  61. @classmethod
  62. def get_val(cls, group, name, default=''):
  63. return settings.get(group, {}).get(name) or default
  64. @property
  65. def SEARX_URL(self):
  66. return self.get_val('server', 'base_url')
  67. @property
  68. def CONTACT_URL(self):
  69. return self.get_val('general', 'contact_url')
  70. @property
  71. def GIT_URL(self):
  72. return self.get_val('brand', 'git_url')
  73. @property
  74. def GIT_BRANCH(self):
  75. return self.get_val('brand', 'git_branch')
  76. @property
  77. def ISSUE_URL(self):
  78. return self.get_val('brand', 'issue_url')
  79. @property
  80. def DOCS_URL(self):
  81. return self.get_val('brand', 'docs_url')
  82. @property
  83. def PUBLIC_INSTANCES(self):
  84. return self.get_val('brand', 'public_instances')
  85. @property
  86. def WIKI_URL(self):
  87. return self.get_val('brand', 'wiki_url')
  88. @property
  89. def TWITTER_URL(self):
  90. return self.get_val('brand', 'twitter_url')
  91. brand = _brand_namespace()