Skip to main content

A bridge between an Objective C runtime environment and Python.

Project description

http://pybee.org/project/projects/bridges/rubicon/rubicon.png

Rubicon-ObjC

https://img.shields.io/pypi/pyversions/rubicon-objc.svg https://img.shields.io/pypi/v/rubicon-objc.svg https://img.shields.io/pypi/status/rubicon-objc.svg https://img.shields.io/pypi/l/rubicon-objc.svg https://travis-ci.org/pybee/rubicon-objc.svg?branch=master https://badges.gitter.im/pybee/general.svg

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, and

  • Subclass and extend Objective-C classes in Python.

It also includes wrappers of the some key data types from the 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, NSObject, 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 Objective-C method descriptor, up to the first colon.
# The first argument (if it exists) is passed in as is; subsequent
# arguments are passed in as keyword arguments. Properties can be
# accessed and modified directly. So, the equivalent of:
# NSURL *base = [NSURL URLWithString:@"http://pybee.org"];
# NSURL *full = [NSURL URLWithString:@"contributing/" relativeToURL:base];
# NSLog(@"absoluteURL = %@", [full absoluteURL]);
# would be:
>>> base = NSURL.URLWithString("http://pybee.org/")
>>> full = NSURL.URLWithString("http://pybee.org/", relativeToURL=base)
>>> print("absoluteURL = %s" % full.absoluteURL)

# Sometimes, a method will use have the same keyword argument name twice.
# This is legal in Objective-C, but not in Python; in this case, you can
# explicitly name a descriptor by replacing colons with underscores:
>>> base = NSURL.URLWithString_("http://pybee.org/")
>>> full = NSURL.URLWithString_relativeToURL_("http://pybee.org/", base)
>>> print("absoluteURL = %s" % full.absoluteURL)

# To create a new Objective-C class, define a Python class that
# has the methods you want to define, decorate it to indicate that it
# should be exposed to the Objective-C runtime, and annotate it to
# describe the type of any arguments that aren't of type ``id``:
>>> class Handler(NSObject):
...     @objc_method
...     def initWithValue_(self, v: int):
...         self.value = v
...         return self
...
...     @objc_method
...     def pokeWithValue_(self, v: int) -> None:
...         print("Poking with", v)
...         print("Internal value is", self.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.

  2. 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 :-)

  3. 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:

We foster a welcoming and respectful community as described in our BeeWare Community Code of Conduct.

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.2.6.tar.gz (32.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rubicon_objc-0.2.6-py3-none-any.whl (25.7 kB view details)

Uploaded Python 3

File details

Details for the file rubicon-objc-0.2.6.tar.gz.

File metadata

  • Download URL: rubicon-objc-0.2.6.tar.gz
  • Upload date:
  • Size: 32.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for rubicon-objc-0.2.6.tar.gz
Algorithm Hash digest
SHA256 9d6cff0513ef7e4f20fe650587b3d4681800cb7de68a14d331d3a45a37f2ca98
MD5 8c62c0381cf64f1314599cfba1d3aa4d
BLAKE2b-256 e2a4f262297b871d85ed1d59c953427824588c857eeb486a1ba29cff46621e40

See more details on using hashes here.

File details

Details for the file rubicon_objc-0.2.6-py3-none-any.whl.

File metadata

File hashes

Hashes for rubicon_objc-0.2.6-py3-none-any.whl
Algorithm Hash digest
SHA256 be0ff9dc224225b8219d72f1f42db71f8e6c6a09b86a6fab4e20450f7578224d
MD5 b6ad618e0e68c9384489746ef01ac9b2
BLAKE2b-256 1c88c40928a827831902811b324468a5bca42dd59a7ef934efb821b0e76a8aec

See more details on using hashes here.

Supported by

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