-
Notifications
You must be signed in to change notification settings - Fork 2
/
convert.py
77 lines (64 loc) · 2.28 KB
/
convert.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python3
import re
from dateutil.parser import parse
from beancount.core import data, amount
from beancount.core.number import D
from beancount.ingest import importer
PAYEE_REGEX = re.compile(r",?(.*)(捐赠|垫付)")
class CashImporter(importer.ImporterProtocol):
def process_line(self, cols):
no, _, time_str, src, dst, cur, amt_str, note = cols
metadata = data.new_metadata('cash.md', no)
time = parse(time_str)
date = time.date()
units = amount.Amount(D(amt_str), cur)
metadata["time"] = time.time().isoformat()
if m := PAYEE_REGEX.match(note):
payee = m.group(1)
else:
payee = None
return data.Transaction(
meta=metadata,
date=date,
payee=payee,
flag=self.FLAG,
narration=note,
tags=set(),
links=data.EMPTY_SET,
postings=[
data.Posting(
account=src,
units=units,
cost=None,
price=None,
flag=None,
meta=None,
),
data.Posting(
account=dst,
units=None,
cost=None,
price=None,
flag=None,
meta=None,
),
],
)
def identify(self, file):
return True
def extract(self, file, existing_entries):
entries = list(existing_entries or [])
with open('cash.md') as f:
lines = f.readlines()
begin_table = False
for l in lines:
l = l.strip()
if begin_table:
cols = [c.strip() for c in l.split('|')]
entries.append(self.process_line(cols))
elif l.startswith('---'):
begin_table = True
return entries
CONFIG = [
CashImporter()
]