From 53d3a32c9fe0b18fb7aa550de4478cc18550bc2f Mon Sep 17 00:00:00 2001 From: Cole Crawford <16374762+ColeDCrawford@users.noreply.github.com> Date: Tue, 28 May 2024 15:10:46 -0400 Subject: [PATCH 1/5] Add a global debug setting If not in debug mode, use a simpler EDTFParseException rather than returning the full pyparsing error --- edtf/appsettings.py | 2 ++ edtf/parser/grammar.py | 11 +++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/edtf/appsettings.py b/edtf/appsettings.py index e00a223..8e15846 100644 --- a/edtf/appsettings.py +++ b/edtf/appsettings.py @@ -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 diff --git a/edtf/parser/grammar.py b/edtf/parser/grammar.py index f458b2b..1e624fc 100644 --- a/edtf/parser/grammar.py +++ b/edtf/parser/grammar.py @@ -4,6 +4,7 @@ # https://github.com/pyparsing/pyparsing/wiki/Performance-Tips import pyparsing +from edtf.appsettings import DEBUG_PYPARSING pyparsing.ParserElement.enablePackrat() @@ -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") @@ -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 From ab6c41320eb2354bbf68b78ec5d121a0709dd777 Mon Sep 17 00:00:00 2001 From: Cole Crawford <16374762+ColeDCrawford@users.noreply.github.com> Date: Tue, 28 May 2024 15:39:42 -0400 Subject: [PATCH 2/5] Handle empty string --- edtf/parser/grammar.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/edtf/parser/grammar.py b/edtf/parser/grammar.py index 1e624fc..773f806 100644 --- a/edtf/parser/grammar.py +++ b/edtf/parser/grammar.py @@ -343,13 +343,13 @@ def f(toks): ) -def parse_edtf(str, parseAll=True, fail_silently=False, debug=None): +def parse_edtf(input_string, parseAll=True, fail_silently=False, debug=None): if debug is None: debug = DEBUG_PYPARSING try: - if not str: + if not input_string: raise ParseException("You must supply some input text") - p = edtfParser.parseString(str.strip(), parseAll) + p = edtfParser.parseString(input_string.strip(), parseAll) if p: return p[0] except ParseException as err: @@ -357,6 +357,8 @@ def parse_edtf(str, parseAll=True, fail_silently=False, debug=None): return None if debug: raise - near_text = str[max(err.loc - 10, 0) : err.loc + 10] + near_text = "" + if input_string: + near_text = input_string[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 From 55b0723754b7eb606820b11ccc7bb04d5a6232b3 Mon Sep 17 00:00:00 2001 From: Cole Crawford <16374762+ColeDCrawford@users.noreply.github.com> Date: Mon, 3 Jun 2024 15:58:07 -0400 Subject: [PATCH 3/5] Add targeted failure and tests for empty and null inputs --- edtf/parser/grammar.py | 4 ++-- edtf/parser/tests.py | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/edtf/parser/grammar.py b/edtf/parser/grammar.py index 773f806..651b4b3 100644 --- a/edtf/parser/grammar.py +++ b/edtf/parser/grammar.py @@ -346,9 +346,9 @@ def f(toks): def parse_edtf(input_string, parseAll=True, fail_silently=False, debug=None): if debug is None: debug = DEBUG_PYPARSING + if not input_string: + raise EDTFParseException("You must supply some input text") try: - if not input_string: - raise ParseException("You must supply some input text") p = edtfParser.parseString(input_string.strip(), parseAll) if p: return p[0] diff --git a/edtf/parser/tests.py b/edtf/parser/tests.py index 199f245..15875b9 100644 --- a/edtf/parser/tests.py +++ b/edtf/parser/tests.py @@ -347,6 +347,14 @@ def test_non_parsing(bad_input): parse(bad_input) +@pytest.mark.parametrize("bad_input", [None, ""]) +def test_empty_input(bad_input): + """Test that empty input raises a specific exception.""" + with pytest.raises(EDTFParseException) as exc_info: + parse(bad_input) + assert "You must supply some input text" in str(exc_info.value) + + def test_comparisons(): """Test comparisons between parsed EDTF objects and standard dates.""" d1 = parse("1979-08~") From d5ad27b37916ebe333642de1cc5b20ea5986465a Mon Sep 17 00:00:00 2001 From: Cole Crawford <16374762+ColeDCrawford@users.noreply.github.com> Date: Mon, 3 Jun 2024 18:00:32 -0400 Subject: [PATCH 4/5] Improve EDTFParseException handling Includes handling for empty or null input strings and null errs passed to the constructor Co-Authored-By: aweakley <224316+aweakley@users.noreply.github.com> --- edtf/fields.py | 12 ++++++++---- edtf/parser/edtf_exceptions.py | 26 +++++++++++++++++++++++++- edtf/parser/grammar.py | 8 ++------ 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/edtf/fields.py b/edtf/fields.py index f717592..2f25c94 100644 --- a/edtf/fields.py +++ b/edtf/fields.py @@ -4,10 +4,12 @@ from django.db import models from django.db.models import signals from django.db.models.query_utils import DeferredAttribute +from pyparsing import ParseException from edtf import EDTFObject, parse_edtf from edtf.convert import struct_time_to_date, struct_time_to_jd from edtf.natlang import text_to_edtf +from edtf.parser.edtf_exceptions import EDTFParseException DATE_ATTRS = ( "lower_strict", @@ -132,10 +134,12 @@ def update_values(self, instance, *args, **kwargs): if direct_input and ( existing_value is None or str(existing_value) != direct_input ): - edtf = parse_edtf( - direct_input, fail_silently=True - ) # ParseException if invalid; should this be raised? - # TODO pyparsing.ParseExceptions are very noisy and dumps the whole grammar (see https://github.com/ixc/python-edtf/issues/46) + try: + edtf = parse_edtf( + direct_input, fail_silently=True + ) # ParseException if invalid; should this be raised? + except ParseException as err: + raise EDTFParseException(direct_input, err) from None # set the natural_text (display) field to the direct_input if it is not provided if natural_text == "": diff --git a/edtf/parser/edtf_exceptions.py b/edtf/parser/edtf_exceptions.py index 9530602..d906d58 100644 --- a/edtf/parser/edtf_exceptions.py +++ b/edtf/parser/edtf_exceptions.py @@ -2,4 +2,28 @@ class EDTFParseException(ParseException): - pass + """Raised when an input cannot be parsed as an EDTF string. + + Attributes: + input_string - the input string that could not be parsed + err -- the original ParseException that caused this one + """ + + def __init__(self, input_string, err=None): + if input_string is None: + input_string = "" + self.input_string = input_string + if err is None: + err = ParseException(input_string, 0, "Invalid input or format.") + self.err = err + super().__init__(str(err), err.loc if err.loc else 0, self.input_string) + + def __str__(self): + if not self.input_string: + return "You must supply some input text" + near_text = ( + self.input_string[max(self.err.loc - 10, 0) : self.err.loc + 10] + if hasattr(self.err, "loc") + else "" + ) + return f"Error at position {self.err.loc}: Invalid input or format near '{near_text}'. Please provide a valid EDTF string." diff --git a/edtf/parser/grammar.py b/edtf/parser/grammar.py index 651b4b3..beabf52 100644 --- a/edtf/parser/grammar.py +++ b/edtf/parser/grammar.py @@ -347,7 +347,7 @@ def parse_edtf(input_string, parseAll=True, fail_silently=False, debug=None): if debug is None: debug = DEBUG_PYPARSING if not input_string: - raise EDTFParseException("You must supply some input text") + raise EDTFParseException(input_string) try: p = edtfParser.parseString(input_string.strip(), parseAll) if p: @@ -357,8 +357,4 @@ def parse_edtf(input_string, parseAll=True, fail_silently=False, debug=None): return None if debug: raise - near_text = "" - if input_string: - near_text = input_string[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 + raise EDTFParseException(input_string, err) from None From daf0d041dc739975e822f35813dfd82ca75eacea Mon Sep 17 00:00:00 2001 From: Cole Crawford <16374762+ColeDCrawford@users.noreply.github.com> Date: Mon, 3 Jun 2024 18:40:15 -0400 Subject: [PATCH 5/5] Add the TestEvent model to Django admin Make the string representation of TestEvent simpler --- edtf_django_tests/edtf_integration/admin.py | 44 +++++++++++++++++++- edtf_django_tests/edtf_integration/models.py | 4 -- 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/edtf_django_tests/edtf_integration/admin.py b/edtf_django_tests/edtf_integration/admin.py index 846f6b4..3051891 100644 --- a/edtf_django_tests/edtf_integration/admin.py +++ b/edtf_django_tests/edtf_integration/admin.py @@ -1 +1,43 @@ -# Register your models here. +from django.contrib import admin + +from .models import TestEvent + + +class TestEventAdmin(admin.ModelAdmin): + list_display = ( + "date_display", + "date_edtf_direct", + "date_earliest", + "date_latest", + "date_sort_ascending", + "date_sort_descending", + "date_edtf", + ) + search_fields = ("date_display", "date_edtf_direct") + list_filter = ("date_earliest", "date_latest") + readonly_fields = ( + "date_earliest", + "date_latest", + "date_sort_ascending", + "date_sort_descending", + "date_edtf", + ) + + fieldsets = ( + (None, {"fields": ("date_display", "date_edtf_direct", "date_edtf")}), + ( + "Computed Dates", + { + "classes": ("collapse",), + "fields": ( + "date_earliest", + "date_latest", + "date_sort_ascending", + "date_sort_descending", + ), + }, + ), + ) + + +admin.site.register(TestEvent, TestEventAdmin) diff --git a/edtf_django_tests/edtf_integration/models.py b/edtf_django_tests/edtf_integration/models.py index 5120889..5e66592 100644 --- a/edtf_django_tests/edtf_integration/models.py +++ b/edtf_django_tests/edtf_integration/models.py @@ -49,9 +49,5 @@ def __str__(self) -> str: return ( f"Test Event: {self.date_display=}, " f"{self.date_edtf_direct=}, " - f"{self.date_earliest=}, " - f"{self.date_latest=}, " - f"{self.date_sort_ascending=}, " - f"{self.date_sort_descending=}, " f"{self.date_edtf=}" )