From 6515f9c55a45ff870375bae112624c23201d1373 Mon Sep 17 00:00:00 2001 From: discopatrick Date: Fri, 14 Apr 2017 15:25:03 +0100 Subject: [PATCH 1/4] changes date validation method to try each format until a match is found --- beets/dbcore/query.py | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/beets/dbcore/query.py b/beets/dbcore/query.py index cf0c13792..1c5c58e53 100644 --- a/beets/dbcore/query.py +++ b/beets/dbcore/query.py @@ -533,8 +533,8 @@ class Period(object): instants of time during January 2014. """ - precisions = ('year', 'month', 'day') - date_formats = ('%Y', '%Y-%m', '%Y-%m-%d') + precisions = ('day', 'month', 'year') + date_formats = ('%Y-%m-%d', '%Y-%m', '%Y') def __init__(self, date, precision): """Create a period with the given date (a `datetime` object) and @@ -553,17 +553,19 @@ class Period(object): """ if not string: return None - ordinal = string.count('-') - if ordinal >= len(cls.date_formats): - # Too many components. - raise InvalidQueryArgumentTypeError(string, 'a valid datetime string') - date_format = cls.date_formats[ordinal] - try: - date = datetime.strptime(string, date_format) - except ValueError: - # Parsing failed. - raise InvalidQueryArgumentTypeError(string, 'a valid datetime string') - precision = cls.precisions[ordinal] + count = 0 + date = None + for date_format in cls.date_formats: + try: + date = datetime.strptime(string, date_format) + break + except ValueError: + # Parsing failed. + count += 1 + if date is None: + raise InvalidQueryArgumentTypeError(string, + 'a valid datetime string') + precision = cls.precisions[count] return cls(date, precision) def open_right_endpoint(self): From 713c00aea75fb14ae00d4a41c4d17d3b79bba062 Mon Sep 17 00:00:00 2001 From: discopatrick Date: Thu, 20 Apr 2017 13:20:22 +0100 Subject: [PATCH 2/4] reverts order of precisions from broadest to narrowest --- beets/dbcore/query.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/beets/dbcore/query.py b/beets/dbcore/query.py index 1c5c58e53..0ed5f8935 100644 --- a/beets/dbcore/query.py +++ b/beets/dbcore/query.py @@ -533,8 +533,8 @@ class Period(object): instants of time during January 2014. """ - precisions = ('day', 'month', 'year') - date_formats = ('%Y-%m-%d', '%Y-%m', '%Y') + precisions = ('year', 'month', 'day') + date_formats = ('%Y', '%Y-%m', '%Y-%m-%d') def __init__(self, date, precision): """Create a period with the given date (a `datetime` object) and From 85adbd138336826b7250640f369698e099fd4ae1 Mon Sep 17 00:00:00 2001 From: discopatrick Date: Thu, 20 Apr 2017 13:36:31 +0100 Subject: [PATCH 3/4] =?UTF-8?q?gives=20variable=20the=20better=20name=20of?= =?UTF-8?q?=20=E2=80=98ordinal=E2=80=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- beets/dbcore/query.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/beets/dbcore/query.py b/beets/dbcore/query.py index 0ed5f8935..13aca762d 100644 --- a/beets/dbcore/query.py +++ b/beets/dbcore/query.py @@ -553,7 +553,7 @@ class Period(object): """ if not string: return None - count = 0 + ordinal = 0 date = None for date_format in cls.date_formats: try: @@ -561,11 +561,11 @@ class Period(object): break except ValueError: # Parsing failed. - count += 1 + ordinal += 1 if date is None: raise InvalidQueryArgumentTypeError(string, 'a valid datetime string') - precision = cls.precisions[count] + precision = cls.precisions[ordinal] return cls(date, precision) def open_right_endpoint(self): From 4dbc4134b315650786d5667991e5e0198100eaff Mon Sep 17 00:00:00 2001 From: discopatrick Date: Fri, 21 Apr 2017 12:55:44 +0100 Subject: [PATCH 4/4] consolidates the declaration and incrementing of `ordinal` into one line --- beets/dbcore/query.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/beets/dbcore/query.py b/beets/dbcore/query.py index 13aca762d..8ce4dac41 100644 --- a/beets/dbcore/query.py +++ b/beets/dbcore/query.py @@ -553,15 +553,14 @@ class Period(object): """ if not string: return None - ordinal = 0 date = None - for date_format in cls.date_formats: + for ordinal, date_format in enumerate(cls.date_formats): try: date = datetime.strptime(string, date_format) break except ValueError: # Parsing failed. - ordinal += 1 + pass if date is None: raise InvalidQueryArgumentTypeError(string, 'a valid datetime string')