2014-01-27 16:07:09 -08:00
# pycapnp
2013-07-06 18:40:05 -07:00
2013-09-01 01:15:31 -07:00
More thorough docs are available at [http://jparyani.github.io/pycapnp/ ](http://jparyani.github.io/pycapnp/ ).
2013-08-26 10:08:34 -07:00
2013-07-06 18:40:05 -07:00
## Requirements
2015-03-02 14:42:32 -08:00
pycapnp's distribution has no requirements beyond a C++11 compatible compiler. GCC 4.8+ or Clang 3.3+ should work fine.
2014-12-15 12:24:24 -08:00
2015-03-02 14:42:32 -08:00
pycapnp has additional development dependencies, including cython and py.test. See requirements.txt for them all.
2013-07-06 18:40:05 -07:00
## Building and installation
2013-09-03 22:10:37 -07:00
Install with `pip install pycapnp` . You can set the CC environment variable to control which compiler is used, ie `CC=gcc-4.8 pip install pycapnp` .
2013-08-12 15:52:50 -07:00
Or you can clone the repo like so:
2013-09-01 01:15:31 -07:00
git clone https://github.com/jparyani/pycapnp.git
2015-02-23 11:21:19 -08:00
pip install --install-option '--force-cython' ./pycapnp
2013-08-12 15:52:50 -07:00
2015-03-02 14:42:32 -08:00
Note: for OSX, if using clang from Xcode 5, you may need to set `CFLAGS` like so:
2013-10-02 00:34:26 -07:00
CFLAGS='-stdlib=libc++' pip install pycapnp
2013-07-06 18:40:05 -07:00
2016-02-17 17:23:04 -08:00
If you wish to install using the latest upstream C++ Cap'n Proto:
pip install --install-option "--libcapnp-url" --install-option "https://github.com/sandstorm-io/capnproto/archive/master.tar.gz" --install-option "--force-bundled-libcapnp" .
2015-03-02 14:42:32 -08:00
## Python Versions
Python 2.6/2.7 are supported as well as Python 3.2+. PyPy 2.1+ is also supported.
One oddity to note is that `Text` type fields will be treated as byte strings under Python 2, and unicode strings under Python 3. `Data` fields will always be treated as byte strings.
## Development
This project uses [git-flow ](http://jeffkreeftmeijer.com/2010/why-arent-you-using-git-flow/ ). Essentially, just make sure you do your changes in the `develop` branch. You can run the tests by installing pytest with `pip install pytest` , and then run `py.test` from the `test` directory.
2015-02-03 08:37:54 -08:00
### Binary Packages
Building a dumb binary distribution:
2017-02-07 08:51:31 -05:00
python setup.py bdist_dumb
2015-02-03 08:37:54 -08:00
Building a Python wheel distributiion:
2017-02-07 08:51:31 -05:00
python setup.py bdist_wheel
2015-02-03 08:37:54 -08:00
2015-02-20 17:20:35 -08:00
If it fails with an error like `clang: error: no such file or directory: 'capnp/lib/capnp.cpp'` , then you need to cythonize fist. This can be done with:
python setup.py build --force-cython
2013-07-06 18:40:05 -07:00
## Documentation/Example
2013-09-01 01:15:31 -07:00
There is some basic documentation [here ](http://jparyani.github.io/pycapnp/ ).
2013-07-06 18:40:05 -07:00
2015-03-02 14:42:32 -08:00
The examples directory has one example that shows off pycapnp quite nicely. Here it is, reproduced:
2013-08-12 12:39:35 -07:00
2013-07-06 23:57:47 -07:00
```python
2013-09-01 20:10:57 -07:00
from __future__ import print_function
import os
2013-07-06 23:57:47 -07:00
import capnp
2013-09-03 01:16:48 -07:00
import addressbook_capnp
2013-09-01 20:10:57 -07:00
def writeAddressBook(file):
2013-09-03 01:16:48 -07:00
addresses = addressbook_capnp.AddressBook.new_message()
2013-09-01 20:10:57 -07:00
people = addresses.init('people', 2)
2013-07-06 23:57:47 -07:00
alice = people[0]
alice.id = 123
alice.name = 'Alice'
alice.email = 'alice@example .com'
2013-09-01 20:10:57 -07:00
alicePhones = alice.init('phones', 1)
2013-07-06 23:57:47 -07:00
alicePhones[0].number = "555-1212"
alicePhones[0].type = 'mobile'
alice.employment.school = "MIT"
bob = people[1]
bob.id = 456
bob.name = 'Bob'
bob.email = 'bob@example .com'
2013-09-01 20:10:57 -07:00
bobPhones = bob.init('phones', 2)
2013-07-06 23:57:47 -07:00
bobPhones[0].number = "555-4567"
bobPhones[0].type = 'home'
2013-09-01 20:10:57 -07:00
bobPhones[1].number = "555-7654"
2013-08-12 10:47:31 -07:00
bobPhones[1].type = 'work'
2013-07-06 23:58:28 -07:00
bob.employment.unemployed = None
2013-07-06 23:57:47 -07:00
2013-09-01 20:10:57 -07:00
addresses.write(file)
2013-07-06 23:57:47 -07:00
2013-09-01 20:10:57 -07:00
def printAddressBook(file):
2013-09-03 01:16:48 -07:00
addresses = addressbook_capnp.AddressBook.read(file)
2013-07-06 23:57:47 -07:00
2013-09-01 20:10:57 -07:00
for person in addresses.people:
print(person.name, ':', person.email)
2013-07-06 23:57:47 -07:00
for phone in person.phones:
2013-09-01 20:10:57 -07:00
print(phone.type, ':', phone.number)
2013-07-06 23:57:47 -07:00
which = person.employment.which()
2013-09-01 20:10:57 -07:00
print(which)
2013-07-06 23:57:47 -07:00
2013-08-12 10:47:31 -07:00
if which == 'unemployed':
print('unemployed')
elif which == 'employer':
print('employer:', person.employment.employer)
elif which == 'school':
print('student at:', person.employment.school)
elif which == 'selfEmployed':
print('self employed')
2013-09-01 20:10:57 -07:00
print()
if __name__ == '__main__':
f = open('example', 'w')
writeAddressBook(f)
2013-07-06 23:57:47 -07:00
2013-09-01 20:10:57 -07:00
f = open('example', 'r')
printAddressBook(f)
2013-07-06 23:57:47 -07:00
```
2013-08-12 17:20:58 -07:00
2015-03-02 14:42:32 -08:00
Also, pycapnp has gained RPC features that include pipelining and a promise style API. Refer to the calculator example in the examples directory for a much better demonstration:
```python
import capnp
import socket
import test_capability_capnp
class Server(test_capability_capnp.TestInterface.Server):
def __init__ (self, val=1):
self.val = val
def foo(self, i, j, **kwargs):
return str(i * 5 + self.val)
def server(write_end):
server = capnp.TwoPartyServer(write_end, bootstrap=Server(100))
def client(read_end):
client = capnp.TwoPartyClient(read_end)
cap = client.bootstrap()
cap = cap.cast_as(test_capability_capnp.TestInterface)
remote = cap.foo(i=5)
response = remote.wait()
assert response.x == '125'
if __name__ == '__main__':
read_end, write_end = socket.socketpair(socket.AF_UNIX)
# This is a toy example using socketpair.
# In real situations, you can use any socket.
server(write_end)
client(read_end)
```
2013-08-21 17:45:13 -07:00
## Common Problems
If you get an error on installation like:
...
gcc-4.8: error: capnp/capnp.c: No such file or directory
gcc-4.8: fatal error: no input files
Then you have too old a version of setuptools. Run `pip install -U setuptools` then try again.
2013-08-12 17:20:58 -07:00
2013-10-20 17:27:18 -07:00
[](https://travis-ci.org/jparyani/pycapnp)