-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Ininital Config Loader concept
- Loading branch information
Showing
5 changed files
with
129 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |