Skip to content

Commit

Permalink
check for expression and value slots
Browse files Browse the repository at this point in the history
  • Loading branch information
mccalluc committed Nov 19, 2024
1 parent 958d36e commit e2beaa2
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions dp_wizard/utils/code_generators/_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ def __init__(self, path, template=None):
raise Exception('"path" and "template" are mutually exclusive')
self._path = "template-instead-of-path"
self._template = template
# We want a list of the initial slots, because substitutions
# can produce sequences of upper case letters that could be mistaken for slots.
self._initial_slots = self._find_slots()

def _find_slots(self):
Expand All @@ -26,13 +28,17 @@ def _find_slots(self):
def fill_expressions(self, **kwargs):
for k, v in kwargs.items():
k_re = re.escape(k)
self._template = re.sub(rf"\b{k_re}\b", str(v), self._template)
self._template, count = re.subn(rf"\b{k_re}\b", str(v), self._template)
if count == 0:
raise Exception(f"No slot for '{k}' in {self._path}")
return self

def fill_values(self, **kwargs):
for k, v in kwargs.items():
k_re = re.escape(k)
self._template = re.sub(rf"\b{k_re}\b", repr(v), self._template)
self._template, count = re.subn(rf"\b{k_re}\b", repr(v), self._template)
if count == 0:
raise Exception(f"No slot for '{k}' in {self._path}")
return self

def fill_blocks(self, **kwargs):
Expand Down

0 comments on commit e2beaa2

Please sign in to comment.