Skip to content

Commit

Permalink
refractor
Browse files Browse the repository at this point in the history
  • Loading branch information
bitranox committed Sep 18, 2020
1 parent cc218da commit 0b599d1
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .docs/README_template.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ igittigitt
==========


Version v2.0.1 as of 2020-09-18 see `Changelog`_
Version v2.0.2a0 as of 2020-09-18 see `Changelog`_


.. include:: ./badges.rst
Expand Down
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ TODO:
- add nested .gitignore files
- documentation

v2.0.2a0
--------
2020-09-18:
- deduplicate Rules (todo)

v2.0.1
--------
2020-09-18:
Expand Down
9 changes: 8 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ igittigitt
==========


Version v2.0.1 as of 2020-09-18 see `Changelog`_
Version v2.0.2a0 as of 2020-09-18 see `Changelog`_

|travis_build| |license| |jupyter| |pypi| |black|

Expand Down Expand Up @@ -153,6 +153,8 @@ Usage
>>> print(parser)
<...IgnoreParser object at ...>
>>> # init with context manager
>>> with igittigitt.IgnoreParser() as parser:
... print(parser)
Expand Down Expand Up @@ -372,6 +374,11 @@ TODO:
- add nested .gitignore files
- documentation

v2.0.2a0
--------
2020-09-18:
- deduplicate Rules (todo)

v2.0.1
--------
2020-09-18:
Expand Down
4 changes: 2 additions & 2 deletions igittigitt/__init__conf__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "igittigitt"
title = "A spec-compliant gitignore parser for Python"
version = "v2.0.1"
version = "v2.0.2a0"
url = "https://github.com/bitranox/igittigitt"
author = "Robert Nowotny"
author_email = "[email protected]"
Expand All @@ -15,7 +15,7 @@ def print_info() -> None:
A spec-compliant gitignore parser for Python
Version : v2.0.1
Version : v2.0.2a0
Url : https://github.com/bitranox/igittigitt
Author : Robert Nowotny
Email : [email protected]"""
Expand Down
30 changes: 18 additions & 12 deletions igittigitt/igittigitt.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,21 @@

@attr.s(auto_attribs=True)
class IgnoreRule(object):
pattern_fnmatch: str
"""
the ignore rule datastructure
>>> ignore_rule=IgnoreRule('./test/*', 'test', False, pathlib.Path('.gitignore'), 1)
>>> assert str(ignore_rule) == './test/*'
"""

pattern_glob: str
pattern_original: str
is_negation_rule: bool
source_file: Optional[pathlib.Path]
source_line_number: Optional[int]

def __str__(self) -> str:
return self.pattern_fnmatch
return self.pattern_glob


# IgnoreParser{{{
Expand All @@ -40,9 +47,6 @@ def __init__(self) -> None:
# might match again
self.last_matching_rule: Optional[IgnoreRule] = None

def __call__(self,) -> None:
self.__init__() # type: ignore

def __enter__(self) -> "IgnoreParser":
return self

Expand Down Expand Up @@ -82,7 +86,9 @@ def parse_rule_files(
self._parse_rule_file(rule_file)

def _parse_rule_file(
self, rule_file: PathLikeOrString, base_dir: Optional[PathLikeOrString] = None,
self,
rule_file: PathLikeOrString,
base_dir: Optional[PathLikeOrString] = None,
) -> None:
"""
parse a git ignore file, create rules from a gitignore file
Expand Down Expand Up @@ -188,15 +194,15 @@ def _match_rules(self, str_file_path: str) -> bool:
if self.last_matching_rule:
if wcmatch.glob.globmatch(
str_file_path,
[self.last_matching_rule.pattern_fnmatch],
[self.last_matching_rule.pattern_glob],
flags=wcmatch.glob.DOTGLOB | wcmatch.glob.GLOBSTAR,
):
return True

for rule in self.rules:
if wcmatch.glob.globmatch(
str_file_path,
[rule.pattern_fnmatch],
[rule.pattern_glob],
flags=wcmatch.glob.DOTGLOB | wcmatch.glob.GLOBSTAR,
):
self.last_matching_rule = rule
Expand All @@ -210,7 +216,7 @@ def _match_negation_rules(self, str_file_path: str) -> bool:
for rule in self.negation_rules:
if wcmatch.glob.globmatch(
str_file_path,
[rule.pattern_fnmatch],
[rule.pattern_glob],
flags=wcmatch.glob.DOTGLOB | wcmatch.glob.GLOBSTAR,
):
self.last_matching_rule = rule
Expand Down Expand Up @@ -254,7 +260,7 @@ def get_rules_from_git_pattern(
>>> assert get_rules_from_git_pattern('# some comment', some_base_dir) == []
>>> assert get_rules_from_git_pattern(' # some comment', some_base_dir) == []
>>> get_rules_from_git_pattern(r' \\#some_file', some_base_dir)
[IgnoreRule(pattern_fnmatch='.../**/\\\\#some_file', ...)]
[IgnoreRule(pattern_glob='.../**/\\\\#some_file', ...)]
>>> # Trailing spaces are ignored unless
>>> # they are quoted with backslash ("\").
Expand All @@ -263,7 +269,7 @@ def get_rules_from_git_pattern(
>>> # but it seems like !
>>> # see: https://stackoverflow.com/questions/10213653
>>> get_rules_from_git_pattern(r'something \\ ', some_base_dir)
[IgnoreRule(pattern_fnmatch='.../**/something\\\\ ', ...)]
[IgnoreRule(pattern_glob='.../**/something\\\\ ', ...)]
>>> # If there is a separator at the beginning or middle (or both)
>>> # of the pattern, then the pattern is relative to the directory
Expand Down Expand Up @@ -320,7 +326,7 @@ def get_rules_from_git_pattern(
for pattern in l_patterns:
l_ignore_rules.append(
IgnoreRule(
pattern_fnmatch=pattern,
pattern_glob=pattern,
pattern_original=pattern_original,
is_negation_rule=is_negation_rule,
source_file=path_source_file,
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def get_line_data(line: str) -> str:

setup_kwargs: Dict[str, Any] = dict()
setup_kwargs["name"] = "igittigitt"
setup_kwargs["version"] = "v2.0.1"
setup_kwargs["version"] = "v2.0.2a0"
setup_kwargs["url"] = "https://github.com/bitranox/igittigitt"
setup_kwargs["packages"] = find_packages()
setup_kwargs["package_data"] = {"igittigitt": ["py.typed", "*.pyi", "__init__.pyi"]}
Expand Down
3 changes: 3 additions & 0 deletions tests/example/.test_gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
test__pycache__
**/test__pycache__
*.py[cod]
.test_venv/
.test_venv/**
.test_venv/*
5 changes: 5 additions & 0 deletions tests/example/test__pycache__/.test_gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# this file is ignored, since the whole directory is ignored
# therefore it is not processed
test__pycache__
*.py[cod]
.test_venv/
8 changes: 6 additions & 2 deletions tests/test_pytest.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ def test_parse_rule_files():
if not ignore_parser.match(path):
paths_filtered.append(path.name)
assert sorted(paths_filtered) == [
".test_gitignore",
".test_gitignore",
".test_gitignore",
"excluded_not",
Expand Down Expand Up @@ -178,7 +179,9 @@ def test_shutil_ignore_function():
ignore_parser = igittigitt.IgnoreParser()
ignore_parser.parse_rule_files(base_dir=path_source_dir, filename=".test_gitignore")
shutil.copytree(
path_source_dir, path_target_dir, ignore=ignore_parser.shutil_ignore,
path_source_dir,
path_target_dir,
ignore=ignore_parser.shutil_ignore,
)

assert len(list(path_target_dir.glob("**/*"))) == 8
Expand All @@ -198,6 +201,8 @@ def doctest_examples():
>>> print(parser)
<...IgnoreParser object at ...>
>>> # init with context manager
>>> with igittigitt.IgnoreParser() as parser:
... print(parser)
Expand All @@ -211,7 +216,6 @@ def doctest_examples():
# add_rule_Example}}}
>>> path_test_dir = pathlib.Path(__file__).parent.resolve()
>>> path_target_dir = path_test_dir / "target"
>>> shutil.rmtree(path_target_dir, ignore_errors=True)
Expand Down

0 comments on commit 0b599d1

Please sign in to comment.