util: Rename to strparse.

Better describes what the functions in the module do.
This commit is contained in:
Brett Smith 2017-12-19 06:42:05 -05:00
parent 6ea28c2c89
commit 1b98ab2015
7 changed files with 23 additions and 22 deletions

View file

@ -9,7 +9,7 @@ import pathlib
import babel
import babel.numbers
from . import errors, template, util
from . import errors, strparse, template
class Configuration:
HOME_PATH = pathlib.Path(os.path.expanduser('~'))
@ -121,7 +121,7 @@ class Configuration:
def _strpdate(self, date_s, date_fmt):
try:
return util.strpdate(date_s, date_fmt)
return strparse.date(date_s, date_fmt)
except ValueError as error:
raise errors.UserInputConfigurationError(error.args[0], date_s)

View file

@ -2,7 +2,7 @@ import decimal
import functools
import bs4
from .. import util
from .. import strparse
STATUS_INVOICED = 'Invoice'
STATUS_PAID = 'Payment'
@ -62,8 +62,8 @@ class Invoice2017:
self.actions
def _strpdate(self, s):
date_s = util.rejoin_slice_words(s, slice(2), ',', 2)
return util.strpdate(date_s, '%b. %d, %Y')
date_s = strparse.rejoin_slice_words(s, slice(2), ',', 2)
return strparse.date(date_s, '%b. %d, %Y')
def _read_invoice_header(self, table, first_row_text, rows_text):
self.invoice_id = first_row_text[1]
@ -88,7 +88,7 @@ class Invoice2017:
elif description.startswith('Early Bird ('):
self.ticket_rate = self.DISCOUNT_TICKET_RATE
if qty:
self.amount += util.parse_currency_dec(total)
self.amount += strparse.currency_decimal(total)
def _read_invoice_activity(self, table, first_row_text, rows_text):
self.actions = [{
@ -97,7 +97,7 @@ class Invoice2017:
}]
for timestamp, description, amount in rows_text:
if description.startswith('Paid '):
last_stripe_id = util.rslice_words(description, 1, limit=1)
last_stripe_id = strparse.rslice_words(description, 1, limit=1)
action = {
'payment_id': last_stripe_id,
'status': STATUS_PAID,

View file

@ -2,7 +2,7 @@ import pathlib
import re
from . import _csv
from .. import util
from .. import strparse
class IncomeImporter(_csv.CSVImporterBase):
NEEDED_FIELDS = frozenset([
@ -24,7 +24,7 @@ class IncomeImporter(_csv.CSVImporterBase):
match = re.search(r'(?:\b|_)(\d{4}-\d{2}-\d{2})(?:\b|_)',
pathlib.Path(input_file.name).name)
if match:
self.entry_seed['date'] = util.strpdate(match.group(1), '%Y-%m-%d')
self.entry_seed['date'] = strparse.date(match.group(1), '%Y-%m-%d')
def _read_row(self, row):
if row['Status'] != 'Processed':
@ -44,7 +44,7 @@ class FeeImporterBase(_csv.CSVImporterBase):
def _read_row(self, row):
return {
'amount': row[self.AMOUNT_FIELD],
'date': util.strpdate(row['Month'], '%Y-%m'),
'date': strparse.date(row['Month'], '%Y-%m'),
}

View file

@ -1,7 +1,7 @@
import decimal
from . import _csv
from .. import util
from .. import strparse
class PaymentImporter(_csv.CSVImporterBase):
NEEDED_FIELDS = frozenset([
@ -24,9 +24,10 @@ class PaymentImporter(_csv.CSVImporterBase):
if (row['Status'] != 'Paid') and (row['Status'] != 'Refunded'):
return None
else:
date_s = strparse.slice_words(row['Created (UTC)'], 0, limit=1)
return {
'currency': row['Converted Currency'].upper(),
'date': util.strpdate(row['Created (UTC)'].split(None, 1)[0], self.DATE_FMT),
'fee': util.parse_currency_dec(row['Fee']),
'tax': util.parse_currency_dec(row['Tax']),
'date': strparse.date(date_s, self.DATE_FMT),
'fee': strparse.currency_decimal(row['Fee']),
'tax': strparse.currency_decimal(row['Tax']),
}

View file

@ -19,7 +19,7 @@ def _currency_pattern(locale):
re.escape(dec_sym),
)
def parse_currency_dec(s, locale='en_US_POSIX'):
def currency_decimal(s, locale='en_US_POSIX'):
try:
match = re.search(_currency_pattern(locale), s)
except TypeError:
@ -44,6 +44,9 @@ def parse_currency_dec(s, locale='en_US_POSIX'):
raise ValueError("non-currency text in {!r}: {!r}".format(s, extra))
return babel.numbers.parse_decimal(match.group(1) + match.group(3), locale)
def date(date_s, date_fmt):
return datetime.datetime.strptime(date_s, date_fmt).date()
def _rejoin_slice_words(method_name, source, wordslice, sep=None, limit=None, joiner=None):
if joiner is None:
joiner = ' ' if sep is None else sep
@ -55,6 +58,3 @@ def _slice_words(method_name, source, wordslice, sep=None, limit=None):
return getattr(source, method_name)(sep, limit)[wordslice]
slice_words = functools.partial(_slice_words, 'split')
rslice_words = functools.partial(_slice_words, 'rsplit')
def strpdate(date_s, date_fmt):
return datetime.datetime.strptime(date_s, date_fmt).date()

View file

@ -9,7 +9,7 @@ import tokenize
import babel.numbers
from . import errors, util
from . import errors, strparse
class TokenTransformer:
def __init__(self, source):
@ -253,7 +253,7 @@ class Template:
template_vars.update(
date=date.strftime(self.date_fmt),
payee=payee,
amount=util.parse_currency_dec(amount),
amount=strparse.currency_decimal(amount),
currency=currency,
)
for key, value in template_vars.items():

View file

@ -7,7 +7,7 @@ import re
import pytest
import yaml
from import2ledger import importers, util
from import2ledger import importers, strparse
from . import DATA_DIR
@ -35,7 +35,7 @@ class TestImporters:
with source_path.open() as source_file:
importer = import_class(source_file)
for actual, expected in itertools.zip_longest(importer, expect_results):
actual['amount'] = util.parse_currency_dec(actual['amount'])
actual['amount'] = strparse.currency_decimal(actual['amount'])
assert actual == expected
def test_loader(self):