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

« back to all changes in this revision

Viewing changes to bin/ivle-addexercise

Minor updates to the sql for the userdb

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, traceback
 
23
import os, sys
24
24
import xml.dom.minidom as minidom
25
25
 
26
 
from ivle.config import Config
27
26
from ivle.database import Exercise, TestSuite, TestCase, TestSuiteVar, TestCasePart, get_store
28
27
 
29
28
class XMLMalformedError(Exception):
62
61
    cases = []
63
62
    case_num = 0
64
63
    for case_node in suite_node.getElementsByTagName('function'):
 
64
        case_num += 1
65
65
        cases.append(add_test_case(case_node, case_num, store))
66
 
        case_num += 1
67
66
 
68
67
    ## ALLOWED TAGS ##
69
68
    # stdin     - Stdin for the suite - Unique - Text inside element
120
119
    store.add(new_suite)
121
120
    return new_suite
122
121
 
123
 
def add_part(store, part_type, test_type, data, filename=u""):
 
122
def add_part(store, element_type, test_type, data, filename=u""):
124
123
    new_part = TestCasePart()
125
 
    new_part.part_type = unicode(part_type)
126
 
    new_part.test_type = unicode(test_type)
127
 
    new_part.data = unicode(data)
 
124
    new_part.element_type = element_type
 
125
    new_part.test_type = test_type
 
126
    new_part.data = data
128
127
    new_part.filename = unicode(filename)
129
128
    store.add(new_part)
130
129
    return new_part
153
152
            filename = child_node.getAttribute('name')
154
153
            if filename == "":
155
154
                raise XMLMalformedException('file tag must have names')
156
 
            parts.append(add_part(store, part_type, test_type, data,
 
155
            parts.append(add_part(store, element_type, test_type, data,
157
156
                                        filename))
158
157
            
159
158
        elif child_node.tagName in allowed_parts:
174
173
    return new_test_case
175
174
 
176
175
xmlfile = sys.argv[1]
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
 
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()