Changeset - 1b98ab2015f9
[Not reviewed]
1 6 1
Brett Smith - 6 years ago 2017-12-19 11:42:05
brettcsmith@brettcsmith.org
util: Rename to strparse.

Better describes what the functions in the module do.
7 files changed with 23 insertions and 22 deletions:
0 comments (0 inline, 0 general)
import2ledger/config.py
Show inline comments
...
 
@@ -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)
 

	
import2ledger/importers/nbpy2017.py
Show inline comments
...
 
@@ -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,
import2ledger/importers/patreon.py
Show inline comments
...
 
@@ -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'),
 
        }
 

	
 

	
import2ledger/importers/stripe.py
Show inline comments
 
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']),
 
            }
import2ledger/strparse.py
Show inline comments
 
file renamed from import2ledger/util.py to import2ledger/strparse.py
...
 
@@ -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()
import2ledger/template.py
Show inline comments
...
 
@@ -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():
tests/test_importers.py
Show inline comments
...
 
@@ -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):
0 comments (0 inline, 0 general)