1 |
< |
# Common |
1 |
> |
#!/usr/local/bin/python |
2 |
> |
# Reminder |
3 |
|
# |
4 |
|
# Douglas Thrift |
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 |
14 |
> |
import os |
15 |
|
import sys |
16 |
|
|
17 |
< |
FORBIDDEN = ('common', 'creditcard', 'google', 'wesabe') |
17 |
> |
MODULES = frozenset(('creditcards', 'facebook')) |
18 |
|
|
19 |
< |
class Bank(object): |
20 |
< |
def download(self, account): |
21 |
< |
raise NotImplementedError |
19 |
> |
class Config(object): |
20 |
> |
def __init__(self, config, module): |
21 |
> |
self.config = config |
22 |
> |
self.module = module |
23 |
|
|
24 |
< |
def due(self, account): |
25 |
< |
raise NotImplementedError |
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('-b', '--bank', action = 'callback', callback = self.__bank, dest = 'banks', type = 'string') |
63 |
< |
self.add_option('-a', '--account', action = 'callback', callback = self.__account, dest = 'banks', type = 'string') |
64 |
< |
self.add_option('-l', '--list', action = 'store_true', dest = 'list') |
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') |
30 |
– |
|
31 |
– |
def __bank(self, option, opt_str, value, parser): |
32 |
– |
if value in FORBIDDEN: |
33 |
– |
raise optparse.OptionValueError, '%s must not be %s' % (opt_str, value) |
66 |
|
|
67 |
< |
self.bank = value |
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, {}).setdefault(value, []) |
71 |
> |
parser.values.ensure_value(option.dest, []).append(value) |
72 |
|
|
73 |
< |
def __account(self, option, opt_str, value, parser): |
74 |
< |
try: |
41 |
< |
getattr(parser.values, option.dest)[self.bank].append(value) |
42 |
< |
except NameError: |
43 |
< |
raise optparse.OptionValueError, '%s must be after -b/--bank' % opt_str |
73 |
> |
def __password(self, option, opt_str, value, parser): |
74 |
> |
parser.values.ensure_value(option.dest, []).append(value) |
75 |
|
|
76 |
< |
def config(parser, options, file): |
77 |
< |
if not options.all and not options.banks and not options.list: |
78 |
< |
parser.error('-A, -b, or -l not specified') |
76 |
> |
if __name__ == '__main__': |
77 |
> |
parser = OptionParser() |
78 |
> |
options = parser.parse_args()[0] |
79 |
> |
config = ConfigParser() |
80 |
|
|
81 |
< |
config = SafeConfigParser() |
81 |
> |
config.read([os.path.expanduser('~/.reminder')]) |
82 |
|
|
83 |
< |
config.read([os.path.expanduser('~/%s' % file)]) |
83 |
> |
if options.dbus: |
84 |
> |
dbus = os.environ['DBUS_SESSION_BUS_ADDRESS'] |
85 |
|
|
86 |
< |
banks = dict(map(lambda bank: (bank, config.get(bank, 'accounts').split(',')), (options.banks.iterkeys() if options.banks else filter(lambda section: section not in FORBIDDEN, config.sections())))) |
86 |
> |
with open(os.path.expanduser('~/.reminder.dbus'), 'wb') as file: |
87 |
> |
file.write(dbus + '\n') |
88 |
|
|
89 |
< |
if options.banks: |
56 |
< |
for bank in banks.iterkeys(): |
57 |
< |
accounts = frozenset(options.banks[bank]) |
89 |
> |
sys.exit(0) |
90 |
|
|
91 |
< |
if accounts: |
92 |
< |
all_accounts = frozenset(banks[bank]) |
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 |
< |
if accounts <= all_accounts: |
63 |
< |
banks[bank] = accounts |
64 |
< |
else: |
65 |
< |
parser.error('not account(s): %s' % ', '.join(accounts - all_accounts)) |
107 |
> |
sys.exit(0) |
108 |
|
|
109 |
< |
if options.list: |
110 |
< |
for bank, accounts in banks.iteritems(): |
69 |
< |
print bank |
109 |
> |
if not options.all and not options.modules: |
110 |
> |
parser.error('-A or -m not specified') |
111 |
|
|
112 |
< |
for account in accounts: |
113 |
< |
print ' ' + account |
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 |
< |
sys.exit(0) |
117 |
> |
calendar.ProgrammaticLogin() |
118 |
> |
|
119 |
> |
if options.debug: |
120 |
> |
imaplib.Debug = 4 |
121 |
|
|
122 |
< |
return config, banks |
122 |
> |
imap = imaplib.IMAP4_SSL(config.get('imap', 'server')) |
123 |
|
|
124 |
< |
def decode(string): |
79 |
< |
return base64.b64decode(string.decode('rot13')) |
124 |
> |
imap.login(config.getusername('imap'), config.getpassword('imap')) |
125 |
|
|
126 |
< |
def bank(bank, options, config): |
127 |
< |
exec 'import %s' % bank |
128 |
< |
exec "bank = %s.Bank(config.get(bank, 'username'), decode(config.get(bank, 'password')), options.debug)" % bank |
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 |
< |
return bank |
130 |
> |
imap.logout() |