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

« back to all changes in this revision

Viewing changes to www/apps/tutorialservice/test/parse_exercise.py

  • Committer: stevenbird
  • Date: 2008-02-19 22:18:13 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:513
test/test_framework/*, exercises/sample/*
* changed root element to exercise (was problem)
* changed test code to call parse_exercise_file (was
    parse_tutorial_file)
* modified samples to show use of new exercise functionality

www/apps/tutorial*:
* consistent naming of methods (nothing talking about "problem" now)

doc/setup/install_proc.txt
* added apt-get for python-ldap, a new dependency

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
17
17
 
18
 
# Module: parse_tute
 
18
# Module: parse_exercise
19
19
# Author: Dilshan Angampitiya
 
20
#         Steven Bird (revisions)
20
21
# Date:   24/1/2008
21
22
 
22
23
"""
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
26
 
the file.
 
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
 
27
parsing the file.
27
28
"""
28
29
 
29
30
from xml.dom.minidom import *
33
34
DEFAULT_CASE_TYPE = 'match'
34
35
 
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
39
40
 
148
149
 
149
150
    return case
150
151
                
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)
154
155
 
155
 
    # get problem
 
156
    # get exercise
156
157
    count = 0
157
 
    problem = None
 
158
    exercise = None
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':
160
161
            count += 1
161
 
            if count == prob_num:
162
 
                problem = child
 
162
            if count == exercise_num:
 
163
                exercise = child
163
164
                break
164
165
 
165
 
    if problem == None:
166
 
        raise ParseException("Not enough problems")
 
166
    if exercise == None:
 
167
        raise ParseException("Not enough exercises")
167
168
 
168
169
    # get name
169
 
    problem_name = problem.getAttribute('name')
170
 
 
171
 
    if not problem_name:
172
 
        raise ParseException('Problem name not supplied')
173
 
 
174
 
    problem_suite = TestSuite(problem_name)
 
170
    exercise_name = exercise.getAttribute('name')
 
171
 
 
172
    if not exercise_name:
 
173
        raise ParseException('Exercise name not supplied')
 
174
 
 
175
    exercise_suite = TestSuite(exercise_name)
175
176
 
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:
179
180
            continue
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))
186
187
 
187
 
    return problem_suite
 
188
    return exercise_suite
188
189