2019-12-11 22:20:44 -08:00
# pycapnp
2019-10-14 11:19:33 -07:00
2019-12-26 23:16:40 -08:00
[](https://github.com/capnproto/pycapnp/actions)
2023-01-26 16:55:21 -08:00
[](https://github.com/capnproto/pycapnp/actions)
2020-02-09 00:42:08 -08:00
[](https://badge.fury.io/py/pycapnp)
2020-02-12 13:34:59 -08:00
2020-06-08 00:31:33 -07:00
[Cap'n'proto Mailing List ](https://groups.google.com/forum/#!forum/capnproto ) [Documentation ](https://capnproto.github.io/pycapnp )
2013-07-06 18:40:05 -07:00
2013-08-26 10:08:34 -07:00
2013-07-06 18:40:05 -07:00
## Requirements
2019-10-14 11:19:33 -07:00
* C++14 supported compiler
- gcc 6.1+ (5+ may work)
- clang 6 (3.4+ may work)
- Visual Studio 2017+
* cmake (needed for bundled capnproto)
- ninja (macOS + Linux)
- Visual Studio 2017+
2023-01-26 16:55:21 -08:00
* capnproto-0.10 (>=0.7.0 will also work if linking to system libraries)
2019-10-14 11:19:33 -07:00
- Not necessary if using bundled capnproto
2021-06-02 14:10:31 +08:00
* Python development headers (i.e. Python.h)
- Distributables from python.org include these, however they are usually in a separate package on Linux distributions
2019-10-14 11:19:33 -07:00
32-bit Linux requires that capnproto be compiled with `-fPIC` . This is usually set correctly unless you are compiling canproto yourself. This is also called `-DCMAKE_POSITION_INDEPENDENT_CODE=1` for cmake.
pycapnp has additional development dependencies, including cython and pytest. See requirements.txt for them all.
2014-12-15 12:24:24 -08:00
2013-07-06 18:40:05 -07:00
## Building and installation
2019-12-11 22:20:44 -08:00
Install with `pip install pycapnp` . You can set the CC environment variable to control which compiler is used, ie `CC=gcc-8.2 pip install pycapnp` .
2013-08-12 15:52:50 -07:00
Or you can clone the repo like so:
2019-10-14 11:19:33 -07:00
```bash
2019-12-11 22:20:44 -08:00
git clone https://github.com/capnproto/pycapnp.git
cd pycapnp
2019-10-14 11:19:33 -07:00
pip install .
```
2013-08-12 15:52:50 -07:00
2019-10-14 11:19:33 -07:00
If you wish to install using the latest upstream C++ Cap'n Proto:
2013-10-02 00:34:26 -07:00
2019-10-14 11:19:33 -07:00
```bash
pip install \
--install-option "--libcapnp-url" \
2020-06-08 11:14:47 -07:00
--install-option "https://github.com/capnproto/capnproto/archive/master.tar.gz" \
2019-10-14 11:19:33 -07:00
--install-option "--force-bundled-libcapnp" .
```
2013-07-06 18:40:05 -07:00
2019-10-14 11:19:33 -07:00
To force bundled python:
2016-02-17 17:23:04 -08:00
2019-10-14 11:19:33 -07:00
```bash
pip install --install-option "--force-bundled-libcapnp" .
```
2016-02-17 17:23:04 -08:00
2020-06-03 22:49:52 -07:00
Slightly more prompt error messages using distutils rather than pip.
```bash
python setup.py install --force-bundled-libcapnp
```
The bundling system isn't that smart so it might be necessary to clean up the bundled build when changing versions:
```bash
python setup.py clean
```
2016-02-17 17:23:04 -08:00
2023-04-24 09:34:25 -07:00
## Stub-file generation
While not directly supported by pycapnp, a tool has been created to help generate pycapnp stubfile to assist with development (this is very helpful if you're new to pypcapnp!). See [#289 ](https://github.com/capnproto/pycapnp/pull/289#event-9078216721 ) for more details.
[Python Capnp Stub Generator ](https://gitlab.com/mic_public/tools/python-helpers/capnp-stub-generator )
2015-03-02 14:42:32 -08:00
## Python Versions
2019-10-14 11:19:33 -07:00
Python 3.7+ is supported.
2015-03-02 14:42:32 -08:00
## Development
2019-10-14 11:19:33 -07:00
Git flow has been abandoned, use master.
To test, use a pipenv (or install requirements.txt and run pytest manually).
```bash
pip install pipenv
pipenv install
pipenv run pytest
```
2015-03-02 14:42:32 -08:00
2015-02-03 08:37:54 -08:00
### Binary Packages
Building a dumb binary distribution:
2019-10-14 11:19:33 -07:00
```bash
python setup.py bdist_dumb
```
2015-02-03 08:37:54 -08:00
Building a Python wheel distributiion:
2019-10-14 11:19:33 -07:00
```bash
python setup.py bdist_wheel
```
2013-07-06 18:40:05 -07:00
## Documentation/Example
2019-10-14 11:19:33 -07:00
2020-06-08 00:31:33 -07:00
There is some basic documentation [here ](http://capnproto.github.io/pycapnp/ ).
2013-07-06 18:40:05 -07:00
2019-11-27 10:48:50 -08:00
Make sure to look at the [examples ](examples ). The examples are generally kept up to date with the recommended usage of the library.
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
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)
```