public
Created

  • Download Gist
gistfile1.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
 
#!/usr/bin/env python
# Quick and dirty script to convert ktimetracker.ics files
# into TimeCult files.
 
import os.path
import sys
import time
import uuid
 
import elementtree.ElementTree as ET
import vobject
 
 
# Structures to store the tree of data. Use native Python data types, and
# convert to what TimeCult expects when we 'render'
 
class Struct(object):
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
 
 
class TimeCult(Struct):
# <timecult>
# Eventual attributes:
# name
# projectTree
# timeLog
 
def toXML(self):
root = ET.XML('<timecult appVersion="0.12" fileVersion="10" />')
root.attrib['uuid'] = str(uuid.uuid1())
root.attrib['name'] = self.name
pt = ET.Element('projectTree')
for n in self.projectTree:
pt.append(n.toXML())
root.append(pt)
tl = ET.Element('timeLog')
for tr in self.timeLog:
tl.append(tr.toXML())
root.append(tl)
return root
 
class Node(Struct):
# <project> and <task>
# Eventually will have these attributes:
# vtodo
# summary
# uid
# created
# parent
# children
# type = "project|task"
# finished
def __repr__(self):
return "<Node: uid=%s summary=%r, parent=%r>" % (
self.uid,
self.summary,
self.parent.summary if self.parent is not None else None)
 
def toXML(self):
e = ET.Element(self.type)
e.attrib['id'] = self.id
e.attrib['name'] = self.summary
e.attrib['created'] = str(int(time.mktime(self.created.timetuple()) * 1000))
if (self.type == "task"):
assert len(self.children) == 0
e.attrib['status'] = 'finished' if self.finished else 'inProgress'
else:
for c in self.children:
e.append(c.toXML())
return e
 
class TimeRec(Struct):
# <timerec>
# Eventual attributes:
# startTime # datetime
# duration # seconds
# task # Node
def toXML(self):
e = ET.Element("timeRec")
e.attrib['taskId'] = self.taskId
e.attrib['startTime'] = str(int(time.mktime(self.startTime.timetuple()) * 1000))
e.attrib['duration'] = str(int(self.duration * 1000))
e.attrib['notes'] = ""
return e
 
def convert(filename):
f = vobject.readOne(file(filename).read())
 
# First parse Todos
nodes = {}
for vtodo in f.vtodo_list:
n = Node(vtodo = vtodo,
uid=vtodo.uid.value,
summary=vtodo.summary.value,
created=vtodo.created.value,
finished=vtodo.contents['percent-complete'][0].value == "100"
)
nodes[n.uid] = n
 
children = {}
for uid, node in nodes.items():
vtodo = node.vtodo
try:
related_uid = vtodo.contents['related-to'][0].value
node.parent = nodes[related_uid]
children.setdefault(related_uid, []).append(node)
except KeyError:
node.parent = None
 
for uid, node in nodes.items():
node.children = children.get(uid, [])
if len(node.children) == 0:
node.type = "task"
else:
node.type = "project"
 
 
 
autouid = 0 # For newly created Tasks
 
# Now find the time information in the ICS file
time_log = []
for vevent in f.vevent_list:
#if not hasattr(vevent, 'dtstart'):
# sys.stedrr.write("Discarding %r" % vevent)
 
related_uid = vevent.contents['related-to'][0].value
task = nodes[related_uid]
if task.type == "project":
# TimeCult can only cope with time events being against 'leaf' nodes
# in the tree. So we need to adjust and create an additional leaf
project = task
undefined_l = [t for t in project.children if t.uid.startswith('autouid')]
if len(undefined_l) > 0:
task = undefined_l[0]
else:
task = Node(uid='autouid-%d' % autouid,
summary='other',
parent=project,
type="task",
created=project.created,
finished=False,
children=[])
autouid += 1
nodes[task.uid] = task
project.children.insert(0, task)
 
dtstart = vevent.dtstart.value
dtend = vevent.dtend.value
if dtstart.tzinfo is None:
dtstart = dtstart.replace(tzinfo=dtend.tzinfo)
if dtend.tzinfo is None:
dtend = dtend.replace(tzinfo=dtstart.tzinfo)
time_rec = TimeRec(startTime=dtstart,
duration=(dtend - dtstart).total_seconds(),
task=task)
time_log.append(time_rec)
 
# Now need to assign IDs
taskId = 0
for u, n in nodes.items():
taskId += 1
n.id = str(taskId)
 
for tr in time_log:
tr.taskId = tr.task.id
 
return TimeCult(
name=os.path.basename(filename),
projectTree=[n for u, n in nodes.items() if n.parent is None],
timeLog=time_log)
 
 
if __name__ == '__main__':
filename = sys.argv[1]
timecult = convert(filename)
sys.stdout.write(
"""<?xml version="1.0" encoding="UTF-8"?>""" +
ET.tostring(timecult.toXML()))

This script has a bug - it doesn't output - without this timecult will create duplicate IDs, causing corruption

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.