summaryrefslogtreecommitdiff
path: root/convert.py
blob: c1cd77076e508d8ed8d16bd381daaa53f75ab36b (plain)
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
#!/usr/bin/python

import elixir
from model import *

import csv
from decimal import Decimal
from datetime import datetime

elixir.create_all()

# categories
d = dict((x, Category(name = x)) for x in ("Essen", "Kneipe", "Fahrkarten", "Kino", "Mittag", "Sonstiges"))
d["Mittag"].parent = d["Essen"]

session.commit()

# single expenses
with open("kosten.csv") as f:
    dialect = csv.Sniffer().sniff(f.read(1024))
    f.seek(0)
    for row in csv.DictReader(f, dialect = dialect):
        s = SingleExpense()

        # expense
        s.expense = Decimal(row["betrag"])
        
        # date
        s.year, s.month, s.day = map(int, row["datum"].split('-'))

        # description
        s.description = unicode(row["kommentar"], 'utf-8')
        
        # category
        if row["nahrung"] == "1":
            s.category = d["Essen"]
        elif row["kneipe"] == "1":
            s.category = d["Kneipe"]
        elif row["fahrkarte"] == "1":
            s.category = d["Fahrkarten"]
        elif row["kino"] == "1":
            s.category = d["Kino"]
        elif row["mittag"] == "1":
            s.category = d["Mittag"]
        else:
            s.category = d["Sonstiges"]

session.commit()

# const expenses
with open("konstkosten.csv") as f:
    dialect = csv.Sniffer().sniff(f.read(1024))
    f.seek(0)
    for row in csv.DictReader(f, dialect = dialect):
        c = ConstExpense()

        # expense
        c.expense = Decimal(row["betrag"])
        
        # date
        c.start = datetime.strptime(row["start"], "%Y%m")
        c.end = datetime.strptime(row["ende"], "%Y%m")

        # description
        c.description = unicode(row["name"], 'utf-8')

        # interval
        types = {'m': 1, 'q': 3, 'h': 6, 'j': 12}
        c.months = types[row["typ"]]

        # category: just 'Sonstiges' for the moment
        c.category = d["Sonstiges"]

session.commit()