56 lines
1.5 KiB
Python
Executable file
56 lines
1.5 KiB
Python
Executable file
from argparse import ArgumentParser
|
|
from pathlib import Path
|
|
from json import load
|
|
|
|
|
|
# Indico raises a warning for every non prefixed local variable, so we add a
|
|
# prefix to each import and declared function
|
|
PROLOGUE = (
|
|
"import functools as _functools\n"
|
|
"@_functools.cache\n"
|
|
"def _read_cred(name):\n"
|
|
" import os\n"
|
|
" dir = os.environ['CREDENTIALS_DIRECTORY']\n"
|
|
" with open(os.path.join(dir, name)) as fd:\n"
|
|
" return fd.read()\n"
|
|
)
|
|
|
|
|
|
def make_value(value):
|
|
match value["_pyType"]:
|
|
case "str" | "int" | "bool" | "null":
|
|
return repr(value["_value"])
|
|
case "list":
|
|
items = [make_value(i) for i in value["_value"]]
|
|
return f"[{','.join(items)}]"
|
|
case "dict":
|
|
items = [
|
|
f"{repr(k)}: {make_value(v)}"
|
|
for k, v in value["_value"].items()
|
|
]
|
|
return f"{{{','.join(items)}}}"
|
|
case "read-cred":
|
|
return f"_read_cred({repr(value['_value'])})"
|
|
case _:
|
|
raise ValueError("Unknown data type")
|
|
|
|
|
|
def mk_vars(vars):
|
|
if not all(str.isidentifier(n) for n in vars.keys()):
|
|
raise ValueError("At least one variable identifier is invalid")
|
|
eqs = [f"{n} = {make_value(v)}" for n, v in vars.items()]
|
|
return PROLOGUE + "\n".join(eqs)
|
|
|
|
|
|
def main():
|
|
parser = ArgumentParser()
|
|
parser.add_argument("json", type=Path)
|
|
|
|
args = parser.parse_args()
|
|
|
|
with args.json.open() as fd:
|
|
print(mk_vars(load(fd)))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|