About
This package contains a set of utilities useful for building python libraries, scripts, and command-line utilities
It's designed to be easy to include in other projects. all of its mainline dependencies are vendored and all modules which have external un-vendorable dependencies are available as optional extras
Install
pip install si-utils
To make use of optional extras, like the yaml module or the dev_utils module:
pip install si-utils[yaml] # or si-utils[dev_utils], or si-utils[yaml,sentry]
Usage
This toolkit makes it really easy to write small, simple, well designed CLI utilities In fact, the aim of this project is to make well-engineered CLIs almost as easy to write and deploy as basic python scripts it leverages a lot of really fantastic modern libraries and tools to do so, like poetry, typer, and loguru
let's assume you've created a new project with poetry new, and you want to add a CLI interface to it. here's one way to do that:
create a common.py files with your super handy dandy useful function in it:
from loguru import logger
def my_super_awesome_function():
logger.debug("I'm running my_package.common.my_super_awesome_function!")
create a __main__.py file in your package like so:
import os
from .common import my_super_awesome_function
import si_utils
import typer
from loguru import logger
app = typer.Typer()
@app.callback()
def callback(verbose: bool = False):
"""
Here is my app's main help string
Users will see this when they run `python -m my_package`
"""
log_level = 'DEBUG' if verbose else 'INFO'
si_utils.configure_logging(
'my_app_name',
stderr_level=log_level,
logfile_level='DEBUG',
sentry_level=None)
@app.command()
def my_command(option: bool):
"""
Here's the help text my users will see when they run `python -m my_package my-command -h`
"""
logger.debug("running my-command") # this will only get printed if the --verbose flag is set
my_super_awesome_function(option)
if __name__ == "__main__":
if os.environ.get('PYDEBUG'):
# we're in a debugger session
# here we can put whatever debugging code we want
# we can configure logging so all messages up to DEBUG are logged to stderr, and nothing gets logged to file:
configure_logging('my_app_name', 'DEBUG', None, None)
# do debugging stuff here
logger.debug("I'm a debug message!")
exit()
try:
app() # cli code goes here
except KeyboardInterrupt:
print("Aborted!")
exit()
the main api (all the stuff directly importable from si_utils) consists of:
- every function defined in the
mainmodule - the
configure_loggingfunction from thelogmodule
configure_logging has an option to enable logging to sentry. in order to use it, you need to install si_utils with the sentry extra (ie pip install si-utils[sentry] or poetry add -D si-utils[sentry])
apart from that, there are other modules which can be imported separately:
yaml has a whole bunch of useful and sometimes esoteric utilities for working with yaml files, built on top of ruamel.yaml
dev_utils has commmand-line utilities for working with python projects, specifically made for projects that use poetry
Metadata
Release files for si-utils 0.4.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| si_utils-0.4.3.tar.gz | 193.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| si_utils-0.4.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 408.1 kB
Release files / si_utils-0.4.3.tar.gz
| Download URL | si_utils-0.4.3.tar.gz |
|---|---|
| Size | 193.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d9a4e38069a9fc120c040139812c6b725e268585537e004b303ac331ca957c5b
|
|
BLAKE2b-256 checksum How to use checksums |
2fc2afb797ddcf6c140018236183a9d244cca69ca37142a329ecec08ee32f377
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.4 CPython/3.6.8 Linux/3.10.0-1062.18.1.el7.x86_64
|
Release files / si_utils-0.4.3-py3-none-any.whl
| Download URL | si_utils-0.4.3-py3-none-any.whl |
|---|---|
| Size | 214.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
58fa4f155b28f50a94cc1ae2b10d3065c92304414f00e76fff7d6517ca552f2c
|
|
BLAKE2b-256 checksum How to use checksums |
4725d6962ffd429495e8a53c6b0262e6b5dbee0e2c3455c34f313dba0038b024
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.4 CPython/3.6.8 Linux/3.10.0-1062.18.1.el7.x86_64
|