forked from FrederikBaerentsen/BrickTracker
bricktracker
migrations
sql
views
__init__.py
app.py
config.py
configuration.py
configuration_list.py
exceptions.py
fields.py
instructions.py
instructions_list.py
login.py
minifigure.py
minifigure_list.py
navbar.py
parser.py
part.py
part_list.py
rebrickable.py
rebrickable_image.py
rebrickable_minifigure.py
rebrickable_part.py
rebrickable_set.py
rebrickable_set_list.py
record.py
record_list.py
reload.py
retired.py
retired_list.py
set.py
set_checkbox.py
set_checkbox_list.py
set_list.py
socket.py
socket_decorator.py
sql.py
sql_counter.py
sql_migration.py
sql_migration_list.py
sql_stats.py
theme.py
theme_list.py
version.py
wish.py
wish_list.py
docs
static
templates
.dockerignore
.env.sample
.gitignore
CHANGELOG.md
Dockerfile
LICENSE
README.md
__init__.py
app.py
compose.legacy.yml
compose.local.yaml
compose.yaml
entrypoint.sh
requirements.txt
test-server.sh
61 lines
1.9 KiB
Python
61 lines
1.9 KiB
Python
import logging
|
|
from typing import Generator
|
|
|
|
from flask import Flask
|
|
|
|
from .config import CONFIG
|
|
from .configuration import BrickConfiguration
|
|
from .exceptions import ConfigurationMissingException
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
# Application configuration
|
|
class BrickConfigurationList(object):
|
|
app: Flask
|
|
configurations: dict[str, BrickConfiguration]
|
|
|
|
# Load configuration
|
|
def __init__(self, app: Flask, /):
|
|
self.app = app
|
|
|
|
# Load the configurations only there is none already loaded
|
|
configurations = getattr(self, 'configurations', None)
|
|
|
|
if configurations is None:
|
|
logger.info('Loading configuration variables')
|
|
|
|
BrickConfigurationList.configurations = {}
|
|
|
|
# Process all configuration items
|
|
for config in CONFIG:
|
|
item = BrickConfiguration(**config)
|
|
|
|
# Store in the list
|
|
BrickConfigurationList.configurations[item.name] = item
|
|
|
|
# Only store the value in the app to avoid breaking any
|
|
# existing variables
|
|
self.app.config[item.name] = item.value
|
|
|
|
# Check whether a str configuration is set
|
|
@staticmethod
|
|
def error_unless_is_set(name: str):
|
|
configuration = BrickConfigurationList.configurations[name]
|
|
|
|
if configuration.value is None or configuration.value == '':
|
|
raise ConfigurationMissingException(
|
|
'{name} must be defined (using the {environ} environment variable)'.format( # noqa: E501
|
|
name=name,
|
|
environ=configuration.env_name
|
|
),
|
|
)
|
|
|
|
# Get all the configuration items from the app config
|
|
@staticmethod
|
|
def list() -> Generator[BrickConfiguration, None, None]:
|
|
keys = sorted(BrickConfigurationList.configurations.keys())
|
|
|
|
for name in keys:
|
|
yield BrickConfigurationList.configurations[name]
|