util: Rename to strparse.
Better describes what the functions in the module do.
This commit is contained in:
parent
6ea28c2c89
commit
1b98ab2015
7 changed files with 23 additions and 22 deletions
|
@ -9,7 +9,7 @@ import pathlib
|
||||||
|
|
||||||
import babel
|
import babel
|
||||||
import babel.numbers
|
import babel.numbers
|
||||||
from . import errors, template, util
|
from . import errors, strparse, template
|
||||||
|
|
||||||
class Configuration:
|
class Configuration:
|
||||||
HOME_PATH = pathlib.Path(os.path.expanduser('~'))
|
HOME_PATH = pathlib.Path(os.path.expanduser('~'))
|
||||||
|
@ -121,7 +121,7 @@ class Configuration:
|
||||||
|
|
||||||
def _strpdate(self, date_s, date_fmt):
|
def _strpdate(self, date_s, date_fmt):
|
||||||
try:
|
try:
|
||||||
return util.strpdate(date_s, date_fmt)
|
return strparse.date(date_s, date_fmt)
|
||||||
except ValueError as error:
|
except ValueError as error:
|
||||||
raise errors.UserInputConfigurationError(error.args[0], date_s)
|
raise errors.UserInputConfigurationError(error.args[0], date_s)
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@ import decimal
|
||||||
import functools
|
import functools
|
||||||
|
|
||||||
import bs4
|
import bs4
|
||||||
from .. import util
|
from .. import strparse
|
||||||
|
|
||||||
STATUS_INVOICED = 'Invoice'
|
STATUS_INVOICED = 'Invoice'
|
||||||
STATUS_PAID = 'Payment'
|
STATUS_PAID = 'Payment'
|
||||||
|
@ -62,8 +62,8 @@ class Invoice2017:
|
||||||
self.actions
|
self.actions
|
||||||
|
|
||||||
def _strpdate(self, s):
|
def _strpdate(self, s):
|
||||||
date_s = util.rejoin_slice_words(s, slice(2), ',', 2)
|
date_s = strparse.rejoin_slice_words(s, slice(2), ',', 2)
|
||||||
return util.strpdate(date_s, '%b. %d, %Y')
|
return strparse.date(date_s, '%b. %d, %Y')
|
||||||
|
|
||||||
def _read_invoice_header(self, table, first_row_text, rows_text):
|
def _read_invoice_header(self, table, first_row_text, rows_text):
|
||||||
self.invoice_id = first_row_text[1]
|
self.invoice_id = first_row_text[1]
|
||||||
|
@ -88,7 +88,7 @@ class Invoice2017:
|
||||||
elif description.startswith('Early Bird ('):
|
elif description.startswith('Early Bird ('):
|
||||||
self.ticket_rate = self.DISCOUNT_TICKET_RATE
|
self.ticket_rate = self.DISCOUNT_TICKET_RATE
|
||||||
if qty:
|
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):
|
def _read_invoice_activity(self, table, first_row_text, rows_text):
|
||||||
self.actions = [{
|
self.actions = [{
|
||||||
|
@ -97,7 +97,7 @@ class Invoice2017:
|
||||||
}]
|
}]
|
||||||
for timestamp, description, amount in rows_text:
|
for timestamp, description, amount in rows_text:
|
||||||
if description.startswith('Paid '):
|
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 = {
|
action = {
|
||||||
'payment_id': last_stripe_id,
|
'payment_id': last_stripe_id,
|
||||||
'status': STATUS_PAID,
|
'status': STATUS_PAID,
|
||||||
|
|
|
@ -2,7 +2,7 @@ import pathlib
|
||||||
import re
|
import re
|
||||||
|
|
||||||
from . import _csv
|
from . import _csv
|
||||||
from .. import util
|
from .. import strparse
|
||||||
|
|
||||||
class IncomeImporter(_csv.CSVImporterBase):
|
class IncomeImporter(_csv.CSVImporterBase):
|
||||||
NEEDED_FIELDS = frozenset([
|
NEEDED_FIELDS = frozenset([
|
||||||
|
@ -24,7 +24,7 @@ class IncomeImporter(_csv.CSVImporterBase):
|
||||||
match = re.search(r'(?:\b|_)(\d{4}-\d{2}-\d{2})(?:\b|_)',
|
match = re.search(r'(?:\b|_)(\d{4}-\d{2}-\d{2})(?:\b|_)',
|
||||||
pathlib.Path(input_file.name).name)
|
pathlib.Path(input_file.name).name)
|
||||||
if match:
|
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):
|
def _read_row(self, row):
|
||||||
if row['Status'] != 'Processed':
|
if row['Status'] != 'Processed':
|
||||||
|
@ -44,7 +44,7 @@ class FeeImporterBase(_csv.CSVImporterBase):
|
||||||
def _read_row(self, row):
|
def _read_row(self, row):
|
||||||
return {
|
return {
|
||||||
'amount': row[self.AMOUNT_FIELD],
|
'amount': row[self.AMOUNT_FIELD],
|
||||||
'date': util.strpdate(row['Month'], '%Y-%m'),
|
'date': strparse.date(row['Month'], '%Y-%m'),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import decimal
|
import decimal
|
||||||
|
|
||||||
from . import _csv
|
from . import _csv
|
||||||
from .. import util
|
from .. import strparse
|
||||||
|
|
||||||
class PaymentImporter(_csv.CSVImporterBase):
|
class PaymentImporter(_csv.CSVImporterBase):
|
||||||
NEEDED_FIELDS = frozenset([
|
NEEDED_FIELDS = frozenset([
|
||||||
|
@ -24,9 +24,10 @@ class PaymentImporter(_csv.CSVImporterBase):
|
||||||
if (row['Status'] != 'Paid') and (row['Status'] != 'Refunded'):
|
if (row['Status'] != 'Paid') and (row['Status'] != 'Refunded'):
|
||||||
return None
|
return None
|
||||||
else:
|
else:
|
||||||
|
date_s = strparse.slice_words(row['Created (UTC)'], 0, limit=1)
|
||||||
return {
|
return {
|
||||||
'currency': row['Converted Currency'].upper(),
|
'currency': row['Converted Currency'].upper(),
|
||||||
'date': util.strpdate(row['Created (UTC)'].split(None, 1)[0], self.DATE_FMT),
|
'date': strparse.date(date_s, self.DATE_FMT),
|
||||||
'fee': util.parse_currency_dec(row['Fee']),
|
'fee': strparse.currency_decimal(row['Fee']),
|
||||||
'tax': util.parse_currency_dec(row['Tax']),
|
'tax': strparse.currency_decimal(row['Tax']),
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,7 @@ def _currency_pattern(locale):
|
||||||
re.escape(dec_sym),
|
re.escape(dec_sym),
|
||||||
)
|
)
|
||||||
|
|
||||||
def parse_currency_dec(s, locale='en_US_POSIX'):
|
def currency_decimal(s, locale='en_US_POSIX'):
|
||||||
try:
|
try:
|
||||||
match = re.search(_currency_pattern(locale), s)
|
match = re.search(_currency_pattern(locale), s)
|
||||||
except TypeError:
|
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))
|
raise ValueError("non-currency text in {!r}: {!r}".format(s, extra))
|
||||||
return babel.numbers.parse_decimal(match.group(1) + match.group(3), locale)
|
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):
|
def _rejoin_slice_words(method_name, source, wordslice, sep=None, limit=None, joiner=None):
|
||||||
if joiner is None:
|
if joiner is None:
|
||||||
joiner = ' ' if sep is None else sep
|
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]
|
return getattr(source, method_name)(sep, limit)[wordslice]
|
||||||
slice_words = functools.partial(_slice_words, 'split')
|
slice_words = functools.partial(_slice_words, 'split')
|
||||||
rslice_words = functools.partial(_slice_words, 'rsplit')
|
rslice_words = functools.partial(_slice_words, 'rsplit')
|
||||||
|
|
||||||
def strpdate(date_s, date_fmt):
|
|
||||||
return datetime.datetime.strptime(date_s, date_fmt).date()
|
|
|
@ -9,7 +9,7 @@ import tokenize
|
||||||
|
|
||||||
import babel.numbers
|
import babel.numbers
|
||||||
|
|
||||||
from . import errors, util
|
from . import errors, strparse
|
||||||
|
|
||||||
class TokenTransformer:
|
class TokenTransformer:
|
||||||
def __init__(self, source):
|
def __init__(self, source):
|
||||||
|
@ -253,7 +253,7 @@ class Template:
|
||||||
template_vars.update(
|
template_vars.update(
|
||||||
date=date.strftime(self.date_fmt),
|
date=date.strftime(self.date_fmt),
|
||||||
payee=payee,
|
payee=payee,
|
||||||
amount=util.parse_currency_dec(amount),
|
amount=strparse.currency_decimal(amount),
|
||||||
currency=currency,
|
currency=currency,
|
||||||
)
|
)
|
||||||
for key, value in template_vars.items():
|
for key, value in template_vars.items():
|
||||||
|
|
|
@ -7,7 +7,7 @@ import re
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
import yaml
|
import yaml
|
||||||
from import2ledger import importers, util
|
from import2ledger import importers, strparse
|
||||||
|
|
||||||
from . import DATA_DIR
|
from . import DATA_DIR
|
||||||
|
|
||||||
|
@ -35,7 +35,7 @@ class TestImporters:
|
||||||
with source_path.open() as source_file:
|
with source_path.open() as source_file:
|
||||||
importer = import_class(source_file)
|
importer = import_class(source_file)
|
||||||
for actual, expected in itertools.zip_longest(importer, expect_results):
|
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
|
assert actual == expected
|
||||||
|
|
||||||
def test_loader(self):
|
def test_loader(self):
|
||||||
|
|
Loading…
Reference in a new issue