f4pga/flows/argparser: cleanup
Signed-off-by: Unai Martinez-Corral <umartinezcorral@antmicro.com>
This commit is contained in:
parent
64022f1e77
commit
5a0c3d1a1f
|
@ -21,12 +21,12 @@ from argparse import ArgumentParser, Namespace
|
|||
from re import finditer as re_finditer
|
||||
|
||||
|
||||
def _add_flow_arg(parser: ArgumentParser):
|
||||
def p_add_flow_arg(parser: ArgumentParser):
|
||||
parser.add_argument("-f", "--flow", metavar="flow_path", type=str, help="Path to flow definition file")
|
||||
|
||||
|
||||
def _setup_build_parser(parser: ArgumentParser):
|
||||
_add_flow_arg(parser)
|
||||
def p_setup_build_parser(parser: ArgumentParser):
|
||||
p_add_flow_arg(parser)
|
||||
|
||||
parser.add_argument(
|
||||
"-t", "--target", metavar="target_name", type=str, help="Perform stages necessary to acquire target"
|
||||
|
@ -51,7 +51,7 @@ def _setup_build_parser(parser: ArgumentParser):
|
|||
parser.add_argument("--val", "-V", action="append", default=[])
|
||||
|
||||
|
||||
def _setup_show_dep_parser(parser: ArgumentParser):
|
||||
def p_setup_show_dep_parser(parser: ArgumentParser):
|
||||
parser.add_argument(
|
||||
"-p", "--part", metavar="part_name", type=str, help="Name of the part (use to display part-specific values.)"
|
||||
)
|
||||
|
@ -64,7 +64,7 @@ def _setup_show_dep_parser(parser: ArgumentParser):
|
|||
help="Name of the stage (use if you want to set the value only for that stage). Requires `-p`.",
|
||||
)
|
||||
|
||||
_add_flow_arg(parser)
|
||||
p_add_flow_arg(parser)
|
||||
|
||||
|
||||
def setup_argparser():
|
||||
|
@ -78,23 +78,23 @@ def setup_argparser():
|
|||
parser.add_argument("-s", "--silent", action="store_true")
|
||||
|
||||
subparsers = parser.add_subparsers(dest="command")
|
||||
_setup_build_parser(subparsers.add_parser("build"))
|
||||
p_setup_build_parser(subparsers.add_parser("build"))
|
||||
show_dep = subparsers.add_parser("showd", description="Show the value(s) assigned to a dependency")
|
||||
_setup_show_dep_parser(show_dep)
|
||||
p_setup_show_dep_parser(show_dep)
|
||||
|
||||
return parser
|
||||
|
||||
|
||||
def _parse_depval(depvalstr: str):
|
||||
def p_parse_depval(depvalstr: str):
|
||||
"""
|
||||
Parse a dependency or value definition in form of:
|
||||
optional_stage_name.value_or_dependency_name=value
|
||||
See `_parse_cli_value` for detail on how to pass different kinds of values.
|
||||
See `p_parse_cli_value` for detail on how to pass different kinds of values.
|
||||
"""
|
||||
|
||||
d = {"name": None, "stage": None, "value": None}
|
||||
|
||||
splitted = list(_unescaped_separated("=", depvalstr))
|
||||
splitted = list(p_unescaped_separated("=", depvalstr))
|
||||
|
||||
if len(splitted) != 2:
|
||||
raise Exception("Too many components")
|
||||
|
@ -111,16 +111,12 @@ def _parse_depval(depvalstr: str):
|
|||
if len(path_components) > 0:
|
||||
raise Exception("Too many path components")
|
||||
|
||||
d["value"] = _parse_cli_value(valstr)
|
||||
d["value"] = p_parse_cli_value(valstr)
|
||||
|
||||
return d
|
||||
|
||||
|
||||
def _null_generator():
|
||||
return (item for item in [])
|
||||
|
||||
|
||||
def _unescaped_matches(regexp: str, s: str, escape_chr="\\"):
|
||||
def p_unescaped_matches(regexp: str, s: str, escape_chr="\\"):
|
||||
"""
|
||||
Find all occurences of a pattern in a string that contains escape sequences.
|
||||
Yields pairs of starting and ending indices of the pattern.
|
||||
|
@ -144,7 +140,7 @@ def _unescaped_matches(regexp: str, s: str, escape_chr="\\"):
|
|||
noescapes += noescape
|
||||
|
||||
if len(offsets) == 0:
|
||||
return _null_generator()
|
||||
return (item for item in [])
|
||||
|
||||
last_offset = offsets[-1]
|
||||
offsets.append(last_offset)
|
||||
|
@ -159,13 +155,13 @@ def _unescaped_matches(regexp: str, s: str, escape_chr="\\"):
|
|||
yield off1, off2
|
||||
|
||||
|
||||
def _unescaped_separated(regexp: str, s: str, escape_chr="\\"):
|
||||
def p_unescaped_separated(regexp: str, s: str, escape_chr="\\"):
|
||||
"""
|
||||
Yields substrings of a string that contains escape sequences.
|
||||
"""
|
||||
|
||||
last_end = 0
|
||||
for start, end in _unescaped_matches(regexp, s, escape_chr=escape_chr):
|
||||
for start, end in p_unescaped_matches(regexp, s, escape_chr=escape_chr):
|
||||
yield s[last_end:start]
|
||||
last_end = end
|
||||
if last_end < len(s):
|
||||
|
@ -174,7 +170,7 @@ def _unescaped_separated(regexp: str, s: str, escape_chr="\\"):
|
|||
yield ""
|
||||
|
||||
|
||||
def _parse_cli_value(s: str):
|
||||
def p_parse_cli_value(s: str):
|
||||
"""
|
||||
Parse a value/dependency passed to CLI
|
||||
CLI values are generated by the following non-contextual grammar:
|
||||
|
@ -207,7 +203,7 @@ def _parse_cli_value(s: str):
|
|||
inner = s[1 : (len(s) - 1)]
|
||||
if inner == "":
|
||||
return []
|
||||
return [_parse_cli_value(v) for v in _unescaped_separated(",", inner)]
|
||||
return [p_parse_cli_value(v) for v in p_unescaped_separated(",", inner)]
|
||||
|
||||
# Dictionary
|
||||
if s[0] == "{":
|
||||
|
@ -217,14 +213,14 @@ def _parse_cli_value(s: str):
|
|||
inner = s[1 : (len(s) - 1)]
|
||||
if inner == "":
|
||||
return {}
|
||||
for kv in _unescaped_separated(",", inner):
|
||||
k_v = list(_unescaped_separated(":", kv))
|
||||
for kv in p_unescaped_separated(",", inner):
|
||||
k_v = list(p_unescaped_separated(":", kv))
|
||||
if len(k_v) < 2:
|
||||
raise Exception("Missing value in dictionary entry")
|
||||
if len(k_v) > 2:
|
||||
raise Exception("Unexpected ':' token")
|
||||
key = k_v[0]
|
||||
value = _parse_cli_value(k_v[1])
|
||||
value = p_parse_cli_value(k_v[1])
|
||||
d[key] = value
|
||||
|
||||
return d
|
||||
|
@ -253,7 +249,7 @@ def get_cli_flow_config(args: Namespace, part: str):
|
|||
part_flow_config = create_defdict()
|
||||
|
||||
def add_entries(arglist: "list[str]", dict_name: str):
|
||||
for value_def in (_parse_depval(cliv) for cliv in arglist):
|
||||
for value_def in (p_parse_depval(cliv) for cliv in arglist):
|
||||
stage = value_def["stage"]
|
||||
if stage is None:
|
||||
part_flow_config[dict_name][value_def["name"]] = value_def["value"]
|
||||
|
|
Loading…
Reference in New Issue