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 |
douglas |
1424 |
import gdata.calendar.service |
10 |
douglas |
1229 |
import getpass |
11 |
douglas |
1424 |
import imaplib |
12 |
douglas |
1229 |
import keyring |
13 |
douglas |
1178 |
import optparse |
14 |
douglas |
1229 |
import os |
15 |
|
|
import sys |
16 |
douglas |
1178 |
|
17 |
douglas |
1424 |
MODULES = frozenset(('creditcards', 'facebook')) |
18 |
douglas |
1178 |
|
19 |
douglas |
1179 |
class Config(object): |
20 |
|
|
def __init__(self, config, module): |
21 |
|
|
self.config = config |
22 |
|
|
self.module = module |
23 |
douglas |
1178 |
|
24 |
douglas |
1179 |
def get(self, *args, **kwargs): |
25 |
|
|
return self.config.get(self.module, *args, **kwargs) |
26 |
douglas |
1178 |
|
27 |
douglas |
1179 |
def getboolean(self, *args, **kwargs): |
28 |
|
|
return self.config.getboolean(self.module, *args, **kwargs) |
29 |
douglas |
1178 |
|
30 |
douglas |
1179 |
def getfloat(self, *args, **kwargs): |
31 |
|
|
return self.config.getfloat(self.module, *args, **kwargs) |
32 |
douglas |
1178 |
|
33 |
douglas |
1179 |
def getint(self, *args, **kwargs): |
34 |
|
|
return self.config.getint(self.module, *args, **kwargs) |
35 |
douglas |
1178 |
|
36 |
douglas |
1179 |
def getlist(self, *args, **kwargs): |
37 |
|
|
return self.config.getlist(self.module, *args, **kwargs) |
38 |
douglas |
1178 |
|
39 |
douglas |
1229 |
def getpassword(self): |
40 |
|
|
return self.config.getpassword(self.module) |
41 |
douglas |
1178 |
|
42 |
douglas |
1229 |
def getusername(self): |
43 |
|
|
return self.config.getusername(self.module) |
44 |
|
|
|
45 |
douglas |
1179 |
class ConfigParser(SafeConfigParser): |
46 |
|
|
def __init__(self, *args, **kwargs): |
47 |
|
|
SafeConfigParser.__init__(self, *args, **kwargs) |
48 |
douglas |
1178 |
|
49 |
douglas |
1179 |
def getlist(self, *args, **kwargs): |
50 |
|
|
return self.get(*args, **kwargs).split(',') |
51 |
douglas |
1178 |
|
52 |
douglas |
1229 |
def getpassword(self, section): |
53 |
|
|
return keyring.get_password('reminder_%s' % section, self.getusername(section)) |
54 |
douglas |
1178 |
|
55 |
douglas |
1229 |
def getusername(self, section): |
56 |
|
|
return self.get(section, 'username') |
57 |
|
|
|
58 |
douglas |
1179 |
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 |
douglas |
1229 |
self.add_option('-d', '--dbus', action = 'store_true', dest = 'dbus') |
63 |
douglas |
1179 |
self.add_option('-m', '--module', action = 'callback', callback = self.__module, dest = 'modules', type = 'string') |
64 |
douglas |
1229 |
self.add_option('-p', '--password', action = 'callback', callback = self.__password, dest = 'passwords', type = 'string') |
65 |
douglas |
1179 |
self.add_option('-D', '--debug', action = 'store_true', dest = 'debug') |
66 |
douglas |
1178 |
|
67 |
douglas |
1179 |
def __module(self, option, opt_str, value, parser): |
68 |
|
|
if value not in MODULES: |
69 |
|
|
raise optparse.OptionValueError, '%s unknown module %s' % (opt_str, value) |
70 |
douglas |
1178 |
|
71 |
douglas |
1179 |
parser.values.ensure_value(option.dest, []).append(value) |
72 |
douglas |
1178 |
|
73 |
douglas |
1229 |
def __password(self, option, opt_str, value, parser): |
74 |
|
|
parser.values.ensure_value(option.dest, []).append(value) |
75 |
|
|
|
76 |
douglas |
1425 |
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 |
douglas |
1179 |
if __name__ == '__main__': |
93 |
|
|
parser = OptionParser() |
94 |
|
|
options = parser.parse_args()[0] |
95 |
douglas |
1229 |
config = ConfigParser() |
96 |
douglas |
1178 |
|
97 |
douglas |
1229 |
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 |
douglas |
1424 |
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 |
douglas |
1179 |
if not options.all and not options.modules: |
126 |
|
|
parser.error('-A or -m not specified') |
127 |
|
|
|
128 |
douglas |
1425 |
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 |
douglas |
1179 |
|
134 |
douglas |
1425 |
calendar.ProgrammaticLogin() |
135 |
douglas |
1179 |
|
136 |
douglas |
1425 |
return calendar |
137 |
douglas |
1179 |
|
138 |
douglas |
1425 |
def imap_constructor(): |
139 |
|
|
if options.debug: |
140 |
|
|
imaplib.Debug = 4 |
141 |
douglas |
1424 |
|
142 |
douglas |
1425 |
imap = imaplib.IMAP4_SSL(config.get('imap', 'server')) |
143 |
douglas |
1424 |
|
144 |
douglas |
1425 |
imap.login(config.getusername('imap'), config.getpassword('imap')) |
145 |
|
|
|
146 |
|
|
return imap |
147 |
|
|
|
148 |
|
|
calendar = OnDemand(calendar_constructor) |
149 |
|
|
imap = OnDemand(imap_constructor, lambda imap: imap.logout()) |
150 |
|
|
|
151 |
douglas |
1179 |
for module in options.modules if not options.all else sorted(MODULES): |
152 |
|
|
exec 'import %s' % module |
153 |
douglas |
1424 |
exec '%s.main(calendar, imap, Config(config, module), options.debug)' % module |