Commits

Anonymous committed ecdc624

Changed getObjFromPath with cleaner implementation

Comments (0)

Files changed (1)

     s += '%s'%time.time()
     return sha.sha(s).hexdigest()
 
-def getObjFromPath(objPathList, objCache):
+def getObjFromPath(objPathList):
     """ For a given objectPathList (like ['root', 'a', 'b', 'index']),
          return the object (or None if it doesn't exist).
-         Also keep a cache for maximum efficiency
     """
-    # Let cpg be the first valid object.
-    validObjects = ["cpg"]
-    
-    # Scan the objPathList in order from left to right
-    for index, obj in enumerate(objPathList):
+    root = cpg
+    for objname in objPathList:
         # maps virtual filenames to Python identifiers (substitutes '.' for '_')
-        obj = obj.replace('.', '_')
+        objname = objname.replace('.', '_')
+        root = getattr(root, objname, None)
+        if root is None:
+            return None
 
-        # currentObjStr holds something like 'cpg.root.something.else'
-        currentObjStr = ".".join(validObjects)
+    return root
 
-        #---------------
-        #   Cache check
-        #---------------
-        # Generate a cacheKey from the first 'index' elements of objPathList
-        cacheKey = tuple(objPathList[:index+1])
-        # Is this cacheKey in the objCache?
-        if cacheKey in objCache: 
-            # And is its value not None?
-            if objCache[cacheKey]:
-                # Yes, then add it to the list of validObjects
-                validObjects.append(obj)
-                # OK, go to the next iteration
-                continue
-            # Its value is None, so we stop
-            # (This means it is not a valid object)
-            break
-        
-        #-----------------
-        # Attribute check
-        #-----------------
-        if getattr(eval(currentObjStr), obj, None):
-            #  obj is a valid attribute of the current object
-            validObjects.append(obj)
-            #  Store it in the cache
-            objCache[cacheKey] = eval(".".join(validObjects))
-        else:
-            # obj is not a valid attribute
-            # Store None in the cache
-            objCache[cacheKey] = None
-            # Stop, we won't process the remaining objPathList
-            break
-
-    # Return the last cached object (even if its None)
-    return objCache[cacheKey]
 
 def mapPathToObject(path = None):
     # Traverse path:
     while objectPathList:
         if isFirst or isSecond:
             # Only try this for a.b.index() or a.b()
-            candidate = getObjFromPath(objectPathList, objCache)
+            candidate = getObjFromPath(objectPathList)
             if callable(candidate) and getattr(candidate, 'exposed', False):
                 foundIt = True
                 break
         if (not isFirst) or (not path):
             virtualPathList.insert(0, lastObj)
             objectPathList.append('default')
-            candidate = getObjFromPath(objectPathList, objCache)
+            candidate = getObjFromPath(objectPathList)
             if callable(candidate) and getattr(candidate, 'exposed', False):
                 foundIt = True
                 isDefault = True