Taken "class name to python class" code from convertNodesToPythonClass and created convertStringToPythonClass.
So that code can be factorized.
This commit is contained in:
parent
1f664b91ab
commit
56824fba54
|
@ -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
|
||||
|
||||
|
|
Reference in New Issue