Skip to content

Commit

Permalink
Add a global debug setting
Browse files Browse the repository at this point in the history
If not in debug mode, use a simpler EDTFParseException rather than returning the full pyparsing error
  • Loading branch information
ColeDCrawford committed May 28, 2024
1 parent c14a57b commit 53d3a32
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
2 changes: 2 additions & 0 deletions edtf/appsettings.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,5 @@
MULTIPLIER_IF_APPROXIMATE = EDTF.get("MULTIPLIER_IF_APPROXIMATE", 1.0)
MULTIPLIER_IF_BOTH = EDTF.get("MULTIPLIER_IF_BOTH", 2.0)
DELTA_IF_UNKNOWN = EDTF.get("DELTA_IF_UNKNOWN", relativedelta(years=10))

DEBUG_PYPARSING = False
11 changes: 9 additions & 2 deletions edtf/parser/grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# https://github.com/pyparsing/pyparsing/wiki/Performance-Tips

import pyparsing
from edtf.appsettings import DEBUG_PYPARSING

pyparsing.ParserElement.enablePackrat()

Expand Down Expand Up @@ -342,7 +343,9 @@ def f(toks):
)


def parse_edtf(str, parseAll=True, fail_silently=False):
def parse_edtf(str, parseAll=True, fail_silently=False, debug=None):
if debug is None:
debug = DEBUG_PYPARSING
try:
if not str:
raise ParseException("You must supply some input text")
Expand All @@ -352,4 +355,8 @@ def parse_edtf(str, parseAll=True, fail_silently=False):
except ParseException as err:
if fail_silently:
return None
raise EDTFParseException(err) from err
if debug:
raise
near_text = str[max(err.loc - 10, 0) : err.loc + 10]
full_msg = f"Error at position {err.loc}: Invalid input or format near '{near_text}'. Please provide a valid EDTF string."
raise EDTFParseException(full_msg) from None

0 comments on commit 53d3a32

Please sign in to comment.