refactor(shared): explode config into multiple files (#1987)
This commit is contained in:
parent
c395fd7f0a
commit
e75426bc4c
|
@ -0,0 +1,8 @@
|
||||||
|
from ._base import DEFAULT_CONFIG_FILEPATH, DEFAULT_ENV_PREFIX, BaseConfig
|
||||||
|
from ._models import (
|
||||||
|
DatabaseConfig,
|
||||||
|
GeneralConfig,
|
||||||
|
RabbitMQConfig,
|
||||||
|
StorageConfig,
|
||||||
|
no_trailing_slash_validator,
|
||||||
|
)
|
|
@ -1,19 +1,16 @@
|
||||||
import sys
|
import sys
|
||||||
from os import environ
|
from os import environ
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union
|
from typing import Any, Dict, List, Optional, Union
|
||||||
|
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
|
|
||||||
# pylint: disable=no-name-in-module
|
# pylint: disable=no-name-in-module
|
||||||
from pydantic import AnyHttpUrl, BaseModel, ValidationError, validator
|
from pydantic import BaseModel, ValidationError
|
||||||
from pydantic.fields import ModelField
|
from pydantic.fields import ModelField
|
||||||
from pydantic.utils import deep_update
|
from pydantic.utils import deep_update
|
||||||
from yaml import YAMLError, safe_load
|
from yaml import YAMLError, safe_load
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
|
||||||
from pydantic.typing import AnyClassMethod
|
|
||||||
|
|
||||||
DEFAULT_ENV_PREFIX = "LIBRETIME"
|
DEFAULT_ENV_PREFIX = "LIBRETIME"
|
||||||
DEFAULT_CONFIG_FILEPATH = Path("/etc/libretime/config.yml")
|
DEFAULT_CONFIG_FILEPATH = Path("/etc/libretime/config.yml")
|
||||||
|
|
||||||
|
@ -105,61 +102,3 @@ class BaseConfig(BaseModel):
|
||||||
logger.error(f"config file '{filepath}' is not a valid yaml file: {error}")
|
logger.error(f"config file '{filepath}' is not a valid yaml file: {error}")
|
||||||
|
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
|
|
||||||
def no_trailing_slash_validator(key: str) -> "AnyClassMethod":
|
|
||||||
# pylint: disable=unused-argument
|
|
||||||
def strip_trailing_slash(cls: Any, value: str) -> str:
|
|
||||||
return value.rstrip("/")
|
|
||||||
|
|
||||||
return validator(key, pre=True, allow_reuse=True)(strip_trailing_slash)
|
|
||||||
|
|
||||||
|
|
||||||
# pylint: disable=too-few-public-methods
|
|
||||||
class GeneralConfig(BaseModel):
|
|
||||||
public_url: AnyHttpUrl
|
|
||||||
api_key: str
|
|
||||||
|
|
||||||
# Validators
|
|
||||||
_public_url_no_trailing_slash = no_trailing_slash_validator("public_url")
|
|
||||||
|
|
||||||
|
|
||||||
# pylint: disable=too-few-public-methods
|
|
||||||
class StorageConfig(BaseModel):
|
|
||||||
path: str = "/srv/libretime"
|
|
||||||
|
|
||||||
# Validators
|
|
||||||
_path_no_trailing_slash = no_trailing_slash_validator("path")
|
|
||||||
|
|
||||||
|
|
||||||
# pylint: disable=too-few-public-methods
|
|
||||||
class DatabaseConfig(BaseModel):
|
|
||||||
host: str = "localhost"
|
|
||||||
port: int = 5432
|
|
||||||
name: str = "libretime"
|
|
||||||
user: str = "libretime"
|
|
||||||
password: str = "libretime"
|
|
||||||
|
|
||||||
@property
|
|
||||||
def url(self) -> str:
|
|
||||||
return (
|
|
||||||
f"postgresql://{self.user}:{self.password}"
|
|
||||||
f"@{self.host}:{self.port}/{self.name}"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
# pylint: disable=too-few-public-methods
|
|
||||||
class RabbitMQConfig(BaseModel):
|
|
||||||
host: str = "localhost"
|
|
||||||
port: int = 5672
|
|
||||||
name: str = "libretime"
|
|
||||||
user: str = "libretime"
|
|
||||||
password: str = "libretime"
|
|
||||||
vhost: str = "/libretime"
|
|
||||||
|
|
||||||
@property
|
|
||||||
def url(self) -> str:
|
|
||||||
return (
|
|
||||||
f"amqp://{self.user}:{self.password}"
|
|
||||||
f"@{self.host}:{self.port}/{self.vhost}"
|
|
||||||
)
|
|
|
@ -0,0 +1,65 @@
|
||||||
|
from typing import TYPE_CHECKING, Any
|
||||||
|
|
||||||
|
# pylint: disable=no-name-in-module
|
||||||
|
from pydantic import AnyHttpUrl, BaseModel, validator
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from pydantic.typing import AnyClassMethod
|
||||||
|
|
||||||
|
|
||||||
|
def no_trailing_slash_validator(key: str) -> "AnyClassMethod":
|
||||||
|
# pylint: disable=unused-argument
|
||||||
|
def strip_trailing_slash(cls: Any, value: str) -> str:
|
||||||
|
return value.rstrip("/")
|
||||||
|
|
||||||
|
return validator(key, pre=True, allow_reuse=True)(strip_trailing_slash)
|
||||||
|
|
||||||
|
|
||||||
|
# pylint: disable=too-few-public-methods
|
||||||
|
class GeneralConfig(BaseModel):
|
||||||
|
public_url: AnyHttpUrl
|
||||||
|
api_key: str
|
||||||
|
|
||||||
|
# Validators
|
||||||
|
_public_url_no_trailing_slash = no_trailing_slash_validator("public_url")
|
||||||
|
|
||||||
|
|
||||||
|
# pylint: disable=too-few-public-methods
|
||||||
|
class StorageConfig(BaseModel):
|
||||||
|
path: str = "/srv/libretime"
|
||||||
|
|
||||||
|
# Validators
|
||||||
|
_path_no_trailing_slash = no_trailing_slash_validator("path")
|
||||||
|
|
||||||
|
|
||||||
|
# pylint: disable=too-few-public-methods
|
||||||
|
class DatabaseConfig(BaseModel):
|
||||||
|
host: str = "localhost"
|
||||||
|
port: int = 5432
|
||||||
|
name: str = "libretime"
|
||||||
|
user: str = "libretime"
|
||||||
|
password: str = "libretime"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def url(self) -> str:
|
||||||
|
return (
|
||||||
|
f"postgresql://{self.user}:{self.password}"
|
||||||
|
f"@{self.host}:{self.port}/{self.name}"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# pylint: disable=too-few-public-methods
|
||||||
|
class RabbitMQConfig(BaseModel):
|
||||||
|
host: str = "localhost"
|
||||||
|
port: int = 5672
|
||||||
|
name: str = "libretime"
|
||||||
|
user: str = "libretime"
|
||||||
|
password: str = "libretime"
|
||||||
|
vhost: str = "/libretime"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def url(self) -> str:
|
||||||
|
return (
|
||||||
|
f"amqp://{self.user}:{self.password}"
|
||||||
|
f"@{self.host}:{self.port}/{self.vhost}"
|
||||||
|
)
|
Loading…
Reference in New Issue