feat: create libretime_shared package (#1349)
* feat: create libretime_shared package - We don't use pydantic.BaseSettings because of some incompatble loading behavior. * fix: whitelist pydantic in pylintrc * docs: update to new BaseConfig behavior * docs: change confusing config filepath
This commit is contained in:
parent
ba8b51af76
commit
3a615cafa0
13 changed files with 568 additions and 0 deletions
77
shared/README.md
Normal file
77
shared/README.md
Normal file
|
@ -0,0 +1,77 @@
|
|||
# Shared
|
||||
|
||||
The `libretime_shared` package contains reusable functions and classes for the Libretime project.
|
||||
|
||||
## Usage
|
||||
|
||||
This library assumes that:
|
||||
|
||||
- You will use [`Click`](https://github.com/pallets/click) to build a CLI for your app.
|
||||
- You will use [`Loguru`](https://github.com/delgan/loguru) to log messages from your app.
|
||||
- You will use [`Pydantic`](https://github.com/samuelcolvin/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`.
|
||||
|
||||
```py
|
||||
from pydantic import BaseModel
|
||||
|
||||
from libretime_shared.config import RabbitMQ, BaseConfig
|
||||
|
||||
class Analyzer(BaseModel):
|
||||
bpm_enabled: bool = False
|
||||
bpm_track_max_length: int
|
||||
|
||||
class Config(BaseConfig):
|
||||
rabbitmq: RabbitMQ
|
||||
analyzer: Analyzer
|
||||
|
||||
config = Config(filepath="/etc/libretime/config.yml")
|
||||
```
|
||||
|
||||
### App
|
||||
|
||||
Create an app class that inherit from `libretime_shared.app.AbstractApp`.
|
||||
|
||||
```py
|
||||
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.
|
||||
|
||||
```py
|
||||
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()
|
||||
```
|
Loading…
Add table
Add a link
Reference in a new issue