|
16 | 16 |
|
17 | 17 | import importlib |
18 | 18 |
|
19 | | -import typer |
20 | | - |
| 19 | +from codesectools.sasts.core.cli import CLIFactory |
21 | 20 | from codesectools.sasts.core.sast import SAST, AnalysisResult |
| 21 | +from codesectools.sasts.core.sast.properties import SASTProperties |
| 22 | +from codesectools.sasts.core.sast.requirements import SASTRequirement |
22 | 23 | from codesectools.utils import SASTS_DIR |
23 | 24 |
|
| 25 | + |
| 26 | +class LazySASTLoader: |
| 27 | + """Lazily load SAST tool components to avoid premature imports.""" |
| 28 | + |
| 29 | + def __init__(self, name: str) -> None: |
| 30 | + """Initialize the lazy loader. |
| 31 | +
|
| 32 | + Args: |
| 33 | + name: The name of the SAST tool to load. |
| 34 | +
|
| 35 | + """ |
| 36 | + self.name = name |
| 37 | + self.loaded = False |
| 38 | + |
| 39 | + def _load(self) -> None: |
| 40 | + """Import the SAST modules and classes on first access.""" |
| 41 | + if not self.loaded: |
| 42 | + sast_module = importlib.import_module( |
| 43 | + f"codesectools.sasts.tools.{self.name}.sast" |
| 44 | + ) |
| 45 | + |
| 46 | + self.sast: SAST = getattr(sast_module, f"{self.name}SAST") |
| 47 | + self.sast_instance: SAST = self.sast() |
| 48 | + self.analysis_result: AnalysisResult = getattr( |
| 49 | + sast_module, f"{self.name}AnalysisResult" |
| 50 | + ) |
| 51 | + |
| 52 | + self.cli_module = importlib.import_module( |
| 53 | + f"codesectools.sasts.tools.{self.name}.cli" |
| 54 | + ) |
| 55 | + self.cli_factory: CLIFactory = getattr( |
| 56 | + self.cli_module, f"{self.name}CLIFactory" |
| 57 | + ) |
| 58 | + |
| 59 | + self._data = { |
| 60 | + "status": self.sast_instance.status, |
| 61 | + "missing": self.sast_instance.missing, |
| 62 | + "properties": self.sast_instance.properties, |
| 63 | + "sast": self.sast, |
| 64 | + "analysis_result": self.analysis_result, |
| 65 | + "cli_factory": self.cli_factory, |
| 66 | + } |
| 67 | + |
| 68 | + self.loaded = True |
| 69 | + |
| 70 | + def __getitem__( |
| 71 | + self, name: str |
| 72 | + ) -> ( |
| 73 | + str |
| 74 | + | list[SASTRequirement] |
| 75 | + | SASTProperties |
| 76 | + | SAST |
| 77 | + | AnalysisResult |
| 78 | + | CLIFactory |
| 79 | + ): |
| 80 | + """Provide dictionary-like access to the loaded SAST components.""" |
| 81 | + self._load() |
| 82 | + return self._data[name] |
| 83 | + |
| 84 | + def __setitem__( |
| 85 | + self, |
| 86 | + name: str, |
| 87 | + value: str |
| 88 | + | list[SASTRequirement] |
| 89 | + | SASTProperties |
| 90 | + | SAST |
| 91 | + | AnalysisResult |
| 92 | + | CLIFactory, |
| 93 | + ) -> None: |
| 94 | + """Provide dictionary-like write access to the loaded SAST components.""" |
| 95 | + self._load() |
| 96 | + self._data[name] = value |
| 97 | + |
| 98 | + |
24 | 99 | SASTS_ALL = {} |
25 | 100 | for child in (SASTS_DIR / "tools").iterdir(): |
26 | 101 | if child.is_dir(): |
27 | 102 | sast_name = child.name |
28 | | - |
29 | | - sast_module = importlib.import_module( |
30 | | - f"codesectools.sasts.tools.{sast_name}.sast" |
31 | | - ) |
32 | | - |
33 | | - sast: SAST = getattr(sast_module, f"{sast_name}SAST") |
34 | | - sast_instance = sast() |
35 | | - analysis_result: AnalysisResult = getattr( |
36 | | - sast_module, f"{sast_name}AnalysisResult" |
37 | | - ) |
38 | | - |
39 | | - cli_module = importlib.import_module( |
40 | | - f"codesectools.sasts.tools.{sast_name}.cli" |
41 | | - ) |
42 | | - cli_factory: typer.Typer = getattr(cli_module, f"{sast_name}CLIFactory") |
43 | | - |
44 | | - SASTS_ALL[sast_name] = { |
45 | | - "status": sast_instance.status, |
46 | | - "missing": sast_instance.missing, |
47 | | - "properties": sast_instance.properties, |
48 | | - "sast": sast, |
49 | | - "analysis_result": analysis_result, |
50 | | - "cli_factory": cli_factory, |
51 | | - } |
| 103 | + SASTS_ALL[sast_name] = LazySASTLoader(sast_name) |
52 | 104 |
|
53 | 105 | SASTS_ALL = dict(sorted(SASTS_ALL.items())) |
0 commit comments