indico: use files for passwords
Signed-off-by: Jeltz <jeltz@federez.net>
This commit is contained in:
parent
dd2afc2cfb
commit
2f93570ac4
3 changed files with 120 additions and 13 deletions
54
pkgs/python-vars-with-env/codegen.py
Executable file
54
pkgs/python-vars-with-env/codegen.py
Executable file
|
@ -0,0 +1,54 @@
|
|||
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_file(name):\n"
|
||||
" with open(name) as f:\n"
|
||||
" return name\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-file":
|
||||
return f"_read_file({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()
|
Loading…
Add table
Add a link
Reference in a new issue