-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuserClass.py
425 lines (318 loc) · 13.7 KB
/
userClass.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
import curses
import csv
import os.path
import sys
import operator
from tempfile import NamedTemporaryFile
import shutil
from itertools import tee
import json
from datetime import datetime
import time
import textwrap
import math
import os
import copy
debugmode = True
def fatalerror(msg):
sys.exit(msg)
def alert(msg):
print(msg)
def errormsg(msg):
print(msg)
def debug(msg):
if debugmode:
print(msg)
# Categorises the data in newfile. and adds
class User:
default_options = {
'accounts': {},
'categories': [],
'dupratio': 0.2,
'fields': ['Transaction Date', 'Transaction Type', 'Sort Code', 'Account Number', 'Transaction Description', 'Debit Amount', 'Credit Amount', 'Balance', 'Category'],
'currency': '£',
'dateformat': '%d/%m/%Y',
'username': ''
}
options = copy.deepcopy(default_options)
MAINCONFIGLOCATION = "main.config"
USERDATA = None
OPTIONSEXTENSION = None
DATAEXTENSION = None
csvlocation = None
optionslocation = None
mainprint = None
maininput = None
default_user_set = False
user_loaded = False
def setup_message_system(self, msgbox, error, fatalerror):
self.msgbox = msgbox
self.errormsg = error
self.fatalerrormsg = fatalerror
def __init__(self, userdatapath, optionsextension, dataextension):
self.USERDATA = userdatapath
self.OPTIONSEXTENSION = optionsextension
self.DATAEXTENSION = dataextension
# Race condition if userdatapath is created between os.path.exists and os.makedirs
if not os.path.exists(self.USERDATA):
os.makedirs(self.USERDATA)
# Find whether a default user exists
main_options_location = self.USERDATA + self.MAINCONFIGLOCATION
self.default_user_set = os.path.isfile(main_options_location)
def load_default(self):
main_options_location = self.USERDATA + self.MAINCONFIGLOCATION
# Load default user
if self.default_user_set:
with open(main_options_location, 'r') as optionsfile:
# Assumes the config file is well formatted TODO
globaloptions = json.loads(optionsfile.read())
self.switchUser(globaloptions["default_name"])
return True
else:
return False
def saveDefaultOptions(self, username):
optionslocation = self.USERDATA + username + self.OPTIONSEXTENSION
with open(optionslocation, 'w', newline='') as optionsfile:
options = copy.deepcopy(self.default_options)
options['username'] = username
options_str = json.dumps(options)
optionsfile.write(options_str)
def saveOptions(self, username=None):
if username == None:
username = self.options['username']
optionslocation = self.USERDATA + username + self.OPTIONSEXTENSION
with open(optionslocation, 'w', newline='') as optionsfile:
options = json.dumps(self.options)
optionsfile.write(options)
# Manipulating user accounts
def get_user_list(self):
f = []
for (dirpath, dirnames, filenames) in os.walk(self.USERDATA):
f.extend(filenames)
break
userloclist = list(filter(lambda x: self.OPTIONSEXTENSION in x, f))
users = list(map(lambda user: user.replace(self.OPTIONSEXTENSION, ''), userloclist))
return users
def userExists(self, username):
f = self.get_user_list()
return username in f
def set_default_user(self, username):
main_options_location = self.USERDATA + self.MAINCONFIGLOCATION
main_options = {
"default_name": username
}
main_options_str = json.dumps(main_options)
# Find the name of the default user
with open(main_options_location, 'w') as optionsfile:
# Assumes the config file is well formatted TODO
optionsfile.write(main_options_str)
self.default_user_set = True
def registerUser(self, username):
main_options_location = self.USERDATA + self.MAINCONFIGLOCATION
csvlocation = self.USERDATA + username + self.DATAEXTENSION
optionslocation = self.USERDATA + username + self.OPTIONSEXTENSION
if self.userExists(username):
self.msgbox("Duplicate user", "User " + username + " already exists!")
return False
# Register a new user
if not os.path.isfile(csvlocation):
self.msgbox("Alert", "No user csv file detected. Creating " + csvlocation + "...")
with open(csvlocation, 'w+') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=self.options['fields'])
row = {}
for field in self.options['fields']:
row[field] = field
writer.writerow(row)
if not os.path.isfile(optionslocation):
self.msgbox("Alert", "No main optionslocation file detected. Creating " + optionslocation + "...")
self.saveDefaultOptions(username)
# If first user, write them as default
if not self.default_user_set:
self.set_default_user(username)
return True
def switchUser(self, username):
self.user_loaded = True
csvlocation = self.USERDATA + username + self.DATAEXTENSION
optionslocation = self.USERDATA + username + self.OPTIONSEXTENSION
if self.userExists(username):
self.csvlocation = csvlocation
self.optionslocation = csvlocation
with open(optionslocation, 'r', newline='') as optionsfile:
self.options = json.loads(optionsfile.read())
return True
else:
self.errormsg("Cannot switch user - " + username + " does not exist!")
return False
# Returns unrecognised accounts and transactions
def get_uncat_data(self, newcsvlocation):
new_accounts = []
new_transactions = []
if not os.path.isfile(newcsvlocation):
self.errormsg("No .csv file found at " + newfile + ".")
return None
with open(newcsvlocation, 'r', newline='') as newcsvfile:
newreader = csv.DictReader(newcsvfile, fieldnames=self.options['fields'])
next(newreader) # Skip the fields
with open(self.csvlocation, 'r') as csvfile:
reader = csv.DictReader(csvfile, fieldnames=self.options['fields'])
# Get list from main spreadsheet without categories
mainreaderlistnocat = []
for row in reader:
newrow = dict(row)
newrow.pop('Category', None)
mainreaderlistnocat.append(newrow)
duprows = []
for row in newreader:
rownocat = dict(row)
rownocat.pop('Category', None)
accno = row['Account Number']
if rownocat in mainreaderlistnocat:
debug("Duplicate row")
duprows.append(row)
else:
new_transactions.append(row)
if accno not in self.options['accounts'].keys():
new_accounts.append(accno)
ratio = len(duprows)/(len(new_transactions) + len(duprows))
if ratio >= self.options['dupratio']:
self.msgbox("WARNING!", "This .csv file appears to be a duplicate of a previously merged change. Proceed with caution.")
new_accounts = list(set(new_accounts))
new_transactions = list(set(map(lambda x: x['Transaction Description'], new_transactions)))
return (new_accounts, new_transactions)
def set_uncat_data(self, newcsvlocation, account_dict, transaction_dict):
if not os.path.isfile(newcsvlocation):
self.errormsg("No .csv file found at " + newfile + ".")
# Create new categories
categories = list(set(transaction_dict.values()))
for cat in categories:
self.options['categories'].append(cat)
tempfile = NamedTemporaryFile(mode='w', delete=False)
with open(newcsvlocation, newline='') as newcsvfile:
newreader = csv.DictReader(newcsvfile, fieldnames=self.options['fields'])
next(newreader) #Skip the header line
with open(self.csvlocation, 'r') as csvfile, tempfile:
reader = csv.DictReader(csvfile, fieldnames=self.options['fields'])
writer = csv.DictWriter(tempfile, fieldnames=self.options['fields'])
mainreaderlistnocat = []
for row in reader:
newrow = dict(row)
newrow.pop('Category', None)
mainreaderlistnocat.append(newrow)
writer.writerow(row)
rows = []
duprows = []
for row in newreader:
rownocat = dict(row)
rownocat.pop('Category', None)
accno = row['Account Number']
if rownocat in mainreaderlistnocat:
debug("Duplicate row")
duprows.append(row)
else:
rows.append(row)
if accno not in self.options['accounts'].keys():
self.options['accounts'][accno] = account_dict[accno]
self.saveOptions()
for row in duprows:
writer.writerow(row)
for row in rows:
if transaction_dict[row['Transaction Description']] != '':
row['Category'] = transaction_dict[row['Transaction Description']]
writer.writerow(row)
self.msgbox("Alert", "Writing changes...")
shutil.move(tempfile.name, self.csvlocation)
# Attempts to find the category of the transaction described by transdescription and transtype
# Guessesby mode
def categorylookup(self, reader, transdescription, transtype):
cats = []
for row in reader:
if row['Transaction Description'] == transdescription and row['Transaction Type'] == transtype:
if row['Category'] != '':
cats.append(row['Category'])
if cats != []:
mode = max(set(cats), key=cats.count)
if cats == [] or mode == '':
return maininput("What is the category of:" + transdescription + ", " + transtype + "? ")
else:
# Return the most probable category
return mode
# Functions to search through spreadsheet data
# Returns all rows from main.csv that satisfy filterrow
def getRows(self, filterrow):
rows = []
with open(self.csvlocation, 'r') as csvfile:
reader = csv.DictReader(csvfile, fieldnames=self.options['fields'])
next(reader)
for row in reader:
if filterrow(row):
rows.append(row)
return rows
# Returns the row immediately before a given time
def getRowAtTime(self, accno, time):
if isinstance(time, str):
try:
date = datetime.strptime(time, self.options['dateformat'])
except Exception:
self.errormsg("Invalid date")
return
else:
date = time
upDate = datetime(1970, 1, 1)
def timefilter(row):
nonlocal upDate
try:
rowdate = datetime.strptime(row['Transaction Date'], self.options['dateformat'])
except Exception:
return False
if row['Account Number'] == accno and rowdate >= upDate and rowdate <= date:
upDate = rowdate
return True
else:
return False
rows = self.getRows(timefilter)
if rows == []:
return None
else:
return rows[-1]
def getBalance(self, accno, time):
row = self.getRowAtTime(accno, time)
if row == None:
return None
else:
return row['Balance']
def acc_name_to_no(self, name):
for k, v in self.options['accounts'].items():
if v == name:
return k
return None
def acc_no_to_name(self, no):
return self.options['accounts'][no]
# Get the rows that exist between given dates, from given accounts and categories
# [datetime1, datetime2]
def getBudgetRows(self, datetime1, datetime2, cats, accs):
def bfilter(row):
accfilter = row['Account Number'] in accs
catfilter = row['Category'] in cats
satisfies = \
datetime.strptime(row['Transaction Date'], self.options['dateformat']) >= datetime1 and \
datetime.strptime(row['Transaction Date'], self.options['dateformat']) <= datetime2 and \
accfilter and catfilter
return satisfies
return self.getRows(bfilter)
# All accs if accs left []
def getBudgetBreakdown(self, datetime1, datetime2, cats, accs):
budget = {}
for cat in cats:
budget[cat] = 0
rows = self.getBudgetRows(datetime1, datetime2, cats, accs)
for row in rows:
if row['Debit Amount'] == '':
debit = 0
else:
debit = float(row['Debit Amount'])
if row['Credit Amount'] == '':
credit = 0
else:
credit = float(row['Credit Amount'])
budget[row['Category']] += debit + credit
return budget