73 |
|
def __password(self, option, opt_str, value, parser): |
74 |
|
parser.values.ensure_value(option.dest, []).append(value) |
75 |
|
|
76 |
+ |
class OnDemand(object): |
77 |
+ |
def __init__(self, constructor, destructor = None): |
78 |
+ |
self.constructor = constructor |
79 |
+ |
self.destructor = destructor |
80 |
+ |
self.instance = None |
81 |
+ |
|
82 |
+ |
def __getattr__(self, name): |
83 |
+ |
if self.instance is None: |
84 |
+ |
self.instance = self.constructor() |
85 |
+ |
|
86 |
+ |
return getattr(self.instance, name) |
87 |
+ |
|
88 |
+ |
def __del__(self): |
89 |
+ |
if self.destructor is not None and self.instance is not None: |
90 |
+ |
self.destructor(self.instance) |
91 |
+ |
|
92 |
|
if __name__ == '__main__': |
93 |
|
parser = OptionParser() |
94 |
|
options = parser.parse_args()[0] |
125 |
|
if not options.all and not options.modules: |
126 |
|
parser.error('-A or -m not specified') |
127 |
|
|
128 |
< |
calendar = gdata.calendar.service.CalendarService() |
129 |
< |
calendar.email = config.getusername('google') |
130 |
< |
calendar.password = config.getpassword('google') |
131 |
< |
calendar.source = 'Reminder-0.9' |
128 |
> |
def calendar_constructor(): |
129 |
> |
calendar = gdata.calendar.service.CalendarService() |
130 |
> |
calendar.email = config.getusername('google') |
131 |
> |
calendar.password = config.getpassword('google') |
132 |
> |
calendar.source = 'Reminder-0.9' |
133 |
> |
|
134 |
> |
calendar.ProgrammaticLogin() |
135 |
> |
|
136 |
> |
return calendar |
137 |
|
|
138 |
< |
calendar.ProgrammaticLogin() |
138 |
> |
def imap_constructor(): |
139 |
> |
if options.debug: |
140 |
> |
imaplib.Debug = 4 |
141 |
|
|
142 |
< |
if options.debug: |
120 |
< |
imaplib.Debug = 4 |
142 |
> |
imap = imaplib.IMAP4_SSL(config.get('imap', 'server')) |
143 |
|
|
144 |
< |
imap = imaplib.IMAP4_SSL(config.get('imap', 'server')) |
144 |
> |
imap.login(config.getusername('imap'), config.getpassword('imap')) |
145 |
|
|
146 |
< |
imap.login(config.getusername('imap'), config.getpassword('imap')) |
146 |
> |
return imap |
147 |
> |
|
148 |
> |
calendar = OnDemand(calendar_constructor) |
149 |
> |
imap = OnDemand(imap_constructor, lambda imap: imap.logout()) |
150 |
|
|
151 |
|
for module in options.modules if not options.all else sorted(MODULES): |
152 |
|
exec 'import %s' % module |
153 |
|
exec '%s.main(calendar, imap, Config(config, module), options.debug)' % module |
129 |
– |
|
130 |
– |
imap.logout() |