Skip to content

Commit

Permalink
[sourcegen] Simplify TagFileParser
Browse files Browse the repository at this point in the history
  • Loading branch information
ischoegl committed Aug 17, 2024
1 parent 1bb16f7 commit b76b5d5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 deletions.
14 changes: 4 additions & 10 deletions interfaces/sourcegen/sourcegen/_TagFileParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
class TagFileParser:
"""Class handling contents of doxygen tag file."""

def _parse_doxyfile(self, class_crosswalk: Dict[str, str]):
def _parse_doxyfile(self, class_crosswalk: List[str]):
"""Retrieve class and function information from Cantera namespace."""

def xml_compounds(kind: str, names: List[str]) -> Dict[str,str]:
Expand All @@ -43,18 +43,15 @@ def xml_compounds(kind: str, names: List[str]) -> Dict[str,str]:
class_names = [_.split(":")[-1] for _ in qualified_names]

# Handle exceptions for unknown/undocumented classes
configured = []
for cls in class_crosswalk.values():
configured.extend(cls) # one prefix can map to multiple classes
unknown = set(configured) - set(class_names)
unknown = set(class_crosswalk) - set(class_names)
if unknown:
unknown = "', '".join(unknown)
logger.warning(
"Class(es) in configuration file are missing from tag file: "
f"'{unknown}'")

# Parse content of classes that are specified by the configuration file
class_names = set(configured) & set(class_names)
class_names = set(class_crosswalk) & set(class_names)
classes = xml_compounds("class", class_names)

def xml_members(kind: str, text: str, prefix="") -> Dict[str, str]:
Expand Down Expand Up @@ -87,10 +84,6 @@ def __init__(self, class_crosswalk: Dict[str, str]) -> None:

self._parse_doxyfile(class_crosswalk)

@staticmethod
def from_parsed(parsed_config_file: dict):
return TagFileParser(parsed_config_file["class_crosswalk"])

def annotated_func(self, parsed: Func) -> Union[AnnotatedFunc, None]:
"""Match function with doxygen tag information."""
comments = parsed.annotations
Expand Down Expand Up @@ -143,6 +136,7 @@ def doxygen_func(tag: str, text: str) -> Union[str, None]:
xml_tags("anchorfile", xml)[0].replace(".html", ".xml"),
xml_tags("anchor", xml)[0])


def xml_tags(tag: str, text: str, suffix: str="") -> Union[str, None]:
if suffix:
suffix = f" {suffix.strip()}"
Expand Down
15 changes: 9 additions & 6 deletions interfaces/sourcegen/sourcegen/_orchestrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,17 @@ def generate_source(lang: str, out_dir: str=""):
reader = ruamel.yaml.YAML(typ="safe")
config = reader.load(config_file)

ignore_files: List[str] = config.get("ignore_files", [])
ignore_files: List[str] = config.get("ignore_files", ["all"])
ignore_funcs: Dict[str, List[str]] = config.get("ignore_funcs", {})

files = (HeaderFileParser(f, ignore_funcs.get(f.name, [])).parse()
for f in _clib_path.glob("*.h")
if f != _clib_defs_path and f.name not in ignore_files)
# removes instances where HeaderFile.parse() returned None
files = list(filter(None, files))
if "all" in ignore_files:
files = []
else:
files = (HeaderFileParser(f, ignore_funcs.get(f.name, [])).parse()
for f in _clib_path.glob("*.h")
if f != _clib_defs_path and f.name not in ignore_files)
# removes instances where HeaderFile.parse() returned None
files = list(filter(None, files))

# find and instantiate the language-specific SourceGenerator
_, scaffolder_type = inspect.getmembers(module,
Expand Down
7 changes: 7 additions & 0 deletions interfaces/sourcegen/sourcegen/yaml/_Config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,10 @@ class Config:
@staticmethod
def from_parsed(parsed_config_file: dict):
return Config(parsed_config_file["class_crosswalk"])

def classes(self):
"""Return all classes referenced in configuration file."""
ret = []
for cls in self.class_crosswalk.values():
ret.extend(cls) # one prefix can map to multiple classes
return ret
4 changes: 2 additions & 2 deletions interfaces/sourcegen/sourcegen/yaml/_YamlSourceGenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def __init__(self, out_dir: str, config: dict):

# use the typed config
self._config = Config.from_parsed(config)
self._doxygen = TagFileParser.from_parsed(config)
self._doxygen_tags = TagFileParser(self._config.classes())

def generate_source(self, headers_files: List[HeaderFile]):
"""Generate output"""
Expand All @@ -36,7 +36,7 @@ def generate_source(self, headers_files: List[HeaderFile]):
for header_file in headers_files:
name = header_file.path.name
annotated_map[name] = []
for func in list(map(self._doxygen.annotated_func, header_file.funcs)):
for func in list(map(self._doxygen_tags.annotated_func, header_file.funcs)):
if func is not None:
annotated_map[name].append(func)

Expand Down

0 comments on commit b76b5d5

Please sign in to comment.