Commits

Anonymous committed 84a900b

Integrated Ronald's changes into the main.m. Normalized the names to match that in the Cocoa project template. Cleaned up the code to reflect Just's auto-nib-file' magic.

Comments (0)

Files changed (8)

pyobjc/Examples/WebServicesTool/Main.py

-import sys
-import os.path
-
-sys.path.insert(0, os.path.join(sys.path[0], "pyobjc"))
-
-import objc
-import Foundation
-import AppKit
-
-# automatically load frameworks that were linked into the bootstrap tooll
-pyFrameworkPathsIndex = sys.argv.index("-PyFrameworkPaths")
-if not (pyFrameworkPathsIndex == -1):
-  import string
-  from Foundation import NSBundle
-  paths = string.split(sys.argv[pyFrameworkPathsIndex + 1], ":")
-  count = 0
-  for path in paths:
-    bundle = NSBundle.bundleWithPath_(path)
-    bundle.principalClass()
-    sys.path.insert(count, bundle.resourcePath())
-    count = count + 1
-    
-    initPath = bundle.pathForResource_ofType_( "Init", "py")
-    if initPath:
-      execfile(initPath, globals(), locals())
-
-import WSTApplicationDelegateClass
-import WSTConnectionWindowControllerClass
-
-sys.exit( AppKit.NSApplicationMain(sys.argv) )

pyobjc/Examples/WebServicesTool/WSTApplicationDelegateClass.py

 from Foundation import NSObject
 from WSTConnectionWindowControllerClass import WSTConnectionWindowController
 
-from AppKit import NibLoader
+from AppKit import NibClassBuilder
+from AppKit.NibClassBuilder import AutoBaseClass
 
-NibLoader.loadClassesForNibFromBundle( "MainMenu" )
-
-class WSTApplicationDelegate:
-  __metaclass__ = NibLoader.NibClassBuilder
+NibClassBuilder.extractClasses( "MainMenu" )
+class WSTApplicationDelegate(AutoBaseClass):
 
   def newConnectionAction_(self, sender):
     WSTConnectionWindowController.connectionWindowController().showWindow_(sender)

pyobjc/Examples/WebServicesTool/WSTConnectionWindowControllerClass.py

 import string
 import traceback
 
-from AppKit import NibLoader
-NibLoader.loadClassesForNibFromBundle( "WSTConnection" )
+from AppKit import NibClassBuilder
+from AppKit.NibClassBuilder import AutoBaseClass
 
 kWSTReloadContentsToolbarItemIdentifier = "WST: Reload Contents Toolbar Identifier"
 kWSTPreferencesToolbarItemIdentifier = "WST: Preferences Toolbar Identifier"
     
     self._toolbarItems.setObject_forKey_(toolbarItem, anIdentifier)
 
-class WSTConnectionWindowController:
-    __metaclass__ = NibLoader.NibClassBuilder
-
+NibClassBuilder.extractClasses( "WSTConnection" )
+class WSTConnectionWindowController(AutoBaseClass, NSTableDataSource, NSToolbarDelegate):
     __slots__ = ('_toolbarItems',
         '_toolbarDefaultItemIdentifiers',
         '_toolbarAllowedItemIdentifiers',
             return aMethod
 
     ### adjust method decls to be in line with ObjC requirements
-    toolbarDefaultItemIdentifiers_ = selector(toolbarDefaultItemIdentifiers_, signature='@@:@')
-    toolbarAllowedItemIdentifiers_ = selector(toolbarAllowedItemIdentifiers_, signature='@@:@')
     connectionWindowController = selector(connectionWindowController, class_method=1)
-    toolbar_itemForItemIdentifier_willBeInsertedIntoToolbar_ = selector(toolbar_itemForItemIdentifier_willBeInsertedIntoToolbar_, signature='@@:@@c')
-    numberOfRowsInTableView_ = selector(numberOfRowsInTableView_, signature="i@:@")
-    tableView_objectValueForTableColumn_row_ = selector(tableView_objectValueForTableColumn_row_, signature='@@:@@i')

pyobjc/Examples/WebServicesTool/Web Services Tool.pbproj/project.pbxproj

 		};
 		080E96DDFE201D6D7F000001 = {
 			children = (
+				F6D75F2C039008BF01A801C8,
 				F6646A4E0344927801F2B36F,
 				F6646A500344928B01F2B36F,
 				F6646A5C0344985501F2B36F,
 		29B97315FDCFA39411CA2CEA = {
 			children = (
 				29B97316FDCFA39411CA2CEA,
+				F6D75F2D0390096901A801C8,
 				F6AAC478035708BD01B76035,
 			);
 			isa = PBXGroup;
 		};
 		F6646A4E0344927801F2B36F = {
 			isa = PBXFileReference;
-			path = Main.py;
+			path = __main__.py;
 			refType = 4;
 		};
 		F6646A4F0344927801F2B36F = {
 			};
 		};
 		F6AAC478035708BD01B76035 = {
+			indentWidth = 4;
 			isa = PBXFileReference;
-			path = "main-bin-python.m";
+			path = "bin-python-main.m";
 			refType = 4;
+			tabWidth = 4;
 		};
 		F6AAC47A035708BD01B76035 = {
 			fileRef = F6AAC478035708BD01B76035;
 			settings = {
 			};
 		};
+		F6D75F2C039008BF01A801C8 = {
+			indentWidth = 2;
+			isa = PBXFileReference;
+			path = Main.py;
+			refType = 4;
+			usesTabs = 0;
+		};
+		F6D75F2D0390096901A801C8 = {
+			indentWidth = 2;
+			isa = PBXFileReference;
+			path = "main-bin-python.m";
+			refType = 4;
+			usesTabs = 0;
+		};
 		F6ED47D803453061016073B7 = {
 			isa = PBXFileReference;
 			path = Preferences.png;

pyobjc/Examples/WebServicesTool/__main__.py

+import sys
+import os.path
+
+sys.path.insert(0, os.path.join(sys.path[0], "pyobjc"))
+
+import objc
+import Foundation
+import AppKit
+
+# automatically load frameworks that were linked into the bootstrap tooll
+pyFrameworkPathsIndex = sys.argv.index("-PyFrameworkPaths")
+if not (pyFrameworkPathsIndex == -1):
+  import string
+  from Foundation import NSBundle
+  paths = string.split(sys.argv[pyFrameworkPathsIndex + 1], ":")
+  count = 0
+  for path in paths:
+    bundle = NSBundle.bundleWithPath_(path)
+    bundle.principalClass()
+    sys.path.insert(count, bundle.resourcePath())
+    count = count + 1
+    
+    initPath = bundle.pathForResource_ofType_( "Init", "py")
+    if initPath:
+      execfile(initPath, globals(), locals())
+
+import WSTApplicationDelegateClass
+import WSTConnectionWindowControllerClass
+
+sys.exit( AppKit.NSApplicationMain(sys.argv) )

pyobjc/Examples/WebServicesTool/bin-python-main.m

+//
+//  bin-python-main.m
+//  Web Services Tool
+//
+//  Created by Bill Bumgarner on 10/10/2002.
+//  Copyright (c) 2002 The PyObjC Project. All rights reserved.
+//
+
+/*
+ This main file uses execve() to transfer control of execution to the standard command line python interpreter.   As such, compiled classes in the project will not actually be linked into the runtime as execve() effectively overlays the existing process with the process being called -- in this case the python command line tool.
+
+ To use compiled classes with this main, create a separate bundle target and load the bundle in the Main.py file.
+
+ This style of execution works with the Apple provided version of Python.
+ */
+
+#import <Foundation/Foundation.h>
+#import <sys/param.h>
+#import <unistd.h>
+
+int pyobjc_main(int argc, char * const *argv, char * const *envp)
+{
+    // The autorelease pool is not released on purpose.   The call to execve() destroys the
+    // calling process entirely and, as such, memory management in the traditional sense
+    // is not necessary (and not doing so avoids potential bugs associated with releasing
+    // the pool prior to the call to execve).
+    [[NSAutoreleasePool alloc] init];
+
+    const char **childArgv = alloca(sizeof(char *) * (argc + 5));
+    NSEnumerator *bundleEnumerator = [[NSBundle allFrameworks] reverseObjectEnumerator];
+    NSBundle *aBundle;
+    NSBundle *mainBundle = [NSBundle mainBundle];
+    NSMutableArray *bundlePaths = [NSMutableArray array];
+    int i;
+    int envc;
+    char** childEnvp;
+    char*  PYTHONPATH = NULL;
+
+    // count entries in environment and find the PYTHONPATH setting, if present
+    for (envc = 0; envp[envc] != NULL; envc++) {
+        if (strncmp(envp[envc], "PYTHONPATH=", sizeof("PYTHONPATH=")-1) == 0) {
+            PYTHONPATH=envp[envc] + sizeof("PYTHONPATH=") - 1;
+            /* No break, we also want to know how large envp is */
+        }
+    }
+
+    childEnvp = alloca(sizeof(char*) * (envc + 10)); // enough for both PYTHONPATH and the DYLD stuff
+    for (envc = 0; envp[envc] != NULL; envc ++) {
+        if (strncmp(envp[envc], "PYTHONPATH=", sizeof("PYTHONPATH=")-1) == 0) {
+            childEnvp[envc] = (char *)[[NSString stringWithFormat: @"PYTHONPATH=%@:%s", [[NSBundle mainBundle] resourcePath], envp[envc]] UTF8String];
+        } else {
+            childEnvp[envc] = envp[envc];
+        }
+    }
+    if (PYTHONPATH) {
+        childEnvp[envc] = NULL;
+    } else {
+        childEnvp[envc] = (char *)[[NSString stringWithFormat: @"PYTHONPATH=%@", [[NSBundle mainBundle] resourcePath]] UTF8String];
+        envc++;
+        childEnvp[envc] = NULL;
+    }
+
+    // if this is set, it is most likely because of PBX or because the developer is doing something....
+    if ( !getenv("DYLD_FRAMEWORK_PATH") ) {
+        // if not, put the DYLD environment into a state where we can actually load frameworks from within the app
+        // wrapper where the frameworks may have inter-dependencies.
+        NSArray *paths = [NSArray arrayWithObjects: [mainBundle sharedFrameworksPath], [mainBundle privateFrameworksPath], nil];
+        NSString *joinedPaths = [paths componentsJoinedByString: @":"];
+        const char *dyldFrameworkPath = [[NSString stringWithFormat: @"DYLD_FRAMEWORK_PATH=%@", joinedPaths] UTF8String];
+        const char *dyldLibraryPath = [[NSString stringWithFormat: @"DYLD_LIBRARY_PATH=%@", joinedPaths] UTF8String];
+
+        childEnvp[envc++] = (char *)dyldFrameworkPath;
+        childEnvp[envc++] = (char *)dyldLibraryPath;
+
+        // useful for debugging-- set this as a default.
+        if ([[NSUserDefaults standardUserDefaults] boolForKey: @"DYLD_PRINT_LIBRARIES"])
+            childEnvp[envc++] = (char *)"DYLD_PRINT_LIBRARIES=1";
+        childEnvp[envc++] = NULL;
+    }
+
+    // grab a list of all frameworks that were linked into this executable
+    while ( aBundle = [bundleEnumerator nextObject] ) {
+        if ( [[[aBundle bundlePath] pathExtension] isEqualToString: @"framework"] )
+            [bundlePaths addObject: [aBundle bundlePath]];
+    }
+
+    // figure out which python interpreter to use
+    NSString *pythonBinPath = [[NSUserDefaults standardUserDefaults] stringForKey: @"PythonBinPath"];
+    pythonBinPath = pythonBinPath ? pythonBinPath : @"/usr/bin/python";
+
+    const char *pythonBinPathPtr = [pythonBinPath UTF8String];
+
+    // find main python file.  __main__.py seems to be a standard.
+    NSArray *possibleMains = [NSArray arrayWithObjects: @"__main__.py", @"__main__.pyc", @"__realmain__.py", @"__realmain.pyc", nil];
+    NSEnumerator *possibleMainsEnumerator = [possibleMains objectEnumerator];
+    NSString *mainPyPath;
+    NSString *nextFileName;
+
+    while (nextFileName = [possibleMainsEnumerator nextObject]) {
+        mainPyPath = [mainBundle pathForResource: nextFileName ofType: nil];
+        if ( mainPyPath )
+            break;
+    }
+
+    if ( !mainPyPath )
+        [NSException raise: NSInternalInconsistencyException
+                    format: @"%s:%d pyobjc_main() Failed to find one of %@ in app wrapper.  Exiting.", __FILE__, __LINE__, possibleMains];
+    const char *mainPyPathPtr = [mainPyPath UTF8String];
+
+    // construct argv for the child
+
+    // the path to the executable in the app wrapper -- must be in the app wrapper or CFBundle does not initialize correctly
+    childArgv[0] = argv[0];
+
+    // path to the python file that acts as the main entry point
+    childArgv[1] = mainPyPathPtr;
+
+    // Pass original arguments (such as -NSOpen) verbatum
+    //
+    // Move each argument right one slot
+    for (i = 1; i<argc; i++)
+        childArgv[i+1] = argv[i];
+    i++; // compensate for i+1 in for() loop
+
+    // add an argument that lists all frameworks
+    childArgv[i++] = "-PyFrameworkPaths";
+    childArgv[i++] = [[bundlePaths componentsJoinedByString: @":"] UTF8String];
+
+    // terminate the arg list
+    childArgv[i++] = NULL;
+
+    // print a nice debugging helper message, if desired
+    if ([[[NSProcessInfo processInfo] environment] objectForKey: @"SHOWPID"])
+        NSLog(@"Process ID is: %d (\n\tgdb %s %d\n to debug)", getpid(), pythonBinPathPtr, getpid());
+
+    // pass control to the python interpreter
+    return execve(pythonBinPathPtr, (char **)childArgv, childEnvp);
+}
+
+int main(int argc, char * const *argv, char * const *envp)
+{
+    return pyobjc_main(argc, argv, envp);
+}

pyobjc/Examples/WebServicesTool/main-bin-python.m

-/*
- This main file uses execve() to transfer control of execution to the standard command line python interpreter.   As such, compiled classes in the project will not actually be linked into the runtime as execve() effectively overlays the existing process with the process being called -- in this case the python command line tool.
-
- To use compiled classes with this main, create a separate bundle target and load the bundle in the Main.py file.
-
- This style of execution works with the Apple provided version of Python.
- */
-
-#import <Foundation/Foundation.h>
-#import <sys/param.h>
-#import <unistd.h>
-
-int pyobjc_main(int argc, char * const *argv, char * const *envp)
-{
-  [[NSAutoreleasePool alloc] init];
-
-  const char **childArgv = alloca(sizeof(char *) * (argc + 5));
-  char **childEnvp = (char **)envp;
-  const char *pythonBinPathPtr;
-  const char *mainPyPathPtr;
-  NSEnumerator *bundleEnumerator = [[NSBundle allFrameworks] reverseObjectEnumerator];
-  NSBundle *aBundle;
-  NSBundle *mainBundle = [NSBundle mainBundle];
-  NSMutableArray *bundlePaths = [NSMutableArray array];
-  int i;
-
-  if ( !getenv("DYLD_FRAMEWORK_PATH") ) {
-    NSArray *paths = [NSArray arrayWithObjects: [mainBundle sharedFrameworksPath], [mainBundle privateFrameworksPath], nil];
-    NSString *joinedPaths = [paths componentsJoinedByString: @":"];
-    const char *dyldFrameworkPath = [[NSString stringWithFormat: @"DYLD_FRAMEWORK_PATH=%@", joinedPaths] UTF8String];
-    const char *dyldLibraryPath = [[NSString stringWithFormat: @"DYLD_LIBRARY_PATH=%@", joinedPaths] UTF8String];
-
-    for(i=0; envp[i]; i++);
-    childEnvp = malloc( sizeof(char *) * (i+5) );
-
-    bcopy( envp, childEnvp, ( i * sizeof(char *) ) );
-
-    childEnvp[i++] = (char *)dyldFrameworkPath;
-    childEnvp[i++] = (char *)dyldLibraryPath;
-    //!    childEnvp[i++] = "DYLD_NO_FIX_PREBINDING=1";  Can't decide if this is a good idea.
-    if ([[NSUserDefaults standardUserDefaults] boolForKey: @"DYLD_PRINT_LIBRARIES"])
-      childEnvp[i++] = (char *)"DYLD_PRINT_LIBRARIES=1";
-    childEnvp[i++] = NULL;
-  }
-
-  while ( aBundle = [bundleEnumerator nextObject] ) {
-    if ( [[[aBundle bundlePath] pathExtension] isEqualToString: @"framework"] )
-      [bundlePaths addObject: [aBundle bundlePath]];
-  }
-
-  NSString *pythonBinPath = [[NSUserDefaults standardUserDefaults] stringForKey: @"PythonBinPath"];
-  pythonBinPath = pythonBinPath ? pythonBinPath : @"/usr/bin/python";
-  pythonBinPathPtr = [pythonBinPath UTF8String];
-
-  NSString *mainPyFile = [[mainBundle infoDictionary] objectForKey: @"PrincipalPythonFile"];
-  NSString *mainPyPath = nil;
-
-  if (mainPyFile)
-    mainPyPath = [mainBundle pathForResource: mainPyFile ofType: nil];
-
-  if ( !mainPyPath )
-    mainPyPath = [mainBundle pathForResource: @"Main.py" ofType: nil];
-
-  if ( !mainPyPath )
-    [NSException raise: NSInternalInconsistencyException
-                format: @"%s:%d pyobjc_main() Failed to find main python entry point for application.  Exiting.", __FILE__, __LINE__];
-  mainPyPathPtr = [mainPyPath UTF8String];
-
-  childArgv[0] = argv[0];
-  childArgv[1] = mainPyPathPtr;
-  for (i = 1; i<argc; i++)
-    childArgv[i+1] = argv[i];
-  childArgv[i+1] = "-PyFrameworkPaths";
-  childArgv[i+2] = [[bundlePaths componentsJoinedByString: @":"] UTF8String];
-  childArgv[i+3] = NULL;
-
-  if ([[[NSProcessInfo processInfo] environment] objectForKey: @"SHOWPID"])
-    NSLog(@"Process ID is: %d (\n\tgdb %s %d\n to debug)", getpid(), pythonBinPathPtr, getpid());
-
-  return execve(pythonBinPathPtr, (char **)childArgv, childEnvp);
-}
-
-int main(int argc, char * const *argv, char * const *envp)
-{
-  return pyobjc_main(argc, argv, envp);
-}

pyobjc/Examples/WebServicesTool/main-embedded-interpreter.m

-#error The project does not use this file.  It is left here both for reference purposes (if anyone is interested in going the embedded interpreter route) and in case Apple ships the Python library in OS X (the embedded interpreter is actually more efficient). 
-
-/*
- This main file can be used in cases where the developer desires to embed the python interpreter directly into the application.   The pyobjc_main() function initializes the python interpreter in an embedded context.
-
- This is useful if the application has a mix of ObjC and Python.  The ObjC can be compiled/linked without having to load a series of bundles (which is considerably slower than launching a normal application).
-
- The point is somewhat moot in that the PyObjC module has to dynamically load the AppKit and related frameworks anyway.
-
- As of 10.2.1 [6d52], This style of build will not work with the python supplied with OS X as Apple did not provide a python library or framework to link against.
- */
-
-#import <Python.h>
-#import <Foundation/Foundation.h>
-#import <Cocoa/Cocoa.h>
-#import <sys/param.h>
-
-int pyobjc_main(int argc, const char *argv[])
-{
-    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
-    NSString *pythonBinPath = [[NSUserDefaults standardUserDefaults] stringForKey: @"PythonBinPath"];
-
-    pythonBinPath = pythonBinPath ? pythonBinPath : @"/usr/bin/python";
-    [pythonBinPath retain];
-    Py_SetProgramName((char *)[pythonBinPath cString]);
-    Py_Initialize();
-
-    NSString *mainPyFile = [[[NSBundle mainBundle] infoDictionary] objectForKey: @"PrincipalPythonFile"];
-    NSString *mainPyPath = nil;
-
-    if (mainPyFile)
-        mainPyPath = [[NSBundle mainBundle] pathForResource: mainPyFile ofType: nil];
-
-    if ( !mainPyPath )
-        mainPyPath = [[NSBundle mainBundle] pathForResource: @"Main.py" ofType: nil];
-
-    if ( !mainPyPath )
-        [NSException raise: NSInternalInconsistencyException
-                    format: @"%s:%d pyobjc_main() Failed to find main python entry point for application.  Exiting.", __FILE__, __LINE__];
-    [mainPyPath retain];
-
-    FILE *mainPy = fopen([mainPyPath cString], "r");
-    int result = PyRun_SimpleFile(mainPy, (char *)[[mainPyPath lastPathComponent] cString]);
-
-    if ( result != 0 )
-        [NSException raise: NSInternalInconsistencyException
-                    format: @"%s:%d pyobjc_main() PyRun_SimpleFile failed with file '%@'.  See console for errors.", __FILE__, __LINE__, mainPyPath];
-
-    [pool release];
-
-    return result;
-}
-
-int main(int argc, const char *argv[])
-{
-    return pyobjc_main(argc, argv);
-}