Skip to content

Commit

Permalink
feat: Convert config template and schema to Pydantic model
Browse files Browse the repository at this point in the history
Co-authored-by: MoritzWeber0 <[email protected]>
  • Loading branch information
romeonicholas and MoritzWeber0 committed Mar 21, 2024
1 parent ed46306 commit 82c125a
Show file tree
Hide file tree
Showing 42 changed files with 653 additions and 619 deletions.
1 change: 0 additions & 1 deletion backend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

.idea
config/*
!config/config_template.yaml
.history
**/__pycache__/
.vscode
Expand Down
5 changes: 0 additions & 5 deletions backend/capellacollab/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,8 @@

from importlib import metadata

from capellacollab import config as config_module

try:
__version__ = metadata.version("capellacollab-backend")
except metadata.PackageNotFoundError:
__version__ = "0.0.0+unknown"
del metadata


config_module.validate_schema()
6 changes: 3 additions & 3 deletions backend/capellacollab/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,18 @@
handlers: list[logging.Handler] = [
logging.StreamHandler(),
core_logging.CustomTimedRotatingFileHandler(
str(config["logging"]["logPath"]) + "backend.log"
str(config.logging.log_path) + "backend.log"
),
]

for handler in handlers:
handler.setFormatter(core_logging.CustomFormatter())

logging.basicConfig(level=config["logging"]["level"], handlers=handlers)
logging.basicConfig(level=config.logging.level, handlers=handlers)


async def startup():
migration.migrate_db(engine, config["database"]["url"])
migration.migrate_db(engine, config.database.url)

Check warning on line 65 in backend/capellacollab/__main__.py

View check run for this annotation

Codecov / codecov/patch

backend/capellacollab/__main__.py#L65

Added line #L65 was not covered by tests
logging.info("Migrations done - Server is running")

# This is needed to load the Kubernetes configuration at startup
Expand Down
4 changes: 2 additions & 2 deletions backend/capellacollab/alembic/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# access to the values within the .ini file in use.
config = context.config

logging.basicConfig(level=cfg["logging"]["level"])
logging.basicConfig(level=cfg.logging.level)
if os.getenv("ALEMBIC_CONFIGURE_LOGGER", "true") != "false":
logging.getLogger("capellacollab").setLevel("WARNING")

Expand All @@ -25,7 +25,7 @@
# this will overwrite the ini-file sqlalchemy.url path
# with the path given in the config of the main code
if not config.get_main_option("sqlalchemy.url"):
config.set_main_option("sqlalchemy.url", cfg["database"]["url"])
config.set_main_option("sqlalchemy.url", cfg.database.url)

# Import models

Expand Down
21 changes: 4 additions & 17 deletions backend/capellacollab/config/__init__.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,10 @@
# SPDX-FileCopyrightText: Copyright DB InfraGO AG and contributors
# SPDX-License-Identifier: Apache-2.0

from . import generate, loader, models

import logging
if not loader.does_config_exist():
generate.write_config()

import jsonschema
import jsonschema.exceptions

from . import exceptions, loader

log = logging.getLogger(__name__)
config = loader.load_yaml()


def validate_schema():
config_schema = loader.load_config_schema()
try:
jsonschema.validate(config, config_schema)
except jsonschema.exceptions.ValidationError as error:
raise exceptions.InvalidConfigurationError(
f"{error.__class__.__name__}: {error.message}",
) from None
config = models.AppConfig(**loader.load_yaml())
304 changes: 0 additions & 304 deletions backend/capellacollab/config/config_schema.yaml

This file was deleted.

Loading

0 comments on commit 82c125a

Please sign in to comment.