15
15
# along with this program; if not, write to the Free Software
16
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
# Module: parse_exercise
19
19
# Author: Dilshan Angampitiya
20
# Steven Bird (revisions)
23
This file provides the function parse_tutorial_file which takes an
24
xml specification of a tutorial problem file and returns a test suite object
25
for that problem. It throws a ParseException if there was a problem parsing
24
This file provides the function parse_exercise_file which takes an xml
25
specification of an exercise file and returns a test suite object for
26
that exercise. It throws a ParseException if there was a problem
29
30
from xml.dom.minidom import *
33
34
DEFAULT_CASE_TYPE = 'match'
35
36
class ParseException(Exception):
36
" Error when parsing the xml problem file "
37
" Error when parsing the xml exercise file "
37
38
def __init___(self, reason):
38
39
self._reason = reason
151
def parse_tutorial_file(filename, prob_num=1):
152
""" Parse an xml problem file and return a testsuite for that problem """
152
def parse_exercise_file(filename, exercise_num=1):
153
""" Parse an xml exercise file and return a testsuite for that exercise """
153
154
dom = parse(filename)
158
159
for child in dom.childNodes:
159
if child.nodeType == child.ELEMENT_NODE and child.tagName == 'problem':
160
if child.nodeType == child.ELEMENT_NODE and child.tagName == 'exercise':
161
if count == prob_num:
162
if count == exercise_num:
166
raise ParseException("Not enough problems")
167
raise ParseException("Not enough exercises")
169
problem_name = problem.getAttribute('name')
172
raise ParseException('Problem name not supplied')
174
problem_suite = TestSuite(problem_name)
170
exercise_name = exercise.getAttribute('name')
172
if not exercise_name:
173
raise ParseException('Exercise name not supplied')
175
exercise_suite = TestSuite(exercise_name)
176
177
# get solution and include info
177
for child in problem.childNodes:
178
for child in exercise.childNodes:
178
179
if child.nodeType != child.ELEMENT_NODE:
180
181
if child.tagName == 'solution':
181
problem_suite.add_solution(getTextData(child))
182
exercise_suite.add_solution(getTextData(child))
182
183
elif child.tagName == 'include':
183
problem_suite.add_include_code(getTextData(child))
184
exercise_suite.add_include_code(getTextData(child))
184
185
elif child.tagName == 'case':
185
problem_suite.add_case(getCaseData(child))
186
exercise_suite.add_case(getCaseData(child))
188
return exercise_suite