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
|
"""
This python module provides functionality to parse the methods of a
VTK object.
Created by Prabhu Ramachandran. Committed in Apr, 2002.
"""
import string, re, sys
import types
# set this to 1 if you want to see debugging messages - very useful if
# you have problems
DEBUG=0
def debug(msg):
if DEBUG:
print(msg)
class VtkDirMethodParser:
"""Parses the methods from dir(vtk_obj)."""
def initialize_methods(self, vtk_obj):
debug("VtkDirMethodParser:: initialize_methods()")
self.methods = dir(vtk_obj)[:]
# stores the <blah>On methods
self.toggle_meths = []
# stores the Set<blah>To<blah> methods
self.state_meths = []
# stores the methods that have a Get<blah> and Set<blah>
# only the <blah> is stored
self.get_set_meths = []
# pure get methods
self.get_meths = []
self.state_patn = re.compile("To[A-Z0-9]")
def parse_methods(self, vtk_obj):
debug("VtkDirMethodParser:: parse_methods()")
self.initialize_methods(vtk_obj)
debug("VtkDirMethodParser:: parse_methods() - initialized methods")
for method in self.methods[:]:
# finding all the methods that set the state.
if method[:3].find("Set") >= 0 and \
self.state_patn.search(method) is not None:
try:
eval("vtk_obj.Get%s" % method[3:])
except AttributeError:
self.state_meths.append(method)
self.methods.remove(method)
# finding all the On/Off toggle methods
elif method[-2:].find("On") >= 0:
try:
self.methods.index("%sOff" % method[:-2])
except ValueError:
pass
else:
self.toggle_meths.append(method)
self.methods.remove(method)
self.methods.remove("%sOff" % method[:-2])
# finding the Get/Set methods.
elif method[:3].find("Get") == 0:
set_m = "Set" + method[3:]
try:
self.methods.index(set_m)
except ValueError:
pass
else:
self.get_set_meths.append(method[3:])
self.methods.remove(method)
self.methods.remove(set_m)
self.clean_up_methods(vtk_obj)
def clean_up_methods(self, vtk_obj):
self.clean_get_set(vtk_obj)
self.clean_state_methods(vtk_obj)
self.clean_get_methods(vtk_obj)
def clean_get_set(self, vtk_obj):
debug("VtkDirMethodParser:: clean_get_set()")
# cleaning up the Get/Set methods by removing the toggle funcs.
for method in self.toggle_meths:
try:
self.get_set_meths.remove(method[:-2])
except ValueError:
pass
# cleaning them up by removing any methods that are responsible for
# other vtkObjects
for method in self.get_set_meths[:]:
try:
eval("vtk_obj.Get%s().GetClassName()" % method)
except (TypeError, AttributeError):
pass
else:
self.get_set_meths.remove(method)
continue
try:
val = eval("vtk_obj.Get%s()" % method)
except (TypeError, AttributeError):
self.get_set_meths.remove(method)
else:
if val is None:
self.get_set_meths.remove(method)
def clean_state_methods(self, vtk_obj):
debug("VtkDirMethodParser:: clean_state_methods()")
# Getting the remaining pure GetMethods
for method in self.methods[:]:
if method[:3].find("Get") == 0:
self.get_meths.append(method)
self.methods.remove(method)
# Grouping similar state methods
if len(self.state_meths) != 0:
tmp = self.state_meths[:]
self.state_meths = []
state_group = [tmp[0]]
end = self.state_patn.search(tmp[0]).start()
# stores the method type common to all similar methods
m = tmp[0][3:end]
for i in range(1, len(tmp)):
if tmp[i].find(m) >= 0:
state_group.append(tmp[i])
else:
self.state_meths.append(state_group)
state_group = [tmp[i]]
end = self.state_patn.search(tmp[i]).start()
m = tmp[i][3:end]
try: # remove the corresponding set method in get_set
val = self.get_set_meths.index(m)
except ValueError:
pass
else:
del self.get_set_meths[val]
#self.get_meths.append("Get" + m)
clamp_m = "Get" + m + "MinValue"
try: # remove the GetNameMax/MinValue in get_meths
val = self.get_meths.index(clamp_m)
except ValueError:
pass
else:
del self.get_meths[val]
val = self.get_meths.index("Get" + m + "MaxValue")
del self.get_meths[val]
if len(state_group) > 0:
self.state_meths.append(state_group)
def clean_get_methods(self, vtk_obj):
debug("VtkDirMethodParser:: clean_get_methods()")
for method in self.get_meths[:]:
debug(method)
try:
res = eval("vtk_obj.%s()" % method)
except (TypeError, AttributeError):
self.get_meths.remove(method)
continue
else:
try:
eval("vtk_obj.%s().GetClassName()" % method)
except AttributeError:
pass
else:
self.get_meths.remove(method)
continue
if method[-8:].find("MaxValue") > -1:
self.get_meths.remove(method)
elif method[-8:].find("MinValue") > -1:
self.get_meths.remove(method)
self.get_meths.sort()
def toggle_methods(self):
return self.toggle_meths
def state_methods(self):
return self.state_meths
def get_set_methods(self):
return self.get_set_meths
def get_methods(self):
return self.get_meths
class VtkPrintMethodParser:
"""This class finds the methods for a given vtkObject. It uses
the output from vtkObject->Print() (or in Python str(vtkObject))
and output from the VtkDirMethodParser to obtain the methods."""
def parse_methods(self, vtk_obj):
"""Parse for the methods."""
debug("VtkPrintMethodParser:: parse_methods()")
self._initialize_methods(vtk_obj)
def _get_str_obj(self, vtk_obj):
debug("VtkPrintMethodParser:: _get_str_obj()")
self.methods = str(vtk_obj)
self.methods = self.methods.split("\n")
del self.methods[0]
def _initialize_methods(self, vtk_obj):
"""Do the basic parsing and setting up"""
debug("VtkPrintMethodParser:: _initialize_methods()")
dir_p = VtkDirMethodParser()
dir_p.parse_methods(vtk_obj)
self.toggle_meths = dir_p.toggle_methods()
self.state_meths = dir_p.state_methods()
self.get_set_meths = dir_p.get_set_methods()
self.get_meths = dir_p.get_methods()
def toggle_methods(self):
return self.toggle_meths
def state_methods(self):
return self.state_meths
def get_set_methods(self):
return self.get_set_meths
def get_methods(self):
return self.get_meths
|