__init__.py 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 certifi
  15. import logging
  16. from os import environ
  17. from os.path import realpath, dirname, join, abspath, isfile
  18. from io import open
  19. from ssl import OPENSSL_VERSION_INFO, OPENSSL_VERSION
  20. try:
  21. from yaml import load
  22. except:
  23. from sys import exit, stderr
  24. stderr.write('[E] install pyyaml\n')
  25. exit(2)
  26. searx_dir = abspath(dirname(__file__))
  27. engine_dir = dirname(realpath(__file__))
  28. def check_settings_yml(file_name):
  29. if isfile(file_name):
  30. return file_name
  31. else:
  32. return None
  33. # find location of settings.yml
  34. if 'SEARX_SETTINGS_PATH' in environ:
  35. # if possible set path to settings using the
  36. # enviroment variable SEARX_SETTINGS_PATH
  37. settings_path = check_settings_yml(environ['SEARX_SETTINGS_PATH'])
  38. else:
  39. # if not, get it from searx code base or last solution from /etc/searx
  40. settings_path = check_settings_yml(join(searx_dir, 'settings.yml')) or check_settings_yml('/etc/searx/settings.yml')
  41. if not settings_path:
  42. raise Exception('settings.yml not found')
  43. # load settings
  44. with open(settings_path, 'r', encoding='utf-8') as settings_yaml:
  45. settings = load(settings_yaml)
  46. '''
  47. enable debug if
  48. the environnement variable SEARX_DEBUG is 1 or true
  49. (whatever the value in settings.yml)
  50. or general.debug=True in settings.yml
  51. disable debug if
  52. the environnement variable SEARX_DEBUG is 0 or false
  53. (whatever the value in settings.yml)
  54. or general.debug=False in settings.yml
  55. '''
  56. searx_debug_env = environ.get('SEARX_DEBUG', '').lower()
  57. if searx_debug_env == 'true' or searx_debug_env == '1':
  58. searx_debug = True
  59. elif searx_debug_env == 'false' or searx_debug_env == '0':
  60. searx_debug = False
  61. else:
  62. searx_debug = settings.get('general', {}).get('debug')
  63. if searx_debug:
  64. logging.basicConfig(level=logging.DEBUG)
  65. else:
  66. logging.basicConfig(level=logging.WARNING)
  67. logger = logging.getLogger('searx')
  68. logger.debug('read configuration from %s', settings_path)
  69. # Workaround for openssl versions <1.0.2
  70. # https://github.com/certifi/python-certifi/issues/26
  71. if OPENSSL_VERSION_INFO[0:3] < (1, 0, 2):
  72. if hasattr(certifi, 'old_where'):
  73. environ['REQUESTS_CA_BUNDLE'] = certifi.old_where()
  74. logger.warning('You are using an old openssl version({0}), please upgrade above 1.0.2!'.format(OPENSSL_VERSION))
  75. logger.info('Initialisation done')
  76. if 'SEARX_SECRET' in environ:
  77. settings['server']['secret_key'] = environ['SEARX_SECRET']