Re: [Pyobjc-dev] Supporting (NSString **) API (and the like)
Brought to you by:
ronaldoussoren
From: Bill B. <bb...@co...> - 2002-11-26 23:38:22
|
On Tuesday, November 26, 2002, at 06:26 PM, Jack Jansen wrote: > Good idea. And this also solves the issue about what to do for > python-specific modules (a Python subpackage or a prefix): use a > prefix there too. Even though I had a slight preference for a > subpackage I think that if we use prefixes for methods that have no > ObjC equivalent we should also use prefixes for modules that have no > ObjC equivalent. It wasn't really intended to be a suggestion regarding the use of module, more a suggestion of how we might provide access to API that cannot generically work with PyObjC. In this case, the additional functionality is rather simple pure ObjC and is, IMHO, a heck of a lot easier to create and maintain than the code found in _AppKitMapping.m (which isn't currently used). The alternative would be to return an NSArray from the method w/the contents in the same order as they appear in the original method. This could easily be mapped to the slightly-more-standard Python pattern of using tuples as return values... Actually, come to think of it, I like the idea of returning tuples *a lot* more than the current dictionary -- it is faster and more consistent with the whole coding paradigm. Since this is all proof of concept, I'm going to experiment with returning an array and see if it proves to more useful in practice. --- The current category is used as such.... infoDictionary = workspace.pyobjcGetInfoForFile_(fullPath) if not infoDictionary: NSLog("Failed to find application to open file %s" % fullPath) return workspace.openFile_withApplication_(fullPath, infoDictionary['application']) ... and the implementation looks like the following. I didn't see a reason to declare this in a formal header file type arrangement since it is unlikely to ever be used directly from the ObjC side of the bridge. Make sense? #import <Cocoa/Cocoa.h> @interface NSWorkspace (PyObjCSupport) @end @implementation NSWorkspace (PyObjCSupport) // - (BOOL)getInfoForFile:(NSString *)fullPath application:(NSString **)appName type:(NSString **)type; - (NSDictionary *) pyobjcGetInfoForFile:(NSString *)fullPath; { NSString *appName, *type; if ( [self getInfoForFile: fullPath application: &appName type: &type] ) { NSMutableDictionary *returnDict = [NSMutableDictionary dictionary]; if (appName) [returnDict setObject: appName forKey: @"application"]; if (type) [returnDict setObject: type forKey: @"type"]; return returnDict; } return nil; } // - (BOOL)getFileSystemInfoForPath:(NSString *)fullPath isRemovable:(BOOL *)removableFlag isWritable:(BOOL *)writableFlag isUnmountable:(BOOL *)unmountableFlag description:(NSString **)description type:(NSString **)fileSystemType; - (NSDictionary *) pyobjcGetFileSystemInfoForPath: (NSString *) fullPath; { BOOL removableFlag, writableFlag, unmountableFlag; NSString *description, *fileSystemType; if ([self getFileSystemInfoForPath: fullPath isRemovable: &removableFlag isWritable: &writableFlag isUnmountable: &unmountableFlag description: &description type: &fileSystemType]) { NSMutableDictionary *returnDict = [NSMutableDictionary dictionary]; [returnDict setObject: [NSNumber numberWithBool: removableFlag] forKey: @"isRemovable"]; [returnDict setObject: [NSNumber numberWithBool: writableFlag] forKey: @"isWritable"]; [returnDict setObject: [NSNumber numberWithBool: unmountableFlag] forKey: @"isUnmountable"]; if (description) [returnDict setObject: description forKey: @"description"]; if (fileSystemType) [returnDict setObject: description forKey: @"description"]; } return nil; } @end |