1. Ronald Oussoren
  2. pyobjc


pyobjc / Doc / PyObjCTools.html

The branch 'pyobjc-ancient' does not exist.
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
PyObjCTools: The PyObjC Toolbox</title>
<h2>PyObjCTools: The PyObjC Toolbox</h2>
<h3 id="introduction">Introduction</h3>
<p>The package <code><span>PyObjCTools</span></code> contains a number of (basically unrelated) modules
with useful functionality. These have been placed inside a module to avoid
cluttering the global namespace.</p>
<p>The rest of this document provides documentation for these modules, but lets
start with a short overview.</p>
<p>Utility functions for use with the <code><span>AppKit</span></code> module.</p>
<p>Functions for converting between Cocoa and pure Python data structures.</p>
<p>A Python API for working with <a href="http://developer.apple.com/documentation/Cocoa/Conceptual/KeyValueCoding/">Key-Value Coding</a>.</p>
<p>Module containing a magic super-class that can read information about the
actual super-class and implemented actions and outlets from a NIB file.</p>
<p>Module to make it possible to integrate signal handling into the main
<p>Allows logging of NSException stack traces.  This module should only be used
during development.</p>
<p>Module that tries to print useful information when the program gets a fatal
exception. This module should only be used during development.</p>
<p>Used by the PyObjC Xcode templates to derive py2app options from an Xcode
project file.</p>
<h2><a name="pyobjctools-apphelper"><code><span>PyObjCTools.AppHelper</span></code></a></h2>
<p>This module exports functions that are useful when working with the
<code><span>AppKit</span></code> framework (or more generally, run loops).</p>
<li><code><span>callAfter(func,</span> <span>*args,</span> <span>**kwargs)</span> <span>-&gt;</span> <span>None</span></code><p>Call a function on the main thread.  Returns immediately.</p>
<li><code><span>callLater(delay,</span> <span>func,</span> <span>*args,</span> <span>**kwargs)</span> <span>-&gt;</span> <span>None</span></code><p>Call a function on the main thread after a delay.  Returns immediately.</p>
<li><code><span>endSheetMethod(method)</span> <span>-&gt;</span> <span>selector</span></code><p>Convert a method to a form that is suitable to use as the delegate callback
for sheet methods.</p>
<li><code><span>stopEventLoop()</span> <span>-&gt;</span> <span>None</span></code><p>Stops the event loop (if started by <code><span>runConsoleEventLoop</span></code>) or sends the
<code><span>NSApplication</span></code> a <code><span>terminate:</span></code> message.</p>
<li><code><span>runConsoleEventLoop(argv=None,</span> <span>installInterrupt=False,</span> <span>mode=NSDefaultRunLoopMode)</span> <span>-&gt;</span> <span>None</span></code><p>Run a <code><span>NSRunLoop</span></code> in a stoppable way (with <code><span>stopEventLoop</span></code>).</p>
<li><code><span>runEventLoop(argv=None,</span> <span>unexpectedErrorAlert=unexpectedErrorAlert,</span> <span>installInterrupt=None,</span> <span>pdb=None,</span> <span>main=NSApplicationMain)</span> <span>-&gt;</span> <span>None</span></code><p>Run the event loop using <code><span>NSApplicationMain</span></code> and ask the user if we should
continue if an exception is caught.</p>
<p>This function doesn't return unless it throws an exception.</p>
<h2><a name="pyobjctools-conversion"><code><span>PyObjCTools.Conversion</span></code></a></h2>
<p>Functions for converting between Cocoa and pure Python data structures.</p>
<li><code><span>propertyListFromPythonCollection(pyCol,</span> <span>conversionHelper=None)</span> <span>-&gt;</span> <span>ocCol</span></code><p>Convert a Python collection (dictionary, array, tuple, string) into an 
Objective-C collection.</p>
<p>If conversionHelper is defined, it must be a callable.  It will be called 
for any object encountered for which <code><span>propertyListFromPythonCollection()</span></code>
cannot automatically convert the object.   The supplied helper function 
should convert the object and return the converted form.  If the conversion 
helper cannot convert the type, it should raise an exception or return None.</p>
<li><code><span>pythonCollectionFromPropertyList(ocCol,</span> <span>conversionHelper=None)</span> <span>-&gt;</span> <span>pyCol</span></code><p>Converts a Foundation based collection-- a property list-- into a Python 
collection.  Like <code><span>propertyListFromPythonCollection()</span></code>, <code><span>conversionHelper</span></code>
is an optional callable that will be invoked any time an encountered object 
cannot be converted.</p>
<h2><a name="pyobjctools-keyvaluecoding"><code><span>PyObjCTools.KeyValueCoding</span></code></a></h2>
<p>A module for working with Key-Value Coding in Python. Key-Value Coding is
explained <a href="http://developer.apple.com/documentation/Cocoa/Conceptual/KeyValueCoding/">on the Apple website</a></p>
<p>This module provides a Python interface to some of that functionality. The
interface is modeled on the <code><span>getattr</span></code> and <code><span>setattr</span></code> functions.</p>
<li><code><span>getKey(object,</span> <span>key)</span> <span>-&gt;</span> <span>value</span></code><blockquote>
<p>Get the attribute referenced by 'key'. The key is used
to build the name of an attribute, or attribute accessor method.</p>
<p>The following attributes and accesors are tried (in this order):</p>
<li>Accessor 'getKey'</li>
<li>Accesoor 'get_key'</li>
<li>Accessor or attribute 'key'</li>
<li>Accessor or attribute 'isKey'</li>
<li>Attribute '_key'</li>
<p>If none of these exist, raise KeyError</p>
<li><code><span>getKeyPath(object,</span> <span>keypath)</span> <span>-&gt;</span> <span>value</span></code><p>Like <code><span>getKey</span></code> but using a key path. The <code><span>keypath</span></code> is a sequence of keys
separated by dots. It calls <code><span>getKey</span></code> to follow the path and returns the
final value.</p>
<li><code><span>setKey(object,</span> <span>key,</span> <span>value)</span> <span>-&gt;</span> <span>None</span></code><p>Set the value of <code><span>key</span></code> to <code><span>value</span></code>.</p>
<p>The following values are used for setting the value for a key named <code><span>key</span></code>
(first match wins):</p>
<li>Call <code><span>object.set_key(value)</span></code></li>
<li>Call <code><span>object.setKey(value)</span></code></li>
<li>Call <code><span>object._set_key(value)</span></code></li>
<li>Call <code><span>object._setKey(value)</span></code></li>
<li>Check if <code><span>_key</span></code> is an attribute and if so, set its value</li>
<li>Try to set the attribute <code><span>key</span></code>.</li>
<p>Raises <code><span>KeyError</span></code> if the key cannot be changed.</p>
<li><code><span>setKeyPath(object,</span> <span>keypath,</span> <span>value)</span> <span>-&gt;</span> <span>None</span></code><p>The same as <code><span>setKey</span></code>, but now using a key path. A key path is a sequence
of keys separated by dots. The <code><span>getKey</span></code> function is used to traverse 
the path up to the last item, and then <code><span>setKey</span></code> is used to change the value.</p>
<h2><a name="pyobjctools-nibclassbuilder">PyObjCTools.NibClassBuilder</a></h2>
<h3><a name="extracting-class-definitions-from-nibs">Extracting class definitions from nibs</a></h3>
<p>The module maintains a global set of class definitions, extracted from
nibs. To add the classes from a nib to this set, use the <code><span>extractClasses()</span></code>
function. It can be called in two ways:</p>
<li><code><span>extractClasses(nibName,</span> <span>bundle=&lt;current-bundle&gt;)</span></code><p>This finds the nib by name from a bundle. If no bundle
if given, the <code><span>objc.currentBundle()</span></code> is searched.</p>
<li><code><span>extractClasses(path=pathToNib)</span></code><p>This uses an explicit path to a nib.</p>
<p><code><span>extractClasses()</span></code> can be called multiple times for the same bundle: the
results are cached so no almost extra overhead is caused.</p>
<h3><a name="using-the-class-definitions">Using the class definitions</a></h3>
<p>The module contains a &quot;magic&quot; base (super) class called <code><span>AutoBaseClass</span></code>.
Subclassing <code><span>AutoBaseClass</span></code> will invoke some magic that will look up the
proper base class in the class definitions extracted from the nib(s).
If you use multiple inheritance to use Cocoa's &quot;informal protocols&quot;,
you <i>must</i> list <code><span>AutoBaseClass</span></code> as the first base class. For example:</p>
class PyModel(AutoBaseClass, NSTableSource):
<h3><a name="the-nibinfo-class">The <code><span>NibInfo</span></code> class</a></h3>
<p>The parsing of nibs and collecting the class definition is done by the
<code><span>NibInfo</span></code> class. You normally don't use it directly, but it's here if you
have special needs.</p>
<h3><a name="the-command-line-tool">The command line tool</a></h3>
<p>When run from the command line, this module invokes a simple command
line program, which you feed paths to nibs. This will print a Python
template for all classes defined in the nib(s). For more documentation,
see the commandline_doc variable, or simply run the program without
arguments. It also contains a simple test program.</p>
<h2><a name="pyobjctools-signals">PyObjCTools.Signals</a></h2>
<p>This module provides two functions that can be useful while investigating
random crashes of a PyObjC program. These crashes are often caused by 
Objective-C style weak references or incorrectly implemented protocols.</p>
<li><code><span>dumpStackOnFatalSignal()</span></code><p>This function will install signal handlers that print a stack trace and
then re-raise the signal.</p>
<li><code><span>resetFatalSignals()</span></code><p>Restores the signal handlers to the state they had before the call to
<p>This module is not designed to provide fine grained control over signal 
handling. Nor is it intended to be terribly robust. It may give useful
information when your program gets unexpected signals, but it might just
as easily cause a crash when such a signal gets in.</p>