Files @ 5c73c40bccfe
Branch filter:

Location: NPO-Accounting/import2ledger/import2ledger/hooks/add_entity.py

Brett Smith
import2ledger: First version.

This is a pretty feature-complete version 1.
I don't know why I waited this long to commit anything.
import re
import unicodedata

class AddEntityHook:
    NONASCII_RE = re.compile(r'[^-A-Za-z0-9]')
    NONALNUM_RE = re.compile(r'[^-\w]')
    OPEN_PARENS = ['\\(', '\\[', '\\{']
    CLOSE_PARENS = ['\\)', '\\]', '\\}']
    NO_PARENS = '[^{}]*'.format(''.join(OPEN_PARENS + CLOSE_PARENS))

    def __init__(self, config):
        pass

    def _remove_parens(self, s):
        last_s = None
        while s != last_s:
            last_s = s
            for open_c, close_c in zip(self.OPEN_PARENS, self.CLOSE_PARENS):
                s = re.sub(open_c + self.NO_PARENS + close_c, '', s)
        return s if s else last_s

    def _entity_parts(self, s, trim_re):
        for word in s.split():
            word = unicodedata.normalize('NFKD', word)
            word = trim_re.sub('', word)
            if word:
                yield word

    def _str2entity(self, s, trim_re):
        parts = list(self._entity_parts(s, trim_re))
        if not parts:
            return ''
        parts.insert(0, parts.pop())
        return '-'.join(parts)

    def run(self, data):
        if ('payee' in data) and ('entity' not in data):
            payee = self._remove_parens(data['payee'])
            entity = self._str2entity(payee, self.NONASCII_RE)
            if not entity:
                entity = self._str2entity(payee, self.NONALNUM_RE)
            data['entity'] = entity