Sfoglia il codice sorgente

Restructure memos to make them alphabetically sortable

karianne
Sindre Stephansen 1 anno fa
parent
commit
81b37ddbd5
Firmato da: sindre <sindre@sindrestephansen.com> ID Chiave GPG: B06FC67D17A46ADE
1 ha cambiato i file con 40 aggiunte e 1 eliminazioni
  1. +40
    -1
      ynab.py

+ 40
- 1
ynab.py Vedi File

@@ -5,6 +5,7 @@ import csv
import re
from pathlib import Path

whitelist_cards = ["7756"]

def usage():
print('Usage: ynab.py <filename>')
@@ -13,6 +14,44 @@ def usage():
def is_reserved(row):
return '(Reservert)' in row[1]

def convert_memo(original):
original = re.match(r'="[ ]?(.+)"', original).groups()[0]
original = original.replace(" Kurs: 1.0000", "")
words = original.split(" ")

for i in range(20):
if words[0] == "":
# It's empty
del words[0]
elif m := re.match(r'\*(\d{4})', words[0]):
# It's the last four digits of a card
if m.groups()[0] in whitelist_cards:
# It's an expected card, ignore it
del words[0]
else:
# It's an unexpected card, move it to the end
words.append(words.pop(0))
elif m := re.match(r'\d{2}\.\d{2}', words[0]):
# It's the date. Move it to the end
words.append(words.pop(0))
elif (m1 := re.match(r'^[A-Z]{3}$', words[0])) and (m2 := re.match(r'[\d]+\.[\d]+', words[1])):
# It's the original currency
if words[0] == "NOK":
# It's Norwegian kroner, ignoring
del words[0]
del words[0]
else:
# It's some other currency, move it to the end
words.append(words.pop(0))
words.append(words.pop(0))
else:
break
else:
raise Exception(f"Infinite loop while parsing \"{original}\"")


return " ".join(words)


def convert(reader, writer):
header = ['Date', 'Payee', 'Memo', 'Outflow', 'Inflow']
@@ -45,7 +84,7 @@ def convert(reader, writer):
else:
money_out = -money

memo = re.match(r'="[ ]?(.+)"', row[2]).groups()[0]
memo = convert_memo(row[2])

new_row = [*row[:2], memo, money_out, money_in]
writer.writerow(new_row)


Loading…
Annulla
Salva