Taken "class name to python class" code from convertNodesToPythonClass and created convertStringToPythonClass.

So that code can be factorized.
This commit is contained in:
sebd 2005-05-10 11:57:32 +00:00
parent 1f664b91ab
commit 56824fba54
1 changed files with 19 additions and 11 deletions

View File

@ -249,20 +249,12 @@ class AbstractStation(object):
raise KeyError(xpath)
return default
classPath = nodes[0].content.strip()
i = classPath.rfind(".")
assert i >= 0
modulePath = classPath[:i]
component = __import__(modulePath)
componentNames = classPath.split(".")
try:
for componentName in componentNames[1:]:
component = getattr(component, componentName)
except AttributeError:
logs.info('Unknown Python class = "%s"' % classPath)
clas = self.convertStringToPythonClass(classPath)
if clas is None:
if default == "none":
raise KeyError(xpath)
return default
return component
return clas
def convertNodesToString(self, nodes, nodesOwner, xpath, default = "none"):
if not nodes:
@ -305,6 +297,22 @@ class AbstractStation(object):
else:
return self.convertPathToAbsolute(location)
def convertStringToPythonClass(self, classPath):
""" Returns the Python class named in classPath.
"""
i = classPath.rfind(".")
assert i >= 0
modulePath = classPath[:i]
component = __import__(modulePath)
componentNames = classPath.split(".")
try:
for componentName in componentNames[1:]:
component = getattr(component, componentName)
except AttributeError:
logs.info('Unknown Python class = "%s"' % classPath)
return None
return component
def doHttpAccess(self):
return self