__init__.py 2.9 KB

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