1 |
#!/usr/local/bin/python |
2 |
# Reminder |
3 |
# |
4 |
# Douglas Thrift |
5 |
# |
6 |
# $Id$ |
7 |
|
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 |
15 |
import sys |
16 |
|
17 |
MODULES = frozenset(('creditcards', 'facebook')) |
18 |
|
19 |
class Config(object): |
20 |
def __init__(self, config, module): |
21 |
self.config = config |
22 |
self.module = module |
23 |
|
24 |
def get(self, *args, **kwargs): |
25 |
return self.config.get(self.module, *args, **kwargs) |
26 |
|
27 |
def getboolean(self, *args, **kwargs): |
28 |
return self.config.getboolean(self.module, *args, **kwargs) |
29 |
|
30 |
def getfloat(self, *args, **kwargs): |
31 |
return self.config.getfloat(self.module, *args, **kwargs) |
32 |
|
33 |
def getint(self, *args, **kwargs): |
34 |
return self.config.getint(self.module, *args, **kwargs) |
35 |
|
36 |
def getlist(self, *args, **kwargs): |
37 |
return self.config.getlist(self.module, *args, **kwargs) |
38 |
|
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): |
47 |
SafeConfigParser.__init__(self, *args, **kwargs) |
48 |
|
49 |
def getlist(self, *args, **kwargs): |
50 |
return self.get(*args, **kwargs).split(',') |
51 |
|
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): |
68 |
if value not in MODULES: |
69 |
raise optparse.OptionValueError, '%s unknown module %s' % (opt_str, value) |
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 |
if __name__ == '__main__': |
77 |
parser = OptionParser() |
78 |
options = parser.parse_args()[0] |
79 |
config = ConfigParser() |
80 |
|
81 |
config.read([os.path.expanduser('~/.reminder')]) |
82 |
|
83 |
if options.dbus: |
84 |
dbus = os.environ['DBUS_SESSION_BUS_ADDRESS'] |
85 |
|
86 |
with open(os.path.expanduser('~/.reminder.dbus'), 'wb') as file: |
87 |
file.write(dbus + '\n') |
88 |
|
89 |
sys.exit(0) |
90 |
|
91 |
try: |
92 |
os.environ['DBUS_SESSION_BUS_ADDRESS'] |
93 |
except KeyError: |
94 |
with open(os.path.expanduser('~/.reminder.dbus'), 'rb') as file: |
95 |
os.environ['DBUS_SESSION_BUS_ADDRESS'] = file.readline().rstrip() |
96 |
|
97 |
if options.passwords: |
98 |
for section in options.passwords: |
99 |
try: |
100 |
username = config.getusername(section) |
101 |
password = getpass.getpass('%s password for %s: ' % (section, username)) |
102 |
|
103 |
keyring.set_password('reminder_%s' % section, username, password) |
104 |
except NoOptionError: |
105 |
pass |
106 |
|
107 |
sys.exit(0) |
108 |
|
109 |
if not options.all and not options.modules: |
110 |
parser.error('-A or -m not specified') |
111 |
|
112 |
calendar = gdata.calendar.service.CalendarService() |
113 |
calendar.email = config.getusername('google') |
114 |
calendar.password = config.getpassword('google') |
115 |
calendar.source = 'Reminder-0.9' |
116 |
|
117 |
calendar.ProgrammaticLogin() |
118 |
|
119 |
if options.debug: |
120 |
imaplib.Debug = 4 |
121 |
|
122 |
imap = imaplib.IMAP4_SSL(config.get('imap', 'server')) |
123 |
|
124 |
imap.login(config.getusername('imap'), config.getpassword('imap')) |
125 |
|
126 |
for module in options.modules if not options.all else sorted(MODULES): |
127 |
exec 'import %s' % module |
128 |
exec '%s.main(calendar, imap, Config(config, module), options.debug)' % module |
129 |
|
130 |
imap.logout() |