Skip to content

Commit

Permalink
Merge pull request #30 from brl0/20210804
Browse files Browse the repository at this point in the history
Fixes #25, #26, #29
  • Loading branch information
andrewfulton9 authored Aug 9, 2021
2 parents dbc57f7 + ea19fc1 commit 9c1b17c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
17 changes: 15 additions & 2 deletions upath/core.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
from abc import ABCMeta
import os
import pathlib
import urllib

from fsspec.registry import known_implementations, registry
from fsspec.utils import stringify_path

from upath.registry import _registry


class UPath(pathlib.Path):
class UPathMeta(ABCMeta):
def __instancecheck__(cls, instance):
return isinstance(instance, pathlib.Path)

def __subclasscheck__(cls, subclass):
return issubclass(subclass, pathlib.Path)


class UPath(pathlib.Path, metaclass=UPathMeta):
def __new__(cls, *args, **kwargs):
if cls is UPath:
if issubclass(cls, UPath):
args_list = list(args)
url = args_list.pop(0)
url = stringify_path(url)
parsed_url = urllib.parse.urlparse(url)
for key in ["scheme", "netloc"]:
val = kwargs.get(key)
Expand All @@ -38,4 +49,6 @@ def __new__(cls, *args, **kwargs):
args = tuple(args_list)
self = cls._from_parts_init(args, init=False)
self._init(*args, **kwargs)
else:
self = super().__new__(*args, **kwargs)
return self
9 changes: 3 additions & 6 deletions upath/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os
import tempfile
import shutil
from pathlib import Path
import subprocess
import shlex
Expand Down Expand Up @@ -48,15 +47,14 @@ def clear_registry():

@pytest.fixture()
def tempdir(clear_registry):
tempdir = tempfile.TemporaryDirectory()
tempdir = tempdir.name
return tempdir
with tempfile.TemporaryDirectory() as tempdir:
yield tempdir


@pytest.fixture()
def local_testdir(tempdir, clear_registry):
tmp = Path(tempdir)
tmp.mkdir()
tmp.mkdir(exist_ok=True)
folder1 = tmp.joinpath("folder1")
folder1.mkdir()
folder1_files = ["file1.txt", "file2.txt"]
Expand All @@ -75,7 +73,6 @@ def local_testdir(tempdir, clear_registry):
yield str(Path(tempdir)).replace("\\", "/")
else:
yield tempdir
shutil.rmtree(tempdir)


@pytest.fixture()
Expand Down
32 changes: 32 additions & 0 deletions upath/tests/test_core.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sys
import pathlib
import warnings
from pathlib import Path

import pytest

Expand Down Expand Up @@ -52,3 +53,34 @@ def test_multiple_backend_paths(local_testdir, s3, hdfs):
path = f"hdfs:{local_testdir}"
UPath(path, host=host, user=user, port=port)
assert s3_path.joinpath("text1.txt")._url.scheme == "s3"


def test_constructor_accept_path(local_testdir):
path = UPath(pathlib.Path(local_testdir))
assert str(path) == str(Path(local_testdir))


def test_constructor_accept_upath(local_testdir):
path = UPath(UPath(local_testdir))
assert str(path) == str(Path(local_testdir))


def test_subclass(local_testdir):
class MyPath(UPath):
pass

path = MyPath(local_testdir)
assert str(path) == str(Path(local_testdir))
assert issubclass(MyPath, UPath)
assert isinstance(path, pathlib.Path)


def test_instance_check(local_testdir):
path = UPath(local_testdir)
assert isinstance(path, UPath)


def test_new_method(local_testdir):
path = UPath.__new__(pathlib.Path, local_testdir)
assert str(path) == str(Path(local_testdir))
assert isinstance(path, pathlib.Path)

0 comments on commit 9c1b17c

Please sign in to comment.