-
Notifications
You must be signed in to change notification settings - Fork 0
/
budget_cli.py
executable file
·377 lines (319 loc) · 14 KB
/
budget_cli.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
import datetime
import budget
if __name__ == "__main__":
account_manager = budget.AccountManager()
user_charge = 0
user_date = ""
user_account_from = ""
user_account_to = ""
user_notes = ""
user_files = []
user_file_data = []
filter_accounts = None
filter_from_to = None
filter_charge_begin = None
filter_charge_end = None
filter_date_begin = None
filter_date_end = None
filter_notes_contain = None
commands = ["list-accounts", "filter", "list-history", "add-account <account>", "transaction"]
default_prompt = "McFalls Budget > "
prompt = default_prompt
state = "top"
while True:
cmd = raw_input(prompt)
cmd = cmd.split()
if len(cmd) == 0:
continue
if state == "top":
if cmd[0] == "help":
print("Print this help. Type 'help <command>' for extended info")
elif cmd[0] == "exit":
break
elif cmd[0] == "list-accounts":
accounts = account_manager.list_accounts()
print("\n".join(["%-20s %0.2f" % (x[0], x[1]) for x in accounts]))
print("------------------------------")
print("Total: " + str(sum([float(x[1]) for x in accounts])))
elif cmd[0] == "start-transaction":
state = "transaction"
prompt = "McFalls Budget (transaction) > "
user_charge = 0
user_date = datetime.datetime.now()
user_account_from = ""
user_account_to = ""
user_notes = ""
user_files = []
user_file_data = []
elif cmd[0] == "undo":
account_manager.undo_last()
elif cmd[0] == "start-filter":
state = "filter"
prompt = "McFalls Budget (filter) > "
elif cmd[0] == "list-filter":
print("Current filter:\nAccounts: %s\nFrom/To: %s\nCharge: [%s, %s]\nDate: [%s, %s]\nNotes: %s" %
(filter_accounts, filter_from_to, filter_charge_begin, filter_charge_end, filter_date_begin,
filter_date_end, filter_notes_contain))
elif cmd[0] == "list-history":
short_history = (cmd[1] == "-s") if len(cmd) > 1 else False
history = account_manager.list_history_filter(accounts=filter_accounts, from_to=filter_from_to,
charge_begin=filter_charge_begin,
charge_end=filter_charge_end,
date_begin=filter_date_begin,
date_end=filter_date_end,
notes_contains=filter_notes_contain)
history = sorted(history, key=lambda item: item.get_date())
for transaction in history:
print(transaction.as_string(short=short_history))
print("")
elif cmd[0] == "add-account":
try:
new_account = " ".join(cmd[1:])
account_manager.add_account(new_account, 0.00)
except Exception as e:
print("Failed to add account: " + e.message)
elif cmd[0] == "edit-charge":
if len(cmd) < 3:
print("usage: edit-charge <id> <charge>")
continue
try:
t_id = int(cmd[1])
except Exception as e:
print("Invalid id: " + e.message)
continue
try:
charge = float(cmd[2])
except Exception as e:
print("Invalid charge: " + e.message)
continue
try:
account_manager.edit_transaction(t_id, charge=charge)
except Exception as e:
print("Failed to edit transaction: " + e.message)
continue
elif cmd[0] == "edit-date":
if len(cmd) < 3:
print("usage: edit-date <id> <YYYY-MM-DD>")
continue
try:
t_id = int(cmd[1])
except Exception as e:
print("Invalid id: " + e.message)
continue
try:
date = datetime.datetime.strptime(cmd[2], "%Y-%m-%d")
except Exception as e:
print("Invalid date: " + e.message)
continue
try:
account_manager.edit_transaction(t_id, date=date)
except Exception as e:
print("Failed to edit transaction: " + e.message)
continue
elif cmd[0] == "edit-notes":
if len(cmd) < 3:
print("usage: edit-notes <id> <notes>")
continue
try:
t_id = int(cmd[1])
except Exception as e:
print("Invalid id: " + e.message)
continue
notes = " ".join(cmd[2:])
try:
account_manager.edit_transaction(t_id, notes=notes)
except Exception as e:
print("Failed to edit transaction: " + e.message)
continue
else:
print("Unrecognized command: %s" % cmd[0])
elif state == "transaction":
if cmd[0] == "help":
print("Print this help.\nlist\ncharge\ndate\naccount-from\naccount-to\nnotes\nadd-file\ncancel\ncommit\n")
if cmd[0] == "list":
print("Charge: " + str(user_charge))
print("Date: " + user_date.strftime("%Y-%m-%d"))
print("Account from: " + user_account_from)
print("Account to: " + user_account_to)
print("Notes: " + user_notes)
print("Files: " + " ".join(user_files))
if cmd[0] == "charge":
if len(cmd) < 2:
print("No charge given")
continue
try:
user_charge = float(cmd[1])
except Exception as e:
print("Failed to set charge: " + e.message)
continue
print("Set charge to %s" % cmd[1])
if cmd[0] == "date":
if len(cmd) < 2:
print("No date given")
if cmd[1] == "today":
user_date = datetime.datetime.now()
else:
try:
user_date = datetime.datetime.strptime(cmd[1], "%Y-%m-%d")
except Exception as e:
print("Could not set date: " + e.message)
continue
print("Set date to: " + user_date.strftime("%Y-%m-%d"))
if cmd[0] == "account-from":
if len(cmd) < 2:
print("No account given")
continue
account_from = " ".join(cmd[1:])
if not account_manager.account_exists(account_from):
print("Account does not exist: " + account_from)
continue
user_account_from = account_from
print("Set account from: " + user_account_from)
if cmd[0] == "account-to":
if len(cmd) < 2:
print("No account given")
continue
account_to = " ".join(cmd[1:])
if not account_manager.account_exists(account_to):
print("Account does not exist: " + account_to)
continue
user_account_to = account_to
print("Set account to: " + user_account_to)
if cmd[0] == "notes":
if len(cmd) < 2:
print("No notes given")
continue
user_notes = " ".join(cmd[1:])
print("Set notes to: " + user_notes)
if cmd[0] == "add-file":
if len(cmd) < 3:
print("No files given")
continue
existing_file_name = cmd[1]
desired_file_name = cmd[2]
if desired_file_name in user_files:
print("Cannot add duplicate file name: " + desired_file_name)
continue
user_files.append(desired_file_name)
with open(existing_file_name, "r") as f:
user_file_data.append(f.read())
print("Successfully added file: " + existing_file_name)
if cmd[0] == "cancel":
print("Canceled transaction")
state = "top"
prompt = default_prompt
if cmd[0] == "commit":
try:
new_transaction = budget.Transaction()
new_transaction.from_new(user_charge, user_date, user_account_from, user_account_to,
user_notes, ",".join(user_files))
account_manager.make_transaction(new_transaction, user_file_data)
except Exception as e:
print("Failed to commit transaction: " + e.message)
continue
print("Finished transaction")
state = "top"
prompt = default_prompt
elif state == "filter":
if cmd[0] == "add_account":
if len(cmd) < 2:
print("No account given")
continue
if filter_accounts is None:
filter_accounts = []
filter_accounts.append(" ".join(cmd[1:]))
print("Set account to %s" % filter_accounts)
if cmd[0] == "clear_accounts":
filter_accounts = None
print("Cleared accounts")
if cmd[0] == "from-to":
if len(cmd) < 2:
print("No state given")
continue
if cmd[1] == "from":
filter_from_to = "from"
elif cmd[1] == "to":
filter_from_to = "to"
elif cmd[1] == "none":
filter_from_to = None
else:
print("Invalid state")
print("From_to state: %s" % filter_from_to)
if cmd[0] == "charge-begin":
if len(cmd) < 2:
print("No charge given")
continue
if cmd[1] == "none":
filter_charge_begin = None
print("Cleared charge begin")
else:
try:
filter_charge_begin = float(cmd[1])
print("Set charge begin to %s" % filter_charge_begin)
except Exception as e:
print(e.message)
continue
if cmd[0] == "charge-end":
if len(cmd) < 2:
print("No charge given")
continue
if cmd[1] == "none":
filter_charge_end = None
print("Cleared charge end")
else:
try:
filter_charge_end = float(cmd[1])
print("Set charge end to %s" % filter_charge_end)
except Exception as e:
print(e.message)
continue
if cmd[0] == "date-begin":
if len(cmd) < 2:
print("No date given")
continue
if cmd[1] == "none":
filter_date_begin = None
print("Cleared date begin")
else:
try:
filter_date_begin = datetime.datetime.strptime(cmd[1], "%Y-%m-%d")
print("Set date begin to %s" % filter_date_begin.strftime("%Y-%m-%d"))
except Exception as e:
print(e.message)
continue
if cmd[0] == "date-end":
if len(cmd) < 2:
print("No date given")
continue
if cmd[1] == "none":
filter_date_end = None
print("Cleared date end")
else:
try:
filter_date_end = datetime.datetime.strptime(cmd[1], "%Y-%m-%d")
print("Set date end to %s" % filter_date_end.strftime("%Y-%m-%d"))
except Exception as e:
print(e.message)
continue
if cmd[0] == "notes":
if len(cmd) < 2:
print("No notes given")
continue
if cmd[1] == "none":
filter_notes_contain = None
print("Cleared notes")
else:
filter_notes_contain = " ".join(cmd[1:])
print("Notes set to: " + filter_notes_contain)
if cmd[0] == "list":
print("Current filter:\nAccounts: %s\nFrom/To: %s\nCharge: [%s, %s]\nDate: [%s, %s]\nNotes: %s" %
(filter_accounts, filter_from_to, filter_charge_begin, filter_charge_end,
filter_date_begin.strftime("%Y-%m-%d") if filter_date_begin else "None",
filter_date_end.strftime("%Y-%m-%d") if filter_date_end else "None",
filter_notes_contain))
if cmd[0] == "commit":
print("Finished filter")
state = "top"
prompt = default_prompt
else:
print("Unrecognized command: " + cmd[0])