Skip to content

Commit

Permalink
FlowParser: Don't parse templates for excluded rows
Browse files Browse the repository at this point in the history
  • Loading branch information
geoo89 committed Feb 8, 2023
1 parent 6a2478b commit 50e0f5e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
14 changes: 9 additions & 5 deletions parsers/common/cellparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,25 +67,29 @@ def parse(self, value, context={}):
return self.split_into_lists(value)

def parse_as_string(self, value, context={}, is_object=None):
# If context is None, template parsing is omitted entirely.
# is_object is a pass-by-reference boolean, realised via
# the class BooleanWrapper, to indicate to the caller
# whether the parsing result represents an object that
# is not to be processed any further.
if value is None:
return ''
if not context and '{' not in value:
if context is None or (not context and '{' not in value):
# This is a hacky optimization.
return value
stripped_value = value.strip()
env = self.env
if stripped_value.startswith('{@') and stripped_value.endswith('@}'):
# Special case: Return a python object rather than a string,
# if possible.
# Ensure this is a single template, not e.g. '{@ x @} {@ y @}'
assert stripped_value[2:].find('{@') == -1
if is_object is not None:
is_object.boolean = True
template = self.native_env.from_string(stripped_value)
return template.render(context)
else:
template = self.env.from_string(stripped_value)
env = self.native_env

try:
template = env.from_string(stripped_value)
return template.render(context)
except Exception as e:
raise ValueError(str(e), stripped_value, context)
5 changes: 3 additions & 2 deletions parsers/common/sheetparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ def go_to_bookmark(self, name):
def remove_bookmark(self, name):
self.bookmarks.pop(name)

def parse_next_row(self):
def parse_next_row(self, omit_templating=False):
try:
input_row = next(self.iterator)
except StopIteration:
return None
row = self.row_parser.parse_row(input_row, self.context)
context = self.context if not omit_templating else None
row = self.row_parser.parse_row(input_row, context)
return row

def parse_all(self):
Expand Down
2 changes: 1 addition & 1 deletion parsers/common/tests/mock_sheetparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def __init__(self, row_parser, rows, context={}):
self.iterator = iter(self.input_rows)
self.context = copy.deepcopy(context)

def parse_next_row(self):
def parse_next_row(self, omit_templating=False):
# Simply return the input.
try:
input_row = next(self.iterator)
Expand Down
4 changes: 2 additions & 2 deletions parsers/creation/flowparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ def parse(self):
return flow_container

def _parse_block(self, depth=0, block_type='root_block', omit_content=False):
row = self.sheet_parser.parse_next_row()
row = self.sheet_parser.parse_next_row(omit_templating=omit_content)
while not self._is_end_of_block(block_type, row):
if omit_content or not row.include_if:
if row.type == 'begin_for':
Expand Down Expand Up @@ -349,7 +349,7 @@ def _parse_block(self, depth=0, block_type='root_block', omit_content=False):
self.append_node_group(new_node_group, row.row_id)
else:
self._parse_row(row)
row = self.sheet_parser.parse_next_row()
row = self.sheet_parser.parse_next_row(omit_templating=omit_content)

def _is_end_of_block(self, block_type, row):
block_end_map = {
Expand Down

0 comments on commit 50e0f5e

Please sign in to comment.