Skip to content

Commit 90fc6d8

Browse files
authored
Merge pull request #1 from epics-containers/auto-normalize
add automatic normalization of expands macro lists
2 parents 4d78e9e + 9e00291 commit 90fc6d8

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

src/vdct2template/convert.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def convert(folder: Path, builder_txt: str):
4141
template_path.write_text(text)
4242

4343
# give warnings if there are inconsistent macro substitutions
44+
# NOTE: process_includes() should have already fixed this!
4445
warning |= Expansion.validate_includes()
4546

4647
if warning:

src/vdct2template/expansion.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ def __init__(self, filename: Path, folder: Path) -> None:
3232

3333
def parse_expands(self) -> int:
3434
"""
35-
Parse the expands() blocks in the VDB file.
35+
Parse an expands() blocks in a VDB file.
3636
37-
Updates the class attribute substitutions with the macro substitutions parsed.
37+
Updates the class attribute 'substitutions' with the macro substitutions parsed.
3838
Updates the class attribute text with the VDB file text with the expands()
3939
blocks processed into MSI substitute/include statements.
4040
@@ -50,6 +50,7 @@ def parse_expands(self) -> int:
5050
include_path = self.folder / match[0]
5151
macros = Macros(self.template_path, include_path, match[2])
5252
self.includes.append(macros)
53+
self._normalise_macros(macros)
5354

5455
# replace the expands() block with the MSI directives
5556
self.text = EXPAND.sub(macros.render_include(), self.text, 1)
@@ -61,9 +62,32 @@ def parse_expands(self) -> int:
6162

6263
return len(expands)
6364

65+
def _normalise_macros(self, macros: Macros):
66+
"""
67+
Given a set of macros for a given expand block, search for all other
68+
instances of an expand against the same template file.
69+
70+
Make sure that this set of macros is consistent with all other instances
71+
by adding in a self referencing macro for any missing ones out of the list
72+
of all macros passed by all instances of such an expansion. (OMLGG!)
73+
"""
74+
vdb_list = self.folder.glob("*.vdb")
75+
for vdb in vdb_list:
76+
vdb_text = vdb.read_text()
77+
expands = EXPAND.findall(vdb_text)
78+
for match in expands:
79+
# match: 0=include path, 1=name, 2=macro text
80+
if match[0] == macros.vdb_path.name:
81+
other_macros = Macros(self.template_path, macros.vdb_path, match[2])
82+
for macro in other_macros.macros:
83+
if macro not in macros.macros:
84+
print(f"adding missing {macro} to {macros.parent.name}")
85+
macros.macros[macro] = f"$({macro})"
86+
6487
def process_includes(self):
6588
"""
66-
Process the included files for this VDB file.
89+
Process the included files for this VDB file. Returns a generator of
90+
tuples of the file and the text to write to the file.
6791
"""
6892
for include in self.includes:
6993
if include.vdb_path.name not in Expansion.processed:
@@ -77,6 +101,9 @@ def validate_includes(cls) -> bool:
77101
every time they are included. If not then the the replacing of macro
78102
names with _ prefix will be inconsistent between uses of the included
79103
templates and this approach will fail.
104+
105+
NOTE: with the introduction of _normalise_macros() this should not be
106+
necessary but it is left in for now as a backup check.
80107
"""
81108
warning = False
82109
index: Dict[str, Macros] = {}

src/vdct2template/regex.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
DROP = re.compile(r"#!.*\n")
55

66
# template blocks are also redundant
7-
TEMPLATE = re.compile(r"template *\( *\) * {[\S\s]*?}")
7+
TEMPLATE = re.compile(r"^ *template *\( *\) * {[\S\s]*?}", re.M)
88

99
# this extracts the arguments from expand blocks
10-
EXPAND = re.compile(r'expand\("(.*)" *, *([^\)]*)\) *[\s\S]*?{([\s\S]*?)}')
10+
EXPAND = re.compile(r'^ *expand\("(.*)" *, *([^\)]*)\) *[\s\S]*?{([\s\S]*?)}', re.M)
1111

1212
# this extracts the macro entries from an expand block's 3rd argument
13-
MACRO = re.compile(r' *macro *\(([^,]*), *"([^"]*) *')
13+
MACRO = re.compile(r'^ *macro *\(([^,]*), *"([^"]*) *', re.M)

0 commit comments

Comments
 (0)