summaryrefslogtreecommitdiffstats
path: root/maketests.py
blob: f5f5cb1fa8d2743a683d9db8598b6299eeaedae1 (plain)
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
#
# Usage: Fill in the configuration variables.  It will download the feed
# for it, parse it, and print out test cases to add to the unit test.
#

EMAIL = "onoratoj@gmail.com"
PRIVATE_COOKIE = "432802670aefa458daf036597ec8136b"
START_DATE = ("2006","01","01")
END_DATE = ("2009","01","01")



import sys, urllib, re
from xml.dom import minidom

def fmt(n):
    if n < 10:
        return "0" + str(n)
    else:
        return str(n)

def makeDate(d):
    return d[0] + "-" + d[1] + "-" + d[2]

def makeZDate(d):
    return d[0] + d[1] + d[2] + "T000000Z"

url = "http://www.google.com/calendar/feeds/onoratoj@gmail.com/private-" \
        + PRIVATE_COOKIE + "/composite?start-min=" + makeDate(START_DATE) \
        + "&start-max=" + makeDate(END_DATE)

#data = open("out.xml")
data = urllib.urlopen(url)

DTSTART_TZID = re.compile("DTSTART;TZID=(.*):(.*)")
DTSTART = re.compile("DTSTART:(.*)")
DURATION = re.compile("DURATION:(.*)")
RRULE = re.compile("RRULE:(.*)")
TIME = re.compile("(....)-(..)-(..)T(..):(..):(..)....([+-])(..):(..)")
TIMEZ = re.compile("(....)-(..)-(..)T(..):(..):(..)....Z")

def stripTimezone(str):
    lines = str.split("\n")
    drop = False
    result = []
    for line in lines:
        if line == "BEGIN:VTIMEZONE":
            drop = True
        if not drop:
            result.append(line)
        if line == "END:VTIMEZONE":
            drop = False
    return result

def fixInstance(s):
    m = TIME.match(s[0])
    if m:
        if m.group(7) == "+":
            sign = -1
        else:
            sign = 1
        hour = int(m.group(4)) + (sign * int(m.group(8)))
        return m.group(1) + m.group(2) + m.group(3) + "T" + fmt(hour) \
                + m.group(5) + m.group(6) + "Z"
    m = TIMEZ.match(s[0])
    if m:
        return m.group(1) + m.group(2) + m.group(3) + "T" + m.group(4) \
                + m.group(5) + m.group(6) + "Z"
    return s[0]

dom = minidom.parse(data)
root = dom.documentElement

entries = root.getElementsByTagName("entry")

for entry in entries:
    recurrences = entry.getElementsByTagName("gd:recurrence")
    dtstart = ""
    tzid = ""
    duration = ""
    rrule = ""
    if len(recurrences) > 0:
        recurrence = recurrences[0]
        s = ""
        for c in recurrence.childNodes:
            s = s + c.nodeValue
        lines = stripTimezone(s)
        for s in lines:
            re_dtstart = DTSTART_TZID.match(s)
            if re_dtstart:
                dtstart = re_dtstart.group(2)
                tzid = re_dtstart.group(1)
            re_dtstart = DTSTART.match(s)
            if re_dtstart:
                dtstart = re_dtstart.group(1)
            re_duration = DURATION.match(s)
            if re_duration:
                duration = re_duration.group(1)
            re_rrule = RRULE.match(s)
            if re_rrule:
                rrule = re_rrule.group(1)
    whens = entry.getElementsByTagName("gd:when")
    instances = []
    for w in whens:
        startTime = w.getAttribute("startTime")
        endTime = w.getAttribute("endTime")
        instances.append((startTime,endTime))

    instances = map(fixInstance, instances)
    instances.sort()
    if dtstart != "":
        title = ""
        for c in entry.getElementsByTagName('title')[0].childNodes:
            title = title + c.nodeValue

        print "            // " + title
        print "            test(\"" + dtstart + "\","
        print "                    \"" + rrule + "\","
        print "                    \"" + makeZDate(START_DATE) \
                                    + "\", \"" + makeZDate(END_DATE) + "\","
        print "                    new String[] {"
        for i in instances:
            print "                        \"" + i + "\","
        print "                    });"
        print