__init__.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import os
  2. import sysconfig # Get path prefix (example: /usr).
  3. from searxqt import SCHEMA_PATH
  4. from searxqt.core.requests import json_schema_load
  5. """ Pre-load json schema objects """
  6. # Get/find path to schema json files.
  7. # Priority:
  8. # 1. Pwd (./searxqt/schema/)
  9. # 2. User (example: ~/.local/share/)
  10. # 3. Sys (example: /usr/share/)
  11. def find_schema_path(schemas):
  12. def __path_has_schemas(path):
  13. for key, filename in schemas:
  14. full_file_path = os.path.join(path, filename)
  15. if (not os.path.isfile(full_file_path) or
  16. not os.access(full_file_path, os.R_OK)):
  17. return None
  18. return path
  19. def __get_config_var_schema_path(var):
  20. path = os.path.join(
  21. sysconfig.get_config_var(var),
  22. 'share/',
  23. SCHEMA_PATH
  24. )
  25. return __path_has_schemas(path)
  26. # Pwd path
  27. path = __path_has_schemas('./data/schema/')
  28. if path is not None:
  29. return path
  30. # User path
  31. path = __get_config_var_schema_path('userbase')
  32. if path is not None:
  33. return path
  34. # Sys path
  35. path = __get_config_var_schema_path('prefix')
  36. if path is not None:
  37. return path
  38. return None
  39. def load_schemas():
  40. # Json schema files to find/use
  41. schemas = [
  42. ('searx_space_instances', 'searx_space_instances.json'),
  43. ('searxng_config' , 'searxng_config.json'),
  44. ('searxng_query' , 'searxng_query.json')
  45. ]
  46. # Find path to schema files
  47. path = find_schema_path(schemas)
  48. if path is None:
  49. raise Exception("Could not read json schema file(s), they are either "
  50. "missing or we don't have rights to read them.")
  51. # Load json schema files into objects
  52. for key, filename in schemas:
  53. json_schema_load(key, os.path.join(path, filename))
  54. load_schemas()