2013-09-01 01:15:31 -07: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
2013-11-14 00:19:52 -08:00
First you need a system-wide installation of the Cap'n Proto C++ library == 0.4.x. Follow the [official installation docs ](http://kentonv.github.io/capnproto/install.html ) or for the lazy:
2013-08-21 17:28:02 -07:00
2013-08-21 17:45:13 -07:00
```bash
2013-12-12 00:03:38 -08:00
curl -O http://capnproto.org/capnproto-c++-0.4.0.tar.gz
tar zxf capnproto-c++-0.4.0.tar.gz
cd capnproto-c++-0.4.0
2013-08-22 08:31:07 -07:00
./configure
2013-09-04 10:38:00 -07:00
make -j6 check
2013-08-22 08:31:07 -07:00
sudo make install
2013-08-21 17:45:13 -07:00
```
2013-08-26 11:12:56 -07:00
A recent version of cython and setuptools is also required. You can install these with:
2013-08-21 17:45:13 -07:00
2013-08-22 08:31:07 -07:00
```bash
2013-08-26 11:12:56 -07:00
pip install -U cython
2013-08-21 17:45:13 -07:00
pip install -U setuptools
```
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
2013-10-02 00:34:26 -07:00
pip install ./pycapnp
2013-08-12 15:52:50 -07:00
2013-10-02 00:34:26 -07:00
Note: for OSX, if using clang from Xcode 5, you will need to set `CFLAGS` like so:
CFLAGS='-stdlib=libc++' pip install pycapnp
2013-07-06 18:40:05 -07:00
2013-08-28 22:55:10 -07:00
## Development
2013-09-20 03:09:07 -07:00
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.
2013-08-28 22:55:10 -07:00
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
2013-07-07 00:11:45 -07:00
The examples directory has one example that shows off the capabilities 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
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-09-19 21:27:20 -07:00
An error like:
...
capnp/capnp.cpp:312:10: fatal error: 'capnp/dynamic.h' file not found
#include "capnp/dynamic.h"
Means you haven't installed the Cap'n Proto C++ library. Please follow the directions at the [official installation docs ](http://kentonv.github.io/capnproto/install.html )
2013-10-20 17:27:18 -07:00
[](https://travis-ci.org/jparyani/pycapnp)
[](https://bitdeli.com/free "Bitdeli Badge")