From dc233c32ccac3b42f844abaae12c30b52fb5e5a1 Mon Sep 17 00:00:00 2001 From: ilonachan Date: Tue, 1 Oct 2024 16:03:57 +0200 Subject: [PATCH] wip (seems to work, but I want to make changes to values, which requires changes to tagged-enums) --- formats/gds/gda.py | 73 +++++++++++++++++++++++++++++++++++---- requirements-dev.txt | 0 requirements-optional.txt | 1 + requirements.txt | 3 ++ 4 files changed, 70 insertions(+), 7 deletions(-) create mode 100644 requirements-dev.txt create mode 100644 requirements-optional.txt diff --git a/formats/gds/gda.py b/formats/gds/gda.py index 0b3c0b3..098ca90 100644 --- a/formats/gds/gda.py +++ b/formats/gds/gda.py @@ -4,7 +4,8 @@ Requires the command definitions provided by `cmddef` for type checking and parameter data, as well as (possibly in the future) documentation features. """ -from typing import Optional, List +import contextlib +from typing import Any, Optional, List from dataclasses import dataclass @@ -191,15 +192,65 @@ def write_while(ctx: WriterContext, cmd: GDSLoopInvocation): def format_comment(comment: str, filename: str, args: List[GDSValue], cmd: GDSCommand): - from parsy import forward_declaration, regex, seq, string, alt + import re + def get_var(name: str) -> Any: + if name.lower() == "lang": + return "en" + if name.lower() == "eventid": + match: re.Match = re.match(r"/data/script/event/e(\d+).gd[as]", filename) + if match: + return int(match.group(1)) + else: + return "?" + with contextlib.suppress(ValueError): + arg_id = int(name) - 1 + if arg_id < 0 or arg_id >= len(args): + return "?" + + arg = args[arg_id] + + + + return "?" + + def format_value(value: Any, modifiers: List[str]) -> str: + for m in modifiers: + if not m.startswith("r"): + continue + step, max_, *_ = m[1:].split("<=") + [None] + if not isinstance(value, int): + value = "?" + break + step = int(step) + value = (value // step) * step + if max_ is not None: + value = min(value, int(max_)) + + for m in modifiers: + if not m.startswith("0"): + continue + l = int(m[1:]) + if not isinstance(value, int): + value = "?" * l + break + value = str(value) + value = "0" * max(0, l - len(value)) + value + + return str(value) + + def readfile(path: str) -> str: + return f"test file content\n({path})" + + from parsy import forward_declaration, regex, seq, string str_part = regex(r'[^$]') + str_esc = string(r'$$') str_part_fvar = regex(r'[^$}:]') str_part_expr = regex(r'[^$)]') fvar_esc = string(r'}}') | string(r'::') expr_esc = string(r'))') - format_args = regex(r"0\d+") + format_args = regex(r"0\d+") | regex(r"r\d+(<=\d+)?") fvar = forward_declaration() expr = forward_declaration() str_var = string('$') >> ( @@ -210,10 +261,18 @@ def format_comment(comment: str, filename: str, args: List[GDSValue], cmd: GDSCo varname = regex(r"\w+") fvar_expr = (str_part_fvar | fvar_esc | str_var).many().concat() - fvar.become( seq ( (varname) , string(":") >> format_args | None ) ) - expr.become( (str_part_expr | expr_esc | str_var).many().concat() ) + fvar.become( seq ( varname.map(get_var) , (string(":") >> format_args).many() ).map(lambda a: format_value(a[0], a[1])) ) + expr.become( (str_part_expr | expr_esc | str_var).many().concat().map(readfile) ) - str_ = (str_part | str_var).many().concat() + str_ = (str_part | str_esc | str_var).many().concat() # TODO: parse variables like $TEST or ${1:03} and $(filepath) - pass \ No newline at end of file + return str_.parse(comment) + +if __name__ == "__main__": + EXAMPLE = """ + /data/etext/${lang}/e${eventid:r100<=300:03}{pcm}/e${eventid}_t${1}.txt: + + $(/data/etext/${lang}/e${eventid:r100<=300:03}{pcm}/e${eventid}_t${1}.txt) + """ + print(format_comment(EXAMPLE, "/data/script/event/e324.gds", [], None)) \ No newline at end of file diff --git a/requirements-dev.txt b/requirements-dev.txt new file mode 100644 index 0000000..e69de29 diff --git a/requirements-optional.txt b/requirements-optional.txt new file mode 100644 index 0000000..fa9cf06 --- /dev/null +++ b/requirements-optional.txt @@ -0,0 +1 @@ +tqdm \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 7389c82..5a305e6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,6 @@ click pillow ndspy +parsy +pyyaml +dacite \ No newline at end of file