Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore rows in excluded blocks #66

Merged
merged 2 commits into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/contentindexparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ def map_template_arguments_to_context(self, arg_defs, args, context):
for arg_def, arg in zip(arg_defs, args + args_padding):
if arg_def.name in context:
raise ValueError(f'Template argument "{arg_def.name}" doubly defined in context')
arg_value = arg or arg_def.default_value
if not arg_value:
arg_value = arg if arg != '' else arg_def.default_value
if arg_value == '':
raise ValueError(f'Required template argument "{arg_def.name}" not provided')
if arg_def.type == 'sheet':
context[arg_def.name] = self.data_sheets[arg_value]
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