Revert files/directories argument to glob, use separate functions.
I thought it was clever at first, but now I'm feeling that having mutually exclusive arguments to a single function in this case is better served by just having two functions. Which also saves us from ever having to deal with the exception case.master
parent
125681da18
commit
e737e60862
|
@ -238,6 +238,12 @@ class Path:
|
||||||
children = [self.with_child(child) for child in children]
|
children = [self.with_child(child) for child in children]
|
||||||
return children
|
return children
|
||||||
|
|
||||||
|
def glob_directories(self, pattern):
|
||||||
|
return [p for p in self.glob(pattern) if p.is_dir]
|
||||||
|
|
||||||
|
def glob_files(self, pattern):
|
||||||
|
return [p for p in self.glob(pattern) if p.is_file]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_directory(self):
|
def is_directory(self):
|
||||||
return os.path.isdir(self.absolute_path)
|
return os.path.isdir(self.absolute_path)
|
||||||
|
@ -465,34 +471,22 @@ def get_path_casing(path):
|
||||||
cased += os.sep
|
cased += os.sep
|
||||||
return cased
|
return cased
|
||||||
|
|
||||||
def glob(pattern, files=None, directories=None):
|
def glob(pattern):
|
||||||
'''
|
'''
|
||||||
Just like regular glob, except it returns Path objects instead of strings.
|
Just like regular glob, except it returns Path objects instead of strings.
|
||||||
|
|
||||||
files, directories:
|
|
||||||
Pass these arguments to filter the results. Leave both as None to get
|
|
||||||
all items, set either to True to get just those items.
|
|
||||||
|
|
||||||
If you want to recurse, consider using spinal.walk with glob_filenames
|
If you want to recurse, consider using spinal.walk with glob_filenames
|
||||||
instead.
|
instead.
|
||||||
'''
|
'''
|
||||||
if files is None and directories is None:
|
return [Path(p) for p in winglob.glob(pattern)]
|
||||||
files = True
|
|
||||||
directories = True
|
|
||||||
|
|
||||||
if not files and not directories:
|
def glob_directories(pattern):
|
||||||
raise ValueError('files and directories can\'t both be False.')
|
return [p for p in glob(pattern) if p.is_dir]
|
||||||
|
|
||||||
paths = (Path(p) for p in winglob.glob(pattern))
|
def glob_files(pattern):
|
||||||
|
return [p for p in glob(pattern) if p.is_file]
|
||||||
|
|
||||||
if files and directories:
|
def glob_many(patterns):
|
||||||
return list(paths)
|
|
||||||
if files:
|
|
||||||
return [p for p in paths if p.is_file]
|
|
||||||
if directories:
|
|
||||||
return [p for p in paths if p.is_dir]
|
|
||||||
|
|
||||||
def glob_many(patterns, files=None, directories=None):
|
|
||||||
'''
|
'''
|
||||||
Given many glob patterns, yield the results as a single generator.
|
Given many glob patterns, yield the results as a single generator.
|
||||||
Saves you from having to write the nested loop.
|
Saves you from having to write the nested loop.
|
||||||
|
@ -503,7 +497,15 @@ def glob_many(patterns, files=None, directories=None):
|
||||||
that match the glob. This function can take patterns with no common root.
|
that match the glob. This function can take patterns with no common root.
|
||||||
'''
|
'''
|
||||||
for pattern in patterns:
|
for pattern in patterns:
|
||||||
yield from glob(pattern, files=files, directories=directories)
|
yield from glob(pattern)
|
||||||
|
|
||||||
|
def glob_many_directories(patterns):
|
||||||
|
for pattern in patterns:
|
||||||
|
yield from glob_directories(pattern)
|
||||||
|
|
||||||
|
def glob_many_files(patterns):
|
||||||
|
for pattern in patterns:
|
||||||
|
yield from glob_files(pattern)
|
||||||
|
|
||||||
def glob_patternize(piece):
|
def glob_patternize(piece):
|
||||||
'''
|
'''
|
||||||
|
|
Loading…
Reference in New Issue