Skip to content

Commit

Permalink
DNP: Handle directories with include, exclude glob patterns - fix: #3
Browse files Browse the repository at this point in the history
  • Loading branch information
andras-tim committed Jan 23, 2017
1 parent 7560b0d commit f411ba5
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).


## [Unreleased][unreleased]
### Added
- Handle directories with include, exclude glob patterns - issue #1


## [0.1.6] - 2016-08-31
Expand Down
55 changes: 55 additions & 0 deletions tests/test_file_iterator.py
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)
19 changes: 17 additions & 2 deletions wscheck/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,24 @@
import os
import sys

from file_iterator import FileIterator
from checker import WhitespaceChecker, RULES
from printer import ErrorPrinter


def main():
"""
:rtype: int
"""

args = _get_args()

if args.list_rules:
_list_rules()
return 0

checker = WhitespaceChecker(excluded_rules=args.excludes)
for file_path in args.paths:
for file_path in FileIterator(include_paths=args.paths):
checker.check_file(file_path)

printer = ErrorPrinter(args.paths, checker.issues)
Expand All @@ -40,9 +45,19 @@ def _list_rules():


def _get_args():
"""
:return: w/ populated argparse.Namespace
"""

def rule(rule_name):
"""
:type rule_name: str
:rtype: str
"""

if rule_name not in RULES:
raise argparse.ArgumentTypeError('Unknown rule')

return rule_name

parser = argparse.ArgumentParser(description=__doc__, formatter_class=WideHelpFormatter)
Expand All @@ -69,7 +84,7 @@ class WideHelpFormatter(argparse.RawDescriptionHelpFormatter):
def __init__(self, prog, *args, **kwargs):
indent_increment = 2
max_help_position = 40
width = int(os.getenv("COLUMNS", 120)) - 2
width = int(os.getenv('COLUMNS', 120)) - 2

super(WideHelpFormatter, self).__init__(prog, indent_increment, max_help_position, width)

Expand Down
53 changes: 53 additions & 0 deletions wscheck/file_iterator.py
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')]
# #

0 comments on commit f411ba5

Please sign in to comment.