Skip to content

Commit

Permalink
addressed an issue where badger was calling mock_settings() through c…
Browse files Browse the repository at this point in the history
…heck_n_config_paths() everytime badger was launched, which was resetting the config path settings
  • Loading branch information
YektaY committed Oct 24, 2024
1 parent 7c53408 commit 847f9dd
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 11 deletions.
10 changes: 8 additions & 2 deletions src/badger/actions/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,14 @@ def config_settings(args):

def _config_path_var(var_name):
config_singleton = init_settings()
display_name = config_singleton[var_name]['display name']
desc = config_singleton[var_name]['description']

is_path = config_singleton.read_is_path(var_name)

if not is_path:
raise KeyError

display_name = config_singleton.read_display_name(var_name)
desc = config_singleton.read_description(var_name)

print(f'=== Configure {display_name} ===')
print(f'*** {desc} ***\n')
Expand Down
8 changes: 2 additions & 6 deletions src/badger/actions/doctor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@


def self_check(args):

config = init_settings()
# Reset Badger
if args.reset:
Expand Down Expand Up @@ -37,17 +36,14 @@ def check_n_config_paths():
issue_list = []

for pname in config._config.dict(by_alias=True):
if not config.read_value(pname):
if config.read_value(pname) == None:
good = False
# dname = BADGER_PATH_DICT[pname]['display name']
# print(f'\n{dname} needs to be configured!')

issue_list.append(pname)

if not good:
# Initial setup
init = True
while False:
while True:
_res = input(
'If this is your first time launching Badger, you should initialize it now.\n'
'Proceed ([y]/n)? ')
Expand Down
95 changes: 92 additions & 3 deletions src/badger/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Setting(BaseModel):
display_name: str
description: str
value: Optional[Union[str, int, bool, None]] = Field(None, description="The value of the setting which can be of different types.")
is_path: bool

class BadgerConfig(BaseModel):
"""
Expand All @@ -50,41 +51,49 @@ class BadgerConfig(BaseModel):
display_name="plugin root",
description="This setting (BADGER_PLUGIN_ROOT) tells Badger where to look for the plugins",
value=None,
is_path=True
)
BADGER_DB_ROOT: Setting = Setting(
display_name="database root",
description="This setting (BADGER_DB_ROOT) tells Badger where to store the routine database",
value=None,
is_path=True
)
BADGER_LOGBOOK_ROOT: Setting = Setting(
display_name="logbook root",
description="This setting (BADGER_LOGBOOK_ROOT) tells Badger where to send the logs (GUI mode)",
value=None,
is_path=True
)
BADGER_ARCHIVE_ROOT: Setting = Setting(
display_name="archive root",
description="This setting (BADGER_ARCHIVE_ROOT) tells Badger where to archive the historical optimization runs",
value=None,
is_path=True
)
BADGER_DATA_DUMP_PERIOD: Setting = Setting(
display_name="data dump period",
description="Minimum time interval between data dumps, unit is second",
value=1,
is_path=False
)
BADGER_THEME: Setting = Setting(
display_name="theme",
description="Theme for the Badger GUI",
value="dark",
is_path=False
)
BADGER_ENABLE_ADVANCED: Setting = Setting(
display_name="enable advanced features",
description="Enable advanced features on the GUI",
value=False,
is_path=False
)
AUTO_REFRESH: Setting = Setting(
display_name="Auto-refresh",
description="Permits each run to start from the initial points calculated based on the current values and the rules",
value=False
value=False,
is_path=False
)

class ConfigSingleton:
Expand Down Expand Up @@ -125,14 +134,16 @@ def load_or_create_config(config_path: str) -> BadgerConfig:
config_data[key] = Setting(
display_name=value.get('display_name', key),
description=value.get('description', f"Setting for {key.replace('_', ' ').lower()}"),
value=value['value']
value=value['value'],
is_path=value.get('is_path', key)
)
else:
# If it's a direct value, wrap it in a Setting
config_data[key] = Setting(
display_name=key,
description=f"Setting for {key.replace('_', ' ').lower()}",
value=value
value=value,
is_path=key
)

try:
Expand Down Expand Up @@ -216,6 +227,84 @@ def read_value(self, key: str, return_value_field: bool = True) -> Any:
return data[key]['value'] if return_value_field else data[key]
raise KeyError(f"Key '{key}' not found in the configuration.")

def read_description(self, key: str, return_description_field: bool = True) -> Any:
"""
Searches for the key in all sections of the configuration.
Parameters
----------
key: str
The key to search for.
return_description_field: bool
If True, returns the 'description' field of the setting; otherwise, returns the entire setting.
Returns
-------
str
The description associated with the provided key.
Raises
------
KeyError
If the key is not found in the configuration.
"""
data = self._config.dict(by_alias=True)
if key in data:
return data[key]['description'] if return_description_field else data[key]
raise KeyError(f"Key '{key}' not found in the configuration.")

def read_display_name(self, key: str, return_display_name_field: bool = True) -> Any:
"""
Searches for the key in all sections of the configuration.
Parameters
----------
key: str
The key to search for.
return_display_name_field: bool
If True, returns the 'display_name' field of the setting; otherwise, returns the entire setting.
Returns
-------
str
The display_name associated with the provided key.
Raises
------
KeyError
If the key is not found in the configuration.
"""
data = self._config.dict(by_alias=True)
if key in data:
return data[key]['display_name'] if return_display_name_field else data[key]
raise KeyError(f"Key '{key}' not found in the configuration.")

def read_is_path(self, key: str, return_is_path_field: bool = True) -> Any:
"""
Searches for the key in all sections of the configuration.
Parameters
----------
key: str
The key to search for.
return_is_path_field: bool
If True, returns the 'is_path' field of the setting; otherwise, returns the entire setting.
Returns
-------
bool
The is_path associated with the provided key.
Raises
------
KeyError
If the key is not found in the configuration.
"""
data = self._config.dict(by_alias=True)
if key in data:
return data[key]['is_path'] if return_is_path_field else data[key]
raise KeyError(f"Key '{key}' not found in the configuration.")

def write_value(self, key: str, value: Any) -> None:
"""A method for setting a new value to the config.
Expand Down

0 comments on commit 847f9dd

Please sign in to comment.