Changeset - 89378cbf90fc
[Not reviewed]
0 3 0
Brett Smith - 4 years ago 2020-01-10 18:27:34
brettcsmith@brettcsmith.org
hooks.ledger_entry: Clean up whitespace in strings.

The hook now ensures it does not output whitespace that could be
significant to Ledger, either because it's a newline or an
account-amount separator.
3 files changed with 44 insertions and 6 deletions:
0 comments (0 inline, 0 general)
import2ledger/hooks/ledger_entry.py
Show inline comments
...
 
@@ -224,5 +224,6 @@ class AccountSplitter:
 
class Template:
 
    ACCOUNT_SPLIT_RE = re.compile(r'(?:\t|  )\s*')
 
    ACCOUNT_SPLIT_RE = re.compile(r'(?: ?\t|  )[ \t]*')
 
    DATE_FMT = '%Y/%m/%d'
 
    PAYEE_LINE_RE = re.compile(r'^\{(\w*_)*date\}\s')
 
    NEWLINE_RE = re.compile(r'[\f\n\r\v\u0085\u2028\u2029]')
 
    SIGNED_CURRENCY_FMT = '¤#,##0.###;¤-#,##0.###'
...
 
@@ -303,8 +304,13 @@ class Template:
 
                ), self.splitter.template_name)
 
        render_vars = {
 
            'amount': strparse.currency_decimal(template_vars['amount']),
 
        }
 
        render_vars = {}
 
        for key, value in template_vars.items():
 
            if value is not None and (key == 'date' or key.endswith('_date')):
 
            if value is None:
 
                pass
 
            elif key == 'date' or key.endswith('_date'):
 
                render_vars[key] = value.strftime(self.date_fmt)
 
            elif isinstance(value, str):
 
                value = self.NEWLINE_RE.sub(' ', value)
 
                value = self.ACCOUNT_SPLIT_RE.sub(' ', value)
 
                render_vars[key] = value
 
        render_vars['amount'] = strparse.currency_decimal(template_vars['amount'])
 
        all_vars = collections.ChainMap(render_vars, template_vars)
setup.py
Show inline comments
...
 
@@ -32,3 +32,3 @@ setup(
 
    description="Import different sources of financial data to Ledger",
 
    version='0.11.0',
 
    version='0.11.1',
 
    author='Brett Smith',
tests/test_hook_ledger_entry.py
Show inline comments
...
 
@@ -94,2 +94,34 @@ def test_complex_template():
 

	
 
def test_variable_whitespace_cleaned():
 
    # There are two critical parts of this to avoid making malformed Ledger
 
    # entries:
 
    # * Ensure newlines become non-newline whitespace so we don't write
 
    #   malformed lines.
 
    # * Collapse multiple spaces into one so variables in account names
 
    #   can't make malformed account lines by having a premature split
 
    #   between the account name and amount.
 
    render_vars = template_vars('W\t\tS', '125.50', other_vars={
 
        'entity': 'W\fS',
 
        'program': 'Spectrum\r\nDefense',
 
        'txid': 'ABC\v\tDEF',
 
    })
 
    lines = render_lines(
 
        render_vars, 'Complex',
 
        date_fmt='%Y-%m-%d',
 
        signed_currencies=['USD'],
 
    )
 
    assert lines == [
 
        "",
 
        "2015-03-14 W S",
 
        "  ;Tag: Value",
 
        "  ;TransactionID: ABC DEF",
 
        "  Accrued:Accounts Receivable  $125.50",
 
        "  ;Entity: Supplier",
 
        "  Income:Donations:Spectrum Defense  $-119.85",
 
        "  ;Program: Spectrum Defense",
 
        "  ;Entity: W S",
 
        "  Income:Donations:General  $-5.65",
 
        "  ;Entity: W S",
 
    ]
 

	
 
def test_balancing():
0 comments (0 inline, 0 general)