-
-
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.
add directory traversal to command line utility (to be documented)
- Loading branch information
Showing
6 changed files
with
138 additions
and
18 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
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,34 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Generic or common utility functions""" | ||
from __future__ import print_function, unicode_literals | ||
from os.path import splitext, join | ||
from scandir import scandir, walk | ||
|
||
DEFAULT_EXTENSIONS = {'xml', 'onx', 'onix'} | ||
|
||
|
||
def iter_files(root, exts=None, recursive=False): | ||
""" | ||
Iterate over file paths that match the given extensions within root. | ||
:param str or unicode root: Root folder to start collecting files | ||
:param iterable exts: file extensions to include | ||
:param bool recursive: Wether to walk the complete directory tree | ||
:return iterable: Generator of absolute file paths with given extensions | ||
""" | ||
|
||
if exts is None: | ||
exts = DEFAULT_EXTENSIONS | ||
|
||
exts = {x.lower() for x in exts} | ||
|
||
if recursive is False: | ||
for entry in scandir(root): | ||
ext = splitext(entry.name)[-1].lstrip('.').lower() | ||
if entry.is_file() and ext in exts: | ||
yield entry.path | ||
else: | ||
for root, folders, files in walk(root): | ||
for f in files: | ||
if splitext(f)[-1].lstrip('.').lower() in exts: | ||
yield join(root, f) |
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,32 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import print_function | ||
import types | ||
from onixcheck import utils | ||
from os.path import abspath, dirname | ||
|
||
|
||
TEST_DIR = abspath(dirname(utils.__file__)) | ||
|
||
|
||
def test_iter_files_simple(): | ||
gen = utils.iter_files(TEST_DIR) | ||
assert isinstance(gen, types.GeneratorType) | ||
assert list(gen) > 5 | ||
|
||
|
||
def test_iter_files_no_matches(): | ||
gen = utils.iter_files(TEST_DIR, ['noext']) | ||
assert len(list(gen)) == 0 | ||
|
||
|
||
def test_iter_files_flat(): | ||
gen = utils.iter_files(TEST_DIR, ['xsd']) | ||
assert len(list(gen)) == 0 | ||
gen = utils.iter_files(TEST_DIR, ['PY']) | ||
assert len(list(gen)) > 5 | ||
|
||
|
||
def test_iter_files_recursive(): | ||
gen = utils.iter_files(TEST_DIR, ['xsd'], recursive=True) | ||
assert len(list(gen)) > 5 | ||
|