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

0.30.6, debiai gui set data provider config in command arguments #240

Merged
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"debiai_data/*",
"dist/",
"venv/",
"build/",
"__pycache__/",
"node_modules/",
"./setup.py",
Expand Down
73 changes: 43 additions & 30 deletions debiaiServer/config/init_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
SUCCESS_COLOR = "green"

# Default config
DATA_FOLDER_PATH = "debiai_data" # The path to the DebiAI data config folder

config = {
"DATA_PROVIDERS_CONFIG": {
"creation": True,
Expand Down Expand Up @@ -94,42 +96,37 @@
changes_made = False


def get_config_value(section, key, config_parser):
# Return the value of the key in the section of the config_parser
# Or return the ENV_VAR if it exists

value = None
ENV_VAR = ENV_VAR_MAPPING[section][key]
def get_config_value(section, key, parameters, config_parser):
# Config priority order: parameters > config file > env var > default value

# Get the value from the config file
if section in config_parser and key in config_parser[section]:
value = str.lower(config_parser[section][key])
# Get the value from the parameters
if key in parameters:
return str.lower(parameters[key])

# Get the value from the environment variables
ENV_VAR = ENV_VAR_MAPPING[section][key]
if ENV_VAR in os.environ:
value = str.lower(os.environ[ENV_VAR])
return str.lower(os.environ[ENV_VAR])

if value is None:
print(
" - Missing "
+ colored(section, DEBUG_SECONDARY_COLOR)
+ " / "
+ colored(key, DEBUG_SECONDARY_COLOR)
+ " in config or in "
+ colored(ENV_VAR, DEBUG_SECONDARY_COLOR)
+ " env var, using default"
)
return None
# Get the value from the config file
if section in config_parser and key in config_parser[section]:
return str.lower(config_parser[section][key])

return value
print(
" - Missing "
+ colored(section, DEBUG_SECONDARY_COLOR)
+ " / "
+ colored(key, DEBUG_SECONDARY_COLOR)
+ " in config or in "
+ colored(ENV_VAR, DEBUG_SECONDARY_COLOR)
+ " env var, using default"
)


def get_config_values(section, config_parser):
# Return a dict of the values of the section of the config_parser
# Or return the ENV_VAR if it exists
def get_config_values(section, parameters, config_parser):
# Config priority order: parameters > config file > env var > default value

values = {}
ENV_VAR = LIST_CONFIG_SECTIONS[section]

# Get the value from the config file
if section in config_parser:
Expand All @@ -138,12 +135,20 @@ def get_config_values(section, config_parser):

# Get the value from the environment variables
# iterate over the keys of the env var
ENV_VAR = LIST_CONFIG_SECTIONS[section]
for key in os.environ.keys():
if key.startswith(ENV_VAR):
# Get the key name without the env var prefix
key_name = key[len(ENV_VAR) :] # noqa
values[key_name] = str.lower(os.environ[key])

# Get the value from the parameters
for key in parameters:
if key.startswith(ENV_VAR):
# Get the key name without the env var prefix
key_name = key[len(ENV_VAR) :] # noqa
values[key_name] = str.lower(parameters[key])

return values


Expand All @@ -166,19 +171,23 @@ def set_config_value(section, key, value):
)


def init_config():
global config
def init_config(data_folder_path: str = None, parameters: dict = {}):
global DATA_FOLDER_PATH, config

print("===================== CONFIG =======================")

# Apply the given data_folder_path
if data_folder_path:
DATA_FOLDER_PATH = data_folder_path

# Read the config file
config_parser.read(config_path)

for section in config.keys():
# Deal with boolean, integer and string values
for key in config[section].keys():
# Get the value from the config file or the environment variables
value = get_config_value(section, key, config_parser)
value = get_config_value(section, key, parameters, config_parser)

if value is None:
continue
Expand Down Expand Up @@ -217,7 +226,7 @@ def init_config():

# Deal with list based config elements
if section in LIST_CONFIG_SECTIONS:
elements = get_config_values(section, config_parser)
elements = get_config_values(section, parameters, config_parser)

for element_name in elements:
print(
Expand All @@ -236,5 +245,9 @@ def init_config():
print(" Default config used")


def get_data_folder_path():
return DATA_FOLDER_PATH


def get_config():
return config
Loading
Loading