Skip to content

Commit 9b7f6de

Browse files
committed
Added generic constructors for folder names, folder paths, and file paths
1 parent e784085 commit 9b7f6de

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

src/murfey/cli/generate_config.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,72 @@ def confirm_duplicate(value: str):
9393
return ask_for_permission(message)
9494

9595

96+
def get_folder_name(message: Optional[str] = None) -> str:
97+
"""
98+
Helper function to interactively generate, validate, and return a folder name.
99+
"""
100+
while True:
101+
message = "Please enter the folder name." if message is None else message
102+
value = prompt(message, style="yellow").strip()
103+
if bool(re.fullmatch(r"[\w\s\-]*", value)) is True:
104+
return value
105+
console.print(
106+
"There are unsafe characters present in this folder name. Please "
107+
"use a different one.",
108+
style="red",
109+
)
110+
if ask_for_input("folder name", True) is False:
111+
return ""
112+
continue
113+
114+
115+
def get_folder_path(
116+
message: Optional[str] = None, as_path: bool = True
117+
) -> str | Path | None:
118+
"""
119+
Helper function to interactively generate, validate, and return the full path
120+
to a folder.
121+
"""
122+
while True:
123+
message = (
124+
"Please enter the full path to the folder." if message is None else message
125+
)
126+
value = prompt(message, style="yellow").strip()
127+
if not value:
128+
return None
129+
try:
130+
path = Path(value).resolve()
131+
return path if as_path is True else path.as_posix()
132+
except Exception:
133+
console.print("Unable to resolve provided file path", style="red")
134+
if ask_for_input("file path", True) is False:
135+
return None
136+
continue
137+
138+
139+
def get_file_path(
140+
message: Optional[str] = None, as_path: bool = True
141+
) -> str | Path | None:
142+
"""
143+
Helper function to interactively generate, validate, and return the full path
144+
to a file.
145+
"""
146+
while True:
147+
message = (
148+
"Please enter the full path to the file." if message is None else message
149+
)
150+
value = prompt(message, style="yellow").strip()
151+
if not value:
152+
return None
153+
file = Path(value).resolve()
154+
if file.suffix:
155+
return file if as_path is True else file.as_posix()
156+
console.print(f"{str(file)!r} doesn't appear to be a file", style="red")
157+
if ask_for_input("file", True) is False:
158+
return None
159+
continue
160+
161+
96162
def construct_list(
97163
value_name: str,
98164
value_method: Optional[Callable] = None,
@@ -405,6 +471,10 @@ def add_calibrations(
405471

406472
def add_software_packages(config: dict, debug: bool = False) -> dict[str, Any]:
407473
def get_software_name() -> str:
474+
"""
475+
Function to interactively generate, validate, and return the name of a
476+
supported software package.
477+
"""
408478
message = (
409479
"What is the name of the software package? Supported options: 'autotem', "
410480
"'epu', 'leica', 'serialem', 'tomo'"

0 commit comments

Comments
 (0)