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 getpass |
10 |
> |
import keyring |
11 |
|
import optparse |
12 |
< |
import os.path |
12 |
> |
import os |
13 |
|
import sys |
14 |
+ |
import warnings |
15 |
|
|
16 |
< |
FORBIDDEN = ('common', 'creditcard', 'google', 'wesabe') |
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 download(self, account): |
26 |
< |
raise NotImplementedError |
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 due(self, account): |
32 |
< |
raise NotImplementedError |
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 |
< |
class OptionParser(optparse.OptionParser): |
37 |
< |
def __init__(self, *args, **kwargs): |
38 |
< |
optparse.OptionParser.__init__(self, *args, **kwargs) |
39 |
< |
self.add_option('-A', '--all', action = 'store_true', dest = 'all') |
40 |
< |
self.add_option('-b', '--bank', action = 'callback', callback = self.__bank, dest = 'banks', type = 'string') |
41 |
< |
self.add_option('-a', '--account', action = 'callback', callback = self.__account, dest = 'banks', type = 'string') |
42 |
< |
self.add_option('-l', '--list', action = 'store_true', dest = 'list') |
43 |
< |
self.add_option('-D', '--debug', action = 'store_true', dest = 'debug') |
44 |
< |
|
45 |
< |
def __bank(self, option, opt_str, value, parser): |
46 |
< |
if value in FORBIDDEN: |
33 |
< |
raise optparse.OptionValueError, '%s must not be %s' % (opt_str, value) |
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 |
< |
self.bank = value |
48 |
> |
class Config(object): |
49 |
> |
def __init__(self, config, module): |
50 |
> |
self.config = config |
51 |
> |
self.module = module |
52 |
|
|
53 |
< |
parser.values.ensure_value(option.dest, {}).setdefault(value, []) |
53 |
> |
def get(self, *args, **kwargs): |
54 |
> |
return self.config.get(self.module, *args, **kwargs) |
55 |
|
|
56 |
< |
def __account(self, option, opt_str, value, parser): |
57 |
< |
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 |
56 |
> |
def getboolean(self, *args, **kwargs): |
57 |
> |
return self.config.getboolean(self.module, *args, **kwargs) |
58 |
|
|
59 |
< |
def config(parser, options, file): |
60 |
< |
if not options.all and not options.banks and not options.list: |
47 |
< |
parser.error('-A, -b, or -l not specified') |
59 |
> |
def getfloat(self, *args, **kwargs): |
60 |
> |
return self.config.getfloat(self.module, *args, **kwargs) |
61 |
|
|
62 |
< |
config = SafeConfigParser() |
62 |
> |
def getint(self, *args, **kwargs): |
63 |
> |
return self.config.getint(self.module, *args, **kwargs) |
64 |
|
|
65 |
< |
config.read([os.path.expanduser('~/%s' % file)]) |
65 |
> |
def getlist(self, *args, **kwargs): |
66 |
> |
return self.config.getlist(self.module, *args, **kwargs) |
67 |
|
|
68 |
< |
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())))) |
68 |
> |
def getpassword(self): |
69 |
> |
return self.config.getpassword(self.module) |
70 |
|
|
71 |
< |
if options.banks: |
72 |
< |
for bank in banks.iterkeys(): |
57 |
< |
accounts = frozenset(options.banks[bank]) |
71 |
> |
def getusername(self): |
72 |
> |
return self.config.getusername(self.module) |
73 |
|
|
74 |
< |
if accounts: |
75 |
< |
all_accounts = frozenset(banks[bank]) |
74 |
> |
class ConfigParser(SafeConfigParser): |
75 |
> |
def __init__(self, *args, **kwargs): |
76 |
> |
SafeConfigParser.__init__(self, *args, **kwargs) |
77 |
|
|
78 |
< |
if accounts <= all_accounts: |
79 |
< |
banks[bank] = accounts |
64 |
< |
else: |
65 |
< |
parser.error('not account(s): %s' % ', '.join(accounts - all_accounts)) |
78 |
> |
def getlist(self, *args, **kwargs): |
79 |
> |
return self.get(*args, **kwargs).split(',') |
80 |
|
|
81 |
< |
if options.list: |
82 |
< |
for bank, accounts in banks.iteritems(): |
69 |
< |
print bank |
81 |
> |
def getpassword(self, section): |
82 |
> |
return keyring.get_password('reminder_%s' % section, self.getusername(section)) |
83 |
|
|
84 |
< |
for account in accounts: |
85 |
< |
print ' ' + account |
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 |
< |
return config, banks |
124 |
> |
if options.dbus: |
125 |
> |
dbus = os.environ['DBUS_SESSION_BUS_ADDRESS'] |
126 |
|
|
127 |
< |
def decode(string): |
128 |
< |
return base64.b64decode(string.decode('rot13')) |
127 |
> |
with open(os.path.expanduser('~/.reminder.dbus'), 'wb') as file: |
128 |
> |
file.write(dbus + '\n') |
129 |
|
|
130 |
< |
def bank(bank, options, config): |
82 |
< |
exec 'import %s' % bank |
83 |
< |
exec "bank = %s.Bank(config.get(bank, 'username'), decode(config.get(bank, 'password')), options.debug)" % bank |
130 |
> |
sys.exit(0) |
131 |
|
|
132 |
< |
return bank |
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']))) |