sintonia/shared
jo 7988b7467b test: allow to set python linters to fail per app 2022-01-26 10:15:35 +02:00
..
libretime_shared test(shared): fix linting 2022-01-26 10:15:35 +02:00
tests test(shared): fix linting 2022-01-26 10:15:35 +02:00
.pylintrc feat: create libretime_shared package (#1349) 2022-01-06 15:40:52 +02:00
Makefile test: allow to set python linters to fail per app 2022-01-26 10:15:35 +02:00
README.md feat(shared): add suffix to shared config models 2022-01-21 11:54:00 +02:00
pyproject.toml chore: set python build system in pyproject.toml 2022-01-26 10:15:35 +02:00
setup.py chore: make setup.py file chdir consistent 2022-01-26 10:15:35 +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(filepath="/etc/libretime/config.yml")

App

Create an app class that inherit from libretime_shared.app.AbstractApp.

from libretime_shared.app import AbstractApp

class LiquidsoapApp(AbstractApp):
    name = "liquidsoap"

    def __init__(self, some_arg, **kwargs):
        super().__init__(**kwargs)
        self.some_arg = some_arg

    def run(self):
        ...


app = LiquidsoapApp(**kwargs)
app.run()

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()