From 3e14132aaf73b274f5942d65829963a39c87eae5 Mon Sep 17 00:00:00 2001 From: "Leslie H." Date: Fri, 14 Jun 2024 19:56:22 -0500 Subject: [PATCH] Handle wrong/unsupported syntax in pred formulas --- sandbox/grist/acl.py | 8 ++++++-- sandbox/grist/dropdown_condition.py | 9 ++++++--- sandbox/grist/predicate_formula.py | 10 ++++++++-- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/sandbox/grist/acl.py b/sandbox/grist/acl.py index d06cc060952..d3059f6d7ba 100644 --- a/sandbox/grist/acl.py +++ b/sandbox/grist/acl.py @@ -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) diff --git a/sandbox/grist/dropdown_condition.py b/sandbox/grist/dropdown_condition.py index 51f3c257901..d13df6d3987 100644 --- a/sandbox/grist/dropdown_condition.py +++ b/sandbox/grist/dropdown_condition.py @@ -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. diff --git a/sandbox/grist/predicate_formula.py b/sandbox/grist/predicate_formula.py index 2e82394fd5d..8b7d2d3694d 100644 --- a/sandbox/grist/predicate_formula.py +++ b/sandbox/grist/predicate_formula.py @@ -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): """ @@ -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)