Skip to main content

A bridge between an Objective C runtime environment and Python.

Project description

Rubicon-ObjC is a bridge between Objective C and Python. It enables you to:

  • Use Python to instantiate objects defined in Objective C

  • Use Python to invoke methods on objects defined in Objective C

  • Subclass and extend Objective C classes in Python

It also includes wrappers of the some key data types from the Core Foundation framework (e.g., NSString).

Quickstart

Rubicon uses a combination of ctypes, plus Objective-C’s own reflection APIs, to enable Objective C objects to be referenced in a Python process.

To install Rubicon, use pip:

$ pip install rubicon-objc

Then, in a Python shell:

>>> from ctypes import cdll
>>> from ctypes import util
>>> from rubicon.objc import ObjCClass, objc_method

# Use ctypes to import a framework into the Python process
>>> cdll.LoadLibrary(util.find_library('Foundation'))

# Wrap an Objective C class contained in the framework
>>> NSURL = ObjCClass("NSURL")

# Then instantiate the Objective C class, using the API
# that is exposed through Objective C. The Python method name
# is the concatenated version of the Objective C method descriptor,
# with colons replaced with underscores. So, the equivalent of
# [NSURL URLWithString:@"http://pybee.org"];
# would be:
>>> NSURL.URLWithString_("http://pybee.org/")

# To create a new Objective C class, define a Python class that
# has the methods you want to define, decorated to provide the
# Objective C prototype:
>>> class Handler(NSObject):
...     @objc_method('@i')
...     def initWithValue_(self, value):
...         # You can't store attributes directly on the object -
...         # you need to put them manually on the Python object
...         self.__dict__['value'] = value
...         return self
...
...     @objc_method('vi')
...     def pokeWithValue_(self, value):
...         print ("Poking with", value)

# Then use the class:
>>> my_handler = Handler.alloc().initWithValue_(42)
>>> my_handler.pokeWithValue_(37)

Testing

To run the Rubicon test suite:

1. Compile the Rubicon test library. A Makefile has been provided to make this easy. Type:

$ make

to compile it.

  1. Put the Rubicon support library somewhere that it will be found by dynamic library discovery. This means:

    1. Under OS X, put the tests/objc directory in your DYLD_LIBRARY_PATH

    2. Under Linux, put the tests/objc directory in your LD_LIBRARY_PATH

    3. Under Windows…. something :-)

  2. Run the test suite:

    $ python setup.py test

    A tox configuration has also been provided; to run the tests across all supported platforms, run:

    $ tox

Community

Rubicon is part of the BeeWare suite. You can talk to the community through:

Contributing

If you experience problems with this backend, log them on GitHub. If you want to contribute code, please fork the code and submit a pull request.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rubicon-objc-0.1.1.tar.gz (24.2 kB view hashes)

Uploaded Source

Built Distribution

rubicon_objc-0.1.1-py2.py3-none-any.whl (21.6 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page