Commits

Anonymous committed 0f247f4

Now using Jesir's iterative implementation of getObjFromPath

Comments (0)

Files changed (1)

 
 def getObjFromPath(objPathList, objCache):
     """ 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
+         return the object (or None if it doesn't exist).
+         Also keep a cache for maximum efficiency
     """
-    if not objPathList: return cpg
-    cacheKey = tuple(objPathList)
-    if cacheKey in objCache: return objCache[cacheKey]
-    previousObj = getObjFromPath(objPathList[:-1], objCache)
-    obj = getattr(previousObj, objPathList[-1], None)
-    objCache[cacheKey] = obj
-    # print "*** GETTING:", objPathList, repr(obj)
-    return obj
+    # Let cpg be the first valid object.
+    validObjects = ["cpg"]
+     
+    # Scan the objPathList in order from left to right
+    for index, obj in enumerate(objPathList):
+        # currentObjStr holds something like 'cpg.root.something.else'
+        currentObjStr = ".".join(validObjects)
+
+        #---------------
+        #   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():
     # Traverse path:
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.