1 |
< |
# Common |
1 |
> |
#!/usr/local/bin/python |
2 |
> |
# Reminder |
3 |
|
# |
4 |
|
# Douglas Thrift |
5 |
|
# |
9 |
|
from ConfigParser import SafeConfigParser |
10 |
|
import optparse |
11 |
|
import os.path |
12 |
< |
import sys |
12 |
> |
import warnings |
13 |
|
|
14 |
< |
FORBIDDEN = ('common', 'creditcard', 'google', 'wesabe') |
14 |
> |
with warnings.catch_warnings(): |
15 |
> |
warnings.filterwarnings('ignore', r'the sha module is deprecated', DeprecationWarning) |
16 |
> |
|
17 |
> |
import gdata.calendar.service |
18 |
> |
|
19 |
> |
MODULES = frozenset(('creditcards', 'facebook', 'wesabe')) |
20 |
> |
NOT_BANKS = MODULES | frozenset(('google',)) |
21 |
|
|
22 |
|
class Bank(object): |
23 |
< |
def download(self, account): |
24 |
< |
raise NotImplementedError |
23 |
> |
def __init__(self, module, config, debug): |
24 |
> |
self.module = module |
25 |
> |
self.bank = None |
26 |
> |
self.config = config |
27 |
> |
self.debug = debug |
28 |
|
|
29 |
< |
def due(self, account): |
30 |
< |
raise NotImplementedError |
29 |
> |
def __call__(self): |
30 |
> |
if self.bank is None: |
31 |
> |
exec 'import %s' % self.module |
32 |
> |
exec "self.bank = %s.Bank(Config(self.config, self.module), self.debug)" % self.module |
33 |
|
|
34 |
< |
class OptionParser(optparse.OptionParser): |
35 |
< |
def __init__(self, *args, **kwargs): |
36 |
< |
optparse.OptionParser.__init__(self, *args, **kwargs) |
37 |
< |
self.add_option('-A', '--all', action = 'store_true', dest = 'all') |
38 |
< |
self.add_option('-b', '--bank', action = 'callback', callback = self.__bank, dest = 'banks', type = 'string') |
39 |
< |
self.add_option('-a', '--account', action = 'callback', callback = self.__account, dest = 'banks', type = 'string') |
40 |
< |
self.add_option('-l', '--list', action = 'store_true', dest = 'list') |
41 |
< |
self.add_option('-D', '--debug', action = 'store_true', dest = 'debug') |
42 |
< |
|
43 |
< |
def __bank(self, option, opt_str, value, parser): |
44 |
< |
if value in FORBIDDEN: |
45 |
< |
raise optparse.OptionValueError, '%s must not be %s' % (opt_str, value) |
34 |
> |
return self.bank |
35 |
> |
|
36 |
> |
class Banks(object): |
37 |
> |
def __init__(self, config, debug): |
38 |
> |
self.banks = {} |
39 |
> |
self.config = config |
40 |
> |
self.debug = debug |
41 |
> |
|
42 |
> |
def bank(self, module): |
43 |
> |
if module not in NOT_BANKS: |
44 |
> |
return self.banks.setdefault(module, Bank(module, self.config, self.debug))() |
45 |
> |
|
46 |
> |
class Config(object): |
47 |
> |
def __init__(self, config, module): |
48 |
> |
self.config = config |
49 |
> |
self.module = module |
50 |
|
|
51 |
< |
self.bank = value |
51 |
> |
def get(self, *args, **kwargs): |
52 |
> |
return self.config.get(self.module, *args, **kwargs) |
53 |
|
|
54 |
< |
parser.values.ensure_value(option.dest, {}).setdefault(value, []) |
54 |
> |
def getboolean(self, *args, **kwargs): |
55 |
> |
return self.config.getboolean(self.module, *args, **kwargs) |
56 |
|
|
57 |
< |
def __account(self, option, opt_str, value, parser): |
58 |
< |
try: |
41 |
< |
getattr(parser.values, option.dest)[self.bank].append(value) |
42 |
< |
except NameError: |
43 |
< |
raise optparse.OptionValueError, '%s must be after -b/--bank' % opt_str |
57 |
> |
def getfloat(self, *args, **kwargs): |
58 |
> |
return self.config.getfloat(self.module, *args, **kwargs) |
59 |
|
|
60 |
< |
def config(parser, options, file): |
61 |
< |
if not options.all and not options.banks and not options.list: |
47 |
< |
parser.error('-A, -b, or -l not specified') |
60 |
> |
def getint(self, *args, **kwargs): |
61 |
> |
return self.config.getint(self.module, *args, **kwargs) |
62 |
|
|
63 |
< |
config = SafeConfigParser() |
63 |
> |
def getlist(self, *args, **kwargs): |
64 |
> |
return self.config.getlist(self.module, *args, **kwargs) |
65 |
|
|
66 |
< |
config.read([os.path.expanduser('~/%s' % file)]) |
66 |
> |
def getpassword(self, *args, **kwargs): |
67 |
> |
return self.config.getpassword(self.module, *args, **kwargs) |
68 |
|
|
69 |
< |
banks = dict(map(lambda bank: (bank, config.get(bank, 'accounts').split(',')), (options.banks.iterkeys() if options.banks else filter(lambda section: section not in FORBIDDEN, config.sections())))) |
69 |
> |
class ConfigParser(SafeConfigParser): |
70 |
> |
def __init__(self, *args, **kwargs): |
71 |
> |
SafeConfigParser.__init__(self, *args, **kwargs) |
72 |
> |
|
73 |
> |
def getlist(self, *args, **kwargs): |
74 |
> |
return self.get(*args, **kwargs).split(',') |
75 |
> |
|
76 |
> |
def getpassword(self, *args, **kwargs): |
77 |
> |
return base64.b64decode(self.get(*args, **kwargs).decode('rot13')) |
78 |
> |
|
79 |
> |
class OptionParser(optparse.OptionParser): |
80 |
> |
def __init__(self, *args, **kwargs): |
81 |
> |
optparse.OptionParser.__init__(self, *args, **kwargs) |
82 |
> |
self.add_option('-A', '--all', action = 'store_true', dest = 'all') |
83 |
> |
self.add_option('-m', '--module', action = 'callback', callback = self.__module, dest = 'modules', type = 'string') |
84 |
> |
self.add_option('-D', '--debug', action = 'store_true', dest = 'debug') |
85 |
|
|
86 |
< |
if options.banks: |
87 |
< |
for bank in banks.iterkeys(): |
88 |
< |
accounts = frozenset(options.banks[bank]) |
86 |
> |
def __module(self, option, opt_str, value, parser): |
87 |
> |
if value not in MODULES: |
88 |
> |
raise optparse.OptionValueError, '%s unknown module %s' % (opt_str, value) |
89 |
|
|
90 |
< |
if accounts: |
60 |
< |
all_accounts = frozenset(banks[bank]) |
90 |
> |
parser.values.ensure_value(option.dest, []).append(value) |
91 |
|
|
92 |
< |
if accounts <= all_accounts: |
93 |
< |
banks[bank] = accounts |
94 |
< |
else: |
65 |
< |
parser.error('not account(s): %s' % ', '.join(accounts - all_accounts)) |
92 |
> |
if __name__ == '__main__': |
93 |
> |
parser = OptionParser() |
94 |
> |
options = parser.parse_args()[0] |
95 |
|
|
96 |
< |
if options.list: |
97 |
< |
for bank, accounts in banks.iteritems(): |
69 |
< |
print bank |
96 |
> |
if not options.all and not options.modules: |
97 |
> |
parser.error('-A or -m not specified') |
98 |
|
|
99 |
< |
for account in accounts: |
72 |
< |
print ' ' + account |
99 |
> |
config = ConfigParser() |
100 |
|
|
101 |
< |
sys.exit(0) |
101 |
> |
config.read([os.path.expanduser('~/.reminder')]) |
102 |
|
|
103 |
< |
return config, banks |
103 |
> |
calendar = gdata.calendar.service.CalendarService() |
104 |
> |
calendar.email = config.get('google', 'username') |
105 |
> |
calendar.password = config.getpassword('google', 'password') |
106 |
> |
calendar.source = 'Reminder-0.9' |
107 |
|
|
108 |
< |
def decode(string): |
79 |
< |
return base64.b64decode(string.decode('rot13')) |
108 |
> |
calendar.ProgrammaticLogin() |
109 |
|
|
110 |
< |
def bank(bank, options, config): |
82 |
< |
exec 'import %s' % bank |
83 |
< |
exec "bank = %s.Bank(config.get(bank, 'username'), decode(config.get(bank, 'password')), options.debug)" % bank |
110 |
> |
banks = Banks(config, options.debug) |
111 |
|
|
112 |
< |
return bank |
112 |
> |
for module in options.modules if not options.all else sorted(MODULES): |
113 |
> |
exec 'import %s' % module |
114 |
> |
exec '%s.main(%s)' % (module, ', '.join(['calendar', 'Config(config, module)'] + (['banks', 'options.debug'] if module in ('creditcards', 'wesabe') else ['options.debug']))) |