diff --git a/scripts/build_chemistry_catalog.py b/scripts/build_chemistry_catalog.py index 5ebf5dd..5679296 100644 --- a/scripts/build_chemistry_catalog.py +++ b/scripts/build_chemistry_catalog.py @@ -9,31 +9,7 @@ import yaml - -class GameYamlLoader(yaml.SafeLoader): - """YAML loader that accepts SS14 custom tags.""" - - -def construct_tagged_value( - loader: GameYamlLoader, - tag_suffix: str, - node: yaml.Node, -) -> Any: - if isinstance(node, yaml.MappingNode): - value = loader.construct_mapping(node, deep=True) - elif isinstance(node, yaml.SequenceNode): - value = loader.construct_sequence(node, deep=True) - else: - value = loader.construct_scalar(node) - - return { - "yamlTag": f"!{tag_suffix}", - "value": value, - } - - -GameYamlLoader.add_multi_constructor("!", construct_tagged_value) - +from chemistry_yaml import GameYamlLoader, normalize_parents FTL_MESSAGE_RE = re.compile( r"^([A-Za-z0-9][A-Za-z0-9_-]*)\s*=\s*(.*)$" @@ -154,14 +130,6 @@ def read_chemical_group_sections( return guide_path, group_sections -def normalize_parents(value: Any) -> list[str]: - if isinstance(value, str): - return [value] - if isinstance(value, list): - return [item for item in value if isinstance(item, str)] - return [] - - def read_upstream_reagents(game_source: Path) -> dict[str, dict[str, Any]]: root = game_source / "Resources/Prototypes/Reagents" if not root.is_dir(): diff --git a/scripts/chemistry_yaml.py b/scripts/chemistry_yaml.py new file mode 100644 index 0000000..d178b0f --- /dev/null +++ b/scripts/chemistry_yaml.py @@ -0,0 +1,38 @@ +from __future__ import annotations + +from typing import Any + +import yaml + + +class GameYamlLoader(yaml.SafeLoader): + """YAML loader that accepts and preserves SS14 custom tags.""" + + +def construct_tagged_value( + loader: GameYamlLoader, + tag_suffix: str, + node: yaml.Node, +) -> Any: + if isinstance(node, yaml.MappingNode): + value = loader.construct_mapping(node, deep=True) + elif isinstance(node, yaml.SequenceNode): + value = loader.construct_sequence(node, deep=True) + else: + value = loader.construct_scalar(node) + + return { + "yamlTag": f"!{tag_suffix}", + "value": value, + } + + +GameYamlLoader.add_multi_constructor("!", construct_tagged_value) + + +def normalize_parents(value: Any) -> list[str]: + if isinstance(value, str): + return [value] + if isinstance(value, list): + return [item for item in value if isinstance(item, str)] + return [] diff --git a/scripts/index_chemistry.py b/scripts/index_chemistry.py index 2f096c5..203c8e5 100644 --- a/scripts/index_chemistry.py +++ b/scripts/index_chemistry.py @@ -8,40 +8,7 @@ import yaml - -class GameYamlLoader(yaml.SafeLoader): - """YAML loader that accepts and preserves SS14 custom tags.""" - - -def construct_tagged_value( - loader: GameYamlLoader, - tag_suffix: str, - node: yaml.Node, -) -> Any: - if isinstance(node, yaml.MappingNode): - value = loader.construct_mapping( - node, - deep=True, - ) - elif isinstance(node, yaml.SequenceNode): - value = loader.construct_sequence( - node, - deep=True, - ) - else: - value = loader.construct_scalar(node) - - return { - "yamlTag": f"!{tag_suffix}", - "value": value, - } - - -GameYamlLoader.add_multi_constructor( - "!", - construct_tagged_value, -) - +from chemistry_yaml import GameYamlLoader, normalize_parents SOURCE_ROOTS = [ { @@ -67,20 +34,6 @@ def construct_tagged_value( ] -def normalize_parents(value: Any) -> list[str]: - if isinstance(value, str): - return [value] - - if isinstance(value, list): - return [ - item - for item in value - if isinstance(item, str) - ] - - return [] - - def read_file( game_source: Path, path: Path,