Compiled metadata system¶
Starting with version 2.4 PyObjC provides a more efficient, lazy loading metadata system. This can greatly reduce the memory use and startup time for PyObjC based applications while still providing full access to Cocoa APIs.
Creating a framework wrapper¶
A framework wrapper with the new metadata system is always a python package. The package contains an “__init__.py” file that creates the lazy loader, a “_metadata.py” file with the compiled metadata and optionally other modules and extensions.
The general structure of the “__init__.py” file is:
import sys, objc
import Foundation
from . import _metadata
sys.modules['FrameworkName'] = objc.ObjCLazyModule('FrameworkName',
"com.apple.FrameworkName',
objc.pathForFramework("/System/Library/Frameworks/FrameworkName.framework"),
_metadata.__dict__, None, {
'__doc__': __doc__,
'objc': objc,
'__path__': __path__,
}, (Foundation,))
The framework name, identifier and path should be replaced by
the correct values for the wrapped framework. The import of “Foundation”
can be replaced by imports of other framework this framework relies on
(also add those to the last argument of objc.ObjCLazyModule
).
Contents of the “_metadata” module¶
The exact contents of the “_metadata” module will be described later.
Generating the “_metadata” module¶
The “objective-metadata” project contains a tool for collecting information about frameworks and compiling that information and manual additions into a “_metadata” module.
That project currently is not a stable as I’d like, this documentation will be updated with more information when that changes.
API description¶
- class objc.ObjCLazyModule(name, frameworkIdentifier, frameworkPath, metadict[, inline_list[, initialdict[, parents]]])¶
A subclass of the built-in
module
type that adds lazy-loading of values defined in PyObjC metadata.- Parameters
frameworkIdentifier – the bundle_identifier argument for a call to
loadBundle()
frameworkPath – the bundle_path argument for a call to
loadBundle()
metadict – the dictionary with metadata, usually the __dict__ of a module generated by the metadata compiler.
inline_list – a capsule object with function definitions, see
loadFunctionList()
for more information.initial_dict – additional values to add to the module dictionary
parents – a list of parent modules, the module behaves as if those modules were imported using
from parent parent import *
, but lazily fetches definitions on first access.
Note
This is the primary entry point for the framework wrappers shipped with PyObjC.