Skip to content

Commit

Permalink
Accept pathlib.Path as arguments (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
pykong authored Mar 2, 2020
1 parent bec1296 commit 1bb298c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
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__ = "cpburnz@gmail.com"
__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

0 comments on commit 1bb298c

Please sign in to comment.