def test__is_glob__glob__manual(): globs = [ "*.py", "*/**", "f*le.py", "f*le?.py", "f*le?.p*y", "file*", "*file", "*fi?le", "*fi?le*", ] for glob in globs: assert has_glob(glob) is True
def _partition_parts_by_glob(self) -> Tuple[str, str, str]: """ >>> ExPath('/usr/local/*/man')._partition_parts_by_glob() ('/usr/local', '*', 'man') >>> ExPath('/usr/local/share/man')._partition_parts_by_glob() ('/usr/local/share/man', '', '') >>> ExPath('*.d.ts')._partition_parts_by_glob() ('*.d.ts', '', '') """ if len(self.parts) <= 1: return str(self), '', '' parts_before_glob = [] parts_after_glob = [] globpart = '' for i, part in enumerate(self.parts): if has_glob(part): globpart = part parts_after_glob = self.parts[i + 1:] break parts_before_glob.append(part) if parts_before_glob and parts_before_glob[0] == '/': # avoid joining '/', 'usr' → '//usr' return '/' + '/'.join(parts_before_glob[1:]), globpart, '/'.join(parts_after_glob) return '/'.join(parts_before_glob), globpart, '/'.join(parts_after_glob)
def is_dir(self) -> bool: """Has to exist and be a directory""" is_dir = super().is_dir() if is_dir: return True if self.has_file_suffix(): return False if not self.exists(): return False if self.has_glob(): glob_parts_idxs = [i for i, part in enumerate(self.parts) if has_glob(part)] if len(glob_parts_idxs) > 1: pass # raise NotImplementedError # # TODO (CONTINUE): resume here: # # https://www.notion.so/Journal-a754564aceed42668474e6446c8028c3#08be0130d0744a57abcd5737587d7dc7 # # noinspection PyUnreachableCode # concrete_paths = [] # for i, glob_idx in enumerate(glob_parts_idxs): # try: # tmp_path = ExPath(*self.parts[:glob_parts_idxs[i + 1]]) # if tmp_path.is_dir(): # concrete = next(tmp_path.glob()) # concrete_paths.append(concrete) # else: # return False # except IndexError: # ... parts_before_glob, glob_part, parts_after_glob = self._partition_parts_by_glob() if is_only_glob(glob_part): # e.g. '.../*/...' if not parts_after_glob: # e.g. '/home/*' return ExPath(parts_before_glob).is_dir() # prev_parts = [] # for i, part in enumerate(self.parts): # if has_glob(part): # if is_only_glob(part): # if ExPath(*prev_parts).is_dir(): # current_path = ExPath(*prev_parts, part) # concrete_path = next(current_path.glob()) # prev_parts.append(concrete_path.stem) # else: # return False # else: # current_path = ExPath(*prev_parts, part) # if current_path.is_dir(): # concrete_path = next(current_path.glob()) # prev_parts.append(concrete_path.stem) # else: # return False # else: # prev_parts.append(part) return all([subpath.is_dir() for subpath in self.glob()]) # if has_glob(parts_after_glob): # # either '/home/*/.b*shrc' or 'home/*/D*wnloads' # # either '/home/*/.bashrc' or 'home/*/Downloads' # return all([subpath.is_dir() for subpath in self.glob()]) # # e.g. '.../f*obar/...' # return all([subpath.is_dir() for subpath in self.glob()]) self_string = str(self) if '*' not in self_string: return False if self_string.endswith('*'): # e.g. 'some_path*', 'some_path/*', ... before_star, *_ = self_string.partition('*') try: return ExPath(before_star).is_dir() except ValueError as e: if any('bad paths' in arg for arg in e.args): # __new__() return False raise # e.g. '*.foo', 'some_*/', ... # TODO: this is a partial solution before_star, star, after_star = self_string.partition('*') if '/' not in after_star: # *.py[cod] is a file, not a dir return False return self.exists() # TODO: maybe just super().exists()
def has_glob(self) -> bool: return has_glob(str(self))
def test__is_glob__nonregex_string(): nonregex_strings = get_permutations(nonregex + '.', 3) for stryng in nonregex_strings: assert has_glob(stryng) is False
def test__is_glob__truth_cases__manual(): globs = [".*steam.*", "*.**", "f*le?.*", ".*fi?le*", "[^.]*.ts"] for regex in globs: assert has_glob(regex) is False
def test__is_glob__adv_regex_char(): for c in ADV_REGEX_CHAR: assert has_glob(c) is False
def test__is_glob__glob_char(): for c in GLOB_CHAR: assert has_glob(c) is True
def test__is_glob__nonregex_char(): for c in nonregex: assert has_glob(c) is False