From 56824fba549fedaa73052253376107bb5e56d070 Mon Sep 17 00:00:00 2001 From: sebd <> Date: Tue, 10 May 2005 11:57:32 +0000 Subject: [PATCH] Taken "class name to python class" code from convertNodesToPythonClass and created convertStringToPythonClass. So that code can be factorized. --- src/core/stations.py | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/src/core/stations.py b/src/core/stations.py index cc22775..b00ce23 100644 --- a/src/core/stations.py +++ b/src/core/stations.py @@ -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