PyXR

C:\Python24\Lib\site-packages\pyxr \ lexicalTree.py



0001 # Copyright 2003, Grant T. Olson, see License.txt for details
0002 import htmlCrawler, sys, os
0003 from misc import libDirectory, useLibraryReference
0004 
0005 class lexicalTree:
0006     """
0007     stores a tree of scopes.
0008     each frame holds a dict of memberNames that point to hyperlinks of the code
0009     """    
0010     def __init__(self, name, ref=None, parent=None, mapBuiltins=None):
0011         self.name = name
0012         self.ref = ref
0013         self.parent = parent
0014         self.members = {}
0015         if mapBuiltins and useLibraryReference:
0016             for member in builtins.members.keys():
0017                 self.members[member] = builtins.members[member]
0018         
0019     def getUrl(self):
0020         if self.ref != None:
0021             if self.ref.count("#") > 0:
0022                 return self.ref
0023             else:
0024                 return self.ref + "#"
0025         elif not self.parent:
0026             return self.name + ":"
0027         else:
0028             return self.parent.getUrl() + self.name + ":"
0029 
0030     def getName(self):
0031         if self.ref != None:
0032             return ""
0033         elif not self.parent:
0034             return self.name + ":"
0035         else:
0036             return self.parent.getName() + self.name + ":"
0037         
0038     def addMember(self, name, ref=None):
0039         tmpMember = lexicalTree(name, ref, self)
0040         self.members[name] = tmpMember
0041         return tmpMember
0042     
0043     def findMember(self, name):
0044         if self.members.has_key(name):
0045             return self.members[name]
0046         elif not self.parent:
0047             return None
0048         else:
0049             return self.parent.findMember(name)
0050 
0051     def getMemberUrl(self, name):
0052         tmpMember = self.findMember(name)
0053         if tmpMember:
0054             if hasattr(tmpMember, "getUrl"):
0055                 return tmpMember.getUrl()
0056             else:
0057                 return tmpMember
0058 
0059     def getScopeList(self, topOfList=None):
0060         retVal = []
0061         if self is topOfList:
0062             return retVal
0063         
0064         if self.parent: retVal = self.parent.getScopeList(topOfList)
0065         if self.name: retVal.append(self.name)
0066         return retVal
0067     
0068     def findInstanceMember(self, name):
0069         """ Quick attempt to find a member for a classes method """
0070         return None
0071 
0072 
0073 
0074 libTree = None
0075 builtins = None
0076 
0077 def libHandler(scope, url):
0078     currentNode = libTree
0079     for subscope in scope:
0080         if currentNode.members.has_key(subscope):
0081             currentNode = currentNode.findMember(subscope)
0082         else:
0083             currentNode = currentNode.addMember(subscope)
0084     if not currentNode.ref: #only save first occurance
0085         currentNode.ref = url
0086 
0087 def getLibraryReference(moduleName):
0088     return libTree.findMember(moduleName)
0089 
0090 if (not libTree) and useLibraryReference:
0091     print "Building Library Reference Indexes (takes a minute or two)"
0092     libTree = lexicalTree("libJunk")
0093     f = file(os.path.join(libDirectory, "lib.html")).read()
0094     x = htmlCrawler.libParser(libHandler)
0095     x.feed(f)
0096     builtins = libTree.findMember("__builtins__")
0097 

Generated by PyXR 0.9.4
SourceForge.net Logo