Skip to content

Commit

Permalink
Handle wrong/unsupported syntax in pred formulas
Browse files Browse the repository at this point in the history
  • Loading branch information
SleepyLeslie committed Jun 15, 2024
1 parent b591d3f commit 3e14132
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
8 changes: 6 additions & 2 deletions sandbox/grist/acl.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,12 @@ def renamer(subject):
return new_col_id

new_acl_formula = predicate_formula.process_renames(formula, _ACLEntityCollector(), renamer)
rule_updates.append((rule_rec, {'aclFormula': new_acl_formula,
'aclFormulaParsed': parse_predicate_formula_json(new_acl_formula)}))
new_rule_record = {"aclFormula": new_acl_formula}
try:
new_rule_record["aclFormulaParsed"] = parse_predicate_formula_json(new_acl_formula)
except SyntaxError:
pass
rule_updates.append(new_rule_record)

useractions.doBulkUpdateFromPairs('_grist_ACLResources', resource_updates)
useractions.doBulkUpdateFromPairs('_grist_ACLRules', rule_updates)
9 changes: 6 additions & 3 deletions sandbox/grist/dropdown_condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,12 @@ def renamer(subject):

new_dc_formula = predicate_formula.process_renames(dc_formula, _DCEntityCollector(), renamer)

# Parse the new dropdown condition formula.
widget_options["dropdownCondition"] = {"text": new_dc_formula,
"parsed": parse_predicate_formula_json(new_dc_formula)}
widget_options["dropdownCondition"] = {"text": new_dc_formula}
try:
# Parse the new dropdown condition formula if it is syntactically correct.
widget_options["dropdownCondition"]["parsed"] = parse_predicate_formula_json(new_dc_formula)
except SyntaxError:
pass
updates.append((col, {"widgetOptions": json.dumps(widget_options)}))

# Update the dropdown condition in the database.
Expand Down
10 changes: 8 additions & 2 deletions sandbox/grist/predicate_formula.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,12 @@ def parse_predicate_formula(formula):
result = ['Comment', result, part[1][1:].strip()]
break
return result
except SyntaxError as err:
except SyntaxError as e:
# In case of an error, include line and offset.
raise SyntaxError("%s on line %s col %s" % (err.args[0], err.lineno, err.offset))
raise SyntaxError("%s on line %s col %s" % (e.args[0], e.lineno, e.offset))
except ValueError as e:
# ValueError can be raised by TreeConverter.generic_visit when there is unsupported syntax.
raise SyntaxError(str(e))

def parse_predicate_formula_json(formula):
"""
Expand Down Expand Up @@ -91,6 +94,9 @@ def process_renames(formula, collector, renamer):
except SyntaxError:
# Don't do anything to a syntactically wrong formula.
return formula
except ValueError as e:
if str(e).startswith("Unsupported syntax"):
return formula

for subject in collector.entities:
new_name = renamer(subject)
Expand Down

0 comments on commit 3e14132

Please sign in to comment.