From 5b922143941f97f468f4221fd027c1f6eb94e398 Mon Sep 17 00:00:00 2001 From: Brian Marco Date: Mon, 20 Nov 2023 15:00:49 -0500 Subject: [PATCH 1/4] partial support for map based selector conversion --- boa/cli/convert.py | 48 +++++++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/boa/cli/convert.py b/boa/cli/convert.py index 784e1e97..28fcac05 100644 --- a/boa/cli/convert.py +++ b/boa/cli/convert.py @@ -6,6 +6,7 @@ from ruamel.yaml.representer import RoundTripRepresenter from ruamel.yaml.comments import CommentedMap from ruamel.yaml import YAML +from ruamel.yaml.parser import ParserError from collections import OrderedDict import re @@ -14,6 +15,10 @@ class MyRepresenter(RoundTripRepresenter): pass +class UnevenSelectorException(Exception): + pass + + ruamel.yaml.add_representer( OrderedDict, MyRepresenter.represent_dict, representer=MyRepresenter ) @@ -68,27 +73,19 @@ def main(docname): result_yaml = CommentedMap() result_yaml["context"] = context - def has_selector(s): - return s.strip().endswith("]") + selector_re = re.compile("( *)(-?)(.*)# \[(.*)\]") - quoted_lines = [] + selector_lines = [] for line in rest_lines: - if has_selector(line): - selector_start = line.rfind("[") - selector_end = line.rfind("]") - selector_content = line[selector_start + 1 : selector_end] - - if line.strip().startswith("-"): - line = ( - line[: line.find("-") + 1] - + f" sel({selector_content}): " - + line[ - line.find("-") + 1 : min(line.rfind("#"), line.rfind("[")) - ].strip() - + "\n" - ) - quoted_lines.append(line) - rest_lines = quoted_lines + m = selector_re.match(line) + if m and m.group(2): + line = m.group(1) + "- sel(" + m.group(4) + "):" + m.group(3) + "\n" + elif m: + sel_line = m.group(1) + "- sel(" + m.group(4) + "):\n" + selector_lines.append(sel_line) + line = m.group(1) + " " + m.group(3) + "\n" + selector_lines.append(line) + rest_lines = selector_lines def check_if_quoted(s): s = s.strip() @@ -128,7 +125,18 @@ def check_if_quoted(s): wo_skip_lines.append(line) rest_lines = wo_skip_lines - result_yaml.update(yaml.load("".join(rest_lines))) + + try: + result_yaml.update(yaml.load("".join(rest_lines))) + except ParserError as e: + if "expected , but found '?'" == e.problem: + msg = str( + 'Possible error due to selector lines disrupting yaml maps. Try adding a "- " before the offending entry to convert it to a list item:\n' + + e.problem_mark.get_snippet() + ) + raise UnevenSelectorException(msg) from e + else: + raise if len(skips) != 0: result_yaml["build"]["skip"] = skips From 4f291469f0786dcb321a25e0a74481986adafd76 Mon Sep 17 00:00:00 2001 From: Brian Marco Date: Mon, 20 Nov 2023 15:48:38 -0500 Subject: [PATCH 2/4] regex double escape backslash --- boa/cli/convert.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/boa/cli/convert.py b/boa/cli/convert.py index 28fcac05..3777a4e1 100644 --- a/boa/cli/convert.py +++ b/boa/cli/convert.py @@ -73,7 +73,7 @@ def main(docname): result_yaml = CommentedMap() result_yaml["context"] = context - selector_re = re.compile("( *)(-?)(.*)# \[(.*)\]") + selector_re = re.compile("( *)(-?)(.*)# \\[(.*)\\]") selector_lines = [] for line in rest_lines: From 3c69d9fc76a140b4c04a0ad56acf8156bb556323 Mon Sep 17 00:00:00 2001 From: Brian Marco Date: Tue, 21 Nov 2023 09:12:10 -0500 Subject: [PATCH 3/4] give selector lines sticky behavior for maps --- boa/cli/convert.py | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/boa/cli/convert.py b/boa/cli/convert.py index 3777a4e1..9e420e4e 100644 --- a/boa/cli/convert.py +++ b/boa/cli/convert.py @@ -6,7 +6,6 @@ from ruamel.yaml.representer import RoundTripRepresenter from ruamel.yaml.comments import CommentedMap from ruamel.yaml import YAML -from ruamel.yaml.parser import ParserError from collections import OrderedDict import re @@ -15,10 +14,6 @@ class MyRepresenter(RoundTripRepresenter): pass -class UnevenSelectorException(Exception): - pass - - ruamel.yaml.add_representer( OrderedDict, MyRepresenter.represent_dict, representer=MyRepresenter ) @@ -76,14 +71,17 @@ def main(docname): selector_re = re.compile("( *)(-?)(.*)# \\[(.*)\\]") selector_lines = [] + prev_selector_line = "" for line in rest_lines: m = selector_re.match(line) if m and m.group(2): line = m.group(1) + "- sel(" + m.group(4) + "):" + m.group(3) + "\n" elif m: - sel_line = m.group(1) + "- sel(" + m.group(4) + "):\n" - selector_lines.append(sel_line) + selector_line = m.group(1) + "sel(" + m.group(4) + "):\n" + if selector_line != prev_selector_line: + selector_lines.append(selector_line) line = m.group(1) + " " + m.group(3) + "\n" + prev_selector_line = selector_line selector_lines.append(line) rest_lines = selector_lines @@ -126,17 +124,7 @@ def check_if_quoted(s): rest_lines = wo_skip_lines - try: - result_yaml.update(yaml.load("".join(rest_lines))) - except ParserError as e: - if "expected , but found '?'" == e.problem: - msg = str( - 'Possible error due to selector lines disrupting yaml maps. Try adding a "- " before the offending entry to convert it to a list item:\n' - + e.problem_mark.get_snippet() - ) - raise UnevenSelectorException(msg) from e - else: - raise + result_yaml.update(yaml.load("".join(rest_lines))) if len(skips) != 0: result_yaml["build"]["skip"] = skips From d68916d9b4c00c44f32bc21450e4b7df9316d59f Mon Sep 17 00:00:00 2001 From: Brian Marco Date: Tue, 21 Nov 2023 09:18:45 -0500 Subject: [PATCH 4/4] more robust regular expression --- boa/cli/convert.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/boa/cli/convert.py b/boa/cli/convert.py index 9e420e4e..a2a5b7a2 100644 --- a/boa/cli/convert.py +++ b/boa/cli/convert.py @@ -68,7 +68,7 @@ def main(docname): result_yaml = CommentedMap() result_yaml["context"] = context - selector_re = re.compile("( *)(-?)(.*)# \\[(.*)\\]") + selector_re = re.compile("( *)(-?)([^#]*)# \\[(.*)\\]\\W*") selector_lines = [] prev_selector_line = ""