~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to bin/ivle-addexercise

  • Committer: Matt Giuca
  • Date: 2009-05-26 04:15:23 UTC
  • mto: This revision was merged to the branch mainline in revision 1322.
  • Revision ID: matt.giuca@gmail.com-20090526041523-hsg5q2enlhvjb5y2
doc/man/architecture.rst: User management server.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
 
21
21
"""Script to upload an exercise file into the database"""
22
22
 
23
 
import os, sys
 
23
import os, sys, traceback
24
24
import xml.dom.minidom as minidom
25
25
 
 
26
from ivle.config import Config
26
27
from ivle.database import Exercise, TestSuite, TestCase, TestSuiteVar, TestCasePart, get_store
27
28
 
28
29
class XMLMalformedError(Exception):
61
62
    cases = []
62
63
    case_num = 0
63
64
    for case_node in suite_node.getElementsByTagName('function'):
 
65
        cases.append(add_test_case(case_node, case_num, store))
64
66
        case_num += 1
65
 
        cases.append(add_test_case(case_node, case_num, store))
66
67
 
67
68
    ## ALLOWED TAGS ##
68
69
    # stdin     - Stdin for the suite - Unique - Text inside element
119
120
    store.add(new_suite)
120
121
    return new_suite
121
122
 
122
 
def add_part(store, element_type, test_type, data, filename=u""):
 
123
def add_part(store, part_type, test_type, data, filename=u""):
123
124
    new_part = TestCasePart()
124
 
    new_part.element_type = element_type
125
 
    new_part.test_type = test_type
126
 
    new_part.data = data
 
125
    new_part.part_type = unicode(part_type)
 
126
    new_part.test_type = unicode(test_type)
 
127
    new_part.data = unicode(data)
127
128
    new_part.filename = unicode(filename)
128
129
    store.add(new_part)
129
130
    return new_part
152
153
            filename = child_node.getAttribute('name')
153
154
            if filename == "":
154
155
                raise XMLMalformedException('file tag must have names')
155
 
            parts.append(add_part(store, element_type, test_type, data,
 
156
            parts.append(add_part(store, part_type, test_type, data,
156
157
                                        filename))
157
158
            
158
159
        elif child_node.tagName in allowed_parts:
173
174
    return new_test_case
174
175
 
175
176
xmlfile = sys.argv[1]
176
 
try:
177
 
    filedom = minidom.parse(xmlfile)
178
 
except IOError, e:
179
 
    sys.exit('ivle-addexercise: error opening file ' + xmlfile + ': ' + e[1])
180
 
 
181
 
 
182
 
for child in filedom.childNodes:
183
 
    if child.nodeType == child.ELEMENT_NODE and child.tagName == 'exercise':
184
 
        exercise = child
185
 
    else:
186
 
        sys.exit('ivle-addexercise: error parsing XML: root node must be "exercise"')
187
 
 
188
 
exercisename = exercise.getAttribute('name')
189
 
rows = exercise.getAttribute('rows')
190
 
solution = None
191
 
partial_solution = None
192
 
include_code = None
193
 
description = None
194
 
test_suite_nodes = []
195
 
for child in exercise.childNodes:
196
 
    if child.nodeType != child.ELEMENT_NODE:
197
 
        continue
198
 
    if child.tagName == 'solution':
199
 
        if solution is not None:
200
 
            sys.exit('ivle-addexercise: error parsing XML: multiple "solution" nodes')
201
 
        solution = getTextData(child)
202
 
    elif child.tagName == 'include':
203
 
        if include_code is not None:
204
 
            sys.exit('ivle-addexercise: error parsing XML: multiple "include" nodes')
205
 
        include_code = getTextData(child)
206
 
    elif child.tagName == 'partial':
207
 
        if partial_solution is not None:
208
 
            sys.exit('ivle-addexercise: error parsing XML: multiple "include" nodes')
209
 
        partial_solution = getTextData(child)
210
 
    elif child.tagName == 'case':
211
 
        test_suite_nodes.append(child)
212
 
    elif child.tagName == 'desc':
213
 
        description = getTextData(child)
214
 
 
215
 
if solution is None:
216
 
    sys.exit("ivle-addexercise: error parsing XML: No solution given")
217
 
if len(test_suite_nodes) == 0:
218
 
    sys.exit("ivle-addexercise: error parsing XML:")
219
 
 
220
 
store = get_store()
221
 
new_exercise = Exercise()
222
 
new_exercise.id = unicode(xmlfile)
223
 
new_exercise.name = exercisename
224
 
new_exercise.num_rows = int(rows)
225
 
new_exercise.partial = partial_solution
226
 
new_exercise.solution = solution
227
 
new_exercise.include = include_code
228
 
new_exercise.description = description
229
 
new_exercise.partial = partial_solution
230
 
store.add(new_exercise)
231
 
suite_num = 0
232
 
for suite in test_suite_nodes:
233
 
    new_exercise.test_suites.add(add_test_suite(suite, suite_num, store))
234
 
    suite_num += 1
235
 
 
236
 
store.add(new_exercise)
237
 
store.commit()
 
177
 
 
178
def add_exercise(xmlfile):
 
179
    # Skip existing ones.
 
180
    if store.find(Exercise, id=unicode(xmlfile)).count():
 
181
        return
 
182
 
 
183
    print "Adding exercise", xmlfile
 
184
    try:
 
185
        filedom = minidom.parse(xmlfile)
 
186
    except IOError, e:
 
187
        raise Exception('ivle-addexercise: error opening file ' + xmlfile + ': ' + e[1])
 
188
 
 
189
    for child in filedom.childNodes:
 
190
        if child.nodeType == child.ELEMENT_NODE and child.tagName == 'exercise':
 
191
            exercise = child
 
192
        else:
 
193
            raise XMLMalformedError('ivle-addexercise: error parsing XML: root node must be "exercise"')
 
194
 
 
195
    exercisename = exercise.getAttribute('name')
 
196
    rows = exercise.getAttribute('rows')
 
197
    if rows == '':
 
198
        rows = 4
 
199
    solution = None
 
200
    partial_solution = None
 
201
    include_code = None
 
202
    description = None
 
203
    test_suite_nodes = []
 
204
    for child in exercise.childNodes:
 
205
        if child.nodeType != child.ELEMENT_NODE:
 
206
            continue
 
207
        if child.tagName == 'solution':
 
208
            if solution is not None:
 
209
                raise XMLMalformedError('ivle-addexercise: error parsing XML: multiple "solution" nodes')
 
210
            solution = getTextData(child)
 
211
        elif child.tagName == 'include':
 
212
            if include_code is not None:
 
213
                raise XMLMalformedError('ivle-addexercise: error parsing XML: multiple "include" nodes')
 
214
            include_code = getTextData(child)
 
215
        elif child.tagName == 'partial':
 
216
            if partial_solution is not None:
 
217
                raise XMLMalformedError('ivle-addexercise: error parsing XML: multiple "include" nodes')
 
218
            partial_solution = getTextData(child)
 
219
        elif child.tagName == 'case':
 
220
            test_suite_nodes.append(child)
 
221
        elif child.tagName == 'desc':
 
222
            description = getTextData(child)
 
223
 
 
224
    new_exercise = Exercise()
 
225
    new_exercise.id = unicode(xmlfile)
 
226
    new_exercise.name = exercisename
 
227
    new_exercise.num_rows = int(rows)
 
228
    new_exercise.partial = partial_solution
 
229
    new_exercise.solution = solution
 
230
    new_exercise.include = include_code
 
231
    new_exercise.description = description
 
232
    new_exercise.partial = partial_solution
 
233
    store.add(new_exercise)
 
234
    suite_num = 0
 
235
    for suite in test_suite_nodes:
 
236
        new_exercise.test_suites.add(add_test_suite(suite, suite_num, store))
 
237
        suite_num += 1
 
238
 
 
239
    store.add(new_exercise)
 
240
    store.commit()
 
241
 
 
242
store = get_store(Config())
 
243
 
 
244
xmlfiles = sys.argv[1:]
 
245
for xmlfile in xmlfiles:
 
246
    try:
 
247
        add_exercise(xmlfile)
 
248
    except Exception, e:
 
249
        print "ERROR: Could not add file", xmlfile