Kleinkram CLI / Python Package
Kleinkram can also be used via a command line interface (CLI) or a python package. This offers an easy way to interact with the platform in a programmatic way. In particular this can be used to upload and download files, create and update projects aswell as missions.
Usage
If you wish to use the CLI follow the Getting Started guide.
Development Setup
First you need to follow the steps described in the Getting Started (For Developers) guide.
Next make sure to navigate to the /cli directory of repository. Now setup a python3.8 virtual enviroment and install the dependencies:
virtualenv -p python3.8 .venv
source .venv/bin/activate
pip install -e . -r requirements-dev.txtAt this point you can already run the CLI locally:
klein --helpFurthermore if you run:
klein endpointthe CLI should automatically detect that you are running the CLI locally as highlighted in the output of the above command. Otherwise you should now set the CLI to use the local endpoint by running:
klein endpoint localIn case you wish to test the local version of the CLI against the dev or even prod backend you can do so by running:
klein endpoint dev
klein endpoint prodIf you want you can also run the CLI as a module which should be functionally equivalent to the above command:
python -m kleinkram
klein # same as aboveProject Structure
The /cli folder is structured as a python package using the flat layout. Which means the layout looks something like this:
cli/
├── README.md
├── pyproject.toml
├── setup.py
├── setup.cfg
├── ...
├── kleinkram/
│ ├── __init__.py
│ ├── __main__.py
│ ├── main.py
│ └── ...
├── tests/
│ ├── __init__.py
│ └── ...
└── testing/
├── __init__.py
└── ...- The
kleinkrammodule contains the actual implementation of the CLI. - The
testsmodule contains the tests for the CLI, we use thepytestframework for this. - The
testingmodule contains utilities used in tests such as fixtures (but no tests themselves).
Packaging
For packaging we use declarative metadata inside the setup.cfg. Depending on the python distrubution you are using you might need to install setuptools seperately (read more here).
Source Code
Let's take a closer look at the kleinkram module. A non-exhaustive list of the most important files and folders is given below:
__init__.py
Here we expose all the public functions that can be used if the cli is used as a python package.
__main__.py
This is the main entry point of the CLI.
main.py
This contains the main function that is called by __main__.py. If you wish to embed the CLI into another application you should import this main function.
core.py
Contains all the highlevel functions that interact with the backend.
models.py
Contains python representations for objects that also exist on the backend.
errors.py
Contains custom exceptions that are raised by the kleinkram package.
api/
In this folder we have all the bindings for the kleinkram API. This includes:
client.pyimplementation of an authenticated HTTP client (we usehttpxas a HTTP framework).pagination.pywrapper around the HTTP client that supports paginated requests via generators.deser.pydeserialization of the API responses (serialization should also go here).routes.pyall the api routes and low level functions that interact with those endpointsquery.pyabstractions for specificying resources on the backend (e.g. querying projects, missions and files)file_transfer.pyimplementation of the uploading and downloading. For the uploads we useboto3to interact with the S3 compatible storage.
cli/
Here we have all the code related to the different cli commands. Each command is mostly a wrapper around the kleinkram package with some addition argument parsing and input validation.
app.pycontains thetyperapplication that is used to define the CLI. We also implement the simpler cli commands directly in this fileerror_handlingcontains a wrapper of the defaulttyper.Typerclass that adds error handling to the CLI._*.pyare the different sub commands that are too complex to keep in a single file.