-
Notifications
You must be signed in to change notification settings - Fork 0
/
outputs.py
83 lines (59 loc) · 1.91 KB
/
outputs.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
78
79
80
81
82
83
import ifttt
import csv
import os.path
import datetime
class Output(object):
def on_income(self, amount, category, message):
pass
def on_expense(self, amount, category, message):
pass
class Writer (object):
def write_row(self, row):
pass
class CsvWriter(Writer):
def __init__(self, path):
self._path = path
def write_row(self, row):
with open(self._path, 'ab') as output_file:
writer = csv.writer(output_file)
writer.writerow(row)
class ExcelWriter(Writer):
def __init__(self, path):
self._path = path
class TimestampSupplier(object):
def get_timestamp(self):
pass
class NowSupplier(TimestampSupplier):
def get_timestamp(self):
return datetime.datetime.now()
class WriterOutput(Output):
def __init__(self, writer, timestamp_supplier):
self._writer = writer
self._timestamp_supplier = timestamp_supplier
def _write_row(self, row):
self._writer.write_row(row)
def on_income(self, amount, category, message):
self._write_row([
self._timestamp_supplier.get_timestamp(),
amount,
None,
category,
message
])
def on_expense(self, amount, category, message):
self._write_row([
self._timestamp_supplier.get_timestamp(),
None,
amount,
category,
message
])
class IftttOutput(Output):
def __init__(self, key, income_event, expense_event):
self._client = ifttt.IftttClient(key)
self._income_event = income_event
self._expense_event = expense_event
def on_income(self, amount, category, message):
self._client.trigger(self._income_event, amount, category, message)
def on_expense(self, amount, category, message):
self._client.trigger(self._expense_event, amount, category, message)