Skip to content

Commit 6667377

Browse files
committed
type hints
1 parent 77719b4 commit 6667377

File tree

1 file changed

+60
-31
lines changed

1 file changed

+60
-31
lines changed

ioc_copier/ioc_copier.py

Lines changed: 60 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import re
77
import sys
88
from shutil import copytree, ignore_patterns
9+
from typing import List, Tuple
910

1011
global START_COPY
1112
global current_copy
@@ -14,7 +15,7 @@
1415
global asub_record
1516

1617

17-
def rename_files(root_folder, rename, ioc):
18+
def rename_files(root_folder: str, rename: str, ioc: str) -> None:
1819
"""
1920
Function to handle renaming of files.
2021
Parameters:
@@ -28,28 +29,34 @@ def rename_files(root_folder, rename, ioc):
2829
os.path.join(root_folder, rename),
2930
os.path.join(
3031
root_folder,
31-
rename.replace(f"IOC_{padded_start_copy}", f"IOC_{padded_current_copy}"),
32+
rename.replace(
33+
f"IOC_{padded_start_copy}", f"IOC_{padded_current_copy}"
34+
),
3235
),
3336
)
3437
if f"IOC-{padded_start_copy}" in rename:
3538
os.rename(
3639
os.path.join(root_folder, rename),
3740
os.path.join(
3841
root_folder,
39-
rename.replace(f"IOC-{padded_start_copy}", f"IOC-{padded_current_copy}"),
42+
rename.replace(
43+
f"IOC-{padded_start_copy}", f"IOC-{padded_current_copy}"
44+
),
4045
),
4146
)
4247
if f"{ioc}_{padded_start_copy}" in rename:
4348
os.rename(
4449
os.path.join(root_folder, rename),
4550
os.path.join(
4651
root_folder,
47-
rename.replace(f"{ioc}_{padded_start_copy}", f"{ioc}_{padded_current_copy}"),
52+
rename.replace(
53+
f"{ioc}_{padded_start_copy}", f"{ioc}_{padded_current_copy}"
54+
),
4855
),
4956
)
5057

5158

52-
def replace_text(text_lines, ioc, skip=None):
59+
def replace_text(text_lines: List[str], ioc: str, skip: bool = None) -> List[str]:
5360
"""
5461
Function to handle replacing of text within files.
5562
Parameters:
@@ -67,7 +74,7 @@ def replace_text(text_lines, ioc, skip=None):
6774
]
6875

6976

70-
def replace_line(ioc, line):
77+
def replace_line(ioc: str, line: str) -> str:
7178
"""
7279
Function to replace a single line in a file.
7380
param ioc: The name of the ioc.
@@ -95,12 +102,14 @@ def replace_line(ioc, line):
95102
line = temp_text
96103
temp_text = re.sub(f"{ioc}_0{START_COPY}", f"{ioc}_{padded_current_copy}", line)
97104
line = temp_text
98-
temp_text = re.sub(f"RAMPFILELIST0{START_COPY}", f"RAMPFILELIST{padded_current_copy}", line)
105+
temp_text = re.sub(
106+
f"RAMPFILELIST0{START_COPY}", f"RAMPFILELIST{padded_current_copy}", line
107+
)
99108
line = temp_text
100109
return line
101110

102111

103-
def help_check():
112+
def help_check() -> None:
104113
"""
105114
Function to handle printing help.
106115
"""
@@ -113,7 +122,8 @@ def help_check():
113122
print("Second Argument: <number-ioc-to-copy")
114123
print(
115124
"This should be the last currently existing ioc number, "
116-
"e.g. 2. If copying IOC-01 please test extremely thoroughly as there may be edge cases the script"
125+
"e.g. 2. If copying IOC-01 please test extremely thoroughly as there"
126+
"may be edge cases the script"
117127
"cannot account for.\n"
118128
)
119129
print("Third Argument: <first-copy>")
@@ -123,11 +133,13 @@ def help_check():
123133
)
124134
print("Fourth Argument: <max-number-ioc>")
125135
print("This should be the maximum number copied to.\n")
126-
print("Make sure to run this file from an epics terminal so that make clean can run.\n")
136+
print(
137+
"Make sure to run this file from an epics terminal so that make clean can run.\n"
138+
)
127139
sys.exit()
128140

129141

130-
def handle_arguments():
142+
def handle_arguments() -> Tuple[int, int, str]:
131143
"""
132144
Function to handle arguments of ioc_copier.py.
133145
Returns:
@@ -163,7 +175,7 @@ def handle_arguments():
163175
return initial_copy, ioc, max_copy
164176

165177

166-
def copy_folder(file_format, ioc_name):
178+
def copy_folder(file_format: str, ioc_name: str) -> str:
167179
"""
168180
Function to handle copying folder before replacing text and names.
169181
Parameters:
@@ -174,12 +186,16 @@ def copy_folder(file_format, ioc_name):
174186
The path of the new folder.
175187
"""
176188
start_path = file_format.format(f"{ioc_name}-{padded_start_copy}")
177-
path = os.path.join(os.getcwd(), file_format.format(f"{ioc_name}-{padded_current_copy}"))
189+
path = os.path.join(
190+
os.getcwd(), file_format.format(f"{ioc_name}-{padded_current_copy}")
191+
)
178192
try:
179193
copytree(
180194
os.path.join(os.getcwd(), start_path),
181195
os.path.join(path),
182-
ignore=ignore_patterns("st-*.cmd", "build.mak", "*.db", "*.substitutions", "*.req"),
196+
ignore=ignore_patterns(
197+
"st-*.cmd", "build.mak", "*.db", "*.substitutions", "*.req"
198+
),
183199
)
184200
except FileExistsError:
185201
raise FileExistsError(
@@ -189,9 +205,10 @@ def copy_folder(file_format, ioc_name):
189205
return path
190206

191207

192-
def generate_config(ioc):
208+
def generate_config(ioc: str) -> List[str]:
193209
"""
194-
Generate the config if copying ioc 01 as it should just reference ioc 01s config rather than duplicating it.
210+
Generate the config if copying ioc 01 as it should just reference ioc 01s config
211+
rather than duplicating it.
195212
:param ioc: the ioc name
196213
:return: the text lines of the config.
197214
"""
@@ -205,7 +222,7 @@ def generate_config(ioc):
205222
]
206223

207224

208-
def remove_db_plus(text):
225+
def remove_db_plus(text: str) -> List[str]:
209226
"""
210227
delete DB += lines from a makefile
211228
:param text: the line to check whether to comment
@@ -215,7 +232,7 @@ def remove_db_plus(text):
215232
return text
216233

217234

218-
def get_file_text(file, ioc, root):
235+
def get_file_text(file: str, ioc: str, root: str) -> List[str]:
219236
"""
220237
function to get the text to write to a file.
221238
:param file: The file to get the initial text from.
@@ -231,7 +248,9 @@ def get_file_text(file, ioc, root):
231248
if START_COPY == 1:
232249
if file == "st.cmd":
233250
skip = [
234-
x for x, val in enumerate(text) if f"< iocBoot/ioc{ioc}-IOC-01/st-common.cmd" in val
251+
x
252+
for x, val in enumerate(text)
253+
if f"< iocBoot/ioc{ioc}-IOC-01/st-common.cmd" in val
235254
]
236255
elif file == "config.xml":
237256
return generate_config(ioc)
@@ -240,12 +259,16 @@ def get_file_text(file, ioc, root):
240259

241260
# Last one handled on starts other than 1 to avoid breaking commenting.
242261
if path.endswith(r"App\src\Makefile"):
243-
skip = [x for x, val in enumerate(text) if "build.mak " in val or "/src/build.mak" in val]
262+
skip = [
263+
x
264+
for x, val in enumerate(text)
265+
if "build.mak " in val or "/src/build.mak" in val
266+
]
244267
text = replace_text(text, ioc, skip)
245268
return text
246269

247270

248-
def write_file_text(file, root, text):
271+
def write_file_text(file: str, root: str, text: List[str]) -> None:
249272
"""
250273
function to write to a file.
251274
:param file: The file to write to.
@@ -258,7 +281,7 @@ def write_file_text(file, root, text):
258281
file_pointer.truncate()
259282

260283

261-
def file_walk(files, ioc, root):
284+
def file_walk(files: List[str], ioc: str, root: str) -> None:
262285
"""
263286
Function to walk through each file retrieved by os.walk and call necessary functions.
264287
:param files: The list of files to walk through.
@@ -273,7 +296,7 @@ def file_walk(files, ioc, root):
273296
rename_files(root, file, ioc)
274297

275298

276-
def folder_walk(ioc, root, sub_folder):
299+
def folder_walk(ioc: str, root: str, sub_folder: str) -> None:
277300
"""
278301
Function to walk through folders and rename them.
279302
:param ioc: The ioc name.
@@ -285,7 +308,7 @@ def folder_walk(ioc, root, sub_folder):
285308
rename_files(root, folder, ioc)
286309

287310

288-
def copy_loop(initial_copy, max_copy, file_format, ioc):
311+
def copy_loop(initial_copy: int, max_copy: int, file_format: str, ioc: str) -> None:
289312
"""
290313
Main loop to handle copy and renaming of files
291314
Parameters:
@@ -305,14 +328,16 @@ def copy_loop(initial_copy, max_copy, file_format, ioc):
305328
for current_copy in range(initial_copy, max_copy + 1):
306329
padded_start_copy = add_zero_padding(START_COPY)
307330
padded_current_copy = add_zero_padding(current_copy)
308-
padded_current_copy = f"0{current_copy}" if len(f"{current_copy}") < 2 else current_copy
331+
padded_current_copy = (
332+
f"0{current_copy}" if len(f"{current_copy}") < 2 else current_copy
333+
)
309334
path = copy_folder(file_format, ioc_name)
310335
for root, sub_folder, files in os.walk(path):
311336
file_walk(files, ioc, root)
312337
folder_walk(ioc, root, sub_folder)
313338

314339

315-
def add_zero_padding(copy):
340+
def add_zero_padding(copy: int) -> str | int:
316341
"""
317342
Function to add zero padding to the copy number if nessecary.
318343
:param copy: The copy number to add zero padding to.
@@ -322,27 +347,31 @@ def add_zero_padding(copy):
322347
return f"0{copy}" if len(f"{copy}") < 2 else copy
323348

324349

325-
def check_valid_ioc_to_copy(ioc):
350+
def check_valid_ioc_to_copy(ioc: str) -> None:
326351
"""
327352
Check that duplicating this IOC is valid
328353
:param ioc: The ioc name.
329354
"""
330355
if not os.path.exists(os.path.join("iocBoot", f"ioc{ioc}-IOC-01", "st-common.cmd")):
331356
print(
332-
"No valid st-common.cmd found, this IOC does not appear to be designed in a way that allows duplicates."
357+
"No valid st-common.cmd found, this IOC does not appear to be designed in a way that"
358+
" allows duplicates."
333359
)
334360
sys.exit()
335361
else:
336-
with open(os.path.join("iocBoot", f"ioc{ioc}-IOC-01", "st-common.cmd")) as file_pointer:
362+
with open(
363+
os.path.join("iocBoot", f"ioc{ioc}-IOC-01", "st-common.cmd")
364+
) as file_pointer:
337365
text = file_pointer.read()
338366
if "seq " in text:
339367
print(
340-
"IOC Appears to contain sequencer commands, duplication should be done manually."
368+
"IOC Appears to contain sequencer commands, duplication should be"
369+
"done manually."
341370
)
342371
sys.exit()
343372

344373

345-
def main():
374+
def main() -> None:
346375
"""Main function, sets ioc-name, calls functions in order, and prints when done."""
347376
help_check()
348377
global asub_record

0 commit comments

Comments
 (0)