From 61479575c8d2a0c9b7478e6dec765d507b97ebae Mon Sep 17 00:00:00 2001 From: blais Date: Sun, 1 Oct 2023 17:32:12 -0400 Subject: [PATCH] Fixed dependency on beancount v2 (parse_date_liberally has been removed) --- beangulp/date_utils.py | 19 ++++++++++++++++--- beangulp/importers/csv.py | 16 ++++++++-------- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/beangulp/date_utils.py b/beangulp/date_utils.py index 2211960..49cae3d 100644 --- a/beangulp/date_utils.py +++ b/beangulp/date_utils.py @@ -2,6 +2,19 @@ import dateutil.parser -def parse_date(string: str) -> datetime.date: - """Parse any date format into a date string.""" - return dateutil.parser.parse(string).date() +def parse_date(string, parse_kwargs_dict=None): + """Parse arbitrary strings to dates. + + This function is intended to support liberal inputs, so that we can use it + in accepting user-specified dates on command-line scripts. + + Args: + string: A string to parse. + parse_kwargs_dict: Dict of kwargs to pass to dateutil parser. + Returns: + A datetime.date object. + """ + # At the moment, rely on the most excellent dateutil. + if parse_kwargs_dict is None: + parse_kwargs_dict = {} + return dateutil.parser.parse(string, **parse_kwargs_dict).date() diff --git a/beangulp/importers/csv.py b/beangulp/importers/csv.py index b16e052..03f411b 100644 --- a/beangulp/importers/csv.py +++ b/beangulp/importers/csv.py @@ -26,8 +26,8 @@ from beancount.core.amount import Amount from beancount.core.number import ZERO, D from beancount.utils import misc_utils -from beancount.utils.date_utils import parse_date_liberally from beangulp import cache +from beangulp import date_utils from beangulp import importer from beangulp.importers.mixins import filing, identifier @@ -266,7 +266,7 @@ def file_date(self, file): if row[0].startswith('#'): continue date_str = row[iconfig[Col.DATE]] - date = parse_date_liberally(date_str, self.dateutil_kwds) + date = date_utils.parse_date(date_str, self.dateutil_kwds) if max_date is None or date > max_date: max_date = date return max_date @@ -346,8 +346,8 @@ def get(row, ftype): # Create a transaction meta = data.new_metadata(file.name, index) if txn_date is not None: - meta['date'] = parse_date_liberally(txn_date, - self.dateutil_kwds) + meta['date'] = date_utils.parse_date(txn_date, + self.dateutil_kwds) if txn_time is not None: meta['time'] = str(dateutil.parser.parse(txn_time).time()) if balance is not None: @@ -355,7 +355,7 @@ def get(row, ftype): if last4: last4_friendly = self.last4_map.get(last4.strip()) meta['card'] = last4_friendly if last4_friendly else last4 - date = parse_date_liberally(date, self.dateutil_kwds) + date = date_utils.parse_date(date, self.dateutil_kwds) txn = data.Transaction(meta, date, self.FLAG, payee, narration, tags, links, []) @@ -383,10 +383,10 @@ def get(row, ftype): entries.append(txn) # Figure out if the file is in ascending or descending order. - first_date = parse_date_liberally(get(first_row, Col.DATE), + first_date = date_utils.parse_date(get(first_row, Col.DATE), + self.dateutil_kwds) + last_date = date_utils.parse_date(get(last_row, Col.DATE), self.dateutil_kwds) - last_date = parse_date_liberally(get(last_row, Col.DATE), - self.dateutil_kwds) is_ascending = first_date < last_date # Reverse the list if the file is in descending order