[Pyobjc-dev] NibLoader.py: parsing nibs at runtime
Brought to you by:
ronaldoussoren
From: Just v. R. <ju...@le...> - 2002-11-15 05:55:02
|
Today, instead of learning how to do useful things with Cocoa <wink>, I wrote a replacement for mknibwrapper/objc.classnib.py, that does the same thing at runtime, making nibwrapper.py files superfluous. This abviously has some startup time hit, but it seems quite quick. Not that I think that having to generate nibwrapper.py files is all that bad, I was just looking for an opportunity to learn a thing or two, eg. about metaclasses... The attached module lets you do this: [...more imports snipped...] from NibLoader import NibLoader, addNib # (the addNib() could be done elewhere, or could be done more # concisely with a helper funciton in the NibLoader module) addNib(os.path.join(sys.path[0], "English.lproj/MainMenu.nib")) class PyModel(NibLoader, NSTableDataSource): ...etc... addNib() parses a nib file and sucks the class info out of it. NibLoader is a magic class (or rather, has a magic superclass) that will make sure the subclass has the right base class, and will populate it with all the stuff that was normally generated in the nibwrapper file. So far I've only tested it with the TableModel demo: it works for me. The NibLoader module contains a tiny test program: you can invoke it from the command line like so: % python NibLoader.py <path-to-nib> [more paths to nibs] It will parse all nibs specified and will build dummy test classes for all found nib classes and print some blurb to stdout. I've tested this on a few nibs found in apps that came with the OS as well as with all the nibs in the Examples folder, and apart from some classes that don't seem to have a SUPERCLASS attribute, building the test classes seems to work. Obviously much of the code is ripped straight from objc/classnib.py. Useful? Just |