-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DNP: Handle directories with include, exclude glob patterns - fix: #3
- Loading branch information
1 parent
7560b0d
commit f411ba5
Showing
4 changed files
with
127 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import pytest | ||
|
||
from wscheck.file_iterator import FileIterator | ||
|
||
|
||
@pytest.fixture() | ||
def file_iterator(tmpdir): | ||
""" | ||
:type tmpdir: py._path.local.LocalPath | ||
""" | ||
return FileIterator(base_path=str(tmpdir)) | ||
|
||
|
||
def assert_end_of_iterator(iterator): | ||
__tracebackhide__ = True # noqa | ||
|
||
with pytest.raises(StopIteration, message='Iterator is iterable yet'): | ||
next(iterator) | ||
|
||
|
||
class TestResultIterator(object): | ||
@pytest.mark.parametrize('paths', [ | ||
[], | ||
['apple'], | ||
['apple', 'banana'], | ||
]) | ||
def test_paths(self, tmpdir, file_iterator, paths): | ||
""" | ||
:type tmpdir: py._path.local.LocalPath | ||
:type file_iterator: FileIterator | ||
:type paths: list | ||
""" | ||
|
||
temp_paths = [] | ||
for path in paths: | ||
temp_file = tmpdir.join(path) | ||
temp_file.write('') | ||
temp_paths.append(str(temp_file)) | ||
|
||
iterator = file_iterator.iter(paths) | ||
|
||
for temp_path in temp_paths: | ||
assert temp_path == next(iterator) | ||
assert_end_of_iterator(iterator) | ||
|
||
|
||
class TestPathsIsExist(object): | ||
def test_non_existed_paths(self, file_iterator): | ||
""" | ||
:type file_iterator: FileIterator | ||
""" | ||
|
||
iterator = file_iterator.iter(['foo', 'bar']) | ||
|
||
assert_end_of_iterator(iterator) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import os | ||
|
||
|
||
class FileIterator(object): | ||
def __init__(self, base_path=None): | ||
""" | ||
:type base_path: str or None | ||
""" | ||
self.__base_path = base_path | ||
if base_path is None: | ||
base_path = os.getcwd() | ||
|
||
def __get_full_path(self, path): | ||
""" | ||
:type path: str | ||
:rtype: str | ||
""" | ||
|
||
if path[0] == os.path.sep: | ||
return path | ||
|
||
return os.path.join(self.__base_path, path) | ||
|
||
def iter(self, paths, include_globs=None, exclude_globs=None): | ||
""" | ||
:type paths: list | ||
:type include_globs: list or None | ||
:type exclude_globs: list or None | ||
:rtype: iterator | ||
""" | ||
|
||
full_paths = [self.__get_full_path(path) for path in paths] | ||
|
||
if include_globs is None: | ||
include_globs = [] | ||
|
||
if exclude_globs is None: | ||
exclude_globs = [] | ||
|
||
for path in full_paths: | ||
yield path | ||
|
||
# if os.path.isfile(include_path): | ||
# yield include_path | ||
# continue | ||
# | ||
# os.walk(include_path) | ||
# # | ||
# # def _ | ||
# # | ||
# # | ||
# # return [x[0] for x in os.walk('/home/tia/bin')] | ||
# # |