Coverage for src / renaissance / syntax_tree / ast_factory.py: 94%
18 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-09-09 14:04 +0000
« prev ^ index » next coverage.py v7.13.4, created at 2026-09-09 14:04 +0000
1from collections.abc import Sequence
2from pathlib import Path
4from .ast_node import ASTNode
7class ASTFactory:
8 """A factory class for creating instances of ASTNode.
10 Attributes:
11 clazz (type[ASTNode]): The class type of the AST nodes to be created.
12 extra_args (Optional[Sequence[str]]): Additional arguments to be passed during the creation of AST nodes.
13 #TODO working_dir
15 """
17 def __init__(
18 self,
19 clazz: type[ASTNode],
20 extra_args: Sequence[str] | None = None,
21 working_dir: Path | None = None,
22 ) -> None:
23 self.clazz = clazz
24 self.extra_args: Sequence[str] = extra_args if isinstance(extra_args, Sequence) else []
25 # TODO: Why not
26 # self.extra_args: Sequence[str] = [] if extra_args is None else extra_args or
27 # self.extra_args: Sequence[str] = extra_args if extra_args else [] ?
28 # As the logic is about providing a value when none is provided.
29 # In other words, the type of the optional argument is not relevant for the logic.
30 self.working_dir = working_dir or Path.cwd()
32 def create(self, file_path: Path) -> ASTNode:
33 atu = self.clazz.load(
34 file_path=file_path,
35 extra_args=self.extra_args,
36 working_dir=self.working_dir,
37 )
38 assert isinstance(atu, self.clazz), "The loaded AST node is not an instance of the expected type"
39 return atu
41 def create_from_text(self, text: str, file_name: str) -> ASTNode:
42 atu = self.clazz.load_from_text(text, file_name, extra_args=self.extra_args, working_dir=self.working_dir)
43 assert isinstance(atu, self.clazz), "The loaded AST node is not an instance of the expected type"
44 return atu
47if __name__ == "__main__":
48 pass