From 475b41c49aac7799a148b07856fb8705b3a66e07 Mon Sep 17 00:00:00 2001 From: Christian Kellner Date: Tue, 1 Jun 2021 11:15:17 +0200 Subject: [PATCH] meta: promote list of modules to class level Define the mapping of modules and their paths at the `ModuleInfo` class level instead of having it inline in a function. This makes it possible to use it from other places in the code. --- osbuild/meta.py | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/osbuild/meta.py b/osbuild/meta.py index 7caa48e7..6a1c3aa7 100644 --- a/osbuild/meta.py +++ b/osbuild/meta.py @@ -278,6 +278,14 @@ class ModuleInfo: Normally this class is instantiated via its `load` method. """ + # Known modules and their corresponding directory name + MODULES = { + "Assembler": "assemblers", + "Input": "inputs", + "Source": "sources", + "Stage": "stages", + } + def __init__(self, klass: str, name: str, path: str, info: Dict): self.name = name self.type = klass @@ -354,7 +362,7 @@ class ModuleInfo: def targets(a): return [t.id for t in filter_type(a.targets, ast.Name)] - base = cls.module_class_to_directory(klass) + base = cls.MODULES.get(klass) if not base: raise ValueError(f"Unsupported type: {klass}") @@ -385,17 +393,6 @@ class ModuleInfo: } return cls(klass, name, path, info) - @staticmethod - def module_class_to_directory(klass: str) -> str: - mapping = { - "Assembler": "assemblers", - "Input": "inputs", - "Source": "sources", - "Stage": "stages", - } - - return mapping.get(klass) - class FormatInfo: """Meta information about a format @@ -469,7 +466,7 @@ class Index: def list_modules_for_class(self, klass: str) -> List[str]: """List all available modules for the given `klass`""" - module_path = ModuleInfo.module_class_to_directory(klass) + module_path = ModuleInfo.MODULES.get(klass) if not module_path: raise ValueError(f"Unsupported nodule class: {klass}") @@ -507,7 +504,7 @@ class Index: with contextlib.suppress(FileNotFoundError): with open(path, "r") as f: schema = json.load(f) - elif klass in ["Assembler", "Input", "Source", "Stage"]: + elif klass in ModuleInfo.MODULES: info = self.get_module_info(klass, name) if info: schema = info.get_schema(version)