add "ticker" config component, move LogConfig to __init__
This commit is contained in:
parent
077ac8efa5
commit
1d62e59052
3 changed files with 42 additions and 31 deletions
|
@ -7,6 +7,38 @@ This file: Sets up logging.
|
|||
|
||||
import logging.config
|
||||
|
||||
from .config import LogConfig
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class LogConfig(BaseModel):
|
||||
"""
|
||||
Logging configuration to be set for the server.
|
||||
https://stackoverflow.com/a/67937084
|
||||
"""
|
||||
|
||||
LOG_FORMAT: str = "%(levelprefix)s [%(asctime)s] %(name)s: %(message)s"
|
||||
LOG_LEVEL: str = "DEBUG"
|
||||
|
||||
# Logging config
|
||||
version = 1
|
||||
disable_existing_loggers = False
|
||||
formatters = {
|
||||
"default": {
|
||||
"()": "uvicorn.logging.DefaultFormatter",
|
||||
"fmt": LOG_FORMAT,
|
||||
"datefmt": "%Y-%m-%d %H:%M:%S",
|
||||
},
|
||||
}
|
||||
handlers = {
|
||||
"default": {
|
||||
"formatter": "default",
|
||||
"class": "logging.StreamHandler",
|
||||
"stream": "ext://sys.stderr",
|
||||
},
|
||||
}
|
||||
loggers = {
|
||||
"ovdashboard_api": {"handlers": ["default"], "level": LOG_LEVEL},
|
||||
}
|
||||
|
||||
|
||||
logging.config.dictConfig(LogConfig().dict())
|
||||
|
|
|
@ -13,35 +13,13 @@ from webdav3.exceptions import RemoteResourceNotFound
|
|||
from .dav_file import DavFile
|
||||
|
||||
|
||||
class LogConfig(BaseModel):
|
||||
class TickerConfig(BaseModel):
|
||||
"""
|
||||
Logging configuration to be set for the server.
|
||||
https://stackoverflow.com/a/67937084
|
||||
Sections "[ticker.*]" in "config.txt".
|
||||
"""
|
||||
|
||||
LOG_FORMAT: str = "%(levelprefix)s [%(asctime)s] %(name)s: %(message)s"
|
||||
LOG_LEVEL: str = "DEBUG"
|
||||
|
||||
# Logging config
|
||||
version = 1
|
||||
disable_existing_loggers = False
|
||||
formatters = {
|
||||
"default": {
|
||||
"()": "uvicorn.logging.DefaultFormatter",
|
||||
"fmt": LOG_FORMAT,
|
||||
"datefmt": "%Y-%m-%d %H:%M:%S",
|
||||
},
|
||||
}
|
||||
handlers = {
|
||||
"default": {
|
||||
"formatter": "default",
|
||||
"class": "logging.StreamHandler",
|
||||
"stream": "ext://sys.stderr",
|
||||
},
|
||||
}
|
||||
loggers = {
|
||||
"ovdashboard_api": {"handlers": ["default"], "level": LOG_LEVEL},
|
||||
}
|
||||
separator: str = " +++ "
|
||||
comment_marker: str = "#"
|
||||
|
||||
|
||||
class ImageConfig(BaseModel):
|
||||
|
@ -61,8 +39,7 @@ class Config(BaseModel):
|
|||
Main representation of "config.txt".
|
||||
"""
|
||||
|
||||
ticker_separator: str = " +++ "
|
||||
|
||||
ticker: TickerConfig = TickerConfig()
|
||||
image: ImageConfig = ImageConfig()
|
||||
|
||||
@classmethod
|
||||
|
|
|
@ -46,10 +46,12 @@ async def get_ticker_lines() -> Iterator[str]:
|
|||
async def get_ticker_content_lines(
|
||||
ticker_lines: Iterator[str] = Depends(get_ticker_lines),
|
||||
) -> Iterator[str]:
|
||||
cfg = await Config.get()
|
||||
|
||||
return (
|
||||
line
|
||||
for line in ticker_lines
|
||||
if not line.startswith("#")
|
||||
if not line.startswith(cfg.ticker.comment_marker)
|
||||
)
|
||||
|
||||
|
||||
|
@ -58,7 +60,7 @@ async def get_ticker_content(
|
|||
) -> str:
|
||||
cfg = await Config.get()
|
||||
ticker_content_lines = ["", *ticker_content_lines, ""]
|
||||
ticker_content = cfg.ticker_separator.join(ticker_content_lines)
|
||||
ticker_content = cfg.ticker.separator.join(ticker_content_lines)
|
||||
|
||||
return ticker_content.strip()
|
||||
|
||||
|
|
Loading…
Reference in a new issue