|
1 | | -import ast |
2 | | -import glob |
3 | | -import json |
4 | 1 | import logging |
5 | 2 | import os |
6 | | -import pkgutil |
7 | | -import re |
| 3 | +import yaml |
8 | 4 |
|
9 | | -from utils import exceptions |
10 | 5 | from utils.config import Config |
11 | | -from collections import deque |
12 | 6 |
|
13 | 7 | logger = logging.getLogger('backend') |
14 | 8 |
|
15 | 9 |
|
16 | 10 | class PSModuleManager: |
17 | | - pass |
| 11 | + |
| 12 | + def __init__(self): |
| 13 | + module_folder = self._find_module_folder() |
| 14 | + self.folder = module_folder |
| 15 | + |
| 16 | + def _find_module_folder(self): |
| 17 | + powershell_folder = Config.POWERSHELL_PATH |
| 18 | + if not os.path.exists(powershell_folder) or not os.path.isdir(powershell_folder): |
| 19 | + raise ValueError(f"Invalid PowerShell folder: '{powershell_folder}'") |
| 20 | + module_folder = os.path.join(powershell_folder, "src") |
| 21 | + if not os.path.exists(module_folder): |
| 22 | + raise ValueError(f"Invalid PowerShell folder: cannot find modules in: '{module_folder}'") |
| 23 | + return module_folder |
| 24 | + |
| 25 | + def list_modules(self): |
| 26 | + modules = [] |
| 27 | + for folder_name in os.listdir(self.folder): |
| 28 | + path = os.path.join(self.folder, folder_name) |
| 29 | + if os.path.isdir(path): |
| 30 | + for sub_folder in os.listdir(path): |
| 31 | + if os.path.isdir(os.path.join(path, sub_folder)) and sub_folder.endswith(".Autorest"): |
| 32 | + name = f"{folder_name}/{sub_folder}" |
| 33 | + modules.append({ |
| 34 | + "name": name, |
| 35 | + "folder": os.path.join(path, sub_folder) |
| 36 | + }) |
| 37 | + return sorted(modules, key=lambda a: a['name']) |
| 38 | + |
| 39 | + def create_new_mod(self, module_names): |
| 40 | + if isinstance(module_names, str): |
| 41 | + module_names = module_names.split('/') |
| 42 | + folder = os.path.join(self.folder, *module_names) |
| 43 | + os.makedirs(folder, exist_ok=True) |
| 44 | + |
| 45 | + def load_module(self, module_names): |
| 46 | + if isinstance(module_names, str): |
| 47 | + module_names = module_names.split('/') |
| 48 | + folder = os.path.join(self.folder, *module_names) |
| 49 | + if not os.path.exists(folder): |
| 50 | + raise ValueError(f"Module folder not found: '{folder}'") |
| 51 | + autorest_config = self.load_autorest_config(module_names) |
| 52 | + return { |
| 53 | + **autorest_config, |
| 54 | + "name": "/".join(module_names), |
| 55 | + "folder": folder |
| 56 | + } |
| 57 | + |
| 58 | + def load_autorest_config(self, module_names): |
| 59 | + if isinstance(module_names, str): |
| 60 | + module_names = module_names.split('/') |
| 61 | + folder = os.path.join(self.folder, *module_names) |
| 62 | + readme_file = os.path.join(folder, "README.md") |
| 63 | + if not os.path.exists(readme_file): |
| 64 | + raise ValueError(f"README.md not found in: '{readme_file}'") |
| 65 | + with open(readme_file, "r") as f: |
| 66 | + content = f.readlines() |
| 67 | + autorest_config = [] |
| 68 | + in_autorest_config_section = False |
| 69 | + in_yaml_section = False |
| 70 | + for line in content: |
| 71 | + if line.strip().startswith("### AutoRest Configuration"): |
| 72 | + in_autorest_config_section = True |
| 73 | + elif in_autorest_config_section: |
| 74 | + if line.strip().startswith("###"): |
| 75 | + break |
| 76 | + if line.strip().startswith("```") and 'yaml' in line: |
| 77 | + in_yaml_section = True |
| 78 | + elif in_yaml_section: |
| 79 | + if line.strip().startswith("```"): |
| 80 | + in_yaml_section = False |
| 81 | + else: |
| 82 | + if line.strip(): |
| 83 | + autorest_config.append(line) |
| 84 | + else: |
| 85 | + autorest_config.append("") |
| 86 | + autorest_config_raw = "\n".join(autorest_config) |
| 87 | + try: |
| 88 | + yaml_config = yaml.load(autorest_config_raw, Loader=yaml.FullLoader) |
| 89 | + except Exception as e: |
| 90 | + raise ValueError(f"Failed to parse autorest config: {e} for readme_file: {readme_file}") |
| 91 | + return { |
| 92 | + "autorest_config": yaml_config, |
| 93 | + # "raw": autorest_config_raw # can be used for directive merging |
| 94 | + } |
0 commit comments