/usr/share/pyshared/vtk/util/vtkMethodParser.py is in python-vtk 5.8.0-14.1ubuntu3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | """
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 string.find (method[:3], "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 string.find (method[-2:], "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 string.find (method[:3], "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 string.find (method[:3], "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 string.find (tmp[i], 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 string.find (method[-8:], "MaxValue") > -1:
self.get_meths.remove( method)
elif string.find (method[-8:], "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()")
if self._initialize_methods (vtk_obj):
# if David Gobbi's improvements are in this version of VTK
# then I need to go no further.
return
for method in self.methods[:]:
# removing methods that have nothing to the right of the ':'
if (method[1] == '') or \
(string.find (method[1], "none") > -1) :
self.methods.remove (method)
for method in self.methods:
# toggle methods are first identified
if (method[1] == "On") or (method[1] == "Off"):
try:
val = eval ("vtk_obj.Get%s ()"%method[0])
if val == 1:
eval ("vtk_obj.%sOn ()"%method[0])
elif val == 0:
eval ("vtk_obj.%sOff ()"%method[0])
except AttributeError:
pass
else:
self.toggle_meths.append (method[0]+"On")
else: # see it it is get_set or get or a state method
found = 0
# checking if it is a state func.
# figure out the long names from the dir_state_meths
for sms in self.dir_state_meths[:]:
if string.find (sms[0], method[0]) >= 0:
self.state_meths.append (sms)
self.dir_state_meths.remove (sms)
found = 1
if found:
self.get_meths.append ("Get"+method[0])
try:
t = eval ("vtk_obj.Get%sAsString ()"%method[0])
except AttributeError:
pass
else:
self.get_meths.append ("Get"+method[0]+"AsString")
else:
# the long name is inherited or it is not a state method
try:
t = eval ("vtk_obj.Get%s ().GetClassName ()"%
method[0])
except AttributeError:
pass
else:
continue
val = 0
try:
val = eval ("vtk_obj.Get%s ()"%method[0])
except (TypeError, AttributeError):
pass
else:
try:
f = eval ("vtk_obj.Set%s"%method[0])
except AttributeError:
self.get_meths.append ("Get"+method[0])
else:
try:
apply (f, val)
except TypeError:
try:
apply (f, (val, ))
except TypeError:
self.get_meths.append ("Get"+method[0])
else:
self.get_set_meths.append (method[0])
else:
self.get_set_meths.append (method[0])
self._clean_up_methods (vtk_obj)
def _get_str_obj (self, vtk_obj):
debug ("VtkPrintMethodParser:: _get_str_obj()")
self.methods = str (vtk_obj)
self.methods = string.split (self.methods, "\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)
# testing if this version of vtk has David Gobbi's cool
# stuff. If it does then no need to do other things.
try:
junk = vtk_obj.__class__
except AttributeError:
pass
else:
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 ()
return 1
self.dir_toggle_meths = dir_p.toggle_methods ()
self.dir_state_meths = dir_p.state_methods ()
self.dir_get_set_meths = dir_p.get_set_methods ()
self.dir_get_meths = dir_p.get_methods ()
self._get_str_obj (vtk_obj)
patn = re.compile (" \S")
for method in self.methods[:]:
if not patn.match (method):
self.methods.remove (method)
for method in self.methods[:]:
if string.find (method, ":") == -1:
self.methods.remove (method)
for i in range (0, len (self.methods)):
strng = self.methods[i]
strng = string.replace (strng, " ", "")
self.methods[i] = string.split (strng, ":")
self.toggle_meths = []
self.state_meths = []
self.get_set_meths = []
self.get_meths = []
return 0
def _clean_up_methods (self, vtk_obj):
"Merge dir and str methods. Finish up."
debug ("VtkPrintMethodParser:: _clean_up_methods()")
for meth_list in ((self.dir_toggle_meths, self.toggle_meths),\
(self.dir_get_set_meths, self.get_set_meths),\
(self.dir_get_meths, self.get_meths)):
for method in meth_list[0]:
try:
meth_list[1].index (method)
except ValueError:
meth_list[1].append (method)
# Remove all get_set methods that are already in toggle_meths
# This case can happen if the str produces no "On/Off" but
# dir does and str produces a get_set instead.
for method in self.toggle_meths:
try:
self.get_set_meths.remove (method[:-2])
except ValueError:
pass
self.toggle_meths.sort ()
self.state_meths.sort ()
self.get_set_meths.sort ()
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
|