File: TestSetGet.py

package info (click to toggle)
vtk7 7.1.1%2Bdfsg2-8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 127,396 kB
  • sloc: cpp: 1,539,584; ansic: 124,382; python: 78,038; tcl: 47,013; xml: 8,142; yacc: 5,040; java: 4,439; perl: 3,132; lex: 1,926; sh: 1,500; makefile: 126; objc: 83
file content (444 lines) | stat: -rwxr-xr-x 13,850 bytes parent folder | download | duplicates (5)
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#!/usr/bin/env python

import sys
import inspect
import re
import optparse

import vtk
from pydoc import classname

'''
   This is a translation from the TCL code of the same name.
'''
vtk.vtkObject.GlobalWarningDisplayOff()

def RedirectVTKMessages():
    '''
    Can be used to redirect VTK related error messages to a
    file.
    '''
    log = vtk.vtkFileOutputWindow()
    log.SetFlush(1)
    log.AppendOff()
    log.SetFileName('TestSetGet-err.log')
    log.SetInstance(log)


commonExceptions = set([
"vtkDistributedDataFilter",  # core dump
"vtkDataEncoder",  # Use after free error.
"vtkWebApplication",  # Thread issues - calls vtkDataEncoder
"vtkView",  # vtkView.SetRepresentation(None) fails

"vtkGenericAttributeCollection",  # Assert error
"vtkOverlappingAMR",

 # These give an HDF5 no file name error.
"vtkAMRFlashParticlesReader",
"vtkAMREnzoParticlesReader",
"vtkAMRFlashReader",

# core dump in release mode, issue is vtkChartMatrix.GetChart() & vtkScatterPlotMatrix.SetActivePlot()
"vtkChartMatrix",
"vtkScatterPlotMatrix"
])

classLinuxExceptions = set([
"vtkAMREnzoReader"  # core dump
])

# In the case of Windows, these classes cause a crash.
classWindowsExceptions = set([
"vtkWin32VideoSource",  # Update() works the first time but a subsequent run calls up the webcam which crashes on close.

"vtkCMLMoleculeReader",
"vtkCPExodusIIInSituReader",
"vtkMINCImageWriter",
"vtkQtInitialization"
])

classExceptions = set()

# This will hold the classes to be tested.
vtkClasses = set()
classNames = None
classesTested = set()

# Keep a record of the classes tested.
nonexistentClasses = set()
abstractClasses = set()
noConcreteImplementation = set()
noObserver = set()
setGetWorked = set()
setGetFailed = set()

#------------------------
# These variables are used for further record keeping
# should users wish to investigate or debug further.
noGetSetPairs = set()
# These will be a list of the get/set functions keyed on class name.
getParameterFail = dict()
setParameterFail = dict()
#------------------------

# Controls the verbosity of the output.
verbose = False


class ErrorObserver:
   '''
   See: http://public.kitware.com/pipermail/vtkusers/2012-June/074703.html
   '''
   def __init__(self):
       self.__ErrorOccurred = False
       self.__ErrorMessage = None
       self.CallDataType = 'string0'

   def __call__(self, obj, event, message):
       self.__ErrorOccurred = True
       self.__ErrorMessage = message

   def ErrorOccurred(self):
       occ = self.__ErrorOccurred
       self.__ErrorOccurred = False
       return occ

   def ErrorMessage(self):
       return self.__ErrorMessage

def GetVTKClasses():
    '''
    :return: a set of all the VTK classes.
    '''
    # This pattern will get the name and type of the member in the vtk classes.
    pattern = r'\<vtkclass (.*)\.(.*)\>'
    regEx = re.compile(pattern)
    vtkClasses = inspect.getmembers(
                    vtk, inspect.isclass and not inspect.isabstract)
    res = set()
    for name, obj in vtkClasses:
            result = re.match(regEx, repr(obj))
            if result:
                res.add(result.group(2))
    return res

def GetVTKClassGroups(vtkClasses, subStr):
    '''
    :param: vtkClasses - the set of VTK classes
    :param: subStr - the substring for the VTK class to match e.g Readers
    :return: a set of all the VTK classes that are e.g. Readers.
    '''
    res = set()
    for obj in list(vtkClasses):
            if obj.find(subStr) > -1:
                res.add(obj)
    return res

def FilterClasses(allClasses, filter):
    '''
    :param: vtkCLasses - the set of VTK classes
    :param: filters - a list of substrings of classes to be removed
    :return: a set of all the VTK classes that do not have the substrings
     in their names.
    '''
    res = allClasses
    for f in filter:
        c = GetVTKClassGroups(allClasses, f)
        res = res - c
    return res

def TestOne(cname):
    '''
    Test the named class looking for complementary Set/Get pairs to test.

    The test is essentially this: Set(Get()).

    Some classes will generate a TypeError or an AttributeError,
    in this case, the name of the class is stored in the global variable
    abstractClasses or noConcreteImplementation with False being returned.

    If Set... or Get... fails a record of this is kept in the global classes:
    setParameterFail and getParameterFail

    Return values:
    0: If not any of the Set(Get()) tests work.
    1: If at least one of Set(Get()) tests works.
    2: No observer could be added.
    3: If it is an abstract class.
    4: No concrete implementation.
    5: Class does not exist.

    :param: cname - the name of the class to be tested.
    :return: One of the above return values.
    '''
    try:
        b = getattr(vtk, cname)()
        e = ErrorObserver()
        try:
            b.AddObserver('ErrorEvent', e)
        except AttributeError:
            noObserver.add(cname)
            return 2
        except:
            raise

        getPattern = r'(^Set(.*)$)'
        getRegEx = re.compile(getPattern)
        setPattern = r'(^Get(.*)$)'
        setRegEx = re.compile(setPattern)

        methods = [method for method in dir(getattr(vtk, cname))
                    if callable(getattr(getattr(vtk, cname), method))]
        # Partition the Set/Get functions into separate sets.
        gets = set()
        sets = set()
        for m in methods:
            result = re.match(getRegEx, m)
            if result:
                gets.add(result.group(2))
            result = re.match(setRegEx, m)
            if result:
                sets.add(result.group(2))

        ok = False
        # These are our Set... Get... functions.
        matchingGetSet = set.intersection(gets, sets)
        setGetStatus = False
        if matchingGetSet:
            for m in set.intersection(gets, sets):
                # Get...() with no parameters.
                try:
                    x = getattr(b, 'Get' + m)()
                    try:
                        if type(x) is tuple:
                            x = list(x)
                            getattr(b, 'Set' + m)(*x)
                        else:
                            getattr(b, 'Set' + m)(x)
                        setGetStatus |= True
                    except TypeError:
                        # The parameter passed is of the wrong type
                        # or wrong number of parameters.
                        try:
                            value = setParameterFail[cname]
                            value.add('Set' + m)
                            setParameterFail[cname] = value
                        except KeyError:
                            # Key is not present
                            setParameterFail[cname] = set(['Set' + m])
                    except Exception as err:
                        print(cname + 'Set' + m + ' ' + str(err))
                except TypeError:
                    # Get...() takes 1 or more arguments.
                    try:
                        value = getParameterFail[cname]
                        value.add('Get' + m)
                        getParameterFail[cname] = value
                    except KeyError:
                        # Key is not present
                        getParameterFail[cname] = set(['Get' + m])
                except Exception as err:
                    print(cname + 'Get' + m + ' ' + str(err))
            ok = setGetStatus
        else:
            noGetSetPairs.add(cname)

        b = None
        if ok:
            setGetWorked.add(cname)
            return 1
        setGetFailed.add(cname)
        return 0
    except TypeError:
        # Trapping abstract classes.
        abstractClasses.add(cname)
        return 3
    except NotImplementedError:
        # No concrete implementation
        noConcreteImplementation.add(cname)
        return 4
    except AttributeError:
        # Class does not exist
        nonexistentClasses.add(cname)
        return 5
    except:
        raise

def TestSetGet(batch, batchNo=0, batchSize=0):
    '''
    Test Set/Get pairs in each batch.

    If at least one of the Set(Get()) tests pass, the name of the
    class is added to the global variable setGetWorked otherwise
    to the global variable setGetFailed.

    :param: batch - the set of classes to be tested.
    :param: batchNo - if the set of classes is a subgroup then this
                     is the index of the subgroup.
    :param: batchSize - if the set of classes is a subgroup then this
                     is the size of the subgroup.
    '''
    baseIdx = batchNo * batchSize
    idx = baseIdx
    for a in batch:
        batchIdx = idx - baseIdx
        # res = " Testing -- {:4d} - {:s}".format(idx,a)
        # There is no format method in Python 2.5
        res = " Testing -- %4d - %s" % (idx, a)
        if (batchIdx < len(batch) - 1):
            # nextRes = " Next -- {:4d} - {:s}".format(idx + 1,list(batch)[batchIdx +1])
            nextRes = " Next -- %4d - %s" % (idx + 1, list(batch)[batchIdx + 1])
        else:
            nextRes = "No next"
#         if verbose:
#             print(res, nextRes)
        classesTested.add(a)
        ok = TestOne(a)
        if ok == 0:
            if verbose:
                print(res + ' - Fail')
        elif ok == 1:
            if verbose:
                print(res + ' - Ok')
        elif ok == 2:
            if verbose:
                print(res + ' - no observer could be added.')
        elif ok == 3:
            if verbose:
                print(res + ' - is Abstract')
        elif ok == 4:
            if verbose:
                print(res + ' - No concrete implementation')
        elif ok == 5:
            if verbose:
                print(res + ' - Does not exist')
        else:
            if verbose:
                print(res + ' - Unknown status')
        idx += 1
#        print(nextRes)

def BatchTest(vtkClasses, batchNo, batchSize):
    '''
    Batch the classes into groups of batchSize.

    :param: batchNo - if the set of classes is a subgroup then this
                     is the index of the subgroup.
    :param: batchSize - if the set of classes is a subgroup then this
                     is the size of the subgroup.
    '''
    idx = 0
    total = 0;
    batch = set()
    for a in vtkClasses:
        currentBatchNo = idx // batchSize;
        if currentBatchNo == batchNo:
            batch.add(a)
            total += 1
            if total == batchSize:
                TestSetGet(batch, batchNo, batchSize)
                batch = set()
                total = 0
        idx += 1
    if batch:
        TestSetGet(batch, batchNo, batchSize)

def PrintResultSummary():
    print('-' * 40)
    print('Set(Get()) worked: %i' % len(setGetWorked))
    print('Set(Get()) failed: %i' % len(setGetFailed))
    print('Abstract classes: %i' % len(abstractClasses))
    print('Non-existent classes: %i' % len(nonexistentClasses))
    print('No concrete implementation: %i' % len(noConcreteImplementation))
    print('No observer could be added: %i' % len(noObserver))
    print('-' * 40)
    print('Total number of classes tested: %i' % len(classesTested))
    print('-' * 40)
    print('Excluded from testing: %i' % len(classExceptions))
    print('-' * 40)

def ProgramOptions():
    desc = """
    %prog Tests each VTK class for Set(Get()) where Get() has no parameters.
    """
    parser = optparse.OptionParser(description=desc)
    parser.set_defaults(verbose=False)

    parser.add_option('-c', '--classnames',
        help='The name of the class or a list of classes in quotes separated by commas.',
        type='string',
        dest='classnames',
        default=None,
        action='store')
    parser.add_option('-q', '--quiet',
        help='Do not print status messages to stdout (default)',
        dest='verbose',
        action="store_false")
    parser.add_option('-v', '--verbose',
        help='Print status messages to stdout',
        dest='verbose',
        action="store_true")

    (opts, args) = parser.parse_args()

    return (True, opts)

def CheckPythonVersion(ver):
    '''
    Check the Python version.

    :param: ver - the minimum required version number as hexadecimal.
    :return: True if if the Python version is greater than or equal to ver.
    '''
    if sys.hexversion < ver:
        return False
    return True

def main(argv=None):
    if not CheckPythonVersion(0x02060000):
        print('This program requires Python 2.6 or greater.')
        return

    global classExceptions
    global vtkClasses
    global classNames
    global verbose

    if argv is None:
        argv = sys.argv

    (res, opts) = ProgramOptions()
    if opts.classnames:
        cn = [x.strip() for x in opts.classnames.split(',')]
        classNames = set(cn)
    if opts.verbose:
        verbose = opts.verbose

    print('CTEST_FULL_OUTPUT (Avoid ctest truncation of output)')

    # RedirectVTKMessages()
    if classNames:
        TestSetGet(classNames)
    else:
        classExceptions = commonExceptions.union(classLinuxExceptions)
        classExceptions = classExceptions.union(classWindowsExceptions)

        vtkClasses = GetVTKClasses()
#         filter = ['Reader', 'Writer', 'Array_I', 'Qt']
#         vtkClasses = FilterClasses(vtkClasses, filter)
        vtkClasses = vtkClasses - classExceptions
        TestSetGet(vtkClasses)
#         In Windows
#         0-10, 10-17, 17-18, 18-23 in steps of 100 work but not if called
#         in a loop.
#         intervals = [[0,10]]  # [[0,10]], [10,17], [17,18], [18,20]]
#         for j in intervals:
#             for i in range(j[0], j[1]):
#                 BatchTest(vtkClasses, i, 100)
#                 print(vtkClasses)

    PrintResultSummary()

if __name__ == '__main__':
    sys.exit(main())