File diff cc2b3978011a → 89378cbf90fc
import2ledger/hooks/ledger_entry.py
Show inline comments
...
 
@@ -213,27 +213,28 @@ class AccountSplitter:
 
                    account_s, ' ' * sep_len, amount_s,
 
                    metadata.format_map(template_vars),
 
                )
 

	
 
    def render_next(self, template_vars):
 
        if template_vars is not self._last_template_vars:
 
            self._split_iter = self._iter_splits(template_vars)
 
            self._last_template_vars = template_vars
 
        return next(self._split_iter)
 

	
 

	
 
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.###'
 
    UNSIGNED_CURRENCY_FMT = '#,##0.### ¤¤'
 

	
 
    def __init__(self, template_s, signed_currencies=frozenset(),
 
                 date_fmt=DATE_FMT,
 
                 signed_currency_fmt=SIGNED_CURRENCY_FMT,
 
                 unsigned_currency_fmt=UNSIGNED_CURRENCY_FMT,
 
                 template_name='<template>'):
 
        self.date_fmt = date_fmt
 
        self.date_field = 'date'
 
        self.splitter = AccountSplitter(
 
            signed_currencies, signed_currency_fmt, unsigned_currency_fmt, template_name)
...
 
@@ -292,30 +293,35 @@ class Template:
 
        else:
 
            self.splitter.set_metadata(str_flat)
 

	
 
    def render(self, template_vars):
 
        # template_vars must have these keys.  Raise a KeyError if not.
 
        template_vars['currency']
 
        template_vars['payee']
 
        if template_vars.get(self.date_field) is None:
 
            raise errors.UserInputConfigurationError(
 
                "entry needs {} field but that's not set by the importer".format(
 
                    self.date_field,
 
                ), 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)
 
        return ''.join(f(all_vars) for f in self.format_funcs)
 

	
 
    def is_empty(self):
 
        return not self.format_funcs
 

	
 

	
 
class LedgerEntryHook:
 
    KIND = HOOK_KINDS.OUTPUT
 

	
 
    def __init__(self, config):
 
        self.config = config