1 |
douglas |
1179 |
#!/usr/local/bin/python |
2 |
|
|
# Reminder |
3 |
douglas |
1178 |
# |
4 |
|
|
# Douglas Thrift |
5 |
|
|
# |
6 |
|
|
# $Id$ |
7 |
|
|
|
8 |
|
|
import base64 |
9 |
|
|
from ConfigParser import SafeConfigParser |
10 |
|
|
import optparse |
11 |
|
|
import os.path |
12 |
douglas |
1179 |
import warnings |
13 |
douglas |
1178 |
|
14 |
douglas |
1179 |
with warnings.catch_warnings(): |
15 |
|
|
warnings.filterwarnings('ignore', r'the sha module is deprecated', DeprecationWarning) |
16 |
douglas |
1178 |
|
17 |
douglas |
1179 |
import gdata.calendar.service |
18 |
|
|
|
19 |
|
|
MODULES = frozenset(('creditcards', 'facebook', 'wesabe')) |
20 |
|
|
NOT_BANKS = MODULES | frozenset(('google',)) |
21 |
|
|
|
22 |
douglas |
1178 |
class Bank(object): |
23 |
douglas |
1179 |
def __init__(self, module, config, debug): |
24 |
|
|
self.module = module |
25 |
|
|
self.bank = None |
26 |
|
|
self.config = config |
27 |
|
|
self.debug = debug |
28 |
douglas |
1178 |
|
29 |
douglas |
1179 |
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 |
douglas |
1178 |
|
34 |
douglas |
1179 |
return self.bank |
35 |
douglas |
1178 |
|
36 |
douglas |
1179 |
class Banks(object): |
37 |
|
|
def __init__(self, config, debug): |
38 |
|
|
self.banks = {} |
39 |
|
|
self.config = config |
40 |
|
|
self.debug = debug |
41 |
douglas |
1178 |
|
42 |
douglas |
1179 |
def bank(self, module): |
43 |
|
|
if module not in NOT_BANKS: |
44 |
|
|
return self.banks.setdefault(module, Bank(module, self.config, self.debug))() |
45 |
douglas |
1178 |
|
46 |
douglas |
1179 |
class Config(object): |
47 |
|
|
def __init__(self, config, module): |
48 |
|
|
self.config = config |
49 |
|
|
self.module = module |
50 |
douglas |
1178 |
|
51 |
douglas |
1179 |
def get(self, *args, **kwargs): |
52 |
|
|
return self.config.get(self.module, *args, **kwargs) |
53 |
douglas |
1178 |
|
54 |
douglas |
1179 |
def getboolean(self, *args, **kwargs): |
55 |
|
|
return self.config.getboolean(self.module, *args, **kwargs) |
56 |
douglas |
1178 |
|
57 |
douglas |
1179 |
def getfloat(self, *args, **kwargs): |
58 |
|
|
return self.config.getfloat(self.module, *args, **kwargs) |
59 |
douglas |
1178 |
|
60 |
douglas |
1179 |
def getint(self, *args, **kwargs): |
61 |
|
|
return self.config.getint(self.module, *args, **kwargs) |
62 |
douglas |
1178 |
|
63 |
douglas |
1179 |
def getlist(self, *args, **kwargs): |
64 |
|
|
return self.config.getlist(self.module, *args, **kwargs) |
65 |
douglas |
1178 |
|
66 |
douglas |
1179 |
def getpassword(self, *args, **kwargs): |
67 |
|
|
return self.config.getpassword(self.module, *args, **kwargs) |
68 |
douglas |
1178 |
|
69 |
douglas |
1179 |
class ConfigParser(SafeConfigParser): |
70 |
|
|
def __init__(self, *args, **kwargs): |
71 |
|
|
SafeConfigParser.__init__(self, *args, **kwargs) |
72 |
douglas |
1178 |
|
73 |
douglas |
1179 |
def getlist(self, *args, **kwargs): |
74 |
|
|
return self.get(*args, **kwargs).split(',') |
75 |
douglas |
1178 |
|
76 |
douglas |
1179 |
def getpassword(self, *args, **kwargs): |
77 |
|
|
return base64.b64decode(self.get(*args, **kwargs).decode('rot13')) |
78 |
douglas |
1178 |
|
79 |
douglas |
1179 |
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 |
douglas |
1178 |
|
86 |
douglas |
1179 |
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 |
douglas |
1178 |
|
90 |
douglas |
1179 |
parser.values.ensure_value(option.dest, []).append(value) |
91 |
douglas |
1178 |
|
92 |
douglas |
1179 |
if __name__ == '__main__': |
93 |
|
|
parser = OptionParser() |
94 |
|
|
options = parser.parse_args()[0] |
95 |
douglas |
1178 |
|
96 |
douglas |
1179 |
if not options.all and not options.modules: |
97 |
|
|
parser.error('-A or -m not specified') |
98 |
|
|
|
99 |
|
|
config = ConfigParser() |
100 |
|
|
|
101 |
|
|
config.read([os.path.expanduser('~/.reminder')]) |
102 |
|
|
|
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 |
|
|
calendar.ProgrammaticLogin() |
109 |
|
|
|
110 |
|
|
banks = Banks(config, options.debug) |
111 |
|
|
|
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']))) |