Skip to content

Commit 53db488

Browse files
committed
fix: reduce complexity in run function to pass CI checks
Refactored the run function to extract helper functions (apply_defaults, check_interactive, prompt_missing_values) to reduce the McCabe complexity from 12 to below the threshold of 10.
1 parent 4807a0b commit 53db488

File tree

1 file changed

+104
-42
lines changed

1 file changed

+104
-42
lines changed

setup_project.py

Lines changed: 104 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,73 @@ def process_templates(replacements: dict) -> None:
104104
log_message(f"Copied: {dst_path.relative_to(ROOT_DIR)}")
105105

106106

107+
def apply_defaults(
108+
github_username: str | None,
109+
project_name: str | None,
110+
project_description: str | None,
111+
author_name: str | None,
112+
author_email: str | None,
113+
defaults: dict,
114+
) -> tuple:
115+
"""Apply defaults from JSON file when yes or dry_run is set."""
116+
return (
117+
github_username or defaults.get("github_username"),
118+
project_name or defaults.get("project_name"),
119+
project_description or defaults.get("project_description"),
120+
author_name or defaults.get("author_name"),
121+
author_email or defaults.get("author_email"),
122+
)
123+
124+
125+
def check_interactive(
126+
github_username: str | None,
127+
project_name: str | None,
128+
project_description: str | None,
129+
author_name: str | None,
130+
author_email: str | None,
131+
yes: bool,
132+
dry_run: bool,
133+
) -> bool:
134+
"""Check if interactive mode is needed."""
135+
return (
136+
not yes
137+
and not dry_run
138+
and any(
139+
val is None
140+
for val in (
141+
github_username,
142+
project_name,
143+
project_description,
144+
author_name,
145+
author_email,
146+
)
147+
)
148+
)
149+
150+
151+
def prompt_missing_values(
152+
github_username: str | None,
153+
project_name: str | None,
154+
project_description: str | None,
155+
author_name: str | None,
156+
author_email: str | None,
157+
defaults: dict,
158+
) -> tuple:
159+
"""Prompt for missing values in interactive mode."""
160+
log_message("\n--- Interactive Mode ---")
161+
log_message("Press Enter to accept default value shown in brackets\n")
162+
163+
return (
164+
github_username
165+
or prompt_for_value("GitHub Username", defaults.get("github_username")),
166+
project_name or prompt_for_value("Project Name", defaults.get("project_name")),
167+
project_description
168+
or prompt_for_value("Project Description", defaults.get("project_description")),
169+
author_name or prompt_for_value("Author Name", defaults.get("author_name")),
170+
author_email or prompt_for_value("Author Email", defaults.get("author_email")),
171+
)
172+
173+
107174
def run(
108175
github_username: str | None = None,
109176
project_name: str | None = None,
@@ -117,49 +184,46 @@ def run(
117184
defaults = load_defaults()
118185

119186
if yes or dry_run:
120-
github_username = github_username or defaults.get("github_username")
121-
project_name = project_name or defaults.get("project_name")
122-
project_description = project_description or defaults.get("project_description")
123-
author_name = author_name or defaults.get("author_name")
124-
author_email = author_email or defaults.get("author_email")
125-
126-
interactive = (
127-
not yes
128-
and not dry_run
129-
and any(
130-
(val is None)
131-
for key, val in {
132-
"github_username": github_username,
133-
"project_name": project_name,
134-
"project_description": project_description,
135-
"author_name": author_name,
136-
"author_email": author_email,
137-
}.items()
187+
(
188+
github_username,
189+
project_name,
190+
project_description,
191+
author_name,
192+
author_email,
193+
) = apply_defaults(
194+
github_username,
195+
project_name,
196+
project_description,
197+
author_name,
198+
author_email,
199+
defaults,
138200
)
139-
)
140201

141-
if interactive:
142-
log_message("\n--- Interactive Mode ---")
143-
log_message("Press Enter to accept default value shown in brackets\n")
202+
is_interactive = check_interactive(
203+
github_username,
204+
project_name,
205+
project_description,
206+
author_name,
207+
author_email,
208+
yes,
209+
dry_run,
210+
)
144211

145-
if not github_username:
146-
github_username = prompt_for_value(
147-
"GitHub Username", defaults.get("github_username")
148-
)
149-
if not project_name:
150-
project_name = prompt_for_value(
151-
"Project Name", defaults.get("project_name")
152-
)
153-
if not project_description:
154-
project_description = prompt_for_value(
155-
"Project Description", defaults.get("project_description")
156-
)
157-
if not author_name:
158-
author_name = prompt_for_value("Author Name", defaults.get("author_name"))
159-
if not author_email:
160-
author_email = prompt_for_value(
161-
"Author Email", defaults.get("author_email")
162-
)
212+
if is_interactive:
213+
(
214+
github_username,
215+
project_name,
216+
project_description,
217+
author_name,
218+
author_email,
219+
) = prompt_missing_values(
220+
github_username,
221+
project_name,
222+
project_description,
223+
author_name,
224+
author_email,
225+
defaults,
226+
)
163227

164228
if not all(
165229
[github_username, project_name, project_description, author_name, author_email]
@@ -199,11 +263,9 @@ def run(
199263

200264
process_templates(replacements)
201265

202-
# Rename parent folder based on project name
203266
if project_name:
204267
rename_parent_folder(project_name)
205268

206-
# Show Git configuration instructions
207269
if github_username and project_name and author_name and author_email:
208270
show_git_setup_instructions(
209271
github_username, project_name, author_name, author_email

0 commit comments

Comments
 (0)