__init__.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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
  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. # if possible set path to settings using the
  28. # enviroment variable SEARX_SETTINGS_PATH
  29. if 'SEARX_SETTINGS_PATH' in environ:
  30. settings_path = environ['SEARX_SETTINGS_PATH']
  31. # otherwise using default path
  32. else:
  33. settings_path = join(searx_dir, 'settings.yml')
  34. # load settings
  35. with open(settings_path) as settings_yaml:
  36. settings = load(settings_yaml)
  37. '''
  38. enable debug if
  39. the environnement variable SEARX_DEBUG is 1 or true
  40. (whatever the value in settings.yml)
  41. or general.debug=True in settings.yml
  42. disable debug if
  43. the environnement variable SEARX_DEBUG is 0 or false
  44. (whatever the value in settings.yml)
  45. or general.debug=False in settings.yml
  46. '''
  47. searx_debug_env = environ.get('SEARX_DEBUG', '').lower()
  48. if searx_debug_env == 'true' or searx_debug_env == '1':
  49. searx_debug = True
  50. elif searx_debug_env == 'false' or searx_debug_env == '0':
  51. searx_debug = False
  52. else:
  53. searx_debug = settings.get('general', {}).get('debug')
  54. if searx_debug:
  55. logging.basicConfig(level=logging.DEBUG)
  56. else:
  57. logging.basicConfig(level=logging.WARNING)
  58. logger = logging.getLogger('searx')
  59. # Workaround for openssl versions <1.0.2
  60. # https://github.com/certifi/python-certifi/issues/26
  61. if OPENSSL_VERSION_INFO[0:3] < (1, 0, 2):
  62. if hasattr(certifi, 'old_where'):
  63. environ['REQUESTS_CA_BUNDLE'] = certifi.old_where()
  64. logger.warning('You are using an old openssl version({0}), please upgrade above 1.0.2!'.format(OPENSSL_VERSION))
  65. logger.info('Initialisation done')