1 |
douglas |
1179 |
#!/usr/local/bin/python |
2 |
|
|
# Reminder |
3 |
douglas |
1178 |
# |
4 |
|
|
# Douglas Thrift |
5 |
|
|
# |
6 |
|
|
# $Id$ |
7 |
|
|
|
8 |
douglas |
1229 |
from ConfigParser import NoOptionError, SafeConfigParser |
9 |
|
|
import getpass |
10 |
|
|
import keyring |
11 |
douglas |
1178 |
import optparse |
12 |
douglas |
1229 |
import os |
13 |
|
|
import sys |
14 |
douglas |
1179 |
import warnings |
15 |
douglas |
1178 |
|
16 |
douglas |
1179 |
with warnings.catch_warnings(): |
17 |
|
|
warnings.filterwarnings('ignore', r'the sha module is deprecated', DeprecationWarning) |
18 |
douglas |
1178 |
|
19 |
douglas |
1179 |
import gdata.calendar.service |
20 |
|
|
|
21 |
|
|
MODULES = frozenset(('creditcards', 'facebook', 'wesabe')) |
22 |
|
|
NOT_BANKS = MODULES | frozenset(('google',)) |
23 |
|
|
|
24 |
douglas |
1178 |
class Bank(object): |
25 |
douglas |
1179 |
def __init__(self, module, config, debug): |
26 |
|
|
self.module = module |
27 |
|
|
self.bank = None |
28 |
|
|
self.config = config |
29 |
|
|
self.debug = debug |
30 |
douglas |
1178 |
|
31 |
douglas |
1179 |
def __call__(self): |
32 |
|
|
if self.bank is None: |
33 |
|
|
exec 'import %s' % self.module |
34 |
|
|
exec "self.bank = %s.Bank(Config(self.config, self.module), self.debug)" % self.module |
35 |
douglas |
1178 |
|
36 |
douglas |
1179 |
return self.bank |
37 |
douglas |
1178 |
|
38 |
douglas |
1179 |
class Banks(object): |
39 |
|
|
def __init__(self, config, debug): |
40 |
|
|
self.banks = {} |
41 |
|
|
self.config = config |
42 |
|
|
self.debug = debug |
43 |
douglas |
1178 |
|
44 |
douglas |
1179 |
def bank(self, module): |
45 |
|
|
if module not in NOT_BANKS: |
46 |
|
|
return self.banks.setdefault(module, Bank(module, self.config, self.debug))() |
47 |
douglas |
1178 |
|
48 |
douglas |
1179 |
class Config(object): |
49 |
|
|
def __init__(self, config, module): |
50 |
|
|
self.config = config |
51 |
|
|
self.module = module |
52 |
douglas |
1178 |
|
53 |
douglas |
1179 |
def get(self, *args, **kwargs): |
54 |
|
|
return self.config.get(self.module, *args, **kwargs) |
55 |
douglas |
1178 |
|
56 |
douglas |
1179 |
def getboolean(self, *args, **kwargs): |
57 |
|
|
return self.config.getboolean(self.module, *args, **kwargs) |
58 |
douglas |
1178 |
|
59 |
douglas |
1179 |
def getfloat(self, *args, **kwargs): |
60 |
|
|
return self.config.getfloat(self.module, *args, **kwargs) |
61 |
douglas |
1178 |
|
62 |
douglas |
1179 |
def getint(self, *args, **kwargs): |
63 |
|
|
return self.config.getint(self.module, *args, **kwargs) |
64 |
douglas |
1178 |
|
65 |
douglas |
1179 |
def getlist(self, *args, **kwargs): |
66 |
|
|
return self.config.getlist(self.module, *args, **kwargs) |
67 |
douglas |
1178 |
|
68 |
douglas |
1229 |
def getpassword(self): |
69 |
|
|
return self.config.getpassword(self.module) |
70 |
douglas |
1178 |
|
71 |
douglas |
1229 |
def getusername(self): |
72 |
|
|
return self.config.getusername(self.module) |
73 |
|
|
|
74 |
douglas |
1179 |
class ConfigParser(SafeConfigParser): |
75 |
|
|
def __init__(self, *args, **kwargs): |
76 |
|
|
SafeConfigParser.__init__(self, *args, **kwargs) |
77 |
douglas |
1178 |
|
78 |
douglas |
1179 |
def getlist(self, *args, **kwargs): |
79 |
|
|
return self.get(*args, **kwargs).split(',') |
80 |
douglas |
1178 |
|
81 |
douglas |
1229 |
def getpassword(self, section): |
82 |
|
|
return keyring.get_password('reminder_%s' % section, self.getusername(section)) |
83 |
douglas |
1178 |
|
84 |
douglas |
1229 |
def getusername(self, section): |
85 |
|
|
return self.get(section, 'username') |
86 |
|
|
|
87 |
douglas |
1179 |
class OptionParser(optparse.OptionParser): |
88 |
|
|
def __init__(self, *args, **kwargs): |
89 |
|
|
optparse.OptionParser.__init__(self, *args, **kwargs) |
90 |
|
|
self.add_option('-A', '--all', action = 'store_true', dest = 'all') |
91 |
douglas |
1229 |
self.add_option('-d', '--dbus', action = 'store_true', dest = 'dbus') |
92 |
douglas |
1179 |
self.add_option('-m', '--module', action = 'callback', callback = self.__module, dest = 'modules', type = 'string') |
93 |
douglas |
1229 |
self.add_option('-p', '--password', action = 'callback', callback = self.__password, dest = 'passwords', type = 'string') |
94 |
douglas |
1179 |
self.add_option('-D', '--debug', action = 'store_true', dest = 'debug') |
95 |
douglas |
1178 |
|
96 |
douglas |
1179 |
def __module(self, option, opt_str, value, parser): |
97 |
|
|
if value not in MODULES: |
98 |
|
|
raise optparse.OptionValueError, '%s unknown module %s' % (opt_str, value) |
99 |
douglas |
1178 |
|
100 |
douglas |
1179 |
parser.values.ensure_value(option.dest, []).append(value) |
101 |
douglas |
1178 |
|
102 |
douglas |
1229 |
def __password(self, option, opt_str, value, parser): |
103 |
|
|
parser.values.ensure_value(option.dest, []).append(value) |
104 |
|
|
|
105 |
douglas |
1179 |
if __name__ == '__main__': |
106 |
|
|
parser = OptionParser() |
107 |
|
|
options = parser.parse_args()[0] |
108 |
douglas |
1229 |
config = ConfigParser() |
109 |
douglas |
1178 |
|
110 |
douglas |
1229 |
config.read([os.path.expanduser('~/.reminder')]) |
111 |
|
|
|
112 |
|
|
if options.passwords: |
113 |
|
|
for section in options.passwords: |
114 |
|
|
try: |
115 |
|
|
username = config.getusername(section) |
116 |
|
|
password = getpass.getpass('%s password for %s: ' % (section, username)) |
117 |
|
|
|
118 |
|
|
keyring.set_password('reminder_%s' % section, username, password) |
119 |
|
|
except NoOptionError: |
120 |
|
|
pass |
121 |
|
|
|
122 |
|
|
sys.exit(0) |
123 |
|
|
|
124 |
|
|
if options.dbus: |
125 |
|
|
dbus = os.environ['DBUS_SESSION_BUS_ADDRESS'] |
126 |
|
|
|
127 |
|
|
with open(os.path.expanduser('~/.reminder.dbus'), 'wb') as file: |
128 |
|
|
file.write(dbus + '\n') |
129 |
|
|
|
130 |
|
|
sys.exit(0) |
131 |
|
|
|
132 |
|
|
try: |
133 |
|
|
os.environ['DBUS_SESSION_BUS_ADDRESS'] |
134 |
|
|
except KeyError: |
135 |
|
|
with open(os.path.expanduser('~/.reminder.dbus'), 'rb') as file: |
136 |
|
|
os.environ['DBUS_SESSION_BUS_ADDRESS'] = file.readline().rstrip() |
137 |
|
|
|
138 |
douglas |
1179 |
if not options.all and not options.modules: |
139 |
|
|
parser.error('-A or -m not specified') |
140 |
|
|
|
141 |
|
|
calendar = gdata.calendar.service.CalendarService() |
142 |
douglas |
1229 |
calendar.email = config.getusername('google') |
143 |
|
|
calendar.password = config.getpassword('google') |
144 |
douglas |
1179 |
calendar.source = 'Reminder-0.9' |
145 |
|
|
|
146 |
|
|
calendar.ProgrammaticLogin() |
147 |
|
|
|
148 |
|
|
banks = Banks(config, options.debug) |
149 |
|
|
|
150 |
|
|
for module in options.modules if not options.all else sorted(MODULES): |
151 |
|
|
exec 'import %s' % module |
152 |
|
|
exec '%s.main(%s)' % (module, ', '.join(['calendar', 'Config(config, module)'] + (['banks', 'options.debug'] if module in ('creditcards', 'wesabe') else ['options.debug']))) |