ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/FreeBSDAdmin/Reminder/reminder.py
Revision: 1179
Committed: 2009-05-19T16:53:29-07:00 (16 years, 1 month ago) by douglas
Content type: text/x-python
File size: 3459 byte(s)
Log Message:
Working somewhat again.

File Contents

# Content
1 #!/usr/local/bin/python
2 # Reminder
3 #
4 # Douglas Thrift
5 #
6 # $Id$
7
8 import base64
9 from ConfigParser import SafeConfigParser
10 import optparse
11 import os.path
12 import warnings
13
14 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))()
45
46 class Config(object):
47 def __init__(self, config, module):
48 self.config = config
49 self.module = module
50
51 def get(self, *args, **kwargs):
52 return self.config.get(self.module, *args, **kwargs)
53
54 def getboolean(self, *args, **kwargs):
55 return self.config.getboolean(self.module, *args, **kwargs)
56
57 def getfloat(self, *args, **kwargs):
58 return self.config.getfloat(self.module, *args, **kwargs)
59
60 def getint(self, *args, **kwargs):
61 return self.config.getint(self.module, *args, **kwargs)
62
63 def getlist(self, *args, **kwargs):
64 return self.config.getlist(self.module, *args, **kwargs)
65
66 def getpassword(self, *args, **kwargs):
67 return self.config.getpassword(self.module, *args, **kwargs)
68
69 class ConfigParser(SafeConfigParser):
70 def __init__(self, *args, **kwargs):
71 SafeConfigParser.__init__(self, *args, **kwargs)
72
73 def getlist(self, *args, **kwargs):
74 return self.get(*args, **kwargs).split(',')
75
76 def getpassword(self, *args, **kwargs):
77 return base64.b64decode(self.get(*args, **kwargs).decode('rot13'))
78
79 class OptionParser(optparse.OptionParser):
80 def __init__(self, *args, **kwargs):
81 optparse.OptionParser.__init__(self, *args, **kwargs)
82 self.add_option('-A', '--all', action = 'store_true', dest = 'all')
83 self.add_option('-m', '--module', action = 'callback', callback = self.__module, dest = 'modules', type = 'string')
84 self.add_option('-D', '--debug', action = 'store_true', dest = 'debug')
85
86 def __module(self, option, opt_str, value, parser):
87 if value not in MODULES:
88 raise optparse.OptionValueError, '%s unknown module %s' % (opt_str, value)
89
90 parser.values.ensure_value(option.dest, []).append(value)
91
92 if __name__ == '__main__':
93 parser = OptionParser()
94 options = parser.parse_args()[0]
95
96 if not options.all and not options.modules:
97 parser.error('-A or -m not specified')
98
99 config = ConfigParser()
100
101 config.read([os.path.expanduser('~/.reminder')])
102
103 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'
107
108 calendar.ProgrammaticLogin()
109
110 banks = Banks(config, options.debug)
111
112 for module in options.modules if not options.all else sorted(MODULES):
113 exec 'import %s' % module
114 exec '%s.main(%s)' % (module, ', '.join(['calendar', 'Config(config, module)'] + (['banks', 'options.debug'] if module in ('creditcards', 'wesabe') else ['options.debug'])))

Properties

Name Value
svn:executable *
svn:keywords Id