Utilities/ModuleParser.py

branch
Py2 comp.
changeset 3057
10516539f238
parent 3056
9986ec0e559a
parent 2997
7f0ef975da9e
child 3058
0a02c433f52d
--- a/Utilities/ModuleParser.py	Tue Oct 15 22:03:54 2013 +0200
+++ b/Utilities/ModuleParser.py	Fri Oct 18 23:00:41 2013 +0200
@@ -27,8 +27,8 @@
 from functools import reduce
 import Preferences
 
-__all__ = ["Module", "Class", "Function", "Attribute", "RbModule", "readModule",
-           "getTypeFromTypeName"]
+__all__ = ["Module", "Class", "Function", "Attribute", "RbModule",
+           "readModule", "getTypeFromTypeName"]
 
 TABWIDTH = 4
 
@@ -125,7 +125,8 @@
     )
 
 |   (?P<Method>
-        (^ [ \t]* @ (?: PyQt4 \. )? (?: QtCore \. )? (?: pyqtSignature | pyqtSlot )
+        (^ [ \t]* @ (?: PyQt4 \. )? (?: QtCore \. )?
+            (?: pyqtSignature | pyqtSlot )
             [ \t]* \(
                 (?P<MethodPyQtSignature> [^)]* )
             \) \s*
@@ -217,7 +218,8 @@
         (?P<MethodIndent> [ \t]* )
         def [ \t]+
         (?:
-            (?P<MethodName2> [a-zA-Z0-9_]+ (?: \. | :: ) [a-zA-Z_] [a-zA-Z0-9_?!=]* )
+            (?P<MethodName2> [a-zA-Z0-9_]+ (?: \. | :: )
+            [a-zA-Z_] [a-zA-Z0-9_?!=]* )
         |
             (?P<MethodName> [a-zA-Z_] [a-zA-Z0-9_?!=]* )
         |
@@ -270,11 +272,13 @@
         (?:
             (?P<AccessControlType> private | public | protected ) [^_]
         |
-            (?P<AccessControlType2> private_class_method | public_class_method )
+            (?P<AccessControlType2>
+            private_class_method | public_class_method )
         )
         \(?
         [ \t]*
-        (?P<AccessControlList> (?: : [a-zA-Z0-9_]+ , \s* )* (?: : [a-zA-Z0-9_]+ )+ )?
+        (?P<AccessControlList> (?: : [a-zA-Z0-9_]+ , \s* )*
+        (?: : [a-zA-Z0-9_]+ )+ )?
         [ \t]*
         \)?
     )
@@ -293,7 +297,8 @@
         (?P<AttrType> (?: _accessor | _reader | _writer ) )?
         \(?
         [ \t]*
-        (?P<AttrList> (?: : [a-zA-Z0-9_]+ , \s* )* (?: : [a-zA-Z0-9_]+ | true | false )+ )
+        (?P<AttrList> (?: : [a-zA-Z0-9_]+ , \s* )*
+        (?: : [a-zA-Z0-9_]+ | true | false )+ )
         [ \t]*
         \)?
     )
@@ -377,9 +382,9 @@
 
 
 class Module(object):
-    '''
+    """
     Class to represent a Python module.
-    '''
+    """
     def __init__(self, name, file=None, type=None):
         """
         Constructor
@@ -473,7 +478,8 @@
     
     def scan(self, src):
         """
-        Public method to scan the source text and retrieve the relevant information.
+        Public method to scan the source text and retrieve the relevant
+        information.
         
         @param src the source text to be scanned (string)
         """
@@ -497,8 +503,8 @@
     
     def __py_scan(self, src):
         """
-        Private method to scan the source text of a Python module and retrieve the
-        relevant information.
+        Private method to scan the source text of a Python module and retrieve
+        the relevant information.
         
         @param src the source text to be scanned (string)
         """
@@ -553,8 +559,10 @@
                 if conditionalsstack:
                     if thisindent > conditionalsstack[-1]:
                         if not deltaindentcalculated:
-                            deltastack.append(thisindent - conditionalsstack[-1])
-                            deltaindent = reduce(lambda x, y:  x + y, deltastack)
+                            deltastack.append(
+                                thisindent - conditionalsstack[-1])
+                            deltaindent = reduce(
+                                lambda x, y:  x + y, deltastack)
                             deltaindentcalculated = 1
                         thisindent -= deltaindent
                     else:
@@ -583,15 +591,17 @@
                         
                         if isinstance(cur_class, Class):
                             # it's a class method
-                            f = Function(None, meth_name, None, lineno,
-                                         meth_sig, meth_pyqtSig, modifierType=modifier)
+                            f = Function(
+                                None, meth_name, None, lineno,
+                                meth_sig, meth_pyqtSig, modifierType=modifier)
                             self.__py_setVisibility(f)
                             cur_class.addMethod(meth_name, f)
                             break
                     else:
                         # it's a nested function of a module function
-                        f = Function(self.name, meth_name, self.file, lineno,
-                                     meth_sig, meth_pyqtSig, modifierType=modifier)
+                        f = Function(
+                            self.name, meth_name, self.file, lineno,
+                            meth_sig, meth_pyqtSig, modifierType=modifier)
                         self.__py_setVisibility(f)
                         self.addFunction(meth_name, f)
                 else:
@@ -702,7 +712,8 @@
                 while index >= -len(classstack):
                     if classstack[index][0] is not None:
                         attrName = m.group("AttributeName")
-                        attr = Attribute(self.name, attrName, self.file, lineno)
+                        attr = Attribute(
+                            self.name, attrName, self.file, lineno)
                         self.__py_setVisibility(attr)
                         classstack[index][0].addAttribute(attrName, attr)
                         break
@@ -717,8 +728,9 @@
                 last_lineno_pos = start
                 if thisindent == 0:
                     # global variable
-                    attr = Attribute(self.name, variable_name, self.file, lineno,
-                                     isSignal=isSignal)
+                    attr = Attribute(
+                        self.name, variable_name, self.file, lineno,
+                        isSignal=isSignal)
                     self.__py_setVisibility(attr)
                     self.addGlobal(variable_name, attr)
                     if lastGlobalEntry:
@@ -732,16 +744,19 @@
                         else:
                             if classstack[index][0] is not None and \
                                isinstance(classstack[index][0], Class):
-                                attr = Attribute(self.name, variable_name, self.file,
-                                                 lineno, isSignal=isSignal)
+                                attr = Attribute(
+                                    self.name, variable_name, self.file,
+                                    lineno, isSignal=isSignal)
                                 self.__py_setVisibility(attr)
-                                classstack[index][0].addGlobal(variable_name, attr)
+                                classstack[index][0].addGlobal(
+                                    variable_name, attr)
                             break
 
             elif m.start("Import") >= 0:
                 # import module
-                names = [n.strip() for n in "".join(
-                    m.group("ImportList").splitlines()).replace("\\", "").split(',')]
+                names = [n.strip() for n in 
+                         "".join(m.group("ImportList").splitlines())
+                         .replace("\\", "").split(',')]
                 for name in names:
                     if not name in self.imports:
                         self.imports.append(name)
@@ -749,8 +764,9 @@
             elif m.start("ImportFrom") >= 0:
                 # from module import stuff
                 mod = m.group("ImportFromPath")
-                names = [n.strip() for n in "".join(
-                    m.group("ImportFromList").splitlines()).replace("\\", "").split(',')]
+                names = [n.strip() for n in 
+                         "".join(m.group("ImportFromList").splitlines())
+                         .replace("\\", "").split(',')]
                 if mod not in self.from_imports:
                     self.from_imports[mod] = []
                 self.from_imports[mod].extend(names)
@@ -773,8 +789,8 @@
     
     def __rb_scan(self, src):
         """
-        Private method to scan the source text of a Python module and retrieve the
-        relevant information.
+        Private method to scan the source text of a Python module and retrieve
+        the relevant information.
         
         @param src the source text to be scanned (string)
         """
@@ -810,7 +826,8 @@
                 while classstack and \
                       classstack[-1][1] >= thisindent:
                     if classstack[-1][0] is not None and \
-                       isinstance(classstack[-1][0], (Class, Function, RbModule)):
+                       isinstance(classstack[-1][0],
+                                  (Class, Function, RbModule)):
                         # record the end line of this class, function or module
                         classstack[-1][0].setEndLine(lineno - 1)
                     del classstack[-1]
@@ -835,7 +852,8 @@
                             break
                     else:
                         # it's a nested function of a module function
-                        f = Function(self.name, meth_name, self.file, lineno, meth_sig)
+                        f = Function(
+                            self.name, meth_name, self.file, lineno, meth_sig)
                         self.addFunction(meth_name, f)
                     # set access control
                     if acstack:
@@ -848,7 +866,8 @@
                             f.setPublic()
                 else:
                     # it's a function
-                    f = Function(self.name, meth_name, self.file, lineno, meth_sig)
+                    f = Function(
+                        self.name, meth_name, self.file, lineno, meth_sig)
                     self.addFunction(meth_name, f)
                 if not classstack:
                     if lastGlobalEntry:
@@ -885,7 +904,8 @@
                 while classstack and \
                       classstack[-1][1] >= thisindent:
                     if classstack[-1][0] is not None and \
-                       isinstance(classstack[-1][0], (Class, Function, RbModule)):
+                       isinstance(classstack[-1][0],
+                                  (Class, Function, RbModule)):
                         # record the end line of this class, function or module
                         classstack[-1][0].setEndLine(lineno - 1)
                     del classstack[-1]
@@ -933,7 +953,8 @@
                 while classstack and \
                       classstack[-1][1] >= thisindent:
                     if classstack[-1][0] is not None and \
-                       isinstance(classstack[-1][0], (Class, Function, RbModule)):
+                       isinstance(classstack[-1][0],
+                                  (Class, Function, RbModule)):
                         # record the end line of this class, function or module
                         classstack[-1][0].setEndLine(lineno - 1)
                     del classstack[-1]
@@ -964,8 +985,9 @@
                     index = -1
                     while index >= -len(acstack):
                         if acstack[index][1] < indent:
-                            actype = m.group("AccessControlType") or \
-                                     m.group("AccessControlType2").split('_')[0]
+                            actype = \
+                                m.group("AccessControlType") or \
+                                m.group("AccessControlType2").split('_')[0]
                             acstack[index][0] = actype.lower()
                             break
                         else:
@@ -977,11 +999,13 @@
                            not isinstance(classstack[index][0], Function) and \
                            not classstack[index][1] >= indent:
                             parent = classstack[index][0]
-                            actype = m.group("AccessControlType") or \
-                                     m.group("AccessControlType2").split('_')[0]
+                            actype = \
+                                m.group("AccessControlType") or \
+                                m.group("AccessControlType2").split('_')[0]
                             actype = actype.lower()
                             for name in aclist.split(","):
-                                name = name.strip()[1:]     # get rid of leading ':'
+                                # get rid of leading ':'
+                                name = name.strip()[1:]
                                 acmeth = parent.getMethod(name)
                                 if acmeth is None:
                                     continue
@@ -1004,7 +1028,8 @@
                        not isinstance(classstack[index][0], Function) and \
                        not classstack[index][1] >= indent:
                         attrName = m.group("AttributeName")
-                        attr = Attribute(self.name, attrName, self.file, lineno)
+                        attr = Attribute(
+                            self.name, attrName, self.file, lineno)
                         if attrName.startswith("@@") or attrName[0].isupper():
                             classstack[index][0].addGlobal(attrName, attr)
                         else:
@@ -1015,7 +1040,8 @@
                 else:
                     attrName = m.group("AttributeName")
                     if attrName[0] != "@":
-                        attr = Attribute(self.name, attrName, self.file, lineno)
+                        attr = Attribute(
+                            self.name, attrName, self.file, lineno)
                         self.addGlobal(attrName, attr)
                     if lastGlobalEntry:
                         lastGlobalEntry.setEndLine(lineno - 1)
@@ -1034,10 +1060,12 @@
                             nv = m.group("AttrList").split(",")
                             if not nv:
                                 break
-                            name = nv[0].strip()[1:]    # get rid of leading ':'
+                            # get rid of leading ':'
+                            name = nv[0].strip()[1:]
                             attr = parent.getAttribute("@" + name) or \
                                    parent.getAttribute("@@" + name) or \
-                                   Attribute(self.name, "@" + name, self.file, lineno)
+                                   Attribute(
+                                    self.name, "@" + name, self.file, lineno)
                             if len(nv) == 1 or nv[1].strip() == "false":
                                 attr.setProtected()
                             elif nv[1].strip() == "true":
@@ -1046,13 +1074,17 @@
                         else:
                             access = m.group("AttrType")
                             for name in m.group("AttrList").split(","):
-                                name = name.strip()[1:]     # get rid of leading ':'
+                                # get rid of leading ':'
+                                name = name.strip()[1:]
                                 attr = parent.getAttribute("@" + name) or \
                                        parent.getAttribute("@@" + name) or \
-                                       Attribute(self.name, "@" + name, self.file, lineno)
+                                       Attribute(
+                                        self.name, "@" + name, self.file,
+                                        lineno)
                                 if access == "_accessor":
                                     attr.setPublic()
-                                elif access == "_reader" or access == "_writer":
+                                elif access == "_reader" or \
+                                        access == "_writer":
                                     if attr.isPrivate():
                                         attr.setProtected()
                                     elif attr.isProtected():
@@ -1085,7 +1117,8 @@
     
     def createHierarchy(self):
         """
-        Public method to build the inheritance hierarchy for all classes of this module.
+        Public method to build the inheritance hierarchy for all classes of
+        this module.
         
         @return A dictionary with inheritance hierarchies.
         """
@@ -1118,7 +1151,8 @@
                     rv[cls] = {}
                     exhausted = path + [cls]
                     exhausted.reverse()
-                    self.addPathToHierarchy(exhausted, result, self.addPathToHierarchy)
+                    self.addPathToHierarchy(
+                        exhausted, result, self.addPathToHierarchy)
                 else:
                     rv[cls] = self.assembleHierarchy(cls,
                                    classes, path + [cls], result)
@@ -1181,9 +1215,9 @@
 
 
 class Class(VisibilityBase):
-    '''
+    """
     Class to represent a Python class.
-    '''
+    """
     def __init__(self, module, name, super, file, lineno):
         """
         Constructor
@@ -1284,9 +1318,9 @@
 
 
 class RbModule(Class):
-    '''
+    """
     Class to represent a Ruby module.
-    '''
+    """
     def __init__(self, module, name, file, lineno):
         """
         Constructor
@@ -1310,15 +1344,15 @@
 
 
 class Function(VisibilityBase):
-    '''
+    """
     Class to represent a Python function or method.
-    '''
+    """
     General = 0
     Static = 1
     Class = 2
     
-    def __init__(self, module, name, file, lineno, signature='', pyqtSignature=None,
-                 modifierType=General):
+    def __init__(self, module, name, file, lineno, signature='',
+                 pyqtSignature=None, modifierType=General):
         """
         Constructor
         
@@ -1360,9 +1394,9 @@
 
 
 class Attribute(VisibilityBase):
-    '''
+    """
     Class to represent a Python function or method.
-    '''
+    """
     def __init__(self, module, name, file, lineno, isSignal=False):
         """
         Constructor
@@ -1385,7 +1419,8 @@
         """
         Public method to add another assignment line number.
         
-        @param lineno linenumber of the additional attribute assignment (integer)
+        @param lineno linenumber of the additional attribute assignment
+            (integer)
         """
         if lineno not in self.linenos:
             self.linenos.append(lineno)
@@ -1393,7 +1428,7 @@
 
 def readModule(module, path=[], inpackage=False, basename="",
                extensions=None, caching=True):
-    '''
+    """
     Function to read a module file and parse it.
 
     The module is searched in path and sys.path, read and parsed.
@@ -1412,7 +1447,7 @@
         cached (boolean)
     @return reference to a Module object containing the parsed
         module information (Module)
-    '''
+    """
     global _modules
     
     if extensions is None:
@@ -1464,12 +1499,14 @@
     f = None
     if inpackage:
         try:
-            f, file, (suff, mode, type) = find_module(module, path, _extensions)
+            f, file, (suff, mode, type) = find_module(
+                module, path, _extensions)
         except ImportError:
             f = None
     if f is None:
         fullpath = list(path) + sys.path
-        f, file, (suff, mode, type) = find_module(module, fullpath, _extensions)
+        f, file, (suff, mode, type) = find_module(
+            module, fullpath, _extensions)
     if f:
         f.close()
     if type not in SUPPORTED_TYPES:
@@ -1522,12 +1559,15 @@
                     pathname = os.path.join(p, name)
                     if ext == '.ptl':
                         # Quixote page template
-                        return (open(pathname), pathname, ('.ptl', 'r', PTL_SOURCE))
+                        return (open(pathname), pathname,
+                                ('.ptl', 'r', PTL_SOURCE))
                     elif ext == '.rb':
                         # Ruby source file
-                        return (open(pathname), pathname, ('.rb', 'r', RB_SOURCE))
+                        return (open(pathname), pathname,
+                                ('.rb', 'r', RB_SOURCE))
                     else:
-                        return (open(pathname), pathname, (ext, 'r', imp.PY_SOURCE))
+                        return (open(pathname), pathname,
+                                (ext, 'r', imp.PY_SOURCE))
             raise ImportError
     
     # standard Python module file
@@ -1564,7 +1604,8 @@
             modname = module.replace(os.sep, '.')
         else:
             modname = os.path.basename(module)
-        if modname.lower().endswith(".ptl") or modname.lower().endswith(".pyw"):
+        if modname.lower().endswith(".ptl") or \
+                modname.lower().endswith(".pyw"):
             modname = modname[:-4]
         elif modname.lower().endswith(".rb"):
             modname = modname[:-3]

eric ide

mercurial