Skip to content

Commit ac459aa

Browse files
author
Bal0o
committed
refactor(hacs): improve build_ukbcd_args with formatter functions
Replace imperative if/elif logic with declarative formatter functions for better maintainability and extensibility. Each special case now has its own formatter function, making it easier to add new cases and test individual formatting logic.
1 parent 15f2fe5 commit ac459aa

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

custom_components/uk_bin_collection/__init__.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -283,21 +283,26 @@ def build_ukbcd_args(config_data: dict) -> list:
283283
"""Build the argument list for UKBinCollectionApp from config data."""
284284
council = config_data.get("original_parser") or config_data.get("council", "")
285285
url = config_data.get("url", "")
286-
287286
args = [council, url]
288287

288+
# Per-key formatters: return a list of CLI args for that key
289+
def _format_headless(v):
290+
return ["--headless"] if v else ["--not-headless"]
291+
292+
def _format_web_driver(v):
293+
return [f"--web_driver={v.rstrip('/')}"] if v is not None else []
294+
295+
formatters = {
296+
"headless": _format_headless,
297+
"web_driver": _format_web_driver,
298+
}
299+
289300
for key, value in config_data.items():
290301
if key in EXCLUDED_ARG_KEYS:
291302
continue
292-
if key == "web_driver" and value is not None:
293-
value = value.rstrip("/")
294-
args.append(f"--{key}={value}")
295-
elif key == "headless":
296-
# Handle boolean headless argument correctly
297-
if value:
298-
args.append("--headless")
299-
else:
300-
args.append("--not-headless")
303+
fmt = formatters.get(key)
304+
if fmt:
305+
args.extend(fmt(value))
301306
else:
302307
args.append(f"--{key}={value}")
303308

0 commit comments

Comments
 (0)