13 |
|
import gdata.calendar.service |
14 |
|
import optparse |
15 |
|
import os.path |
16 |
+ |
import sys |
17 |
|
|
18 |
|
def decode(string): |
19 |
|
return base64.b64decode(string.decode('rot13')) |
20 |
|
|
21 |
+ |
def _bank(option, opt_str, value, parser): |
22 |
+ |
if value == 'google': |
23 |
+ |
raise optparse.OptionValueError, '%s must not be google' % opt_str |
24 |
+ |
|
25 |
+ |
global bank |
26 |
+ |
|
27 |
+ |
bank = value |
28 |
+ |
|
29 |
+ |
parser.values.ensure_value(option.dest, {}).setdefault(bank, []) |
30 |
+ |
|
31 |
+ |
def _account(option, opt_str, value, parser): |
32 |
+ |
try: |
33 |
+ |
getattr(parser.values, option.dest)[bank].append(value) |
34 |
+ |
except NameError: |
35 |
+ |
raise optparse.OptionValueError, '%s must be after -b/--bank' % opt_str |
36 |
+ |
|
37 |
|
if __name__ == '__main__': |
38 |
|
parser = optparse.OptionParser() |
39 |
|
|
40 |
+ |
parser.add_option('-A', '--all', action = 'store_true', dest = 'all') |
41 |
+ |
parser.add_option('-b', '--bank', action = 'callback', callback = _bank, dest = 'banks', type = 'string') |
42 |
+ |
parser.add_option('-a', '--account', action = 'callback', callback = _account, dest = 'banks', type = 'string') |
43 |
+ |
parser.add_option('-l', '--list', action = 'store_true', dest = 'list') |
44 |
|
parser.add_option('-D', '--debug', action = 'store_true', dest = 'debug') |
45 |
|
|
46 |
|
options = parser.parse_args()[0] |
47 |
< |
parser = SafeConfigParser() |
47 |
> |
|
48 |
> |
if not options.all and not options.banks and not options.list: |
49 |
> |
parser.error('-A, -b, or -l not specified') |
50 |
> |
|
51 |
> |
config = SafeConfigParser() |
52 |
> |
|
53 |
> |
config.read([os.path.expanduser('~/.creditcard')]) |
54 |
> |
|
55 |
> |
banks = dict(map(lambda bank: (bank, config.get(bank, 'accounts').split(',')), (options.banks.iterkeys() if options.banks else filter(lambda section: section != 'google', config.sections())))) |
56 |
> |
|
57 |
> |
for bank in banks.iterkeys(): |
58 |
> |
accounts = frozenset(options.banks[bank]) |
59 |
> |
|
60 |
> |
if accounts: |
61 |
> |
all_accounts = frozenset(banks[bank]) |
62 |
> |
|
63 |
> |
if accounts <= all_accounts: |
64 |
> |
banks[bank] = accounts |
65 |
> |
else: |
66 |
> |
parser.error('not account(s): %s' % ', '.join(accounts - all_accounts)) |
67 |
> |
|
68 |
> |
if options.list: |
69 |
> |
for bank, accounts in banks.iteritems(): |
70 |
> |
print bank |
71 |
> |
|
72 |
> |
for account in accounts: |
73 |
> |
print ' ' + account |
74 |
> |
|
75 |
> |
sys.exit(0) |
76 |
> |
|
77 |
|
service = gdata.calendar.service.CalendarService() |
78 |
|
|
79 |
< |
parser.read([os.path.expanduser('~/.creditcard')]) |
30 |
< |
service.ClientLogin(parser.get('google', 'username'), decode(parser.get('google', 'password'))) |
79 |
> |
service.ClientLogin(config.get('google', 'username'), decode(config.get('google', 'password'))) |
80 |
|
|
81 |
< |
for section in filter(lambda section: section != 'google', parser.sections()): |
82 |
< |
exec 'import %s' % section |
83 |
< |
exec "bank = %s.Bank(parser.get(section, 'username'), decode(parser.get(section, 'password')), options.debug)" % section |
81 |
> |
for bank, accounts in banks.iteritems(): |
82 |
> |
exec 'import %s' % bank |
83 |
> |
exec "bank = %s.Bank(config.get(bank, 'username'), decode(config.get(bank, 'password')), options.debug)" % bank |
84 |
|
|
85 |
< |
for account in parser.get(section, 'accounts').split(','): |
85 |
> |
for account in accounts: |
86 |
|
name, account = account.rsplit(' ', 1) |
87 |
< |
due = bank(account) |
87 |
> |
due = bank.due(account) |
88 |
|
today = date.today() |
89 |
|
|
90 |
|
if due <= today: |