Hi,
I've just checked in new, and extended, support for the Key-Value Coding
informal protocol. This is meant as a complete replacement for the
module
PyObjCTools.KeyValueSupport. The new support code consists of three
parts.
The first part is the end-user API in PyObjCTools.KeyValueCoding. This
module
contains a number of functions that give a convenient API to use
key-value
coding with Python objects (both for ObjC classes and unmodified plain
python classes). The API currently consists of 4 functions and is
modelled
on the 'getattr' and 'setattr' functions that can be used to access
attributes.
The second part is an implementation of the NSKeyValueCoding protocol
for
the OC_PythonObject class. This makes it possible to use the Key-Value
Coding
protocol for plain python objects from Objective-C.
And the last part is an implementation for the relevant parts of the
NSKeyValueCoding protocol for hybrid Python/ObjC objects. This is
actually the
most complex bit to get right (see below).
Documentation is a problem, as usual. There is some documentation in the
online documetation but not much more.
How do we access attributes through 'keys'? For reading we try an
attribute
named 'key', if that doesn't exist we try accessor methods
(getKey and get_key) and finally we try the attribute '_key'. For
writing we
first try accessor method ('setKey' and 'set_key'), than the attribute
'_key' and finally 'key'. This is roughly simular to the way ObjC works.
For hybrid classes we use the Objective-C rules,
but check for Python attributes/properties before defering(sp?) to the
super-class implementation. The 3 implementations are consistent with
each other, thanks to the unitttests.
The major advantage over PyObjCTools.KeyValueCodingSupport is that the
new
support code allows you to use Key-Value Coding with unmodified existing
Python classes. This should make it easier to split an application into
a
platform independent core and a Cocoa GUI layer.
Open issues:
- Should probably check the accessor 'isKey' as well when reading
attributes,
but Cocoa doesn't do this, probably due to their naming convention.
- I'm not happy with the implementation of Key-Value Coding for hybrid
classes. It feels hackish.
- The implementation for setting attributes in hybrid classes is not
entirely
consistant with the default implementation mentioned in the
NSKeyValueCoding
documentation, I think it is possible to work fix this later on.
- We don't access the private instance variables (e.g. __key). IMHO
this would be a little too invasive (as well as non-trivial).
- PyObjCTools.KeyValueCoding should be removed, it is incomplete and no
longer
necessary.
Ronald
|