rules.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import json
  2. import os
  3. def init_provider_rules():
  4. # Get the absolute path of the subdirectory
  5. subdirectory_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'rules')
  6. # Path to the providers.json file
  7. providers_json_file_path = os.path.join(subdirectory_path, '_providers.json')
  8. try:
  9. # Open the JSON file and read its content
  10. with open(providers_json_file_path, 'r') as json_file:
  11. data = json.load(json_file)
  12. # Store the content in a dictionary with the key as the file name (without extension)
  13. provider_names = data
  14. except FileNotFoundError:
  15. return "JSON file not found or path error"
  16. except json.JSONDecodeError:
  17. return "JSON file decoding error"
  18. # Dictionary to store the content of all JSON files
  19. json_data = {}
  20. try:
  21. # Loop through all files in the directory
  22. for provider_name in provider_names:
  23. filename = provider_name + '.json'
  24. # Path to each JSON file
  25. json_file_path = os.path.join(subdirectory_path, filename)
  26. # Open each JSON file and read its content
  27. with open(json_file_path, 'r') as json_file:
  28. data = json.load(json_file)
  29. # Store the content in the dictionary with the key as the file name (without extension)
  30. json_data[os.path.splitext(filename)[0]] = data
  31. return json_data
  32. except FileNotFoundError:
  33. return "JSON file not found or path error"
  34. except json.JSONDecodeError:
  35. return "JSON file decoding error"
  36. provider_rules = init_provider_rules()