1 |
#!/usr/local/bin/python |
2 |
# Reminder |
3 |
# |
4 |
# Douglas Thrift |
5 |
# |
6 |
# $Id$ |
7 |
|
8 |
from ConfigParser import NoOptionError, SafeConfigParser |
9 |
import getpass |
10 |
import keyring |
11 |
import optparse |
12 |
import os |
13 |
import sys |
14 |
import warnings |
15 |
|
16 |
with warnings.catch_warnings(): |
17 |
warnings.filterwarnings('ignore', r'the sha module is deprecated', DeprecationWarning) |
18 |
|
19 |
import gdata.calendar.service |
20 |
|
21 |
MODULES = frozenset(('creditcards', 'facebook', 'wesabe')) |
22 |
NOT_BANKS = MODULES | frozenset(('google',)) |
23 |
|
24 |
class Bank(object): |
25 |
def __init__(self, module, config, debug): |
26 |
self.module = module |
27 |
self.bank = None |
28 |
self.config = config |
29 |
self.debug = debug |
30 |
|
31 |
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 |
|
36 |
return self.bank |
37 |
|
38 |
class Banks(object): |
39 |
def __init__(self, config, debug): |
40 |
self.banks = {} |
41 |
self.config = config |
42 |
self.debug = debug |
43 |
|
44 |
def bank(self, module): |
45 |
if module not in NOT_BANKS: |
46 |
return self.banks.setdefault(module, Bank(module, self.config, self.debug))() |
47 |
|
48 |
class Config(object): |
49 |
def __init__(self, config, module): |
50 |
self.config = config |
51 |
self.module = module |
52 |
|
53 |
def get(self, *args, **kwargs): |
54 |
return self.config.get(self.module, *args, **kwargs) |
55 |
|
56 |
def getboolean(self, *args, **kwargs): |
57 |
return self.config.getboolean(self.module, *args, **kwargs) |
58 |
|
59 |
def getfloat(self, *args, **kwargs): |
60 |
return self.config.getfloat(self.module, *args, **kwargs) |
61 |
|
62 |
def getint(self, *args, **kwargs): |
63 |
return self.config.getint(self.module, *args, **kwargs) |
64 |
|
65 |
def getlist(self, *args, **kwargs): |
66 |
return self.config.getlist(self.module, *args, **kwargs) |
67 |
|
68 |
def getpassword(self): |
69 |
return self.config.getpassword(self.module) |
70 |
|
71 |
def getusername(self): |
72 |
return self.config.getusername(self.module) |
73 |
|
74 |
class ConfigParser(SafeConfigParser): |
75 |
def __init__(self, *args, **kwargs): |
76 |
SafeConfigParser.__init__(self, *args, **kwargs) |
77 |
|
78 |
def getlist(self, *args, **kwargs): |
79 |
return self.get(*args, **kwargs).split(',') |
80 |
|
81 |
def getpassword(self, section): |
82 |
return keyring.get_password('reminder_%s' % section, self.getusername(section)) |
83 |
|
84 |
def getusername(self, section): |
85 |
return self.get(section, 'username') |
86 |
|
87 |
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 |
self.add_option('-d', '--dbus', action = 'store_true', dest = 'dbus') |
92 |
self.add_option('-m', '--module', action = 'callback', callback = self.__module, dest = 'modules', type = 'string') |
93 |
self.add_option('-p', '--password', action = 'callback', callback = self.__password, dest = 'passwords', type = 'string') |
94 |
self.add_option('-D', '--debug', action = 'store_true', dest = 'debug') |
95 |
|
96 |
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 |
|
100 |
parser.values.ensure_value(option.dest, []).append(value) |
101 |
|
102 |
def __password(self, option, opt_str, value, parser): |
103 |
parser.values.ensure_value(option.dest, []).append(value) |
104 |
|
105 |
if __name__ == '__main__': |
106 |
parser = OptionParser() |
107 |
options = parser.parse_args()[0] |
108 |
config = ConfigParser() |
109 |
|
110 |
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 |
if not options.all and not options.modules: |
139 |
parser.error('-A or -m not specified') |
140 |
|
141 |
calendar = gdata.calendar.service.CalendarService() |
142 |
calendar.email = config.getusername('google') |
143 |
calendar.password = config.getpassword('google') |
144 |
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']))) |