Skip to content

Commit 7aaac45

Browse files
add distinguish_inout foo to avoid code duplication
1 parent 7a98903 commit 7aaac45

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

acclimatise/model.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,20 @@ def capital(self):
388388
int_num_re = re.compile(r"([+-]?[0-9]+)", flags=re.IGNORECASE)
389389

390390

391+
def distinguish_inout(string, cls) -> cli_types.CliFileSystemType:
392+
"""
393+
distinguish input/output files/directories given a string
394+
"""
395+
im = input_re.search(string)
396+
om = output_re.search(string)
397+
if im and not om:
398+
return cls(output=False)
399+
elif not im and om:
400+
return cls(output=True)
401+
else:
402+
return cls()
403+
404+
391405
def infer_type(string) -> typing.Optional[cli_types.CliType]:
392406
"""
393407
Reads a string (argument description etc) to find hints about what type this argument might be. This is
@@ -400,19 +414,9 @@ def infer_type(string) -> typing.Optional[cli_types.CliType]:
400414
elif int_re.search(string):
401415
return cli_types.CliInteger()
402416
elif file_re.search(string):
403-
if input_re.search(string) and not output_re.search(string):
404-
return cli_types.CliFile(output=False)
405-
elif not input_re.search(string) and output_re.search(string):
406-
return cli_types.CliFile(output=True)
407-
else:
408-
return cli_types.CliFile()
417+
return distinguish_inout(string, cli_types.CliFile)
409418
elif dir_re.search(string):
410-
if input_re.search(string) and not output_re.search(string):
411-
return cli_types.CliDir(output=False)
412-
elif not input_re.search(string) and output_re.search(string):
413-
return cli_types.CliDir(output=True)
414-
else:
415-
return cli_types.CliDir()
419+
return distinguish_inout(string, cli_types.CliDir)
416420
elif str_re.search(string):
417421
return cli_types.CliString()
418422
elif float_num_re.search(string):

0 commit comments

Comments
 (0)