Skip to content

Commit

Permalink
feat: Ininital Config Loader concept
Browse files Browse the repository at this point in the history
  • Loading branch information
techman83 committed Mar 16, 2024
1 parent 2b9d69f commit 130d6f1
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 2 deletions.
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ dynamic = ["version"]
dependencies = [
"cdktf",
"click",
"dynaconf",
"PyYAML",
]
requires-python = ">=3.8"
authors = [
Expand Down
6 changes: 4 additions & 2 deletions src/cally/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from click import Group

from ._version import VERSION as __version__ # noqa: N811
from .config import CallyConfig


# TODO: Investigate the lazy loader as this might become quite slow
Expand All @@ -34,18 +35,19 @@ def get_commands(package_name: str) -> List:
'--config',
type=click.Path(path_type=Path),
default=Path(Path.cwd(), '.cally.yaml'),
envvar='CALLY_PROJECT_CONFIG',
envvar='CALLY_CONFIG',
help='Path to the project config file',
)
@click.version_option(__version__)
@click.pass_context
def cally(
ctx: click.Context, # noqa: ARG001
config: click.Path, # noqa: ARG001
config: Path, # noqa: ARG001
) -> None:
"""
Top level click command group for Cally
"""
ctx.obj = CallyConfig(config_file=config)


commands = get_commands('cally.cli.commands')
Expand Down
19 changes: 19 additions & 0 deletions src/cally/cli/commands/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import click
import yaml

from ..config import CallyConfig, service_options


@click.group()
def config() -> None:
pass


@click.command()
@service_options
@click.pass_obj
def print_service(config: CallyConfig):
click.secho(yaml.dump(config.settings))


config.add_command(print_service)
77 changes: 77 additions & 0 deletions src/cally/cli/config/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
from pathlib import Path
from typing import Union

import click
from dynaconf import Dynaconf # type: ignore


class CallyConfig:
config_file: Path
_environment: str
_service: str
_settings: Dynaconf

def __init__(self, config_file: Path) -> None:
self.config_file = config_file

@property
def environment(self):
return self._environment

@environment.setter
def environment(self, value: str):
self._environment = value

@property
def service(self):
return self._service

@service.setter
def service(self, value: str):
self._service = value

@property
def settings(self):
if getattr(self, '_settings', None) is None:
self._settings = Dynaconf(
envvar_prefix='CALLY',
environment=self.environment,
key=self.service,
settings_files=[self.config_file],
loaders=[
'cally.cli.config.loader',
'dynaconf.loaders.env_loader',
],
)
return self._settings


def ctx_callback(
ctx: click.Context, param: click.Parameter, value: Union[str, int]
) -> Union[str, int]:
setattr(ctx.obj, str(param.name), value)
return value


def service_options(func):
options = [
click.option(
'--environment',
envvar='CALLY_ENVIRONMENT',
expose_value=False,
required=True,
help='Environment to operate within',
callback=ctx_callback,
),
click.option(
'--service',
envvar='CALLY_SERVICE',
expose_value=False,
required=True,
help='Service name to retrieve config details',
callback=ctx_callback,
),
]
for option in reversed(options):
func = option(func)
return func
27 changes: 27 additions & 0 deletions src/cally/cli/config/loader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from dynaconf import LazySettings # type: ignore


def load(
obj: LazySettings,
env: str,
key: str,
filename: str,
silent=True,
) -> None:
import pdb

pdb.set_trace()

"""
Reads and loads in to "obj" a single key or all keys from source
:param obj: the settings instance
:param env: settings current env (upper case) default='DEVELOPMENT'
:param silent: if errors should raise
:param key: if defined load a single key, else load all from `env`
:param filename: Custom filename to load (useful for tests)
:return: None
"""
# Load data from your custom data source (file, database, memory etc)
# use `obj.set(key, value)` or `obj.update(dict)` to load data
# use `obj.find_file('filename.ext')` to find the file in search tree
# Return nothing

0 comments on commit 130d6f1

Please sign in to comment.