Skip to content

Commit

Permalink
[chore] Improve config parsing (#218)
Browse files Browse the repository at this point in the history
- Print pre-parsed config (input file) on boot
- Print post-parsed config (in-memory settings) on boot
- Reroute Pydantic validation errors during config parsing to avoid endless rebooting
  • Loading branch information
nwithan8 authored Apr 30, 2024
1 parent b550d17 commit 8eb4784
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
9 changes: 5 additions & 4 deletions modules/settings/config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,8 @@ def __init__(self, config_path: str, **docker_kwargs):
self.yaml_data = yaml.load(open(config_path), Loader=yaml.FullLoader)
self.json_data = json.loads(json.dumps(self.yaml_data, indent=4))

logging.debug(f"Parsing config data:\n{utils.pretty_print_json(json_data=self.json_data)}")

start = ConfigSection(data=self.json_data)

self.tautulli = TautulliConfig(data=start.get_subsection_data(key="Tautulli")).to_model()
Expand All @@ -467,6 +469,8 @@ def __init__(self, config_path: str, **docker_kwargs):
self.stats = StatsConfig(data=start.get_subsection_data(key="Stats")).to_model()
self.run_args = RunArgsConfig(data=docker_kwargs).to_model()

logging.debug(f"Config loaded. Using the following settings:\n{self.__repr__()}")

def as_json(self) -> dict:
return {
"Tautulli": self.tautulli.as_dict(),
Expand All @@ -481,7 +485,4 @@ def as_yaml(self) -> str:
return yaml.dump(self.as_json(), default_flow_style=False, sort_keys=False)

def __repr__(self) -> str:
return self.as_yaml()

def print(self) -> str:
return self.as_yaml()
return utils.pretty_print_json(self.as_json(), sort=True)
15 changes: 15 additions & 0 deletions modules/utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
import json
from datetime import datetime, timedelta
from typing import Optional, Any
from urllib.parse import quote_plus

from pytz import timezone


def pretty_print_json(json_data: dict, sort: bool = False) -> str:
"""
Return a pretty printed JSON string
:param json_data: JSON data to pretty print
:type json_data: dict
:param sort: (Optional) sort the keys in the JSON data
:type sort: bool, optional
:return: pretty printed JSON string
:rtype: str
"""
return json.dumps(json_data, indent=4, sort_keys=sort)


def make_plural(word, count: int, suffix_override: str = 's') -> str:
if count > 1:
return f"{word}{suffix_override}"
Expand Down
9 changes: 7 additions & 2 deletions run.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import argparse
import os

from pydantic import ValidationError as PydanticValidationError

import modules.logs as logging
import modules.tautulli.tautulli_connector as tautulli
from consts import (
Expand All @@ -21,7 +23,7 @@
from modules.discord.services.slash_commands import SlashCommandManager
from modules.discord.services.tagged_message import TaggedMessagesManager
from modules.emojis import EmojiManager
from modules.errors import determine_exit_code, TauticordMigrationFailure
from modules.errors import determine_exit_code, TauticordMigrationFailure, TauticordSetupFailure
from modules.settings.config_parser import Config
from modules.statics import (
splash_logo,
Expand Down Expand Up @@ -88,7 +90,10 @@ def set_up_configuration() -> Config:
KEY_RUN_ARGS_CONFIG_PATH: config_directory,
KEY_RUN_ARGS_LOG_PATH: args.log,
}
return Config(config_path=f"{args.config}", **kwargs)
try:
return Config(config_path=f"{args.config}", **kwargs)
except PydanticValidationError as e: # Redirect Pydantic validation errors during config parsing
raise TauticordSetupFailure(f"Configuration error: {e}")


@run_with_potential_exit_on_error
Expand Down

0 comments on commit 8eb4784

Please sign in to comment.