ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/FreeBSDAdmin/Reminder/creditcards.py
Revision: 1145
Committed: 2009-03-14T22:48:45-07:00 (16 years, 3 months ago) by douglas
Content type: text/x-python
Original Path: FreeBSDAdmin/Reminder/creditcard.py
File size: 3975 byte(s)
Log Message:
Improved handling!

File Contents

# User Rev Content
1 douglas 1131 #!/usr/local/bin/python
2     # Credit Card
3     #
4     # Douglas Thrift
5     #
6     # $Id$
7    
8     import atom
9     import base64
10     from ConfigParser import SafeConfigParser
11     from datetime import date, timedelta
12     import gdata.calendar
13     import gdata.calendar.service
14     import optparse
15     import os.path
16 douglas 1145 import sys
17 douglas 1131
18     def decode(string):
19     return base64.b64decode(string.decode('rot13'))
20    
21 douglas 1145 def _bank(option, opt_str, value, parser):
22     if value == 'google':
23     raise optparse.OptionValueError, '%s must not be google' % opt_str
24    
25     global bank
26    
27     bank = value
28    
29     parser.values.ensure_value(option.dest, {}).setdefault(bank, [])
30    
31     def _account(option, opt_str, value, parser):
32     try:
33     getattr(parser.values, option.dest)[bank].append(value)
34     except NameError:
35     raise optparse.OptionValueError, '%s must be after -b/--bank' % opt_str
36    
37 douglas 1131 if __name__ == '__main__':
38     parser = optparse.OptionParser()
39    
40 douglas 1145 parser.add_option('-A', '--all', action = 'store_true', dest = 'all')
41     parser.add_option('-b', '--bank', action = 'callback', callback = _bank, dest = 'banks', type = 'string')
42     parser.add_option('-a', '--account', action = 'callback', callback = _account, dest = 'banks', type = 'string')
43     parser.add_option('-l', '--list', action = 'store_true', dest = 'list')
44 douglas 1131 parser.add_option('-D', '--debug', action = 'store_true', dest = 'debug')
45    
46     options = parser.parse_args()[0]
47 douglas 1145
48     if not options.all and not options.banks and not options.list:
49     parser.error('-A, -b, or -l not specified')
50    
51     config = SafeConfigParser()
52    
53     config.read([os.path.expanduser('~/.creditcard')])
54    
55     banks = dict(map(lambda bank: (bank, config.get(bank, 'accounts').split(',')), (options.banks.iterkeys() if options.banks else filter(lambda section: section != 'google', config.sections()))))
56    
57     for bank in banks.iterkeys():
58     accounts = frozenset(options.banks[bank])
59    
60     if accounts:
61     all_accounts = frozenset(banks[bank])
62    
63     if accounts <= all_accounts:
64     banks[bank] = accounts
65     else:
66     parser.error('not account(s): %s' % ', '.join(accounts - all_accounts))
67    
68     if options.list:
69     for bank, accounts in banks.iteritems():
70     print bank
71    
72     for account in accounts:
73     print ' ' + account
74    
75     sys.exit(0)
76    
77 douglas 1131 service = gdata.calendar.service.CalendarService()
78    
79 douglas 1145 service.ClientLogin(config.get('google', 'username'), decode(config.get('google', 'password')))
80 douglas 1131
81 douglas 1145 for bank, accounts in banks.iteritems():
82     exec 'import %s' % bank
83     exec "bank = %s.Bank(config.get(bank, 'username'), decode(config.get(bank, 'password')), options.debug)" % bank
84 douglas 1131
85 douglas 1145 for account in accounts:
86 douglas 1131 name, account = account.rsplit(' ', 1)
87 douglas 1145 due = bank.due(account)
88 douglas 1131 today = date.today()
89    
90     if due <= today:
91     continue
92    
93     title = '%s Due' % name
94     query = gdata.calendar.service.CalendarEventQuery('default', 'private', 'full', '"%s"' % title)
95     query.start_min = str(today)
96     events = service.CalendarQuery(query).entry
97    
98     if events == []:
99     event = gdata.calendar.CalendarEventEntry()
100     event.title = atom.Title(text = title)
101     event.transparency = gdata.calendar.Transparency()
102     event.transparency.value = 'TRANSPARENT'
103     event.visibility = gdata.calendar.Visibility()
104     event.visibility.value = 'PRIVATE'
105    
106     event.when.append(gdata.calendar.When(start_time = str(due), end_time = str(due + timedelta(1))))
107     event.when[0].reminder.append(gdata.calendar.Reminder(minutes = 10))
108    
109     service.InsertEvent(event, '/calendar/feeds/default/private/full')
110     else:
111     for index, event in enumerate(filter(lambda event: event.title.text == title, events)):
112     if index == 0:
113     event.transparency.value = 'TRANSPARENT'
114     event.visibility.value = 'PRIVATE'
115     event.when[0].start_time = str(due)
116     event.when[0].end_time = str(due + timedelta(1))
117    
118     if event.when[0].reminder != []:
119     event.when[0].reminder[0].minutes = str(10)
120     else:
121     event.when[0].reminder.append(gdata.calendar.Reminder(minutes = 10))
122    
123     service.UpdateEvent(event.GetEditLink().href, event)
124     else:
125     service.DeleteEvent(event.GetEditLink().href)

Properties

Name Value
svn:executable *
svn:keywords Id