#!/usr/bin/env python
-# find_todo.py -- Find transactions marked "To Do" and update CalDAV.
+# find_todo.py -- Find transactions marked "To Do", "AP" and "DD" and update
+# CalDAV.
#
# Copyright (C) 2009, 2010 ParIT Worker Co-operative <transparency@parit.ca>
+# Copyright (C) 2011, Andrew Ruthven <andrew@etc.gen.nz>
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of
# Boston, MA 02110-1301, USA gnu@gnu.org
#
# @author Andrew Ruthven <andrew@etc.gen.nz>
-# based on account-anlysis.py by Mark Jenkins, ParIT Worker Co-operative
+# based on account-analysis.py by Mark Jenkins, ParIT Worker Co-operative
# <mark@parit.ca>
# python imports
import csv
import caldav
from caldav.elements import dav, cdav
-import yaml
+import getopt
# gnucash imports
from gnucash import Session, GncNumeric, Split
def main():
- (gnucash_file) = argv[1]
-
- account_path = argv[2:]
-
- # Principal url
- url = "https://xxx:xxx@xxx.etc.gen.nz/caldav.php/gnucash/home"
+ gnucash_file = None
+ username = None
+ password = None
+ url = None
+ identifier = '@gnucash'
+ optlist, accounts = getopt.getopt(argv[1:],'f:p:n:s:u:i:h',
+ ['file=','username=','password=','url=','identifier','help'])
+ for o, a in optlist:
+ if o in ( "-f", "--file"):
+ gnucash_file = a
+ if o in ("-n", "--username"):
+ username = a
+ if o in ("-p", "--password"):
+ password = a
+ if o in ("-u", "--url"):
+ url = a
+ if o in ("-i", "--identifier"):
+ identifier = '@' + a
+ if o in ("-h", "--help"):
+ usage()
+ exit()
+ if gnucash_file == None:
+ print "please specify a gnucash file or database to use"
+ exit(1)
+
+ if url == None:
+ print "Please specify a URL for the CalDAV calendar to use"
+ exit(1)
+
+ # Find the calender to use
client = caldav.DAVClient(url)
principal = caldav.Principal(client, url)
calendars = principal.calendars()
gnucash_session = Session(gnucash_file, is_new=False)
root_account = gnucash_session.book.get_root_account()
+ current_events = dict()
+
+ for account in accounts:
+ process_account(calendar, client, identifier, current_events, root_account, account)
+
+ # Go through the events on the server and remove anything that we've added
+ # previously that is now stale.
+ events = calendar.events()
+ for event in events:
+ event.load()
+ if not current_events.has_key(event.instance.vevent.uid.value):
+ if (re.search(identifier, event.instance.vevent.uid.value)):
+ print "Event", event, "deleted, %s" % event.instance.vevent.uid.value
+ event.delete()
+
+ # no save needed, we're just reading..
+ gnucash_session.end()
+
+def process_account(calendar, client, identifier, current_events, root_account, account_name):
+ account_path = account_name.split(':')
+
account_of_interest = account_from_path(root_account, account_path)
header = """BEGIN:VCALENDAR
"""
footer = "END:VCALENDAR"
- current_events = dict()
-
# insert and add all splits in the periods of interest
for split in account_of_interest.GetSplitList():
trans = split.parent
num = trans.GetNum()
- if (re.search('To Do', num, re.I)):
+ if (re.search('(To Do|AP|DD)', num, re.I)):
split_amount = gnc_numeric_to_python_Decimal(split.GetAmount())
if split_amount < ZERO:
else:
type = "credit"
-
day = trans_date.strftime("%Y%m%dZ");
stamp = datetime.utcnow().strftime("%Y%m%dT%H%M%SZ");
- guid = trans.GetGUID().to_string() + "@gnucash"
+ guid = trans.GetGUID().to_string() + identifier
current_events[guid] = 1
- summary = "%s - %s $%d" % (trans.GetDescription(), type, split_amount)
+ summary = "%s:%s - %s $%d" % (num, trans.GetDescription(), type, split_amount)
ical = header + """BEGIN:VEVENT
UID:""" + guid + """
DTSTAMP:""" + stamp + """
DTSTART:""" + day + """
SUMMARY:""" + summary + """
-LOCATION:""" + account.name + """
+LOCATION:""" + account_name + """
END:VEVENT
""" + footer
if summary != event.instance.vevent.summary.value or trans_date != event.instance.vevent.dtstart.value:
event.set_data(ical)
event.save()
- print "Event", event, "updated"
+ print event, "updated"
else:
- print "Event", event, "unchanged, skipping"
+ print event, "unchanged, skipping"
except caldav.lib.error.NotFoundError:
url = "/caldav.php/gnucash/home/" + guid + ".ics"
event = caldav.Event(client, url = url, data = ical, parent = calendar).save()
- print "Event", event, "created"
+ print event, "created"
current_events[guid] = 1
else:
continue
- # Go through the events on the server and remove anything that we've added
- # previously that is now stale.
- events = calendar.events()
- for event in events:
- event.load()
- if not current_events.has_key(event.instance.vevent.uid.value):
- if (re.search('@gnucash', event.instance.vevent.uid.value)):
- print "Event", event, "deleted, %s" % event.instance.vevent.uid.value
- event.delete()
-
- # no save needed, we're just reading..
- gnucash_session.end()
-
-
if __name__ == "__main__": main()
-
-