vtkMethodParser.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. """
  2. This python module provides functionality to parse the methods of a
  3. VTK object.
  4. Created by Prabhu Ramachandran. Committed in Apr, 2002.
  5. """
  6. import string, re, sys
  7. import types
  8. # set this to 1 if you want to see debugging messages - very useful if
  9. # you have problems
  10. DEBUG=0
  11. def debug (msg):
  12. if DEBUG:
  13. print(msg)
  14. class VtkDirMethodParser:
  15. """ Parses the methods from dir(vtk_obj). """
  16. def initialize_methods (self, vtk_obj):
  17. debug ("VtkDirMethodParser:: initialize_methods ()")
  18. self.methods = dir (vtk_obj)[:]
  19. # stores the <blah>On methods
  20. self.toggle_meths = []
  21. # stores the Set<blah>To<blah> methods
  22. self.state_meths = []
  23. # stores the methods that have a Get<blah> and Set<blah>
  24. # only the <blah> is stored
  25. self.get_set_meths = []
  26. # pure get methods
  27. self.get_meths = []
  28. self.state_patn = re.compile ("To[A-Z0-9]")
  29. def parse_methods (self, vtk_obj):
  30. debug ("VtkDirMethodParser:: parse_methods()")
  31. self.initialize_methods (vtk_obj)
  32. debug ("VtkDirMethodParser:: parse_methods() - initialized methods")
  33. for method in self.methods[:]:
  34. # finding all the methods that set the state.
  35. if method[:3].find("Set") >= 0 and \
  36. self.state_patn.search (method) is not None :
  37. try:
  38. eval ("vtk_obj.Get%s"%method[3:])
  39. except AttributeError:
  40. self.state_meths.append (method)
  41. self.methods.remove (method)
  42. # finding all the On/Off toggle methods
  43. elif method[-2:].find("On") >= 0:
  44. try:
  45. self.methods.index ("%sOff"%method[:-2])
  46. except ValueError:
  47. pass
  48. else:
  49. self.toggle_meths.append (method)
  50. self.methods.remove (method)
  51. self.methods.remove ("%sOff"%method[:-2])
  52. # finding the Get/Set methods.
  53. elif method[:3].find("Get") == 0:
  54. set_m = "Set"+method[3:]
  55. try:
  56. self.methods.index (set_m)
  57. except ValueError:
  58. pass
  59. else:
  60. self.get_set_meths.append (method[3:])
  61. self.methods.remove (method)
  62. self.methods.remove (set_m)
  63. self.clean_up_methods (vtk_obj)
  64. def clean_up_methods (self, vtk_obj):
  65. self.clean_get_set (vtk_obj)
  66. self.clean_state_methods (vtk_obj)
  67. self.clean_get_methods (vtk_obj)
  68. def clean_get_set (self, vtk_obj):
  69. debug ("VtkDirMethodParser:: clean_get_set()")
  70. # cleaning up the Get/Set methods by removing the toggle funcs.
  71. for method in self.toggle_meths:
  72. try:
  73. self.get_set_meths.remove (method[:-2])
  74. except ValueError:
  75. pass
  76. # cleaning them up by removing any methods that are responsible for
  77. # other vtkObjects
  78. for method in self.get_set_meths[:]:
  79. try:
  80. eval ("vtk_obj.Get%s ().GetClassName ()"%method)
  81. except (TypeError, AttributeError):
  82. pass
  83. else:
  84. self.get_set_meths.remove (method)
  85. continue
  86. try:
  87. val = eval ("vtk_obj.Get%s ()"%method)
  88. except (TypeError, AttributeError):
  89. self.get_set_meths.remove (method)
  90. else:
  91. if val is None:
  92. self.get_set_meths.remove (method)
  93. def clean_state_methods (self, vtk_obj):
  94. debug ("VtkDirMethodParser:: clean_state_methods()")
  95. # Getting the remaining pure GetMethods
  96. for method in self.methods[:]:
  97. if method[:3].find("Get") == 0:
  98. self.get_meths.append (method)
  99. self.methods.remove (method)
  100. # Grouping similar state methods
  101. if len (self.state_meths) != 0:
  102. tmp = self.state_meths[:]
  103. self.state_meths = []
  104. state_group = [tmp[0]]
  105. end = self.state_patn.search (tmp[0]).start ()
  106. # stores the method type common to all similar methods
  107. m = tmp[0][3:end]
  108. for i in range (1, len (tmp)):
  109. if tmp[i].find(m) >= 0:
  110. state_group.append (tmp[i])
  111. else:
  112. self.state_meths.append (state_group)
  113. state_group = [tmp[i]]
  114. end = self.state_patn.search (tmp[i]).start ()
  115. m = tmp[i][3:end]
  116. try: # remove the corresponding set method in get_set
  117. val = self.get_set_meths.index (m)
  118. except ValueError:
  119. pass
  120. else:
  121. del self.get_set_meths[val]
  122. #self.get_meths.append ("Get"+m)
  123. clamp_m = "Get" + m + "MinValue"
  124. try: # remove the GetNameMax/MinValue in get_meths
  125. val = self.get_meths.index (clamp_m)
  126. except ValueError:
  127. pass
  128. else:
  129. del self.get_meths[val]
  130. val = self.get_meths.index ("Get" + m + "MaxValue")
  131. del self.get_meths[val]
  132. if len (state_group) > 0:
  133. self.state_meths.append (state_group)
  134. def clean_get_methods (self, vtk_obj):
  135. debug ("VtkDirMethodParser:: clean_get_methods()")
  136. for method in self.get_meths[:]:
  137. debug (method)
  138. try:
  139. res = eval ("vtk_obj.%s ()"%method)
  140. except (TypeError, AttributeError):
  141. self.get_meths.remove (method)
  142. continue
  143. else:
  144. try:
  145. eval ("vtk_obj.%s ().GetClassName ()"%method)
  146. except AttributeError:
  147. pass
  148. else:
  149. self.get_meths.remove (method)
  150. continue
  151. if method[-8:].find("MaxValue") > -1:
  152. self.get_meths.remove( method)
  153. elif method[-8:].find("MinValue") > -1:
  154. self.get_meths.remove( method)
  155. self.get_meths.sort ()
  156. def toggle_methods (self):
  157. return self.toggle_meths
  158. def state_methods (self):
  159. return self.state_meths
  160. def get_set_methods (self):
  161. return self.get_set_meths
  162. def get_methods (self):
  163. return self.get_meths
  164. class VtkPrintMethodParser:
  165. """ This class finds the methods for a given vtkObject. It uses
  166. the output from vtkObject->Print() (or in Python str(vtkObject))
  167. and output from the VtkDirMethodParser to obtain the methods. """
  168. def parse_methods (self, vtk_obj):
  169. "Parse for the methods."
  170. debug ("VtkPrintMethodParser:: parse_methods()")
  171. if self._initialize_methods (vtk_obj):
  172. # if David Gobbi's improvements are in this version of VTK
  173. # then I need to go no further.
  174. return
  175. for method in self.methods[:]:
  176. # removing methods that have nothing to the right of the ':'
  177. if (method[1] == '') or \
  178. (method[1].find("none") > -1) :
  179. self.methods.remove (method)
  180. for method in self.methods:
  181. # toggle methods are first identified
  182. if (method[1] == "On") or (method[1] == "Off"):
  183. try:
  184. val = eval ("vtk_obj.Get%s ()"%method[0])
  185. if val == 1:
  186. eval ("vtk_obj.%sOn ()"%method[0])
  187. elif val == 0:
  188. eval ("vtk_obj.%sOff ()"%method[0])
  189. except AttributeError:
  190. pass
  191. else:
  192. self.toggle_meths.append (method[0]+"On")
  193. else: # see if it is get_set or get or a state method
  194. found = 0
  195. # checking if it is a state func.
  196. # figure out the long names from the dir_state_meths
  197. for sms in self.dir_state_meths[:]:
  198. if sms[0].find(method[0]) >= 0:
  199. self.state_meths.append (sms)
  200. self.dir_state_meths.remove (sms)
  201. found = 1
  202. if found:
  203. self.get_meths.append ("Get"+method[0])
  204. try:
  205. t = eval ("vtk_obj.Get%sAsString ()"%method[0])
  206. except AttributeError:
  207. pass
  208. else:
  209. self.get_meths.append ("Get"+method[0]+"AsString")
  210. else:
  211. # the long name is inherited or it is not a state method
  212. try:
  213. t = eval ("vtk_obj.Get%s ().GetClassName ()"%
  214. method[0])
  215. except AttributeError:
  216. pass
  217. else:
  218. continue
  219. val = 0
  220. try:
  221. val = eval ("vtk_obj.Get%s ()"%method[0])
  222. except (TypeError, AttributeError):
  223. pass
  224. else:
  225. try:
  226. f = eval ("vtk_obj.Set%s"%method[0])
  227. except AttributeError:
  228. self.get_meths.append ("Get"+method[0])
  229. else:
  230. try:
  231. f(*val)
  232. except TypeError:
  233. try:
  234. f(*(val, ))
  235. except TypeError:
  236. self.get_meths.append ("Get"+method[0])
  237. else:
  238. self.get_set_meths.append (method[0])
  239. else:
  240. self.get_set_meths.append (method[0])
  241. self._clean_up_methods (vtk_obj)
  242. def _get_str_obj (self, vtk_obj):
  243. debug ("VtkPrintMethodParser:: _get_str_obj()")
  244. self.methods = str (vtk_obj)
  245. self.methods = self.methods.split ("\n")
  246. del self.methods[0]
  247. def _initialize_methods (self, vtk_obj):
  248. "Do the basic parsing and setting up"
  249. debug ("VtkPrintMethodParser:: _initialize_methods()")
  250. dir_p = VtkDirMethodParser ()
  251. dir_p.parse_methods (vtk_obj)
  252. # testing if this version of vtk has David Gobbi's cool
  253. # stuff. If it does then no need to do other things.
  254. try:
  255. junk = vtk_obj.__class__
  256. except AttributeError:
  257. pass
  258. else:
  259. self.toggle_meths = dir_p.toggle_methods ()
  260. self.state_meths = dir_p.state_methods ()
  261. self.get_set_meths = dir_p.get_set_methods ()
  262. self.get_meths = dir_p.get_methods ()
  263. return 1
  264. self.dir_toggle_meths = dir_p.toggle_methods ()
  265. self.dir_state_meths = dir_p.state_methods ()
  266. self.dir_get_set_meths = dir_p.get_set_methods ()
  267. self.dir_get_meths = dir_p.get_methods ()
  268. self._get_str_obj (vtk_obj)
  269. patn = re.compile (" \S")
  270. for method in self.methods[:]:
  271. if not patn.match (method):
  272. self.methods.remove (method)
  273. for method in self.methods[:]:
  274. if method.find(":") == -1:
  275. self.methods.remove (method)
  276. for i in range (0, len (self.methods)):
  277. strng = self.methods[i]
  278. strng = strng.replace (" ", "")
  279. self.methods[i] = strng.split (":")
  280. self.toggle_meths = []
  281. self.state_meths = []
  282. self.get_set_meths = []
  283. self.get_meths = []
  284. return 0
  285. def _clean_up_methods (self, vtk_obj):
  286. "Merge dir and str methods. Finish up."
  287. debug ("VtkPrintMethodParser:: _clean_up_methods()")
  288. for meth_list in ((self.dir_toggle_meths, self.toggle_meths),\
  289. (self.dir_get_set_meths, self.get_set_meths),\
  290. (self.dir_get_meths, self.get_meths)):
  291. for method in meth_list[0]:
  292. try:
  293. meth_list[1].index (method)
  294. except ValueError:
  295. meth_list[1].append (method)
  296. # Remove all get_set methods that are already in toggle_meths
  297. # This case can happen if the str produces no "On/Off" but
  298. # dir does and str produces a get_set instead.
  299. for method in self.toggle_meths:
  300. try:
  301. self.get_set_meths.remove (method[:-2])
  302. except ValueError:
  303. pass
  304. self.toggle_meths.sort ()
  305. self.state_meths.sort ()
  306. self.get_set_meths.sort ()
  307. self.get_meths.sort ()
  308. def toggle_methods (self):
  309. return self.toggle_meths
  310. def state_methods (self):
  311. return self.state_meths
  312. def get_set_methods (self):
  313. return self.get_set_meths
  314. def get_methods (self):
  315. return self.get_meths