Codegen/Rust: allow breaking up schema file

This commit is contained in:
Paolo Tranquilli
2024-09-19 15:57:42 +02:00
parent 3c09f70e0d
commit a5e3fbf367
8 changed files with 74 additions and 64 deletions

View File

@@ -1,4 +1,5 @@
""" schema loader """
import sys
import inflection
import typing
@@ -140,6 +141,8 @@ def load(m: types.ModuleType) -> schema.Schema:
continue
if name.startswith("__"):
continue
if isinstance(data, types.ModuleType):
continue
cls = _get_class(data)
if classes and not cls.bases:
raise schema.Error(
@@ -160,7 +163,10 @@ def load(m: types.ModuleType) -> schema.Schema:
def load_file(path: pathlib.Path) -> schema.Schema:
spec = importlib.util.spec_from_file_location("schema", path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
assert path.suffix in ("", ".py")
sys.path.insert(0, str(path.parent))
try:
module = importlib.import_module(path.with_suffix("").name)
finally:
sys.path.remove(str(path.parent))
return load(module)