def get_path_list(cls, paths: KGTKFiles, who: str, default_stdio: bool)->typing.List[Path]: # Builds a list of paths from the awkward possible returns from argument parsing. if paths is None: if default_stdio: return [ Path("-") ] else: return [ ] elif isinstance(paths, Path): return [ paths ] elif isinstance(paths, list): result: typing.List[Path] = [ ] pl: typing.Union[typing.Optional[Path], typing.List[Path]] for pl in paths: if pl is None: continue if isinstance(pl, Path): result.append(pl) elif isinstance(pl, list): result.extend(pl) else: raise KGTKArgumentParseException("%s: Unexpected component '%s' in path list '%s'." % (who, str(pl), str(paths))) if len(result) == 0 and default_stdio: return [ Path("-") ] else: return result else: raise KGTKArgumentParseException("%s: Unexpected path list '%s'." % (who, str(paths)))
def get_output_file(cls, paths: KGTKFiles, who: typing.Optional[str] = None, default_stdout: bool = True, )->Path: if who is None: who = cls.DEFAULT_OUTPUT_FILE_WHO p: typing.List[Path] = cls.get_path_list(paths, who, default_stdio=default_stdout) if len(p) == 1: return p[0] elif len(p) == 0: raise KGTKArgumentParseException("%s: Please supply a filename path." % who) else: raise KGTKArgumentParseException("%s: Too many files: '%s'" % (who, str(paths)))
def optional_bool(value) -> typing.Optional[bool]: if value is None: return None if isinstance(value, bool): return value if value.lower() in {'false', 'f', '0', 'no', 'n'}: return False elif value.lower() in {'true', 't', '1', 'yes', 'y'}: return True raise KGTKArgumentParseException(f'{value} is not a valid boolean value')
def get_optional_output_file(cls, paths: KGTKFiles, who: typing.Optional[str] = None, )->typing.Optional[Path]: if who is None: who = cls.DEFAULT_OUTPUT_FILE_WHO p: typing.List[Path] = cls.get_path_list(paths, who, default_stdio=False) if len(p) == 0: return None elif len(p) == 1: return p[0] else: raise KGTKArgumentParseException("%s: Too many files: '%s'" % (who, str(paths)))