-
Notifications
You must be signed in to change notification settings - Fork 0
/
exporter.py
133 lines (118 loc) · 3.85 KB
/
exporter.py
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
from xml.etree import ElementTree
from xml.dom import minidom
import sys
import dataAccessors as DA
import dataStore as DS
def addText(node, name, text) :
"""
Adds a text node to the input node element, with
the name and text given.
"""
subnode = addNode(node, name)
subnode.text = text
return subnode
def addNode(node, name) :
"""
Adds a node to the input node, with the given name.
"""
subnode = ElementTree.SubElement(node, name)
return subnode
def addRating(node, r) :
"""
Adds a pair of rating text nodes to the input element.
"""
addText(node, "rating", str(r.rating))
if not r.comment :
addText(node, "comment", "None")
else :
addText(node, "comment", r.comment.text)
def personStr(r) :
"""
Combines the first, middle and last name of the person
into a single person name.
"""
if (r.mname) :
return r.fname + " " + r.mname + " " + r.lname
else :
return r.fname + " " + r.lname
nodetypes = {
DS.PlaceLive : 'live_place',
DS.PlaceEat : 'eat_place',
DS.PlaceFun : 'fun_place',
DS.PlaceStudy : 'study_place',
DS.Internship : 'internship'
}
def export() :
"""
Exports all the s in the datastore out to an XML file.
"""
students = DS.User.all().filter('userType =','STUDENT')
root = ElementTree.Element("students")
for s in students :
student = addNode(root, "student")
addText(student, "id", s.uid)
addText(student, "password", s.password)
for r in DS.Rating.all().filter('rater =', s) :
obj = r.rated;
if isinstance(obj, DS.Course) :
grade = DS.Grade.all().filter('course =', obj).filter('student =', s).get()
exportCourse(student, r, obj, grade)
elif isinstance(obj, DS.Book) :
exportBook(student, r, obj)
elif isinstance(obj, DS.Paper) :
exportPaper(student, r, obj)
elif isinstance(obj, DS.Game) :
exportGame(student, r, obj)
elif issubclass(type(obj), DS.Place) :
exportPlace(student, r, obj)
else:
assert False
xml = ElementTree.tostring(root)
return minidom.parseString(xml).toprettyxml(indent="\t")
def exportCourse(p, rating, course, grade) :
"""
Exports a course instance to XML
"""
c = addNode(p, "class")
addText(c, "unique", course.unique)
addText(c, "course_num", course.courseNum)
addText(c, "course_name", course.name)
addText(c, "semester", course.semester.capitalize() + " " + course.year)
addText(c, "instructor", personStr(course.instructor))
if grade: addText(c, "grade", grade.grade)
addRating(c, rating)
def exportBook(p, rating, book) :
"""
Exports a book instance into XML
"""
c = addNode(p, 'book')
addText(c, 'isbn', book.isbn)
addText(c, 'title', book.title)
addText(c, 'author', personStr(book.author))
addRating(c, rating)
def exportPaper(p, rating, paper):
"""
Exports a paper to XML
"""
c = addNode(p, 'paper')
addText(c, 'paper_category', paper.paperType)
addText(c, 'title', paper.title)
addText(c, 'author', personStr(paper.author))
addRating(c, rating)
def exportGame(p, rating, game):
"""
Exports a game to XML
"""
c = addNode(p, 'game')
addText(c, 'title', game.title)
addText(c, 'os', game.platform)
addRating(c, rating)
def exportPlace(p, rating, place):
"""
Exports a place to XML. This encompases all places.
"""
c = addNode(p, nodetypes[type(place)])
addText(c, 'place_name', place.name)
addText(c, 'location', str(place.location))
addText(c, "semester", place.semester.capitalize() + " " + place.year)
addRating(c, rating)