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
|
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")
|
parser.add_argument("-f", "--flow", metavar="flow_path", type=str, help="Path to flow definition file")
|
||||||
|
|
||||||
|
|
||||||
def _setup_build_parser(parser: ArgumentParser):
|
def p_setup_build_parser(parser: ArgumentParser):
|
||||||
_add_flow_arg(parser)
|
p_add_flow_arg(parser)
|
||||||
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-t", "--target", metavar="target_name", type=str, help="Perform stages necessary to acquire target"
|
"-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=[])
|
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(
|
parser.add_argument(
|
||||||
"-p", "--part", metavar="part_name", type=str, help="Name of the part (use to display part-specific values.)"
|
"-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`.",
|
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():
|
def setup_argparser():
|
||||||
|
@ -78,23 +78,23 @@ def setup_argparser():
|
||||||
parser.add_argument("-s", "--silent", action="store_true")
|
parser.add_argument("-s", "--silent", action="store_true")
|
||||||
|
|
||||||
subparsers = parser.add_subparsers(dest="command")
|
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")
|
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
|
return parser
|
||||||
|
|
||||||
|
|
||||||
def _parse_depval(depvalstr: str):
|
def p_parse_depval(depvalstr: str):
|
||||||
"""
|
"""
|
||||||
Parse a dependency or value definition in form of:
|
Parse a dependency or value definition in form of:
|
||||||
optional_stage_name.value_or_dependency_name=value
|
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}
|
d = {"name": None, "stage": None, "value": None}
|
||||||
|
|
||||||
splitted = list(_unescaped_separated("=", depvalstr))
|
splitted = list(p_unescaped_separated("=", depvalstr))
|
||||||
|
|
||||||
if len(splitted) != 2:
|
if len(splitted) != 2:
|
||||||
raise Exception("Too many components")
|
raise Exception("Too many components")
|
||||||
|
@ -111,16 +111,12 @@ def _parse_depval(depvalstr: str):
|
||||||
if len(path_components) > 0:
|
if len(path_components) > 0:
|
||||||
raise Exception("Too many path components")
|
raise Exception("Too many path components")
|
||||||
|
|
||||||
d["value"] = _parse_cli_value(valstr)
|
d["value"] = p_parse_cli_value(valstr)
|
||||||
|
|
||||||
return d
|
return d
|
||||||
|
|
||||||
|
|
||||||
def _null_generator():
|
def p_unescaped_matches(regexp: str, s: str, escape_chr="\\"):
|
||||||
return (item for item in [])
|
|
||||||
|
|
||||||
|
|
||||||
def _unescaped_matches(regexp: str, s: str, escape_chr="\\"):
|
|
||||||
"""
|
"""
|
||||||
Find all occurences of a pattern in a string that contains escape sequences.
|
Find all occurences of a pattern in a string that contains escape sequences.
|
||||||
Yields pairs of starting and ending indices of the pattern.
|
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
|
noescapes += noescape
|
||||||
|
|
||||||
if len(offsets) == 0:
|
if len(offsets) == 0:
|
||||||
return _null_generator()
|
return (item for item in [])
|
||||||
|
|
||||||
last_offset = offsets[-1]
|
last_offset = offsets[-1]
|
||||||
offsets.append(last_offset)
|
offsets.append(last_offset)
|
||||||
|
@ -159,13 +155,13 @@ def _unescaped_matches(regexp: str, s: str, escape_chr="\\"):
|
||||||
yield off1, off2
|
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.
|
Yields substrings of a string that contains escape sequences.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
last_end = 0
|
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]
|
yield s[last_end:start]
|
||||||
last_end = end
|
last_end = end
|
||||||
if last_end < len(s):
|
if last_end < len(s):
|
||||||
|
@ -174,7 +170,7 @@ def _unescaped_separated(regexp: str, s: str, escape_chr="\\"):
|
||||||
yield ""
|
yield ""
|
||||||
|
|
||||||
|
|
||||||
def _parse_cli_value(s: str):
|
def p_parse_cli_value(s: str):
|
||||||
"""
|
"""
|
||||||
Parse a value/dependency passed to CLI
|
Parse a value/dependency passed to CLI
|
||||||
CLI values are generated by the following non-contextual grammar:
|
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)]
|
inner = s[1 : (len(s) - 1)]
|
||||||
if inner == "":
|
if inner == "":
|
||||||
return []
|
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
|
# Dictionary
|
||||||
if s[0] == "{":
|
if s[0] == "{":
|
||||||
|
@ -217,14 +213,14 @@ def _parse_cli_value(s: str):
|
||||||
inner = s[1 : (len(s) - 1)]
|
inner = s[1 : (len(s) - 1)]
|
||||||
if inner == "":
|
if inner == "":
|
||||||
return {}
|
return {}
|
||||||
for kv in _unescaped_separated(",", inner):
|
for kv in p_unescaped_separated(",", inner):
|
||||||
k_v = list(_unescaped_separated(":", kv))
|
k_v = list(p_unescaped_separated(":", kv))
|
||||||
if len(k_v) < 2:
|
if len(k_v) < 2:
|
||||||
raise Exception("Missing value in dictionary entry")
|
raise Exception("Missing value in dictionary entry")
|
||||||
if len(k_v) > 2:
|
if len(k_v) > 2:
|
||||||
raise Exception("Unexpected ':' token")
|
raise Exception("Unexpected ':' token")
|
||||||
key = k_v[0]
|
key = k_v[0]
|
||||||
value = _parse_cli_value(k_v[1])
|
value = p_parse_cli_value(k_v[1])
|
||||||
d[key] = value
|
d[key] = value
|
||||||
|
|
||||||
return d
|
return d
|
||||||
|
@ -253,7 +249,7 @@ def get_cli_flow_config(args: Namespace, part: str):
|
||||||
part_flow_config = create_defdict()
|
part_flow_config = create_defdict()
|
||||||
|
|
||||||
def add_entries(arglist: "list[str]", dict_name: str):
|
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"]
|
stage = value_def["stage"]
|
||||||
if stage is None:
|
if stage is None:
|
||||||
part_flow_config[dict_name][value_def["name"]] = value_def["value"]
|
part_flow_config[dict_name][value_def["name"]] = value_def["value"]
|
||||||
|
|
Loading…
Reference in New Issue