Before proceeding with installation, you will need to ensure that you have the buf CLI installed.
If $ which buf generates a path to the executable, you may proceed to the installation steps.
Installing Sift protos into your project is done in two phases:
- The first phase is generating a local Python package out of the code generated from the Sift protos.
- The second phase is using
pip installto add the Sift generated code as a dependency along with any other missing dependencies.
To get started, make sure that you have python3 installed as well as an empty directory that will serve as the place to build the
Sift python package. Inside of that directory, it is recommended that you create a virtual environment. Example:
$ python -m venv venvOnce that is done, proceed with the following steps.
- Clone this repository onto your local machine and
cdinto it:
$ git clone https://github.com/sift-stack/sift
$ cd sift- Assuming the path to the aforementioned empty directory where we'll build the package is
$PACKAGE_DIR, run the following command in thesiftdirectory that you just cloned:
$ buf export protos --output=$PACKAGE_DIR/protos --config protos/buf.yamlThe Sift protos and its imports can now be found in your $PACKAGE_DIR/protos directory.
- Copy the
buftemplate for Python to$PACKAGE_DIR
$ cp buf_templates/buf.gen.python.yaml $PACKAGE_DIR/buf.gen.yamlCopy setup.py as well:
$ cp scripts/setup.py $PACKAGE_DIR/setup.py-
cdinto$PACKAGE_DIR. -
Once inside of
$PACKAGE_DIR, ensure thatbuf.gen.yamlis at the root. -
Compile your protobufs.
$ buf generate protosYour project up to this point should look like the following (full depth not shown and virtual env files omitted):
python_example
├─ buf.gen.yaml
├─ gen
│ ├─ protoc_gen_openapiv2
│ │ └─ options
│ ├─ google
│ │ └─ api
│ └─ sift
│ ├─ notifications
│ ├─ data
│ ├─ runs
│ ├─ users
│ ├─ rules
│ ├─ assets
│ ├─ tags
│ ├─ calculated_channels
│ ├─ annotations
│ ├─ common
│ └─ annotation_logs
└─ protos
├─ protoc-gen-openapiv2
│ └─ options
├─ google
│ └─ api
└─ sift
├─ common
├─ notifications
├─ tags
├─ runs
├─ assets
├─ data
├─ rules
├─ users
├─ calculated_channels
├─ annotations
└─ annotation_logs
- Execute the following script from your package root (i.e.
$PACKAGE_DIR) to turn each directory in the generated code into a Python module:
for dir in $(find gen -type d); do
touch $dir/__init__.py
done- Ensure that your virtual environment is active:
$ source venv/bin/activate- Install the following dependencies:
$ pip install build protobuf grpcio-
Inspect
setup.pyand customize the metadata if necessary. -
Build the source distribution and wheel to generate the Python package:
$ python -m build --sdist && python -m build --wheel- Once that is complete, you should now have a
distin$PACKAGE_DIRwhich contains your Python package. For a givensetup.pythat looks like this:
from setuptools import setup, find_packages
setup(
name='sift_protos',
version='0.1',
author='Sift Stack',
author_email='siftstack@example.com',
description='Sift generated protos',
packages=find_packages('gen'),
package_dir={'': 'gen'},
)the generated wheel file should be outputted into $PACKAGE_DIR/dist/sift_protos-0.1-py3-none-any.whl.
- Now from your actual Python project, you can install the newly generated package via
pip:
$ pip install $PACKAGE_DIR/sift_protos-0.1-py3-none-any.whl- Now your project should be ready to use the generated Python code to interact with Sift's gRPC API. Please refer to the example code for usage.