ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/FreeBSDAdmin/Reminder/reminder.py
(Generate patch)

Comparing FreeBSDAdmin/Reminder/reminder.py (file contents):
Revision 1179 by douglas, 2009-05-19T16:53:29-07:00 vs.
Revision 1424 by douglas, 2011-05-03T00:02:31-07:00

# Line 5 | Line 5
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
15 < import warnings
14 > import os
15 > import sys
16  
17 < 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))()
17 > MODULES = frozenset(('creditcards', 'facebook'))
18  
19   class Config(object):
20          def __init__(self, config, module):
# Line 63 | Line 36 | class Config(object):
36          def getlist(self, *args, **kwargs):
37                  return self.config.getlist(self.module, *args, **kwargs)
38  
39 <        def getpassword(self, *args, **kwargs):
40 <                return self.config.getpassword(self.module, *args, **kwargs)
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):
# Line 73 | Line 49 | class ConfigParser(SafeConfigParser):
49          def getlist(self, *args, **kwargs):
50                  return self.get(*args, **kwargs).split(',')
51  
52 <        def getpassword(self, *args, **kwargs):
53 <                return base64.b64decode(self.get(*args, **kwargs).decode('rot13'))
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):
# Line 89 | Line 70 | class OptionParser(optparse.OptionParser
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]
95
96        if not options.all and not options.modules:
97                parser.error('-A or -m not specified')
98
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.get('google', 'username')
114 <        calendar.password = config.getpassword('google', 'password')
113 >        calendar.email = config.getusername('google')
114 >        calendar.password = config.getpassword('google')
115          calendar.source = 'Reminder-0.9'
116  
117          calendar.ProgrammaticLogin()
118  
119 <        banks = Banks(config, options.debug)
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(%s)' % (module, ', '.join(['calendar', 'Config(config, module)'] + (['banks', 'options.debug'] if module in ('creditcards', 'wesabe') else ['options.debug'])))
128 >                exec '%s.main(calendar, imap, Config(config, module), options.debug)' % module
129 >
130 >        imap.logout()

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines