1#
2# Usage: Fill in the configuration variables.  It will download the feed
3# for it, parse it, and print out test cases to add to the unit test.
4#
5
6EMAIL = "onoratoj@gmail.com"
7PRIVATE_COOKIE = "432802670aefa458daf036597ec8136b"
8START_DATE = ("2006","01","01")
9END_DATE = ("2009","01","01")
10
11
12
13import sys, urllib, re
14from xml.dom import minidom
15
16def fmt(n):
17    if n < 10:
18        return "0" + str(n)
19    else:
20        return str(n)
21
22def makeDate(d):
23    return d[0] + "-" + d[1] + "-" + d[2]
24
25def makeZDate(d):
26    return d[0] + d[1] + d[2] + "T000000Z"
27
28url = "http://www.google.com/calendar/feeds/onoratoj@gmail.com/private-" \
29        + PRIVATE_COOKIE + "/composite?start-min=" + makeDate(START_DATE) \
30        + "&start-max=" + makeDate(END_DATE)
31
32#data = open("out.xml")
33data = urllib.urlopen(url)
34
35DTSTART_TZID = re.compile("DTSTART;TZID=(.*):(.*)")
36DTSTART = re.compile("DTSTART:(.*)")
37DURATION = re.compile("DURATION:(.*)")
38RRULE = re.compile("RRULE:(.*)")
39TIME = re.compile("(....)-(..)-(..)T(..):(..):(..)....([+-])(..):(..)")
40TIMEZ = re.compile("(....)-(..)-(..)T(..):(..):(..)....Z")
41
42def stripTimezone(str):
43    lines = str.split("\n")
44    drop = False
45    result = []
46    for line in lines:
47        if line == "BEGIN:VTIMEZONE":
48            drop = True
49        if not drop:
50            result.append(line)
51        if line == "END:VTIMEZONE":
52            drop = False
53    return result
54
55def fixInstance(s):
56    m = TIME.match(s[0])
57    if m:
58        if m.group(7) == "+":
59            sign = -1
60        else:
61            sign = 1
62        hour = int(m.group(4)) + (sign * int(m.group(8)))
63        return m.group(1) + m.group(2) + m.group(3) + "T" + fmt(hour) \
64                + m.group(5) + m.group(6) + "Z"
65    m = TIMEZ.match(s[0])
66    if m:
67        return m.group(1) + m.group(2) + m.group(3) + "T" + m.group(4) \
68                + m.group(5) + m.group(6) + "Z"
69    return s[0]
70
71dom = minidom.parse(data)
72root = dom.documentElement
73
74entries = root.getElementsByTagName("entry")
75
76for entry in entries:
77    recurrences = entry.getElementsByTagName("gd:recurrence")
78    dtstart = ""
79    tzid = ""
80    duration = ""
81    rrule = ""
82    if len(recurrences) > 0:
83        recurrence = recurrences[0]
84        s = ""
85        for c in recurrence.childNodes:
86            s = s + c.nodeValue
87        lines = stripTimezone(s)
88        for s in lines:
89            re_dtstart = DTSTART_TZID.match(s)
90            if re_dtstart:
91                dtstart = re_dtstart.group(2)
92                tzid = re_dtstart.group(1)
93            re_dtstart = DTSTART.match(s)
94            if re_dtstart:
95                dtstart = re_dtstart.group(1)
96            re_duration = DURATION.match(s)
97            if re_duration:
98                duration = re_duration.group(1)
99            re_rrule = RRULE.match(s)
100            if re_rrule:
101                rrule = re_rrule.group(1)
102    whens = entry.getElementsByTagName("gd:when")
103    instances = []
104    for w in whens:
105        startTime = w.getAttribute("startTime")
106        endTime = w.getAttribute("endTime")
107        instances.append((startTime,endTime))
108
109    instances = map(fixInstance, instances)
110    instances.sort()
111    if dtstart != "":
112        title = ""
113        for c in entry.getElementsByTagName('title')[0].childNodes:
114            title = title + c.nodeValue
115
116        print "            // " + title
117        print "            test(\"" + dtstart + "\","
118        print "                    \"" + rrule + "\","
119        print "                    \"" + makeZDate(START_DATE) \
120                                    + "\", \"" + makeZDate(END_DATE) + "\","
121        print "                    new String[] {"
122        for i in instances:
123            print "                        \"" + i + "\","
124        print "                    });"
125        print
126
127
128