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

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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# IVLE - Informatics Virtual Learning Environment
# Copyright (C) 2007-2008 The University of Melbourne
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

# Module: parse_exercise
# Author: Dilshan Angampitiya
#         Steven Bird (revisions)
# Date:   24/1/2008

"""
This file provides the function parse_exercise_file which takes an xml
specification of an exercise file and returns a test suite object for
that exercise. It throws a ParseException if there was a problem
parsing the file.
"""

from xml.dom.minidom import *
from TestFramework import *

DEFAULT_TEST_TYPE = 'norm'
DEFAULT_CASE_TYPE = 'match'

class ParseException(Exception):
    " Error when parsing the xml exercise file "
    def __init___(self, reason):
        self._reason = reason

    def __str__(self):
        return self._reason

def getTextData(element):
    """ Get the text and cdata inside an element
    Leading and trailing whitespace are stripped
    """
    data = ''
    for child in element.childNodes:
        if child.nodeType == child.CDATA_SECTION_NODE:
            data += child.data
        if child.nodeType == child.TEXT_NODE:
            data += child.data

    return data.strip()

def getCasePartData(partNode):
    """ Create an TestCasePart instance from test part xml data
    """
    
    func_pass = partNode.getAttribute('pass')
    func_fail = partNode.getAttribute('fail')
    default = partNode.getAttribute('default')
    if default == '': default = DEFAULT_CASE_TYPE
    
    part = TestCasePart(func_pass, func_fail, default)

    for child in partNode.childNodes:
        if child.nodeType != child.ELEMENT_NODE:
            continue

        if child.tagName == 'stdout':
            test_type = child.getAttribute('type')
            if test_type == '': test_type = DEFAULT_TEST_TYPE
            part.add_stdout_test(getTextData(child), test_type)
        elif child.tagName == 'stderr':
            test_type = child.getAttribute('type')
            if test_type == '': test_type = DEFAULT_TEST_TYPE
            part.add_stderr_test(getTextData(child), test_type)
        elif child.tagName == 'result':
            test_type = child.getAttribute('type')
            if test_type == '': test_type = DEFAULT_TEST_TYPE
            part.add_result_test(getTextData(child), test_type)
        elif child.tagName == 'exception':
            test_type = child.getAttribute('type')
            if test_type == '': test_type = DEFAULT_TEST_TYPE
            part.add_exception_test(getTextData(child), test_type)
        elif child.tagName == 'file':
            test_type = child.getAttribute('type')
            if test_type == '': test_type = DEFAULT_TEST_TYPE
            filename = child.getAttribute('name')
            if filename == '':
                raise ParseException("File without name in case %s" %case_name)
            part.add_file_test(filename, getTextData(child), test_type)
        elif child.tagName == 'code':
            test_type = child.getAttribute('type')
            if test_type == '': test_type = DEFAULT_TEST_TYPE
            part.add_code_test(getTextData(child), test_type)

    return part

def getCaseData(caseNode, console):
    """ Create a TestCase instance from test case xml data
    """
    
    case_name = caseNode.getAttribute('name')
    function = caseNode.getAttribute('function')
    if function == '': function = None
    case = TestCase(console, case_name, function)
    
    for child in caseNode.childNodes:
        if child.nodeType != child.ELEMENT_NODE:
            continue

        if child.tagName == 'stdin':
            # standard input
            case.set_stdin(getTextData(child))
        elif child.tagName == 'file':
            # file
            filename = child.getAttribute('name')
            if filename == '':
                raise ParseException("File without name in case %s" %case_name)
            case.add_file(filename, getTextData(child))
        elif child.tagName == 'var':
            # global variable
            var_name = child.getAttribute('name')
            var_val = child.getAttribute('value')
            if not var_name or not var_val:
                raise ParseException("Incomplete variable in case %s" %case_name)
            case.add_variable(var_name, var_val)
        elif child.tagName == 'arg':
            # function argument
            arg_val = child.getAttribute('value')
            arg_name = child.getAttribute('name')
            if arg_name == '': arg_name = None
            
            if not arg_val:
                raise ParseException("Incomplete argument in case %s" %case_name)
            case.add_arg(arg_val, arg_name)
        elif child.tagName == 'exception':
            exception_name = child.getAttribute('name')
            if not exception_name:
                raise ParseException("Incomplete exception in case %s" %case_name)
            case.add_exception(exception_name)
        elif child.tagName == 'function':
            # specify a test case part
            case.add_part(getCasePartData(child))

    return case
                
def parse_exercise_file(fileobj, console, exercise_num=1):
    """ Parse an xml exercise file and return a testsuite for that exercise """
    dom = parse(fileobj)

    # get exercise
    count = 0
    exercise = None
    for child in dom.childNodes:
        if child.nodeType == child.ELEMENT_NODE and child.tagName == 'exercise':
            count += 1
            if count == exercise_num:
                exercise = child
                break

    if exercise == None:
        raise ParseException("Not enough exercises")

    # get name
    exercise_name = exercise.getAttribute('name')
    if not exercise_name:
        raise ParseException('Exercise name not supplied')

    exercise_suite = TestSuite(exercise_name, console)

    # get solution and include info
    for child in exercise.childNodes:
        if child.nodeType != child.ELEMENT_NODE:
            continue
        if child.tagName == 'solution':
            exercise_suite.add_solution(getTextData(child))
        elif child.tagName == 'include':
            exercise_suite.add_include_code(getTextData(child))
        elif child.tagName == 'case':
            exercise_suite.add_case(getCaseData(child, console))

    return exercise_suite