Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding default values and converters #11

Merged
merged 1 commit into from
Nov 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 34 additions & 24 deletions uplib/uplib/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,41 @@
from enum import Enum

class Config(Enum):
default_image = "default_image"
welcome_message = "welcome_message"
log_level = "log_level"
volumes = "volumes"
ports = "ports"

# (key, default_value)
default_image = ("default_image", "fedora")
welcome_message = ("welcome_message", "Thank your for running UP!")
log_level = ("log_level", "INFO", logging.getLevelName)
volumes = ("volumes", {})
ports = ("ports", {})

__settings = None

#TODO: Add optional prompt
@classmethod
def settings(cls):
_settings = Config.__settings
if not _settings:
_settings = Dynaconf(
environments=False,
envvar_prefix="UP")
up_yaml = pkgutil.get_data(__name__, "up.yaml")
if up_yaml:
up_yaml = up_yaml.decode("utf-8")
yaml_loader.load(_settings, filename=up_yaml)
return _settings

def get(self):
key = self.value
return settings().get(key)

_settings = None
def settings():
global _settings
if not _settings:
_settings = Dynaconf(
environments=False,
envvar_prefix="UP")
up_yaml = pkgutil.get_data(__name__, "up.yaml")
if up_yaml:
up_yaml = up_yaml.decode("utf-8")
yaml_loader.load(_settings, filename=up_yaml)
return _settings
value = self.value
key = value[0]
result = Config.settings().get(key)
if not result:
result = value[1] if len(value) > 1 else None
converter = value[2] if len(value) > 2 else None
if converter and result:
result = converter(result)
return result

def get_log_level():
level_name = settings().get("log_level", "INFO")
level = logging.getLevelName(level_name)
return level
return Config.log_level.get()


Loading