5 |
|
# |
6 |
|
# $Id$ |
7 |
|
|
8 |
< |
import base64 |
9 |
< |
from ConfigParser import SafeConfigParser |
8 |
> |
from ConfigParser import NoOptionError, SafeConfigParser |
9 |
> |
import gdata.calendar.service |
10 |
> |
import getpass |
11 |
> |
import imaplib |
12 |
> |
import keyring |
13 |
|
import optparse |
14 |
< |
import os.path |
15 |
< |
import warnings |
14 |
> |
import os |
15 |
> |
import sys |
16 |
|
|
17 |
< |
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 __init__(self, module, config, debug): |
24 |
< |
self.module = module |
25 |
< |
self.bank = None |
26 |
< |
self.config = config |
27 |
< |
self.debug = debug |
28 |
< |
|
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 |
< |
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))() |
17 |
> |
MODULES = frozenset(('creditcards', 'facebook')) |
18 |
|
|
19 |
|
class Config(object): |
20 |
|
def __init__(self, config, module): |
36 |
|
def getlist(self, *args, **kwargs): |
37 |
|
return self.config.getlist(self.module, *args, **kwargs) |
38 |
|
|
39 |
< |
def getpassword(self, *args, **kwargs): |
40 |
< |
return self.config.getpassword(self.module, *args, **kwargs) |
39 |
> |
def getpassword(self): |
40 |
> |
return self.config.getpassword(self.module) |
41 |
> |
|
42 |
> |
def getusername(self): |
43 |
> |
return self.config.getusername(self.module) |
44 |
|
|
45 |
|
class ConfigParser(SafeConfigParser): |
46 |
|
def __init__(self, *args, **kwargs): |
49 |
|
def getlist(self, *args, **kwargs): |
50 |
|
return self.get(*args, **kwargs).split(',') |
51 |
|
|
52 |
< |
def getpassword(self, *args, **kwargs): |
53 |
< |
return base64.b64decode(self.get(*args, **kwargs).decode('rot13')) |
52 |
> |
def getpassword(self, section): |
53 |
> |
return keyring.get_password('reminder_%s' % section, self.getusername(section)) |
54 |
> |
|
55 |
> |
def getusername(self, section): |
56 |
> |
return self.get(section, 'username') |
57 |
|
|
58 |
|
class OptionParser(optparse.OptionParser): |
59 |
|
def __init__(self, *args, **kwargs): |
60 |
|
optparse.OptionParser.__init__(self, *args, **kwargs) |
61 |
|
self.add_option('-A', '--all', action = 'store_true', dest = 'all') |
62 |
+ |
self.add_option('-d', '--dbus', action = 'store_true', dest = 'dbus') |
63 |
|
self.add_option('-m', '--module', action = 'callback', callback = self.__module, dest = 'modules', type = 'string') |
64 |
+ |
self.add_option('-p', '--password', action = 'callback', callback = self.__password, dest = 'passwords', type = 'string') |
65 |
|
self.add_option('-D', '--debug', action = 'store_true', dest = 'debug') |
66 |
|
|
67 |
|
def __module(self, option, opt_str, value, parser): |
70 |
|
|
71 |
|
parser.values.ensure_value(option.dest, []).append(value) |
72 |
|
|
73 |
+ |
def __password(self, option, opt_str, value, parser): |
74 |
+ |
parser.values.ensure_value(option.dest, []).append(value) |
75 |
+ |
|
76 |
+ |
class OnDemand(object): |
77 |
+ |
def __init__(self, constructor, destructor = None): |
78 |
+ |
self.constructor = constructor |
79 |
+ |
self.destructor = destructor |
80 |
+ |
self.instance = None |
81 |
+ |
|
82 |
+ |
def __getattr__(self, name): |
83 |
+ |
if self.instance is None: |
84 |
+ |
self.instance = self.constructor() |
85 |
+ |
|
86 |
+ |
return getattr(self.instance, name) |
87 |
+ |
|
88 |
+ |
def __del__(self): |
89 |
+ |
if self.destructor is not None and self.instance is not None: |
90 |
+ |
self.destructor(self.instance) |
91 |
+ |
|
92 |
|
if __name__ == '__main__': |
93 |
|
parser = OptionParser() |
94 |
|
options = parser.parse_args()[0] |
95 |
+ |
config = ConfigParser() |
96 |
+ |
|
97 |
+ |
config.read([os.path.expanduser('~/.reminder')]) |
98 |
+ |
|
99 |
+ |
if options.dbus: |
100 |
+ |
dbus = os.environ['DBUS_SESSION_BUS_ADDRESS'] |
101 |
+ |
|
102 |
+ |
with open(os.path.expanduser('~/.reminder.dbus'), 'wb') as file: |
103 |
+ |
file.write(dbus + '\n') |
104 |
+ |
|
105 |
+ |
sys.exit(0) |
106 |
+ |
|
107 |
+ |
try: |
108 |
+ |
os.environ['DBUS_SESSION_BUS_ADDRESS'] |
109 |
+ |
except KeyError: |
110 |
+ |
with open(os.path.expanduser('~/.reminder.dbus'), 'rb') as file: |
111 |
+ |
os.environ['DBUS_SESSION_BUS_ADDRESS'] = file.readline().rstrip() |
112 |
+ |
|
113 |
+ |
if options.passwords: |
114 |
+ |
for section in options.passwords: |
115 |
+ |
try: |
116 |
+ |
username = config.getusername(section) |
117 |
+ |
password = getpass.getpass('%s password for %s: ' % (section, username)) |
118 |
+ |
|
119 |
+ |
keyring.set_password('reminder_%s' % section, username, password) |
120 |
+ |
except NoOptionError: |
121 |
+ |
pass |
122 |
+ |
|
123 |
+ |
sys.exit(0) |
124 |
|
|
125 |
|
if not options.all and not options.modules: |
126 |
|
parser.error('-A or -m not specified') |
127 |
|
|
128 |
< |
config = ConfigParser() |
128 |
> |
def calendar_constructor(): |
129 |
> |
calendar = gdata.calendar.service.CalendarService() |
130 |
> |
calendar.email = config.getusername('google') |
131 |
> |
calendar.password = config.getpassword('google') |
132 |
> |
calendar.source = 'Reminder-0.9' |
133 |
|
|
134 |
< |
config.read([os.path.expanduser('~/.reminder')]) |
134 |
> |
calendar.ProgrammaticLogin() |
135 |
> |
|
136 |
> |
return calendar |
137 |
> |
|
138 |
> |
def imap_constructor(): |
139 |
> |
if options.debug: |
140 |
> |
imaplib.Debug = 4 |
141 |
> |
|
142 |
> |
imap = imaplib.IMAP4_SSL(config.get('imap', 'server')) |
143 |
|
|
144 |
< |
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' |
144 |
> |
imap.login(config.getusername('imap'), config.getpassword('imap')) |
145 |
|
|
146 |
< |
calendar.ProgrammaticLogin() |
146 |
> |
return imap |
147 |
|
|
148 |
< |
banks = Banks(config, options.debug) |
148 |
> |
calendar = OnDemand(calendar_constructor) |
149 |
> |
imap = OnDemand(imap_constructor, lambda imap: imap.logout()) |
150 |
|
|
151 |
|
for module in options.modules if not options.all else sorted(MODULES): |
152 |
|
exec 'import %s' % module |
153 |
< |
exec '%s.main(%s)' % (module, ', '.join(['calendar', 'Config(config, module)'] + (['banks', 'options.debug'] if module in ('creditcards', 'wesabe') else ['options.debug']))) |
153 |
> |
exec '%s.main(calendar, imap, Config(config, module), options.debug)' % module |