-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimporter.py
126 lines (110 loc) · 4.75 KB
/
importer.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# splitwise_importer - a simple TUI program to export your bank log to Splitwise
# Copyright (C) 2021 Dmitry Frolov
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# DISCLAIMER:
# This is not an official API. All the trademarks and copyright belongs to Splitwise.com
#!/usr/bin/env python3
import os,sys
CURR_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0,CURR_DIR+'/npyscreen')
import npyscreen
import pandas
import yaml
#from splitwise import *
from auth import splitwiseConnector
from math import floor
class Struct:
def __init__(self, **entries):
self.__dict__.update(entries)
class App(npyscreen.StandardApp):
def onStart(self):
self.addForm("MAIN", MainForm, name="Splitwise Importer")
class LogBox(npyscreen.BoxTitle):
# TitleBufferPager now will be box-wrapped
_contained_widget = npyscreen.BufferPager
class MainForm(npyscreen.ActionForm):
def create(self):
with open(CURR_DIR+'/config.yaml') as f:
config = yaml.safe_load(f)
self.config = Struct(**config)
self.layout = Struct(**config['bank_layout'])
new_handlers = {
# Set Ctrl+q for exit
"^Q": self.exit_func,
#"^s" : self.save_func
}
self.add_handlers(new_handlers)
file = npyscreen.selectFile('./',)
y, x = self.useable_space()
# create a MultiSelect form with entris from the database
self.indices = self.add(npyscreen.TitleMultiSelect, name="Pick entries to send", values=self.load_data(file),max_height=-5, scroll_exit=True)
# display status of messages
self.box = self.add(LogBox,name='log',width=x-20,scroll_exit=True)
self.pager = self.box.entry_widget
self.pager.buffer([" Wating for selection"])
#def event_value_edited(self, event):
# self.pager.display()
def load_data(self,file):
# read csv file
self.csvfile = pandas.read_csv(file,encoding=self.layout.encoding,delimiter=';',decimal=',', skiprows=self.layout.skip_rows, header=None)
self.csvfile[self.layout.sum_col] = [float(str(val).replace(' ','').replace(',','.')) for val in self.csvfile[self.layout.sum_col].values]
if self.layout.negative_payments:
# sort only negative operations
self.csvfile = self.csvfile[self.csvfile.iloc[:,self.layout.sum_col]<0]
entries = self.csvfile.copy()
else:
# sort only postive operations
self.csvfile = self.csvfile[self.csvfile.iloc[:,self.layout.sum_col]>0]
entries = self.csvfile.copy()
# cut long strings
maxChars = 17
for col in entries.select_dtypes(include=[object]):
entries[col] = entries[col].str.slice(0, maxChars)
# get number of elements
numel = self.csvfile.shape[0]
# set list of column numbers
columns = list(filter((-1).__ne__, [self.layout.date_col, self.layout.status_col, self.layout.sum_col, self.layout.type_col, self.layout.comment_col]))
# set multiselect values
vals = [entries.iloc[i,columns].to_string(index=False) for i in range(0,numel)]
return vals
def on_ok(self):
self.sender_func()
def on_cancel(self):
self.parentApp.setNextForm(None)
self.exit_func
def sender_func(self):
sc = splitwiseConnector()
# for every value chosen create expense
counter = 0;
# logging
self.pager.buffer(["total entries: "+str(len(self.indices.value))])
for i in self.indices.value:
entry = self.csvfile.iloc[i,:]
errors = sc.createEqualExpenseFromEntry(entry)
# logging
try:
self.pager.buffer([str(counter)+" "+str(errors.getErrors())])
except:
self.pager.buffer([str(counter)+" "+"OK"])
counter += 1
self.pager.display()
self.pager.buffer(["press ctrl+q to exit"])
self.pager.buffer(["\n"])
self.pager.display()
def exit_func(self, _input):
exit(0)
if __name__ == '__main__':
MyApp = App()
MyApp.run()