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

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 douglas 1146 if options.banks:
58     for bank in banks.iterkeys():
59     accounts = frozenset(options.banks[bank])
60 douglas 1145
61 douglas 1146 if accounts:
62     all_accounts = frozenset(banks[bank])
63 douglas 1145
64 douglas 1146 if accounts <= all_accounts:
65     banks[bank] = accounts
66     else:
67     parser.error('not account(s): %s' % ', '.join(accounts - all_accounts))
68 douglas 1145
69     if options.list:
70     for bank, accounts in banks.iteritems():
71     print bank
72    
73     for account in accounts:
74     print ' ' + account
75    
76     sys.exit(0)
77    
78 douglas 1131 service = gdata.calendar.service.CalendarService()
79    
80 douglas 1145 service.ClientLogin(config.get('google', 'username'), decode(config.get('google', 'password')))
81 douglas 1131
82 douglas 1145 for bank, accounts in banks.iteritems():
83     exec 'import %s' % bank
84     exec "bank = %s.Bank(config.get(bank, 'username'), decode(config.get(bank, 'password')), options.debug)" % bank
85 douglas 1131
86 douglas 1145 for account in accounts:
87 douglas 1131 name, account = account.rsplit(' ', 1)
88 douglas 1145 due = bank.due(account)
89 douglas 1131 today = date.today()
90    
91     if due <= today:
92     continue
93    
94     title = '%s Due' % name
95     query = gdata.calendar.service.CalendarEventQuery('default', 'private', 'full', '"%s"' % title)
96     query.start_min = str(today)
97     events = service.CalendarQuery(query).entry
98    
99     if events == []:
100     event = gdata.calendar.CalendarEventEntry()
101     event.title = atom.Title(text = title)
102     event.transparency = gdata.calendar.Transparency()
103     event.transparency.value = 'TRANSPARENT'
104     event.visibility = gdata.calendar.Visibility()
105     event.visibility.value = 'PRIVATE'
106    
107     event.when.append(gdata.calendar.When(start_time = str(due), end_time = str(due + timedelta(1))))
108     event.when[0].reminder.append(gdata.calendar.Reminder(minutes = 10))
109    
110     service.InsertEvent(event, '/calendar/feeds/default/private/full')
111     else:
112     for index, event in enumerate(filter(lambda event: event.title.text == title, events)):
113     if index == 0:
114     event.transparency.value = 'TRANSPARENT'
115     event.visibility.value = 'PRIVATE'
116     event.when[0].start_time = str(due)
117     event.when[0].end_time = str(due + timedelta(1))
118    
119     if event.when[0].reminder != []:
120     event.when[0].reminder[0].minutes = str(10)
121     else:
122     event.when[0].reminder.append(gdata.calendar.Reminder(minutes = 10))
123    
124     service.UpdateEvent(event.GetEditLink().href, event)
125     else:
126     service.DeleteEvent(event.GetEditLink().href)

Properties

Name Value
svn:executable *
svn:keywords Id