Skip to content

Commit

Permalink
config_manager - fix get_all w/ one results (super rare edge); fix …
Browse files Browse the repository at this point in the history
…overwriting self.db in `with_db`
  • Loading branch information
dale-wahl committed Jul 19, 2024
1 parent 6b9cb0b commit 2453182
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions common/config_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,14 @@ def with_db(self, db=None):
:param db: Database object. If None, initialise it using the core config
"""
self.db = db if db else Database(logger=None, dbname=self.get("DB_NAME"), user=self.get("DB_USER"),
if db or not self.db:
# Replace w/ db if provided else only initialise if not already
self.db = db if db else Database(logger=None, dbname=self.get("DB_NAME"), user=self.get("DB_USER"),
password=self.get("DB_PASSWORD"), host=self.get("DB_HOST"),
port=self.get("DB_PORT"), appname="config-reader") if not db else db
else:
# self.db already initialized
pass

def load_user_settings(self):
"""
Expand Down Expand Up @@ -157,6 +162,7 @@ def ensure_database(self):
if setting in known_settings:
continue

self.db.log.debug(f"Creating setting: {setting} with default value {parameters.get('default', '')}")
self.set(setting, parameters.get("default", ""))

# make sure settings and user table are in sync
Expand Down Expand Up @@ -278,17 +284,20 @@ def get(self, attribute_name, default=None, is_json=False, user=None, tags=None)

if not is_json and value is not None:
value = json.loads(value)
# TODO: check this as it feels like it could cause a default to return even if value is not None. - Dale
elif default is not None:
value = default
elif value is None and setting_name in self.config_definition and "default" in self.config_definition[setting_name]:
value = self.config_definition[setting_name]["default"]

final_settings[setting_name] = value

if len(final_settings) == 1:
if attribute_name:
# Single attribute requests; provide only the highest priority result
# print(f"{user}: {attribute_name[0]} = {list(final_settings.values())[0]}")
return list(final_settings.values())[0]
else:
# All settings requested (via get_all)
return final_settings

def get_active_tags(self, user=None, tags=None):
Expand Down Expand Up @@ -372,6 +381,7 @@ def set(self, attribute_name, value, is_json=False, tag="", overwrite_existing=T

self.db.execute(query, (attribute_name, value, tag))
updated_rows = self.db.cursor.rowcount
self.db.log.debug(f"Updated setting for {attribute_name}: {value} (tag: {tag})")

return updated_rows

Expand Down

0 comments on commit 2453182

Please sign in to comment.