meta: extract module class to dir mapping

Make the mapping of module class to the corresponding directory
a method of the ModuleInfo class. This is so it can be re-used
by others in the future.
This commit is contained in:
Christian Kellner 2020-05-28 15:42:51 +02:00 committed by David Rheinsberg
parent 1718740c6c
commit 2d5ec8edad

View file

@ -326,16 +326,10 @@ class ModuleInfo:
def targets(a): def targets(a):
return [t.id for t in filter_type(a.targets, ast.Name)] return [t.id for t in filter_type(a.targets, ast.Name)]
mapping = { base = cls.module_class_to_directory(klass)
"Stage": "stages", if not base:
"Assembler": "assemblers"
}
if klass not in mapping:
raise ValueError(f"Unsupported type: {klass}") raise ValueError(f"Unsupported type: {klass}")
base = mapping[klass]
path = os.path.join(root, base, name) path = os.path.join(root, base, name)
try: try:
with open(path) as f: with open(path) as f:
@ -349,6 +343,15 @@ class ModuleInfo:
info = {k: value(v) for k, v in targets if k in names} info = {k: value(v) for k, v in targets if k in names}
return cls(klass, name, info) return cls(klass, name, info)
@staticmethod
def module_class_to_directory(klass: str) -> str:
mapping = {
"Stage": "stages",
"Assembler": "assemblers"
}
return mapping.get(klass)
class Index: class Index:
"""Index of stages and assemblers """Index of stages and assemblers