Load bundle data from YAML files. :return: a list of bundle ids.
()
| 30 | |
| 31 | |
| 32 | def load_bundle_data() -> List[str]: |
| 33 | """ |
| 34 | Load bundle data from YAML files. |
| 35 | :return: a list of bundle ids. |
| 36 | """ |
| 37 | |
| 38 | global __bundles, __bundle_dict, __bundles_cache, __bundle_checksum |
| 39 | |
| 40 | bundles_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "../../bundles") |
| 41 | # List all entries in the bundles directory that are directories |
| 42 | bundle_ids = [i for i in os.listdir(bundles_path) if os.path.isdir(os.path.join(bundles_path, i))] |
| 43 | pattern = re.compile(r'^[a-z0-9][a-z0-9_]*$') |
| 44 | bundle_ids = [i for i in bundle_ids if pattern.match(i) and not i.startswith("template")] |
| 45 | # todo: check bundle_ids in a-z, 0-9, _ |
| 46 | |
| 47 | # Iterate through each file in the directory |
| 48 | for bundle_id in bundle_ids: |
| 49 | if CONFIG.ALLOWED_BUNDLES and bundle_id not in CONFIG.ALLOWED_BUNDLES: |
| 50 | continue |
| 51 | if CONFIG.FORBIDDEN_BUNDLES and bundle_id in CONFIG.FORBIDDEN_BUNDLES: |
| 52 | continue |
| 53 | file_path = os.path.join(bundles_path, bundle_id, "resources/bundle_schema.yml") |
| 54 | logger.info(f"Loading bundle data from bundles/{bundle_id}/resources/bundle_schema.yml") |
| 55 | i18n_dir_path = os.path.join(bundles_path, bundle_id, "resources/i18n") |
| 56 | # Check if file is not empty |
| 57 | if os.path.getsize(file_path) > 0: |
| 58 | try: |
| 59 | |
| 60 | # Open and read the file |
| 61 | with open(file_path, "r") as file: |
| 62 | bundle_str = file.read() |
| 63 | with open(file_path, "r") as file: |
| 64 | bundle_dict = yaml.safe_load(file) # Use yaml.safe_load to load YAML data |
| 65 | |
| 66 | # read all the necessary i18n keys |
| 67 | i18n_keys = collect_i18n_values(bundle_str) |
| 68 | model_schema_dir = os.path.join(bundles_path, bundle_id, "plugins") |
| 69 | if os.path.exists(model_schema_dir): |
| 70 | for plugin_folder_name in os.listdir(model_schema_dir): |
| 71 | # read plugin_folder_name/plugin_schema.yml |
| 72 | if (plugin_folder_name.startswith("_") or |
| 73 | not os.path.isdir(os.path.join(model_schema_dir, plugin_folder_name))): |
| 74 | continue |
| 75 | file_path = os.path.join(model_schema_dir, plugin_folder_name, "plugin_schema.yml") |
| 76 | if os.path.getsize(file_path) > 0: |
| 77 | with open(file_path, "r") as file: |
| 78 | model_str = file.read() |
| 79 | i18n_keys.extend(collect_i18n_values(model_str)) |
| 80 | |
| 81 | # read i18n files: en.yml, zh.yml, fr.yml, etc. |
| 82 | for i18n_file in os.listdir(i18n_dir_path): |
| 83 | if i18n_file.endswith(".yml"): |
| 84 | lang = i18n_file.split(".")[0] |
| 85 | with open(os.path.join(i18n_dir_path, i18n_file), "r") as file: |
| 86 | i18n_data = yaml.safe_load(file) |
| 87 | # check if all keys are present |
| 88 | for key in i18n_keys: |
| 89 | if key[5:] not in i18n_data: |