File: XReflector.py

package info (click to toggle)
xdmf 3.0%2Bgit20160803-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 35,384 kB
  • sloc: ansic: 265,382; cpp: 162,889; python: 10,976; f90: 1,378; yacc: 687; fortran: 464; xml: 200; java: 187; lex: 125; makefile: 82; sh: 28
file content (81 lines) | stat: -rw-r--r-- 2,002 bytes parent folder | download | duplicates (2)
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
#!/bin/env python

"""Reflection for XDMF and vtk"""

from Xdmf import *
from vtk import *
from libvtkXdmfPython import *
# from libVTKCommonPython import *
# from libVTKGraphicsPython import *
# from libVTKLocalPython import *

import string
import inspect


class XReflector :

	def __init__( self ) :
		self.Lookup = {}


	def RegisterObject(self, ObjectToRegister, Name ) :
		if (ObjectToRegister != None) and (Name != None) :
			self.Lookup[ Name ] = ObjectToRegister
			return( ObjectToRegister )
		return ( None )
	def CreateObject( self, Type, Name ) :

		try :
			NewObject = eval( Type + '()')
			self.RegisterObject( NewObject, Name )
			if Name != None :
				self.Lookup[ Name ] = NewObject
			return( NewObject )
		except :
			return( None )

	def CallMethod( self, ObjectToCall, Method, Args ) :
		# Methods = inspect.getmembers( XdmfDOM, inspect.ismethod)
		if hasattr( ObjectToCall, Method ) :
			RealArgs = ' '
			if Args != None :
				ArgsToTry = string.split( Args, ',' )
				# print 'ArgsToTry = ' + str(ArgsToTry)
				for Arg in ArgsToTry :
					RealArg = self.NameToObject( Arg )
					# print 'RealArg(' + Arg + ') = ' + str( RealArg )
					if RealArg == None :
						RealArgs += Arg + ','
					else :
						try :
							for item in RealArg :
								RealArgs += str(item) + ','
						except :
							RealArgs += 'self.NameToObject("' + Arg + '")'+ ','
					# print 'RealArgs = ' + RealArgs
			RealArgs = RealArgs[:-1]
			try :
				# print 'Calling ... ObjectToCall.' + Method + '(' + RealArgs + ')'
				Result = eval('ObjectToCall.' + Method + '(' + RealArgs + ')' )
				# print 'Back From Call'
				if Result == None :
					Result = 1
				return( Result )
			except :
				return( None )
		else :
			return None


	def NameToObject( self, Name ) :

		if Name in self.Lookup :
			return( self.Lookup[ Name ] )
		return( None )

	def ObjectToName ( self, Object ) :
		for Name in list(self.Lookup.keys()) :
			if self.Lookup[ Name ] == Object :
				return( Name )
		return( None )