From e53a0a977b9c9ee0c2a197191ac736a1ec115774 Mon Sep 17 00:00:00 2001 From: juftin Date: Thu, 12 Oct 2023 14:30:07 -0600 Subject: [PATCH] Add GitHubPath --- upath/implementations/github.py | 25 ++++++++++++++++++++++ upath/registry.py | 1 + upath/tests/implementations/test_github.py | 25 ++++++++++++++++++++++ upath/tests/test_registry.py | 1 + 4 files changed, 52 insertions(+) create mode 100644 upath/implementations/github.py create mode 100644 upath/tests/implementations/test_github.py diff --git a/upath/implementations/github.py b/upath/implementations/github.py new file mode 100644 index 00000000..4417fa20 --- /dev/null +++ b/upath/implementations/github.py @@ -0,0 +1,25 @@ +""" +GitHub implementation of UPath +""" + +import upath.core + + +class _GitHubAccessor(upath.core._FSSpecAccessor): + """ + FSSpecAccessor for GitHub + """ + + def _format_path(self, path: upath.core.UPath) -> str: + """ + Remove the leading slash from the path + """ + return path._path.lstrip("/") + + +class GitHubPath(upath.core.UPath): + """ + GitHubPath supporting the fsspec.GitHubFileSystem + """ + + _default_accessor = _GitHubAccessor diff --git a/upath/registry.py b/upath/registry.py index 085b2274..66c8dd53 100644 --- a/upath/registry.py +++ b/upath/registry.py @@ -74,6 +74,7 @@ class _Registry(MutableMapping[str, "type[upath.core.UPath]"]): "s3a": "upath.implementations.cloud.S3Path", "webdav+http": "upath.implementations.webdav.WebdavPath", "webdav+https": "upath.implementations.webdav.WebdavPath", + "github": "upath.implementations.github.GitHubPath", } def __init__(self) -> None: diff --git a/upath/tests/implementations/test_github.py b/upath/tests/implementations/test_github.py new file mode 100644 index 00000000..8e5ea71d --- /dev/null +++ b/upath/tests/implementations/test_github.py @@ -0,0 +1,25 @@ +import pytest + +from upath import UPath +from upath.implementations.github import GitHubPath +from upath.tests.cases import BaseTests + + +class TestUPathGitHubPath(BaseTests): + """ + Unit-tests for the GitHubPath implementation of UPath. + """ + + @pytest.fixture(autouse=True) + def path(self): + """ + Fixture for the UPath instance to be tested. + """ + path = "github://fsspec:universal_pathlib@main" + self.path = UPath(path) + + def test_is_GitHubPath(self): + """ + Test that the path is a GitHubPath instance. + """ + assert isinstance(self.path, GitHubPath) diff --git a/upath/tests/test_registry.py b/upath/tests/test_registry.py index 93388f11..9306a08c 100644 --- a/upath/tests/test_registry.py +++ b/upath/tests/test_registry.py @@ -22,6 +22,7 @@ "s3a", "webdav+http", "webdav+https", + "github", }