Skip to content

Commit f90560e

Browse files
authored
util: Allow config empty string (#544)
1 parent 5f48d02 commit f90560e

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

pupgui2/util.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -128,17 +128,26 @@ def apply_dark_theme(app: QApplication) -> None:
128128
app.setPalette(QStyleFactory.create('fusion').standardPalette())
129129

130130

131-
def read_update_config_value(option: str, value, section: str = 'pupgui2', config_file: str = CONFIG_FILE) -> str:
132-
131+
def read_update_config_value(option: str, value = None, section: str = 'pupgui2', config_file: str = CONFIG_FILE) -> str | None:
133132
"""
134133
Uses ConfigParser to read a value with a given option from a given section from a given config file.
135134
By default, will read a option and a value from the 'pupgui2' section in CONFIG_FILE path in constants.py.
135+
136+
Args:
137+
option (str): The option to read from or update in the config file.
138+
value (Any, optional): The value to write to the config file. If None, will read the value from the config file.
139+
section (str, optional): The section to read from or update in the config file. Default is 'pupgui2'.
140+
config_file (str, optional): The path to the config file. Default is CONFIG_FILE in constants.py.
141+
142+
143+
Returns:
144+
str | None: The value read from the config file or None if the option does not exist.
136145
"""
137146

138147
config = ConfigParser()
139148

140149
# Write value if given
141-
if value:
150+
if value != None: # "!= None" is used to avoid false positives with empty strings
142151
config.read(config_file)
143152
if not config.has_section(section):
144153
config.add_section(section)
@@ -156,11 +165,11 @@ def read_update_config_value(option: str, value, section: str = 'pupgui2', confi
156165
return value
157166

158167

159-
def config_theme(theme=None) -> str:
168+
def config_theme(theme=None) -> str | None:
160169
"""
161170
Read/update config for the theme
162171
Write theme to config or read if theme=None
163-
Return Type: str
172+
Return Type: str | None
164173
"""
165174

166175
return read_update_config_value('theme', theme, section='pupgui2')
@@ -173,25 +182,23 @@ def config_advanced_mode(advmode=None) -> str:
173182
Return Type: str
174183
"""
175184

176-
return read_update_config_value('advancedmode', advmode, section='pupgui2')
185+
return read_update_config_value('advancedmode', advmode, section='pupgui2') or ""
177186

178187

179-
def config_github_access_token(github_token=None):
180-
188+
def config_github_access_token(github_token=None) -> str:
181189
"""
182190
Read/update config for GitHub Access Token
183191
"""
184192

185-
return read_update_config_value('github_api_token', github_token, section='pupgui2')
186-
193+
return read_update_config_value('github_api_token', github_token, section='pupgui2') or ""
187194

188-
def config_gitlab_access_token(gitlab_token=None):
189195

196+
def config_gitlab_access_token(gitlab_token=None) -> str:
190197
"""
191198
Read/update config for GitLab Access Token
192199
"""
193200

194-
return read_update_config_value('gitlab_api_token', gitlab_token, section='pupgui2')
201+
return read_update_config_value('gitlab_api_token', gitlab_token, section='pupgui2') or ""
195202

196203

197204
def create_compatibilitytools_folder() -> None:

0 commit comments

Comments
 (0)