def test__no_suffix__endswith_path_reg(self): # e.g. 'py_venv.*/'. should return no suffix (False) no_suffix = ExPath('py_venv') assert no_suffix.has_file_suffix() is False for reg in path_regexes(): val = ExPath(f'{no_suffix}{reg}') actual = val.has_file_suffix() assert actual is False
def test__with_suffix__startswith_path_reg(self): # e.g. '.*/py_venv.xml'. should return has suffix (True) with_suffix = ExPath('py_venv.xml') assert with_suffix.has_file_suffix() is True for reg in path_regexes(): val = ExPath(f'{reg}{with_suffix}') actual = val.has_file_suffix() assert actual is True
def test__no_suffix__surrounded_by_path_reg(self): # e.g. '.*/py_venv.*/'. should return no suffix (False) no_suffix = ExPath('py_venv') assert no_suffix.has_file_suffix() is False for reg in path_regexes(): for morereg in path_regexes(): val = ExPath(f'{morereg}{no_suffix}{reg}') actual = val.has_file_suffix() assert actual is False
def test__everything_mixed_with_regex(self): # e.g. '.*/py_v[en]*v.xm?l'. should return has suffix (True) assert ExPath('.*/py_v[en]*v.xm?l').has_file_suffix() is True mixed_stems = get_permutations_in_size_range(f'{REGEX_CHAR}.py_venv-1257', slice(5), has_letters_and_punc) for stem in mixed_stems: for suffix in mixed_suffixes(): name = ExPath(f'{stem}.{suffix}') actual = name.has_file_suffix() assert actual is True for reg in path_regexes(): val = ExPath(f'{reg}{name}') actual = val.has_file_suffix() assert actual is True
def search(self, keyword: Union[str, ExPath], *, noprompt=True) -> ExPath: """Tries to return an ExPath in status. First assumes `keyword` is an exact file (str or ExPath), and if fails, uses `search` module. @param noprompt: specify False to allow using search_and_prompt(keyword, file) in case nothing matched earlier. """ darkprint(f'Status.search({repr(keyword)}) |') path = ExPath(keyword) for file in self.files: if file == path: return file has_suffix = path.has_file_suffix() has_slash = '/' in keyword has_regex = regex.has_regex(keyword) darkprint(f'\t{has_suffix = }, {has_slash = }, {has_regex = }') if has_suffix: files = self.files else: files = [f.with_suffix('') for f in self.files] if has_regex: for expath in files: if re.search(keyword, str(expath)): return expath if has_slash: darkprint( f"looking for the nearest match among status paths for: '{keyword}'" ) return ExPath(search.nearest(keyword, files)) darkprint( f"looking for a matching part among status paths ({'with' if has_suffix else 'without'} suffixes...) for: '{keyword}'" ) for f in files: # TODO: integrate into git.search somehow for i, part in enumerate(f.parts): if part == keyword: ret = ExPath(os.path.join(*f.parts[0:i + 1])) return ret if noprompt: return None darkprint( f"didn't find a matching part, calling search_and_prompt()...") choice = search_and_prompt(keyword, [str(f) for f in files], criterion='substring') if choice: return ExPath(choice) prompt.generic(colors.red(f"'{keyword}' didn't match anything"), flowopts=True)
def test__with_mixed_regex_suffix(self): # e.g. 'py_venv.xm?l'. should return has suffix (True) for suffix in mixed_suffixes(): with_suffix = ExPath(f'py_venv.{suffix}') actual = with_suffix.has_file_suffix() assert actual is True