Skip to content

Commit b5ec562

Browse files
aj-fuentesKangOl
andcommitted
[IMP] util/misc: allow more than two terms in expand_braces
It's useful to specify xmlids, e.g. ``` names = [ *eb("mod_111_{03,06,09,12}"), *eb("mod_303_{01,03,04,06}"), ] ``` Part of odoo/upgrade#5177 Signed-off-by: Alvaro Fuentes Suarez (afu) <[email protected]> Co-authored-by: Christophe Simonis <[email protected]>
1 parent 4daa21b commit b5ec562

File tree

2 files changed

+50
-9
lines changed

2 files changed

+50
-9
lines changed

src/base/tests/test_util.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,3 +753,48 @@ def test_replace_in_all_jsonb_values(self):
753753
self.assertRegex(test_partner_title.name, pattern_new)
754754
self.assertRegex(test_partner_title.name, pattern_notouch)
755755
self.assertNotRegex(test_partner_title.name, pattern_old)
756+
757+
758+
class TestMisc(UnitTestCase):
759+
@parametrize(
760+
[
761+
("{a,b}", ["a", "b"]),
762+
("head_{a,b}_tail", ["head_a_tail", "head_b_tail"]),
763+
("head_only_{a,b}", ["head_only_a", "head_only_b"]),
764+
("{a,b}_tail_only", ["a_tail_only", "b_tail_only"]),
765+
("{with,more,than,one,comma}", ["with", "more", "than", "one", "comma"]),
766+
("head_{one,two,three}_tail", ["head_one_tail", "head_two_tail", "head_three_tail"]),
767+
("same_{a,a}", ["same_a", "same_a"]),
768+
("empty_part_{a,}", ["empty_part_a", "empty_part_"]),
769+
("empty_part_{,b}", ["empty_part_", "empty_part_b"]),
770+
("two_empty_{,}", ["two_empty_", "two_empty_"]),
771+
("with_cr\n_{a,b}", ["with_cr\n_a", "with_cr\n_b"]),
772+
("with_cr_in_{a\nb,c\nd}_end", ["with_cr_in_a\nb_end", "with_cr_in_c\nd_end"]),
773+
]
774+
)
775+
def test_expand_braces(self, value, expected):
776+
self.assertEqual(util.expand_braces(value), expected)
777+
778+
@parametrize(
779+
[
780+
(value,)
781+
for value in [
782+
"",
783+
"no_braces",
784+
"empty_{}",
785+
"one_{item}",
786+
"unclosed_{_brace",
787+
"two_{a,b}_expanses_{x,y}",
788+
# braces into braces
789+
"{a,{b,c},d}",
790+
"{a,{}",
791+
"{a,b}c}",
792+
"{a,{b,}",
793+
"{{}}",
794+
"{{one}}",
795+
]
796+
]
797+
)
798+
def test_expand_braces_failure(self, value):
799+
with self.assertRaises(ValueError):
800+
util.expand_braces(value)

src/util/misc.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,18 +108,14 @@ def splitlines(s):
108108

109109
def expand_braces(s):
110110
# expand braces (a la bash)
111-
# only handle one expension of a 2 parts (because we don't need more)
112-
r = re.compile(r"(.*){([^},]*?,[^},]*?)}(.*)")
111+
r = re.compile(r"(.*){((?:[^},]*?)(?:,[^},]*?)+)}(.*)", flags=re.DOTALL)
113112
m = r.search(s)
114113
if not m:
115114
raise ValueError("No expansion braces found")
116-
head, match, tail = m.groups()
117-
a, b = match.split(",")
118-
first = head + a + tail
119-
second = head + b + tail
120-
if r.search(first): # as the regexp will match the last expansion, we only need to verify first term
121-
raise ValueError("Multiple expansion braces found")
122-
return [first, second]
115+
head, matches, tail = m.groups()
116+
if re.search("[}{]", head + matches + tail):
117+
raise ValueError("Extra braces detected")
118+
return [head + x + tail for x in matches.split(",")]
123119

124120

125121
def split_osenv(name):

0 commit comments

Comments
 (0)