katefike / sage

A personal finance app that's like Mint, but better. It uses a dockerized postfix/dovecot email server. Parses transaction data from alert emails.
MIT License
5 stars 0 forks source link

Flag identical transactions #141

Closed katefike closed 4 months ago

katefike commented 5 months ago

Problems

Solution

For a given day and bank, check if there are other TXNs with the same merchant/payer and raw amount

Pytest

  1. Load a named mbox containing two or more emails - the emails may or may not be duplicates

DONE WHEN

katefike commented 5 months ago

Troubleshooting Pytest

root@9754f7b4e7a9:/# mb2md -s test_data/example_data/unparsable_emails.mbox -d /home/incoming/Maildir/
Fatal: Source is not an mbox file or a directory!

Error appears when refresh_inbox() attempts to load the mbox file. Error is caused by not using an absolute or fully qualified path. Solution is to change path to ./test_data/example_data/

katefike commented 5 months ago

Troubleshooting Pytest

root@9754f7b4e7a9:/# mb2md -s test_data/example_data/unparsable_emails.mbox -d /home/incoming/Maildir/
Fatal: Source is not an mbox file or a directory!

Error appears when refresh_inbox() attempts to load the mbox file. Error is caused by not using an absolute or fully qualified path. Solution is to change path to ./test_data/example_data/

Refactored refresh_inbox to use a lot of error catching but something was going wrong with this style of subprocess calling. Reverted back to original use of subprocess.call without

    _delete_maildir_success, delete_maildir_output = call_subprocess_with_output(
        f"{container} rm -r {maildir_path}"
    )
    print(f"INFO: Deleted existing Maildir/, if any: {delete_maildir_output}")

    recreate_maildir_success, recreate_maildir_output = call_subprocess_with_output(
        f"{container} mkdir {maildir_path}"
    )
    if recreate_maildir_success is False:
        print(f"CRITICAL: Failed to recreate Maildir/: {recreate_maildir_output}")

    load_mbox_success, load_mbox_output = call_subprocess_with_output(
        f"{container} mb2md -s {mbox_path}/{mbox_name} -d {maildir_path}"
    )
    if load_mbox_success is False:
        print(f"CRITICAL: Failed to load mbox: {load_mbox_output}")

    (
        modify_maildir_permissions_success,
        modify_maildir_permissions_output,
    ) = call_subprocess_with_output(
        f"{container} mb2md -s {mbox_path}/{mbox_name} -d {maildir_path}"
    )
    if modify_maildir_permissions_success is False:
        print(
            f"CRITICAL: Failed to modify Maildir/ permissions: {modify_maildir_permissions_output}"
        )

def call_subprocess_with_output(command):
    success = False
    try:
        output = subprocess.check_output(command, stderr=subprocess.STDOUT).decode()
        success = True
    except subprocess.CalledProcessError as e:
        output = e.output.decode()
    except Exception as e:
        # check_call can raise other exceptions, such as FileNotFoundError
        output = str(e)
    return (success, output)