Commits

Anonymous committed aab365b

- added gen_all_protocols.py script, which generate protocols for the
frameworks that PyObjC ships wrappers for. (Except PreferencePanes, as
it doesn't seem to define any protocols.)

- fixed gen_protocols.py so it also handles selectors defined over
multiple lines in the header file.

Comments (0)

Files changed (2)

pyobjc/Scripts/gen_all_protocols.py

+#!/usr/bin/env python
+
+"""Use this script to generate protocol definitions for the frameworks as
+shipped by PyObjC.
+"""
+
+import sys
+import os
+from gen_protocols import genProtocols
+
+script = os.path.abspath(sys.argv[0])
+libdir = os.path.join(os.path.dirname(os.path.dirname(script)), "Lib")
+
+for framework in ["AppKit", "Foundation", "AddressBook", "InterfaceBuilder"]:
+    path = "/System/Library/Frameworks/%s.framework" % framework
+    protfile = file(os.path.join(libdir, framework, "protocols.py"), "w")
+    print "generating protocols for", framework
+    genProtocols(path, protfile)

pyobjc/Scripts/gen_protocols.py

 #!/usr/bin/env python
 
-"""This script will generate Python code that defines (informal) protocols
-byparsing header files from frameworks.
+"""This script/module generate Python code that defines (informal) protocols
+by parsing header files from frameworks. It is used bu gen_all_protocols.py,
+but can also be used separately, in which case you invoke it like this:
 
-Invoke it like this:
     ./gen_protocols.py <full_path_to_a_framework> > <outputfile>.py
 """
 
 argNameRE = re.compile(r"(\w+)\s*")
 
 def getSelector(line):
-    assert line[:2] == "- "
-    line = line[2:]
-    end = line.find(";")
-    assert end > 0
-    line = line[:end]
     types = []
     selParts = []
     m = typeRE.match(line)
         end = header.find("@end", m.end())
         
         protocol = []
-        rawProto = header[m.end():end].splitlines()
-        for line in rawProto:
-            if line.startswith("- "):
-                selector, types = getSelector(line)
-                protocol.append((selector, types, line))
+        rawProto = header[m.end():end]
+        start = 0
+        while 1:
+            start = rawProto.find("\n- ", start)
+            if start < 0:
+                break
+            selend = rawProto.find(";", start)
+            rawSelector = rawProto[start + 3:selend]
+            selector, types = getSelector(rawSelector)
+            protocol.append((selector, types, " ".join(rawSelector.split())))
+            start = selend + 1
         protocol.sort()
         assert end >= 0
         pos = end + 5
 
 
 def genProtocols(frameworkPath, outFile=None):
+    """Generate protocol definitions for a framework. If outFile is None,
+    the generated Python code will be printed to sys.stdout.
+    """
     frameworkPath = os.path.normpath(frameworkPath)
     assert frameworkPath.endswith(".framework")
     frameworkName = os.path.basename(frameworkPath)
             print >> outFile, "        ),"
         print >> outFile, "    ]"
         print >> outFile, ")"
-        print >> outFile,
+        print >> outFile
 
 if __name__ == "__main__":
     if sys.argv[1:]: