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.
This commit is contained in:
Christian Kellner 2021-06-01 11:15:17 +02:00
parent 1c3aadba20
commit 475b41c49a

View file

@ -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)