From 1bb298c5177b9d77f757c909876b96c2f5e76a0b Mon Sep 17 00:00:00 2001 From: Ben Felder <20211140+pykong@users.noreply.github.com> Date: Mon, 2 Mar 2020 15:20:20 +0100 Subject: [PATCH] Accept pathlib.Path as arguments (#34) --- pathspec/__init__.py | 1 + pathspec/pathspec.py | 13 +++++++------ pathspec/tests/test_util.py | 13 ++++++++++++- pathspec/util.py | 8 ++++---- 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/pathspec/__init__.py b/pathspec/__init__.py index dd0f69e..88324be 100644 --- a/pathspec/__init__.py +++ b/pathspec/__init__.py @@ -47,6 +47,7 @@ "jdufresne ", "groodt ", "ftrofin ", + "pykong " ] __email__ = "cpburnz@gmail.com" __license__ = "MPL 2.0" diff --git a/pathspec/pathspec.py b/pathspec/pathspec.py index 0a24364..cb6f767 100644 --- a/pathspec/pathspec.py +++ b/pathspec/pathspec.py @@ -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 `. + *file* (:class:`str`; or :class: `Path`) is the file path to be + matched against :attr:`self.patterns `. *separators* (:class:`~collections.abc.Collection` of :class:`str`) optionally contains the path separators to normalize. See @@ -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 - `. + *files* (:class:`~collections.abc.Iterable` of + (:class:`str`; or :class: `Path`)) contains the file paths to be + matched against :attr:`self.patterns `. *separators* (:class:`~collections.abc.Collection` of :class:`str`; or :data:`None`) optionally contains the path separators to @@ -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 diff --git a/pathspec/tests/test_util.py b/pathspec/tests/test_util.py index cf01aa6..943bde2 100644 --- a/pathspec/tests/test_util.py +++ b/pathspec/tests/test_util.py @@ -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): @@ -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) diff --git a/pathspec/util.py b/pathspec/util.py index 9a34e76..22352c3 100644 --- a/pathspec/util.py +++ b/pathspec/util.py @@ -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. @@ -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) @@ -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.