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

@@ -43,8 +43,8 @@ def _parse_args() -> argparse.Namespace:
"compute QL imports and in some comments and as root for relative paths provided as options. "
"If not provided it defaults to the directory of the configuration file, if any")
path_arguments = [
p.add_argument("--schema", default="schema.py",
help="input schema file (default %(default)s)"),
p.add_argument("--schema",
help="input schema file (default schema.py)"),
p.add_argument("--dbscheme",
help="output file for dbscheme generation, input file for trap generation"),
p.add_argument("--ql-output",
@@ -87,6 +87,8 @@ def _parse_args() -> argparse.Namespace:
setattr(opts, flag, getattr(defaults, flag))
if opts.root_dir is None:
opts.root_dir = opts.configuration_file.parent
if opts.schema is None:
opts.schema = "schema.py"
if not opts.generate:
p.error("Nothing to do, specify --generate")
# absolutize all paths

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)