pycapnp/README.md

221 lines
5.9 KiB
Markdown
Raw Normal View History

# pycapnp
[![Packaging Status](https://github.com/capnproto/pycapnp/workflows/Packaging%20Test/badge.svg)](https://github.com/capnproto/pycapnp/actions)
2023-01-26 16:55:21 -08:00
[![manylinux2014 Status](https://github.com/capnproto/pycapnp/workflows/manylinux2014/badge.svg)](https://github.com/capnproto/pycapnp/actions)
2020-02-09 00:42:08 -08:00
[![PyPI version](https://badge.fury.io/py/pycapnp.svg)](https://badge.fury.io/py/pycapnp)
2020-02-12 13:34:59 -08:00
[Cap'n'proto Mailing List](https://groups.google.com/forum/#!forum/capnproto) [Documentation](https://capnproto.github.io/pycapnp)
## Requirements
* 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)
- 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
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.
## Building and installation
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:
```bash
git clone https://github.com/capnproto/pycapnp.git
cd pycapnp
pip install .
```
2013-08-12 15:52:50 -07:00
If you wish to install using the latest upstream C++ Cap'n Proto:
```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" \
--install-option "--force-bundled-libcapnp" .
```
To force bundled python:
```bash
pip install --install-option "--force-bundled-libcapnp" .
```
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
```
## 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
Python 3.7+ is supported.
2015-03-02 14:42:32 -08:00
## Development
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:
```bash
python setup.py bdist_dumb
```
2015-02-03 08:37:54 -08:00
Building a Python wheel distributiion:
```bash
python setup.py bdist_wheel
```
## Documentation/Example
There is some basic documentation [here](http://capnproto.github.io/pycapnp/).
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
import os
2013-07-06 23:57:47 -07:00
import capnp
2013-09-03 01:16:48 -07:00
import addressbook_capnp
def writeAddressBook(file):
2013-09-03 01:16:48 -07:00
addresses = addressbook_capnp.AddressBook.new_message()
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'
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'
bobPhones = bob.init('phones', 2)
2013-07-06 23:57:47 -07:00
bobPhones[0].number = "555-4567"
bobPhones[0].type = 'home'
bobPhones[1].number = "555-7654"
bobPhones[1].type = 'work'
2013-07-06 23:58:28 -07:00
bob.employment.unemployed = None
2013-07-06 23:57:47 -07:00
addresses.write(file)
2013-07-06 23:57:47 -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
for person in addresses.people:
print(person.name, ':', person.email)
2013-07-06 23:57:47 -07:00
for phone in person.phones:
print(phone.type, ':', phone.number)
2013-07-06 23:57:47 -07:00
which = person.employment.which()
print(which)
2013-07-06 23:57:47 -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')
print()
if __name__ == '__main__':
f = open('example', 'w')
writeAddressBook(f)
2013-07-06 23:57:47 -07:00
f = open('example', 'r')
printAddressBook(f)
2013-07-06 23:57:47 -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)
```