diff --git a/src/unasync/__init__.py b/src/unasync/__init__.py index 7334f71..aa540df 100644 --- a/src/unasync/__init__.py +++ b/src/unasync/__init__.py @@ -52,13 +52,18 @@ def isidentifier(s): StringIO = io.StringIO +if hasattr(os, "fspath"): # PY3 + fspath = os.fspath +else: # PY2 + fspath = str + class Rule: """A single set of rules for 'unasync'ing file(s)""" def __init__(self, fromdir, todir, additional_replacements=None): - self.fromdir = fromdir.replace("/", os.sep) - self.todir = todir.replace("/", os.sep) + self.fromdir = fspath(fromdir).replace("/", os.sep) + self.todir = fspath(todir).replace("/", os.sep) # Add any additional user-defined token replacements to our list. self.token_replacements = _ASYNC_TO_SYNC.copy() @@ -69,6 +74,8 @@ def _match(self, filepath): """Determines if a Rule matches a given filepath and if so returns a higher comparable value if the match is more specific. """ + filepath = fspath(filepath) + file_segments = [x for x in filepath.split(os.sep) if x] from_segments = [x for x in self.fromdir.split(os.sep) if x] len_from_segments = len(from_segments) @@ -83,6 +90,7 @@ def _match(self, filepath): return False def _unasync_file(self, filepath): + filepath = fspath(filepath) with open(filepath, "rb") as f: write_kwargs = {} if sys.version_info[0] >= 3: # PY3 # pragma: no branch diff --git a/test-requirements.txt b/test-requirements.txt index 2c7cf23..55b2caa 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -1,2 +1,3 @@ pytest>=4.3.0 -pytest-cov \ No newline at end of file +pytest-cov +pathlib2 ; python_version < '3.5' \ No newline at end of file diff --git a/tests/test_unasync.py b/tests/test_unasync.py index f8c4ed7..7629bb9 100644 --- a/tests/test_unasync.py +++ b/tests/test_unasync.py @@ -2,6 +2,11 @@ import errno import io import os + +try: + import pathlib +except ImportError: + import pathlib2 as pathlib import shutil import subprocess import sys @@ -43,6 +48,12 @@ def test_rule_on_short_path(): assert rule._match("/ahip/") is False +def test_rule_with_pathlib_path(): + path_async_base = pathlib.Path("/ahip") + path_sync_base = pathlib.Path("/hip") + unasync.Rule(path_async_base / "tests", path_sync_base / "tests") + + @pytest.mark.parametrize("source_file", TEST_FILES) def test_unasync(tmpdir, source_file):