Source

pycode / pycode / stubgen.py

Diff from to

File pycode/stubgen.py

     hypot(x, y)
     factorial(x) -> Integral
     foo(arg=None) -> str
+    QX11Info.isCompositingManagerRunning() -> bool
+    qDrawWinPanel(QPainter, int, int, int, int, QPalette, bool sunken=False, QBrush fill=None)
+    qDrawWinPanel(QPainter, QRect, QPalette, bool sunken=False, QBrush fill=None)
+    qDrawShadeRect(QPainter, int, int, int, int, QPalette, bool sunken=False, int lineWidth=1, int midLineWidth=0, QBrush fill=None)
+    qDrawShadeRect(QPainter, QRect, QPalette, bool sunken=False, int lineWidth=1, int midLineWidth=0, QBrush fill=None)
     """
     
+    example2 = """
+    :param foo: int argument
+    :param bar:
+    :param baz
+    """
     def tokenize(self, s):
         s = s.strip()
         pos = 0
                     yield "EQUALS", "="
                 elif char == " ":
                     pos += 1 # skip whitespace
+                #  number = re.match("^\d+(\.\d*)?", s[pos:])
                 else:
                     ident = re.match("^([A-Za-z_][A-Za-z_0-9]*)", s[pos:])
                     if ident:
                         yield "UNKNOWN", char
                         pos += 1
 
+                        
+    def parse(self, s):
+        for line in s.splitlines():
+            result = self.parse_line(line)
+            if result:
+                yield result
 
-    def parse(self, s):
+            
+    def parse_line(self, s):
         next_token = self.tokenize(s).next
         args = []
         returns = None
         name = None
-        try:
-            token = next_token()
-        except StopIteration:
-            return
+        token = next_token()
         if token[0] != "IDENT":
             return
         name = token[1]
         token = next_token()
         if token[0] != "LPAREN":
             return
+        token = next_token()
         while 1:
-            try:
-                token = next_token()
-            except StopIteration:
-                return
             if token[0] == "RPAREN":
                 break
             if token[0] == "IDENT":
                 args.append(token[1])
+            token = next_token()
+            if token[0] == "IDENT": 
+                # c-like declaration of type
+                args[-1] = token[1]
+                token = next_token()
+            if token[0] == "EQUALS":
+                token = next_token()
+                token = next_token()
+            elif token[0] == "COMMA":
+                token = next_token()
+            elif token[0] == "COLON":
+                token = next_token()
+                token = next_token()
+            else:
+                assert token[0] == "RPAREN", ") expected, %s found" % repr(token)
+
         try:
             token = next_token()
         except StopIteration:
-            pass
+            token = (None, None)
         if token[0] == "RETURNS":
             try:
                 token = next_token()
                 pass
         return name, args, returns
                 
-                        
+
+
 
 class StubCreator(object):
 
 
 
     def extract_signature_from_doc(self, doc):
+        try:
+            sig = SignatureParser().parse(doc).next()
+            name, args, results = sig
+        except:
+            return "()"
+        self.emit("# real signature not available, using information from __doc__")
+        if self.indention_level > 0:
+            args.insert(0, "self")
+        return "(%s)" % ", ".join(args)
+            
+        
+    def old_extract_signature_from_doc(self, doc):
+        
+        
         lines = [l.strip() for l in (doc or "").splitlines() if l.strip()]
         args = []
         if self.indention_level > 0:
         return "\n".join(self.code)
 
         
+
     
-        
-if __name__ == "__main__":        
-    #s = StubCreator("math")
-    #s = StubCreator("PyQt4.QtGui")
-    #s = StubCreator("_socket")
-    #print s
+def test_sp():
     sp = SignatureParser()
     for line in sp.examples.splitlines():
-        print line.strip(), sp.parse(line)
-            
+        print line.strip()
+        for sig in sp.parse(line):
+            print "\t", sig
+            
+    #print sp.parse_line("sin(x)")
+
+
+def test_sc():
+    #s = StubCreator("math")
+    s = StubCreator("PyQt4.QtGui")
+    #s = StubCreator("_socket")
+    print s
+
+    
+if __name__ == "__main__":        
+    test_sp()