Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept pathlib.Path as arguments #34

Merged
merged 9 commits into from
Mar 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pathspec/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"jdufresne <https://github.com/jdufresne>",
"groodt <https://github.com/groodt>",
"ftrofin <https://github.com/ftrofin>",
"pykong <https://github.com/pykong>"
]
__email__ = "[email protected]"
__license__ = "MPL 2.0"
Expand Down
13 changes: 7 additions & 6 deletions pathspec/pathspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ def match_file(self, file, separators=None):
"""
Matches the file to this path-spec.

*file* (:class:`str`) is the file path to be matched against
:attr:`self.patterns <PathSpec.patterns>`.
*file* (:class:`str`; or :class: `Path`) is the file path to be
matched against :attr:`self.patterns <PathSpec.patterns>`.

*separators* (:class:`~collections.abc.Collection` of :class:`str`)
optionally contains the path separators to normalize. See
Expand All @@ -94,9 +94,9 @@ def match_files(self, files, separators=None):
"""
Matches the files to this path-spec.

*files* (:class:`~collections.abc.Iterable` of :class:`str`) contains
the file paths to be matched against :attr:`self.patterns
<PathSpec.patterns>`.
*files* (:class:`~collections.abc.Iterable` of
(:class:`str`; or :class: `Path`)) contains the file paths to be
matched against :attr:`self.patterns <PathSpec.patterns>`.

*separators* (:class:`~collections.abc.Collection` of :class:`str`;
or :data:`None`) optionally contains the path separators to
Expand All @@ -119,7 +119,8 @@ def match_tree_files(self, root, on_error=None, follow_links=None):
Walks the specified root path for all files and matches them to this
path-spec.

*root* (:class:`str`) is the root directory to search for files.
*root* (:class:`str`; or :class: `Path`) is the root directory to
search for files.

*on_error* (:class:`~collections.abc.Callable` or :data:`None`)
optionally is the error handler for file-system exceptions. See
Expand Down
13 changes: 12 additions & 1 deletion pathspec/tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
import os
import os.path
import shutil
import sys
import tempfile
import unittest

from pathspec.util import iter_tree_entries, iter_tree_files, RecursionError
from pathspec.util import iter_tree_entries, iter_tree_files, RecursionError, normalize_file


class IterTreeTest(unittest.TestCase):
Expand Down Expand Up @@ -367,3 +368,13 @@ def test_3_entries(self):
'Dir/Inner/f',
'Empty',
])))

@unittest.skipIf(sys.version_info < (3, 4), "pathlib entered stdlib in Python 3.4")
def test_4_normalizing_pathlib_path(self):
"""
Tests passing pathlib.Path as argument.
"""
from pathlib import Path
first_spec = normalize_file(Path('a.txt'))
second_spec = normalize_file('a.txt')
self.assertEqual(first_spec, second_spec)
8 changes: 4 additions & 4 deletions pathspec/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ def normalize_file(file, separators=None):
"""
Normalizes the file path to use the POSIX path separator (i.e., ``'/'``).

*file* (:class:`str`) is the file path.
*file* (:class:`str`; or :class: `Path`) is the file path.

*separators* (:class:`~collections.abc.Collection` of :class:`str`; or
:data:`None`) optionally contains the path separators to normalize.
Expand All @@ -276,7 +276,7 @@ def normalize_file(file, separators=None):
# Normalize path separators.
if separators is None:
separators = NORMALIZE_PATH_SEPS
norm_file = file
norm_file = str(file) # stringify pathlib.Path
for sep in separators:
norm_file = norm_file.replace(sep, posixpath.sep)

Expand All @@ -290,8 +290,8 @@ def normalize_files(files, separators=None):
"""
Normalizes the file paths to use the POSIX path separator.

*files* (:class:`~collections.abc.Iterable` of :class:`str`) contains
the file paths to be normalized.
*files* (:class:`~collections.abc.Iterable` of :class:`str`;
or :class: `Path`) contains the file paths to be normalized.

*separators* (:class:`~collections.abc.Collection` of :class:`str`; or
:data:`None`) optionally contains the path separators to normalize.
Expand Down