From 652b07ad7f98356a7d509f4fd2278183c435a64c Mon Sep 17 00:00:00 2001 From: Zhiwei Liang Date: Fri, 7 Feb 2025 03:09:16 -0500 Subject: [PATCH 1/2] Add fallback value to replace explicitly option checking for config gets --- linodecli/configuration/config.py | 46 +++++++++--------------------- linodecli/configuration/helpers.py | 4 +-- 2 files changed, 15 insertions(+), 35 deletions(-) diff --git a/linodecli/configuration/config.py b/linodecli/configuration/config.py index de2dca86f..6e499d925 100644 --- a/linodecli/configuration/config.py +++ b/linodecli/configuration/config.py @@ -82,10 +82,7 @@ def default_username(self) -> str: :returns: The `default-user` username or an empty string. :rtype: str """ - if self.config.has_option("DEFAULT", "default-user"): - return self.config.get("DEFAULT", "default-user") - - return "" + return self.config.get("DEFAULT", "default-user", fallback="") def set_user(self, username: str): """ @@ -153,15 +150,11 @@ def get_token(self) -> str: :rtype: str """ if self.used_env_token: - return os.environ.get(ENV_TOKEN_NAME, None) + return os.getenv(ENV_TOKEN_NAME, None) - if self.config.has_option( - self.username or self.default_username(), "token" - ): - return self.config.get( - self.username or self.default_username(), "token" - ) - return "" + return self.config.get( + self.username or self.default_username(), "token", fallback="" + ) def get_value(self, key: str) -> Optional[Any]: """ @@ -180,12 +173,9 @@ def get_value(self, key: str) -> Optional[Any]: current user. :rtype: any """ - username = self.username or self.default_username() - - if not self.config.has_option(username, key): - return None - - return self.config.get(username, key) + return self.config.get( + self.username or self.default_username(), key, fallback=None + ) def get_bool(self, key: str) -> bool: """ @@ -204,12 +194,10 @@ def get_bool(self, key: str) -> bool: current user. :rtype: any """ - username = self.username or self.default_username() - if not self.config.has_option(username, key): - return False - - return self.config.getboolean(username, key) + return self.config.getboolean( + self.username or self.default_username(), key, fallback=False + ) # plugin methods - these are intended for plugins to utilize to store their # own persistent config information @@ -258,10 +246,7 @@ def plugin_get_value(self, key: str) -> Optional[Any]: username = self.username or self.default_username() full_key = f"plugin-{self.running_plugin}-{key}" - if not self.config.has_option(username, full_key): - return None - - return self.config.get(username, full_key) + return self.config.get(username, full_key, fallback=None) # TODO: this is more of an argparsing function than it is a config function # might be better to move this to argparsing during refactor and just have @@ -308,11 +293,8 @@ def update( # these don't get included in the updated namespace if key.startswith("plugin-"): continue - value = None - if self.config.has_option(username, key): - value = self.config.get(username, key) - else: - value = ns_dict[key] + + value = self.config.get(username, key, fallback=ns_dict[key]) if not value: continue diff --git a/linodecli/configuration/helpers.py b/linodecli/configuration/helpers.py index cb0d5e9ea..a0398e2b3 100644 --- a/linodecli/configuration/helpers.py +++ b/linodecli/configuration/helpers.py @@ -299,6 +299,4 @@ def _config_get_with_default( :returns: The value pulled from the config or the default value. :rtype: Any """ - return ( - config.get(user, field) if config.has_option(user, field) else default - ) + return config.get(user, field, fallback=default) From 1bfbee53898f8da6620acd944534ed0758725f4c Mon Sep 17 00:00:00 2001 From: Zhiwei Liang Date: Wed, 12 Feb 2025 12:14:25 -0500 Subject: [PATCH 2/2] Use `get` for getting value from a dict --- linodecli/configuration/config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/linodecli/configuration/config.py b/linodecli/configuration/config.py index 7eaaa50ce..2532ed13a 100644 --- a/linodecli/configuration/config.py +++ b/linodecli/configuration/config.py @@ -294,7 +294,7 @@ def update( if key.startswith("plugin-"): continue - value = self.config.get(username, key, fallback=ns_dict[key]) + value = self.config.get(username, key, fallback=ns_dict.get(key)) if not value: continue