-
Notifications
You must be signed in to change notification settings - Fork 0
/
ql
executable file
·554 lines (520 loc) · 15.6 KB
/
ql
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
#!/usr/bin/env python3
"""
___ _ _ _ _
/ _ \ _ _(_) ___| | _| | ___ __| | __ _ ___ _ __
| | | | | | | |/ __| |/ / | / _ \/ _` |/ _` |/ _ \ '__|
| |_| | |_| | | (__| <| |__| __/ (_| | (_| | __/ |
\__\_\\__,_|_|\___|_|\_\_____\___|\__,_|\__, |\___|_|
|___/
Version 1.0.1
Project Homepage: https://github.com/robotmachine/QuickLedger
QuickLedger
Entry creation utility for https://ledger-cli.org
(C)2014-2016 Brian A. Carter (robotmachine@gmail.com)
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 <http://www.gnu.org/licenses/>.
"""
import os, sys, datetime, textwrap, argparse, configparser, distutils.util
from decimal import *
from datetime import date, timedelta
global qlVer
qlVer = str("1.0.1")
global config
config = configparser.ConfigParser()
def main():
parser = argparse.ArgumentParser(description="QuickLedger: Entry creation utility for http://ledger-cli.org", prog='ql')
parser.add_argument('-f', '--file',
action='store', dest='ledger_file', default=None,
help='Specify Ledger data file.')
parser.add_argument('-a', '--account',
action='store', dest='account', default=None,
help='Specify account.')
parser.add_argument('-m', '--merchant',
action='store', dest='merchant', default=None,
help='Set merchant.')
parser.add_argument('-c', '--category',
action='store', dest='category', default=None,
help='Set transaction category.')
parser.add_argument('-e', '--expense',
action='store', dest='expense', default=None,
help="Set expense category. (Prepends 'Expenses:')")
parser.add_argument('-t', '--amount',
action='store', dest='amount', default=None,
help='Set transaction amount.')
parser.add_argument('-r', '--rdate',
action='store', dest='rdate', default=None,
help='Set number of days ago transaction occurred. Positive value for days in the past, negative value for days in the future. Overrides --date.')
parser.add_argument('-d', '--date',
action='store', dest='date', default=None,
help='Set date of transaction. Format is YYYY-MM-DD.')
parser.add_argument('-s', '--split',
action='store_true', dest='split', default=False,
help='Split payment.',)
parser.add_argument('-x', '--not-cleared',
action='store_true', dest='uncleared',
help='Mark transaction as not-cleared/pending.')
parser.add_argument('--list',
action='store_true', dest='listit',
help='List settings in config file.')
parser.add_argument('--config',
action='store', dest='alt_config', default=None,
help='Specify alternate config file.')
parser.add_argument('-v','--version', default=False,
action='store_true', dest='ArgVer',
help='Print version.')
args = parser.parse_args()
"""
Check version.
"""
if args.ArgVer is True:
PrintVersion()
"""
Checks if an alternate config file was specified and verifies that it exists if so.
If not, then it will check that the default location is there ~/.qlrc
If that isn't present, then it will run the setup.
"""
global settings
if args.alt_config is not None:
pathtest = os.path.exists(os.path.expanduser(args.alt_config))
if pathtest is False:
print('\n%s not found.\n' % args.alt_config)
settings = None
else:
settings = os.path.expanduser(args.alt_config)
else:
settings = None
if settings is None:
settest = os.path.exists(os.path.expanduser("~/.qlrc"))
if settest is True:
settings = os.path.expanduser("~/.qlrc")
else:
settings = set_config()
config.read(settings)
"""
Run setup or list if selected.
"""
if args.listit:
listit()
"""
Determine ledger file.
"""
if args.ledger_file and os.path.exists(os.path.expanduser(args.ledger_file)):
ledger_file = os.path.expanduser(args.ledger_file)
elif os.path.exists(os.path.expanduser(os.environ['LEDGER'])):
ledger_file = os.path.expanduser(os.environ['LEDGER'])
else:
ledger_file = read_config('ledger', None, None)
"""
Set merchant.
"""
if args.merchant is None:
merchant = read_config('merchant', None, None)
else:
merchant = read_config('merchant', args.merchant, None)
"""
Sets category.
"""
split = args.split
if split is False:
if args.category is None and args.expense is None:
category = read_config('category', None, args.merchant)
elif args.category is not None and args.expense is None:
category = read_config('category', args.category, args.merchant)
elif args.expense is not None:
expcat = str('Expenses:'+str(args.expense))
category = read_config('category', expcat, args.merchant)
else:
None
"""
Set amount.
"""
if args.amount is None and split is False:
amlist = []
catlist = []
amlist.append(dollar_tool(query_tool('Amount: $')))
catlist.append(str(category))
trtotal = amlist[0]
elif args.amount is not None and split is False:
amlist = []
catlist = []
amlist.append(dollar_tool(args.amount))
catlist.append(str(category))
trtotal = amlist[0]
elif args.amount is None and split is True:
amlist, catlist, trtotal = splitter(None)
else:
amlist, catlist, trtotal = splitter(dollar_tool(args.amount))
"""
Set account.
"""
if args.account is None:
account = read_config('account', None, None)
else:
account = read_config('account', args.account, None)
"""
Set date.
"""
if args.rdate is not None:
rdate = int(args.rdate)
date = relDate(rdate)
elif args.date is not None:
valiDate(args.date)
date = args.date
else:
date = datetime.date.today()
"""
Sets the cleared status based on -x / --not-cleared
"""
if args.uncleared:
clrstat = str(' ! ')
else:
clrstat = str(' * ')
"""
"""
ledger_entry = assembly(date, clrstat, merchant, amlist, catlist, account, trtotal)
printer(ledger_file, ledger_entry)
quit()
def read_config(query, varone, vartwo):
"""
"""
if query == 'ledger':
result = os.path.expanduser(config['file']['ledger_file'])
elif query == 'account':
if varone is None:
try:
default = config['account']['default_account']
result = config['account'][default]
except:
acct_entry = str(query_tool('Account:\n\tAssets:'))
try:
result = config['account'][acct_entry]
except:
result = str('Assets:'+acct_entry)
else:
try:
result = config['account'][varone]
except:
result = varone
elif query == 'merchant':
if varone is None:
merchant = str(query_tool('Merchant name:\n\t'))
try:
result = config['merchant'][merchant]
except:
result = merchant
else:
try:
result = config['merchant'][varone]
except:
result = varone
elif query == 'category':
try:
result = config['category'][vartwo]
except:
if varone is None:
cat_entry = str(query_tool('Category:\n\tExpenses:'))
try:
result = config['category'][cat_entry]
except:
result = str('Expenses:'+cat_entry)
else:
try:
result = config['category'][varone]
except:
result = varone
return result
def relDate(rdate):
today = date.today()
result = today - timedelta(rdate)
return result
def valiDate(date):
try:
datetime.datetime.strptime(date, '%Y-%m-%d')
except:
print("Incorrect format. YYYY-MM-DD required.")
quit()
def splitter(amount):
if amount is None:
total = dollar_tool(query_tool('\nTotal dollar amount for entry: $'))
else:
total = amount
print('\nTotal is $%.2f' % total)
latot = total
counter = 0
amlist = []
catlist = []
print("Enter '0' for the remainder.")
while latot is not None:
counter = counter + 1
splitamount = dollar_tool(query_tool('\nEnter amount for split number %i:\t$' % counter))
cat_entry = query_tool('\nEnter category for split number %i:\tExpenses:' % counter)
try:
splitcat = config['category'][cat_entry]
except:
splitcat = str('Expenses:'+cat_entry)
if splitamount == 0.00:
amlist.append(latot)
catlist.append(splitcat)
latot = 0.00
else:
amlist.append(splitamount)
catlist.append(splitcat)
latot = latot - splitamount
if latot == 0.00:
latot = None
elif latot < 0.00:
print('Transaction does not balance.')
amlist = []
catlist = []
amount = total
splitter(amount)
else:
print('\n$%.2f remaining.' % latot)
counter = counter - 1
return amlist, catlist, total
def assembly(date, clrstat, merchant, amlist, catlist, account, trtotal):
ledger_list = []
ledger_list.append(date)
ledger_list.append(clrstat)
ledger_list.append(merchant)
ledger_list.append('\n')
counter = len(amlist) - 1
while counter >= 0:
ledger_list.append('\t')
ledger_list.append(str(catlist[counter]))
ledger_list.append('\t\t\t$')
ledger_list.append(str(amlist[counter]))
ledger_list.append('\n')
counter = counter - 1
ledger_list.append('\t')
ledger_list.append(account)
if len(amlist) > 1:
ledger_list.append('\t\t\t$-')
ledger_list.append(str(trtotal))
ledger_list.append('\n')
result = ''.join([str(i) for i in ledger_list])
return result
def printer(ledger_file, ledger_entry):
try:
with open(ledger_file, "a") as ledger_write:
ledger_write.write(ledger_entry)
print('\n\nWrote entry to '+ledger_file+':\n'+ledger_entry)
except PermissionError:
print("Cannot write to %s. Permission error!" % ledger_file)
except FileNotFoundError:
print('Cannot find %s' % ledger_file)
except:
print('Something went wrong with the printer.')
quit()
quit()
def accounts():
shortname = query_tool('\nEnter a short name for the account: ')
acctname = 'Assets:'+query_tool('\nEnter the account name: Assets:')
fullacct = shortname+" = "+acctname
if os.path.exists(settings):
config.read(settings)
else:
print('No .qlrc file found. Creating ~/.qlrc')
try:
defaultq = config['account']['default_account']
except:
defaultq = None
if defaultq is None:
boolquery = bool_tool("\nWould you like to set this as the default account? [y/N]: ")
if boolquery == True:
makedefault = True
else:
makedefault = False
elif defaultq is not None:
defaultqq = config['account'][defaultq]
boolquery = bool_tool("\nThe current default account is "+defaultqq+"\nWould you like to replace it with this one? [Y/n] ")
if boolquery == True:
makedefault = True
else:
makedefault = False
config.set('account',shortname,acctname)
if makedefault == True:
config.set('account','default_account',shortname)
with open(settings, 'w') as configfile:
config.write(configfile)
quit()
"""
def merchants():
nickname = query_tool('\nEnter a short name for the merchant: ')
merchname = query_tool('\nEnter the full merchant name: ')
boolquery = bool_tool("\nWould you like to enter a default category for this merchant? [y/N] ")
if boolquery == True:
merchcat = query_tool('\nEnter a default category for '+merchname+': ')
else:
merchcat = False
if os.path.exists(settings):
config.read(settings)
else:
print('No .qlrc file found. Creating ~/.qlrc')
config ['merchant'] = {nickname: merchname}
try:
with open(settings, 'w') as configfile:
config.write(configfile)
print('Merchant written to %s' % settings)
except PermissionError:
print('\nERROR:\nUnable to write settings to %s' % settings)
quit()
except:
print('Something went wrong.')
quit()
if merchcat:
config ['category'] = {nickname: merchcat}
try:
with open(settings, 'w') as configfile:
config.write(configfile)
print('Category written to %s' % settings)
except PermissionError:
print('\nERROR:\nUnable to write settings to %s' % settings)
quit()
except:
print('Something went wrong.')
quit()
quit()
def categories():
nickname = query_tool('\nEnter a short name for the category: ')
catname = query_tool('\nEnter the full category: ')
if os.path.exists(settings):
config.read(settings)
else:
print('No .qlrc file found. Creating ~/.qlrc')
config ['category'] = {nickname: catname}
try:
with open(settings, 'w') as configfile:
config.write(configfile)
print('Settings written to %s' % settings)
except PermissionError:
print('\nERROR:\nUnable to write settings to %s' % settings)
quit()
except:
print('Something went wrong.')
quit()
quit()
"""
def listit():
try:
config.read(settings)
except:
print('\nERROR:\nNo config file found. Run some kind of setup.')
quit()
try:
print('\nMerchants\n')
for conffile in config['merchant']:
print(conffile+'\t = '+config['merchant'][conffile])
except:
print('\nNo merchants found in %s.\n' % settings)
try:
print('\nAccounts')
print(config['account']['default_account']+' is the default account.\n')
for conffile in config['account']:
if conffile != "default_account":
print(conffile+"\t = "+config['account'][conffile])
except:
print('\nNo accounts found in %s.\n' % settings)
quit()
def set_config():
settings = os.path.expanduser('~/.qlrc')
"""
Checks for both $LEDGER and $LEDGER_FILE environment variables.
Sets system_ledger to their value if they exist.
"""
try:
if os.path.exists(os.path.expanduser(os.environ['LEDGER'])):
system_ledger = os.path.expanduser(os.environ['LEDGER'])
elif os.path.exists(os.path.expanduser(os.environ['LEDGER_FILE'])):
system_ledger = os.path.expanduser(os.environ['LEDGER_FILE'])
else:
system_ledger = None
except:
system_ledger = None
"""
Asks if the above value should be set as the default file for `ql'
"""
if system_ledger is not None:
print("Default Ledger file detected as %s" % system_ledger)
boolquery = bool_tool("Use this file for ql? [y/N] ")
if boolquery is True:
led_file = system_ledger
else:
led_file = None
else:
led_file = None
"""
If either the $LEDGER and $LEDGER_FILE variables are empty or the user
declines to use their value, it will request that the file be typed in manually.
If the file is not found, an error will print.
"""
if led_file is None:
led_input = query_tool('Enter Ledger file location: ')
if os.path.exists(os.path.expanduser(led_input)):
led_file = os.path.expanduser(led_input)
else:
print('\nERROR:\nLedger file not found: %s' % led_input)
quit()
"""
Checks again to make sure the ledger_file actually exists.
If it does, then it writes that value to .qlrc in the $HOME folder.
"""
if os.path.isfile(led_file):
config ['file'] = {'ledger_file': led_file}
try:
with open(settings, 'w') as configfile:
config.write(configfile)
print('Settings written to %s' % settings)
except PermissionError:
print('\nERROR:\nUnable to write settings to %s' % settings)
quit()
except:
print('Something went wrong.')
quit()
else:
print('\nERROR:\nLedger file not found: %s' % led_file)
return settings
def user_exit():
print("\nUser exited.")
quit()
def query_tool(query):
try:
result = input(query)
return result
except KeyboardInterrupt:
user_exit()
except:
print("\nSyntax error.")
quit()
def bool_tool(query):
bquery = query_tool(query)
try:
result = bool(distutils.util.strtobool(bquery))
return result
except:
result = False
return result
def dollar_tool(query):
try:
result = Decimal(query).quantize(Decimal('1.00'))
except KeyboardInterrupt:
user_exit()
except InvalidOperation:
print('\nMust be a number.')
quit()
except:
print('\nSyntax error.')
quit()
return result
def PrintVersion():
print("\nQuickLedger v."+str(qlVer)+"\n\n(C)2014-2016 Brian A. Carter\nrobotmachine@gmail.com\nhttps://github.com/robotmachine/QuickLedger")
quit()
main()