Skip to content

Commit

Permalink
Merge pull request #7 from bitranox/development
Browse files Browse the repository at this point in the history
v1.0.5
  • Loading branch information
bitranox authored Aug 14, 2020
2 parents 024daa1 + bf9d915 commit 66eef2a
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 115 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 v1.0.4 as of 2020-08-14 see `Changelog`_
Version v1.0.5 as of 2020-08-14 see `Changelog`_


.. include:: ./badges.rst
Expand Down
12 changes: 12 additions & 0 deletions .docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,15 @@
:code: python
:start-after: # IgnoreParserExamples{{{
:end-before: # IgnoreParserExamples}}}

- add a rule by string

.. include:: ../igittigitt/igittigitt.py
:code: python
:start-after: # add_rule{{{
:end-before: # add_rule}}}

.. include:: ../tests/test_pytest.py
:code: python
:start-after: # add_rule_Example{{{
:end-before: # add_rule_Example}}}
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ TODO:
- documentation
- asserts for __ALL__ parameters

v1.0.5
--------
2020-08-14: fix Windows and MacOs tests

v1.0.4
--------
2020-08-13: handle trailing spaces
Expand Down
28 changes: 27 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ igittigitt
==========


Version v1.0.4 as of 2020-08-14 see `Changelog`_
Version v1.0.5 as of 2020-08-14 see `Changelog`_

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

Expand Down Expand Up @@ -152,6 +152,28 @@ Usage
... print(parser)
<...IgnoreParser object at ...>
- add a rule by string

.. code-block:: python
def add_rule(self, pattern: str, base_path: PathLikeOrString):
"""
add a rule as a string
Parameter
---------
pattern
the pattern
base_path
since gitignore patterns are relative to a base
directory, that needs to be provided here
"""
.. code-block:: python
>>> parser = igittigitt.IgnoreParser()
>>> parser.add_rule('*.py[cod]', base_path='/home/michael')
Usage from Commandline
------------------------

Expand Down Expand Up @@ -283,6 +305,10 @@ TODO:
- documentation
- asserts for __ALL__ parameters

v1.0.5
--------
2020-08-14: fix Windows and MacOs tests

v1.0.4
--------
2020-08-13: handle trailing spaces
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 = 'v1.0.4'
version = 'v1.0.5'
url = 'https://github.com/bitranox/igittigitt'
author = 'Robert Nowotny'
author_email = '[email protected]'
Expand All @@ -14,7 +14,7 @@ def print_info() -> None:
A spec-compliant gitignore parser for Python
Version : v1.0.4
Version : v1.0.5
Url : https://github.com/bitranox/igittigitt
Author : Robert Nowotny
Email : [email protected]""")
51 changes: 44 additions & 7 deletions igittigitt/igittigitt.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
import re
import sys
from types import TracebackType
from typing import List, Optional, Tuple, Type
from typing import Any, List, Optional, Tuple, Type, Union

PathLikeOrString = Union[str, 'os.PathLike[Any]']

__all__ = ('IgnoreParser', )

whitespace_re = re.compile(r"(\\ )+$")

Expand Down Expand Up @@ -75,7 +78,9 @@ def __exit__(
pass

def parse_rule_file(
self, path_rule_file: os.PathLike, base_dir: Optional[os.PathLike] = None
self,
rule_file: PathLikeOrString,
base_dir: Optional[PathLikeOrString] = None,
):
"""
parse a git ignore file, create rules from a gitignore file
Expand All @@ -85,29 +90,61 @@ def parse_rule_file(
full_path
the full path to the ignore file
base_dir
todo : good description missing
optional base dir, for testing purposes only.
the base dir is the parent of the rule file,
because rules are relative to the directory
were the rule file resides
"""
if isinstance(rule_file, str):
path_rule_file = pathlib.Path(rule_file).resolve()
elif isinstance(rule_file, pathlib.Path):
path_rule_file = rule_file.resolve()
else:
raise TypeError('wrong type for "rule_file"')

if base_dir is None:
base_dir = os.path.dirname(path_rule_file)
path_base_dir = path_rule_file.parent
elif isinstance(base_dir, str):
path_base_dir = pathlib.Path(base_dir).resolve()
elif isinstance(base_dir, pathlib.Path):
path_base_dir = base_dir.resolve()
else:
raise TypeError('wrong type for "base_dir"')

with open(path_rule_file) as ignore_file:
counter = 0
for line in ignore_file:
counter += 1
line = line.rstrip("\n")
rule = rule_from_pattern(
line,
base_path=pathlib.Path(base_dir).resolve(),
base_path=path_base_dir,
source=(path_rule_file, counter),
)
if rule:
self.rules.append(rule)
if rule.negation:
self.rules_contains_negation_rule = True

def add_rule(self, pattern: str, base_path: Optional[os.PathLike] = None):
rule = rule_from_pattern(pattern, base_path)
# add_rule{{{
def add_rule(self, pattern: str, base_path: PathLikeOrString):
"""
add a rule as a string
Parameter
---------
pattern
the pattern
base_path
since gitignore patterns are relative to a base
directory, that needs to be provided here
"""
# add_rule}}}

path_base_path = pathlib.Path(base_path).resolve()

rule = rule_from_pattern(pattern, path_base_path)
if rule:
self.rules.append(rule)
if rule.negation:
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"] = "v1.0.4"
setup_kwargs["version"] = "v1.0.5"
setup_kwargs["url"] = "https://github.com/bitranox/igittigitt"
setup_kwargs["packages"] = find_packages()
setup_kwargs["package_data"] = {"igittigitt": []}
Expand Down
Loading

0 comments on commit 66eef2a

Please sign in to comment.