support implicit globs!
This commit is contained in:
parent
c2961cb1cf
commit
5706f690e7
2 changed files with 37 additions and 8 deletions
11
my/common.py
11
my/common.py
|
@ -1,3 +1,4 @@
|
||||||
|
from glob import glob as do_glob
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import functools
|
import functools
|
||||||
import types
|
import types
|
||||||
|
@ -126,9 +127,13 @@ def get_files(pp: Paths, glob: str='*', sort: bool=True) -> Tuple[Path, ...]:
|
||||||
gp: Iterable[Path] = src.glob(glob)
|
gp: Iterable[Path] = src.glob(glob)
|
||||||
paths.extend(gp)
|
paths.extend(gp)
|
||||||
else:
|
else:
|
||||||
assert src.is_file(), src
|
ss = str(src)
|
||||||
# TODO FIXME assert matches glob??
|
if '*' in ss:
|
||||||
paths.append(src)
|
paths.extend(map(Path, do_glob(ss)))
|
||||||
|
else:
|
||||||
|
assert src.is_file(), src
|
||||||
|
# todo assert matches glob??
|
||||||
|
paths.append(src)
|
||||||
|
|
||||||
if sort:
|
if sort:
|
||||||
paths = list(sorted(paths))
|
paths = list(sorted(paths))
|
||||||
|
|
|
@ -51,9 +51,9 @@ def test_multiple_files():
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_glob():
|
def test_explicit_glob():
|
||||||
'''
|
'''
|
||||||
You can pass a blog to restrict the extensions
|
You can pass a glob to restrict the extensions
|
||||||
'''
|
'''
|
||||||
|
|
||||||
create('/tmp/hpi_test/file_3.zip')
|
create('/tmp/hpi_test/file_3.zip')
|
||||||
|
@ -61,15 +61,39 @@ def test_glob():
|
||||||
create('/tmp/hpi_test/ignoreme')
|
create('/tmp/hpi_test/ignoreme')
|
||||||
create('/tmp/hpi_test/file.zip')
|
create('/tmp/hpi_test/file.zip')
|
||||||
|
|
||||||
assert get_files('/tmp/hpi_test', 'file_*.zip') == (
|
# todo walrus operator would be great here...
|
||||||
|
expected = (
|
||||||
Path('/tmp/hpi_test/file_2.zip'),
|
Path('/tmp/hpi_test/file_2.zip'),
|
||||||
Path('/tmp/hpi_test/file_3.zip'),
|
Path('/tmp/hpi_test/file_3.zip'),
|
||||||
)
|
)
|
||||||
|
assert get_files('/tmp/hpi_test', 'file_*.zip') == expected
|
||||||
|
|
||||||
# named argument should work too
|
"named argument should work too"
|
||||||
assert len(get_files('/tmp/hpi_test', glob='file_*.zip')) > 0
|
assert get_files('/tmp/hpi_test', glob='file_*.zip') == expected
|
||||||
|
|
||||||
|
|
||||||
|
def test_implicit_blog():
|
||||||
|
'''
|
||||||
|
Asterisc in the path results in globing too.
|
||||||
|
'''
|
||||||
|
# todo hopefully that makes sense? dunno why would anyone actually rely on asteriscs in names..
|
||||||
|
# this is very convenient in configs, so people don't have to use some special types
|
||||||
|
|
||||||
|
create('/tmp/hpi_test/123/')
|
||||||
|
create('/tmp/hpi_test/123/dummy')
|
||||||
|
create('/tmp/hpi_test/123/file.zip')
|
||||||
|
create('/tmp/hpi_test/456/')
|
||||||
|
create('/tmp/hpi_test/456/dummy')
|
||||||
|
create('/tmp/hpi_test/456/file.zip')
|
||||||
|
|
||||||
|
assert get_files(['/tmp/hpi_test/*/*.zip']) == (
|
||||||
|
Path('/tmp/hpi_test/123/file.zip'),
|
||||||
|
Path('/tmp/hpi_test/456/file.zip'),
|
||||||
|
)
|
||||||
|
|
||||||
|
# TODO not sure if should uniquify if the filenames end up same?
|
||||||
|
# TODO not sure about the symlinks? and hidden files?
|
||||||
|
|
||||||
test_path = Path('/tmp/hpi_test')
|
test_path = Path('/tmp/hpi_test')
|
||||||
def setup():
|
def setup():
|
||||||
teardown()
|
teardown()
|
||||||
|
|
Loading…
Add table
Reference in a new issue