Skip to content

Commit

Permalink
Fixed dependency on beancount v2 (parse_date_liberally has been removed)
Browse files Browse the repository at this point in the history
  • Loading branch information
blais committed Oct 1, 2023
1 parent 8901d54 commit 6147957
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
19 changes: 16 additions & 3 deletions beangulp/date_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
16 changes: 8 additions & 8 deletions beangulp/importers/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -346,16 +346,16 @@ 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:
meta['balance'] = Amount(self.parse_amount(balance), currency)
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, [])

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6147957

Please sign in to comment.