sintonia/shared
Jonas L 96ded62c32
chore: release 3.0.0 (#2216)
2022-10-10 17:51:15 +02:00
..
libretime_shared feat: move timezone preference to config file (#2096) 2022-09-14 12:48:08 +02:00
tests feat: move timezone preference to config file (#2096) 2022-09-14 12:48:08 +02:00
Makefile test(shared): add bandit linter check 2022-02-14 21:01:35 +02:00
README.md feat(shared): pass config data via init (#2042) 2022-08-12 15:12:39 +02:00
packages.ini fix(shared): install tzdata distibutions package (#2105) 2022-09-08 17:50:39 +02:00
pyproject.toml chore: revert pin setuptools to <64.0.0 for build backend 2022-08-25 09:51:36 +02:00
requirements.txt feat: move timezone preference to config file (#2096) 2022-09-14 12:48:08 +02:00
setup.py chore: release 3.0.0 (#2216) 2022-10-10 17:51:15 +02:00

README.md

Shared

The libretime_shared package contains reusable functions and classes for the LibreTime project.

Usage

This library assumes that:

  • You will use Click to build a CLI for your app.
  • You will use Loguru to log messages from your app.
  • You will use Pydantic to validate objects in your app.

Configuration

First define a schema for your configuration in order to validate it. A schema is a class that inherit from pydantic.BaseModel. Some existing schemas can be reused such as libretime_shared.config.RabbitMQ or libretime_shared.config.Database.

Load your configuration using a subclass of libretime_shared.config.BaseConfig.

from pydantic import BaseModel

from libretime_shared.config import RabbitMQConfig, BaseConfig

class AnalyzerConfig(BaseModel):
    bpm_enabled: bool = False
    bpm_track_max_length: int

class Config(BaseConfig):
    rabbitmq: RabbitMQConfig
    analyzer: AnalyzerConfig

config = Config("/etc/libretime/config.yml")

Don't instantiate a sub model if it has a required field, otherwise the Config class import will raise a ValidationError.

CLI

Decorate your CLI commands with the shared decorators to add extra flags.

import click
from libretime_shared.cli import cli_logging_options, cli_config_options

from .app import App

@click.group()
def cli():
    pass

@cli.command()
@cli_config_options()
@cli_logging_options()
def run(**kwargs):
    app = App(**kwargs)
    return app.run()