Beispiel #1
0
def query_guru_ruler(t):
    """Define rules on a combination of fields. All rules must match
    corresponding fields for the ruler to be valid.
    """
    extras = sorted([k for (k, v) in t.items() if (v and not k.isdigit())])
    matchable_fields = sorted(
        set([x
             for x in t if t[x] and not x.isdigit()]) - {"category", "number"})

    guru_ruler = {}
    extras = {}
    field = True
    while field:
        # Enter field
        while True:
            # Update fields match indicators
            print(TERM.move_y(0))
            print_transaction(t, short=False, extras=extras)
            field = quick_input("\nMatch on field",
                                sugg=matchable_fields,
                                clear=True).lower()
            regex = "*" in field
            field = field.strip("*")
            if not field or field in matchable_fields:
                break
        # Enter match
        while field:
            print(TERM.move_y(0))
            print_transaction(t, short=False, extras=extras)
            existing_match = guru_ruler.get(field, "")
            ruler = quick_input(
                "\n%s match (%s)%s" % (
                    field.title(),
                    "regex" if regex else "chars",
                    " [%s]" % existing_match if existing_match else "",
                ),
                clear=True,
            )
            if ruler.isspace():  # remove field rule from ruler
                extras.pop(field, None)
                guru_ruler.pop(field, None)
            elif ruler:
                guru_ruler[field] = r"%s" % ruler if regex else re.escape(
                    ruler)
            match, extras = check_ruler(guru_ruler, t)
            if match:
                break
    return guru_ruler
Beispiel #2
0
def query_guru_ruler(t):
    """Define rules on a combination of fields. All rules must match
       corresponding fields for the ruler to be valid.
    """
    extras = sorted([k for (k, v) in t.iteritems() if (
                     v and not k.isdigit())])
    set_completer(extras)
    guru_ruler = {}
    extras = {}
    field = True
    while field:
        # Enter field
        while True:
            # Update fields match indicators
            print(TERM.move_y(0))
            print_transaction(t, short=False, extras=extras)
            field = quick_input('\nMatch on field', vanish=True).lower()
            regex = '*' in field
            field = field.strip('*')
            if not field or field in set(FIELDS_FULL.values()) - {'category'}:
                break
        # Enter match
        while field:
            print(TERM.move_y(0))
            print_transaction(t, short=False, extras=extras)
            existing_match = guru_ruler.get(field, '')
            ruler = quick_input('\n%s match (%s)%s' % (field.title(),
                    'regex' if regex else 'chars',
                    ' [%s]' % existing_match if existing_match else ''),
                vanish=True)
            if ruler.isspace():  # remove field rule from ruler
                extras.pop(field, None)
                guru_ruler.pop(field, None)
                # break
            elif ruler:
                guru_ruler[field] = r'%s' % ruler if regex else re.escape(
                    ruler)
            match, extras = check_ruler(guru_ruler, t)
            if match:
                break
    return guru_ruler
Beispiel #3
0
def query_guru_ruler(t):
    """Define rules on a combination of fields. All rules must match
       corresponding fields for the ruler to be valid.
    """
    extras = sorted([k for (k, v) in t.items() if (v and not k.isdigit())])
    set_completer(extras)
    guru_ruler = {}
    extras = {}
    field = True
    while field:
        # Enter field
        while True:
            # Update fields match indicators
            print(TERM.move_y(0))
            print_transaction(t, short=False, extras=extras)
            field = quick_input('\nMatch on field', vanish=True).lower()
            regex = '*' in field
            field = field.strip('*')
            if not field or field in set(
                    config.FIELDS_FULL.values()) - {'category'}:
                break
        # Enter match
        while field:
            print(TERM.move_y(0))
            print_transaction(t, short=False, extras=extras)
            existing_match = guru_ruler.get(field, '')
            ruler = quick_input(
                '\n%s match (%s)%s' %
                (field.title(), 'regex' if regex else 'chars',
                 ' [%s]' % existing_match if existing_match else ''),
                vanish=True)
            if ruler.isspace():  # remove field rule from ruler
                extras.pop(field, None)
                guru_ruler.pop(field, None)
            elif ruler:
                guru_ruler[field] = r'%s' % ruler if regex else re.escape(
                    ruler)
            match, extras = check_ruler(guru_ruler, t)
            if match:
                break
    return guru_ruler
Beispiel #4
0
def query_ruler(t):
    """Prompt user to enter a valid matching ruler for transaction.
       First prompt is used to enter a basic ruler aka successive words to look
       for on payee line.
       This prompt can be skipped by pressing <Enter> to have access to guru
       ruler prompt, where ruler is a list of field/match to validate.
    """
    with TERM.fullscreen():
        extras = {'category': '? Category'}
        ok = False
        ruler = {}
        while True:
            print(TERM.move_y(0))
            print_transaction(t, extras=extras)
            if ok:
                break
            ruler = query_basic_ruler(t, tags.unrulify(ruler)) or \
                query_guru_ruler(t)
            ok, extras = check_ruler(ruler, t)
    return ruler
Beispiel #5
0
def query_ruler(t):
    """Prompt user to enter a valid matching ruler for transaction.
       First prompt is used to enter a basic ruler aka successive words to look
       for on payee line.
       This prompt can be skipped by pressing <Enter> to have access to guru
       ruler prompt, where ruler is a list of field/match to validate.
    """
    with TERM.fullscreen():
        extras = {}
        ok = False
        ruler = {}
        while True:
            print(TERM.move_y(0))
            print_transaction(t, extras=extras)
            if ok:
                break
            ruler = query_basic_ruler(t, tags.unrulify(ruler)) or query_guru_ruler(t)
            ok, extras = check_ruler(ruler, t)

    return ruler