forked from XLSForm/pyxform
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chg: raise an error if a pyxform reference is malformed
- pyxform_reference.py - new module to check pyxform reference syntax - unit test generates 2874 unique usage permutations - xls2json.py - remove extra iteration of sheet data in replace_smart_quotes by combining it with clean_text_values and calling that instead - expression.py - move expression parser from utils to here - update usages of lexer to reference a cached wrapper func instead - use compiled regex patterns directly instead of importing re as well
- Loading branch information
1 parent
a724215
commit 1a01e17
Showing
12 changed files
with
305 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from errors import PyXFormError | ||
from pyxform import constants as co | ||
from pyxform.parsing.expression import parse_expression | ||
|
||
PYXFORM_REFERENCE_INVALID = ( | ||
"[row : {row_number}] On the '{sheet}' sheet, the '{column}' value is invalid. " | ||
"Reference expressions must only include question names, and end with '}}'." | ||
) | ||
|
||
|
||
def validate_pyxform_reference_syntax( | ||
value: str, sheet_name: str, row_number: int, key: str | ||
) -> None: | ||
# Skip columns in potentially large sheets where references are not allowed. | ||
if sheet_name == co.SURVEY: | ||
if key in (co.TYPE, co.NAME): | ||
return | ||
elif sheet_name == co.CHOICES: | ||
if key in (co.LIST_NAME_S, co.LIST_NAME_U, co.NAME): | ||
return | ||
elif sheet_name == co.ENTITIES: | ||
if key == (co.LIST_NAME_S, co.LIST_NAME_U): | ||
return | ||
|
||
tokens, _ = parse_expression(value) | ||
start_token = None | ||
|
||
for t in tokens: | ||
# The start of an expression. | ||
if t is not None and t.name == "PYXFORM_REF_START" and start_token is None: | ||
start_token = t | ||
# Tokens that are part of an expression. | ||
elif start_token is not None: | ||
if t.name == "NAME": | ||
continue | ||
elif t.name == "PYXFORM_REF_END": | ||
start_token = None | ||
elif t.name in ("PYXFORM_REF_START", "PYXFORM_REF"): | ||
msg = PYXFORM_REFERENCE_INVALID.format( | ||
sheet=sheet_name, row_number=row_number, column=key | ||
) | ||
raise PyXFormError(msg) | ||
else: | ||
msg = PYXFORM_REFERENCE_INVALID.format( | ||
sheet=sheet_name, row_number=row_number, column=key | ||
) | ||
raise PyXFormError(msg) | ||
|
||
if start_token is not None: | ||
msg = PYXFORM_REFERENCE_INVALID.format( | ||
sheet=sheet_name, row_number=row_number, column=key | ||
) | ||
raise PyXFormError(msg) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.