def has_file_suffixOLD(path: ExPathOrStr) -> bool: """Returns True when detects file suffix, e.g. '.*/my_weird-file*v.d?.[ts]' (or 'file.txt'). Returns False in cases like '.*/py_venv.*/' (or 'file') >>> all(map(has_file_suffix, ['.*/my_weird-file*v.d?.[ts]', 'file.txt'])) True >>> any(map(has_file_suffix, ['.*/py_venv.*/', 'file'])) False """ path = str(path) if '.' not in path: return False suffixes = [split for split in path.split('.')[1:] if split] # path = Path(path) # suffix = path.suffix if not suffixes: return False stem, *_ = path.partition('.') # stem = path.stem is_stem_only_regex = is_only_regex(stem) if is_stem_only_regex: # something like "*.mp4" returns True return any( bool(re.fullmatch(FILE_SUFFIX, '.' + suffix)) for suffix in suffixes) # nice suffix else: any_suffix_has_non_regex = any(not is_only_regex(suffix) for suffix in suffixes) if any_suffix_has_non_regex: return True return False
def has_file_suffix(self) -> bool: """Returns True when detects file suffix, e.g. '.*/my_weird-file*v.d?.[ts]' (or 'file.txt'). Returns False in cases like '.*/py_venv.*/' (or 'file') >>> all(exp.has_file_suffix() for exp in [ExPath('.*/my_weird-file*v.d?.[ts]'), ExPath('file.txt')]) True >>> any(exp.has_file_suffix() for exp in [ExPath('.*/py_venv.*/'), ExPath('file')]) False """ if '.' not in str(self): return False # suffixes = path.suffixes # path = Path(path) # suffix = path.suffix if not self.suffixes: return False # stem, *_ = path.partition('.') # stem = path.stem is_stem_only_regex = regex.is_only_regex(self.stem) if is_stem_only_regex: # something like "*.mp4" returns True return any(bool(re.fullmatch(regex.FILE_SUFFIX, '.' + suffix)) for suffix in self.suffixes) # nice suffix else: any_suffix_has_non_regex = any(not regex.is_only_regex(suffix) for suffix in self.suffixes) if any_suffix_has_non_regex: return True return False
def test__is_only_regex__false_cases__manual(): vals = ('[', ) for val in vals: actual = is_only_regex(val) assert actual is False
def test__is_only_regex__truth_cases__manual(): vals = ('.?', '\\.?') for val in vals: actual = is_only_regex(val) assert actual is True
def test__is_only_regex__mixed_string(): assert is_only_regex(nonregex + '.') is False mixed_strings = iter_permutations(nonregex + '.', 3, has_regex_and_nonregex) for stryng in mixed_strings: assert is_only_regex(stryng) is False
def test__is_only_regex__nonregex_string(): nonregex_strings = get_permutations(nonregex + '.', 3) for stryng in nonregex_strings: assert is_only_regex(stryng) is False
def test__is_only_regex__regex_string(): regex_strings = get_permutations(REGEX_CHAR + '.', 3) for stryng in regex_strings: assert is_only_regex(stryng) is True
def test__is_only_regex__regex_char(*exclude): for c in REGEX_CHAR: if c in exclude: continue actual = is_only_regex(c) assert actual is True
def test__is_only_regex__nonregex_char(): for c in nonregex: assert is_only_regex(c) is False