Skip to content

Commit 53d3a32

Browse files
committed
Add a global debug setting
If not in debug mode, use a simpler EDTFParseException rather than returning the full pyparsing error
1 parent c14a57b commit 53d3a32

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

edtf/appsettings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,5 @@
9898
MULTIPLIER_IF_APPROXIMATE = EDTF.get("MULTIPLIER_IF_APPROXIMATE", 1.0)
9999
MULTIPLIER_IF_BOTH = EDTF.get("MULTIPLIER_IF_BOTH", 2.0)
100100
DELTA_IF_UNKNOWN = EDTF.get("DELTA_IF_UNKNOWN", relativedelta(years=10))
101+
102+
DEBUG_PYPARSING = False

edtf/parser/grammar.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# https://github.com/pyparsing/pyparsing/wiki/Performance-Tips
55

66
import pyparsing
7+
from edtf.appsettings import DEBUG_PYPARSING
78

89
pyparsing.ParserElement.enablePackrat()
910

@@ -342,7 +343,9 @@ def f(toks):
342343
)
343344

344345

345-
def parse_edtf(str, parseAll=True, fail_silently=False):
346+
def parse_edtf(str, parseAll=True, fail_silently=False, debug=None):
347+
if debug is None:
348+
debug = DEBUG_PYPARSING
346349
try:
347350
if not str:
348351
raise ParseException("You must supply some input text")
@@ -352,4 +355,8 @@ def parse_edtf(str, parseAll=True, fail_silently=False):
352355
except ParseException as err:
353356
if fail_silently:
354357
return None
355-
raise EDTFParseException(err) from err
358+
if debug:
359+
raise
360+
near_text = str[max(err.loc - 10, 0) : err.loc + 10]
361+
full_msg = f"Error at position {err.loc}: Invalid input or format near '{near_text}'. Please provide a valid EDTF string."
362+
raise EDTFParseException(full_msg) from None

0 commit comments

Comments
 (0)