schema.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. ########################################################################
  2. # Searx-Qt - Lightweight desktop application for Searx.
  3. # Copyright (C) 2020-2024 CYBERDEViL
  4. #
  5. # This file is part of Searx-Qt.
  6. #
  7. # Searx-Qt is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # Searx-Qt is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. #
  20. ########################################################################
  21. import os
  22. import sysconfig # Get path prefix (example: /usr).
  23. import json
  24. ## Global JSON Schema container so we won't have to read a json schema file
  25. ## from disk everytime we need to verify some json data.
  26. Schemas = {}
  27. def json_schema_load(key, filepath):
  28. """! Loads an json schema into the global 'Schemas' container.
  29. @param key: A name to store the json schema to, existing keys will be
  30. overwritten!
  31. @type key: string
  32. @param filepath: Path where the json schema file is located (including
  33. filename)
  34. @type filepath: string
  35. May raise an json.JSONDecodeError or UnicodeDecodeError when the schema
  36. json is invalid, or an OSError when it cannot access the given filepath.
  37. """
  38. with open(filepath, 'r') as f:
  39. data = json.load(f)
  40. # An exception should have been raised when open() or json.load() has
  41. # failed, so at this point the schema json looks valid.
  42. # @note: Existing keys will be overwritten/reloaded.
  43. Schemas.update({key: data})
  44. def find_schema_path(schemas):
  45. """! Get/find path to schema json files.
  46. Priority:
  47. 1. Pwd (./schema/)
  48. 2. User (example: ~/.local/share/)
  49. 3. Sys (example: /usr/share/)
  50. """
  51. def __path_has_schemas(path):
  52. for key, filename in schemas:
  53. full_file_path = os.path.join(path, filename)
  54. if (not os.path.isfile(full_file_path) or
  55. not os.access(full_file_path, os.R_OK)):
  56. return None
  57. return path
  58. def __get_config_var_schema_path(var):
  59. path = os.path.join(
  60. sysconfig.get_config_var(var),
  61. "share/",
  62. "searx-qt/schema/"
  63. )
  64. return __path_has_schemas(path)
  65. # Pwd path
  66. path = __path_has_schemas("./data/schema/")
  67. if path is not None:
  68. return path
  69. # User path
  70. path = __get_config_var_schema_path("userbase")
  71. if path is not None:
  72. return path
  73. # Sys path
  74. path = __get_config_var_schema_path("prefix")
  75. if path is not None:
  76. return path
  77. return None
  78. def load_schemas():
  79. # Json schema files to find/use
  80. schemas = [
  81. ("searx_space_instances", "searx_space_instances.json"),
  82. ("searxng_config" , "searxng_config.json"),
  83. ("searxng_query" , "searxng_query.json")
  84. ]
  85. # Find path to schema files
  86. path = find_schema_path(schemas)
  87. if path is None:
  88. raise Exception("Could not read json schema file(s), they are either "
  89. "missing or we don't have rights to read them.")
  90. # Load json schema files into objects
  91. for key, filename in schemas:
  92. json_schema_load(key, os.path.join(path, filename))