From f25f5a960413f3d4f211c0ae2654c7fb708e163c Mon Sep 17 00:00:00 2001 From: Justin Flannery Date: Wed, 13 Mar 2024 14:52:09 -0600 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20SFTPTextualPath?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- textual_universal_directorytree/__init__.py | 4 ++++ .../alternate_paths.py | 23 +++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/textual_universal_directorytree/__init__.py b/textual_universal_directorytree/__init__.py index ab4e2c5..27b5a1b 100644 --- a/textual_universal_directorytree/__init__.py +++ b/textual_universal_directorytree/__init__.py @@ -7,6 +7,7 @@ from textual_universal_directorytree.alternate_paths import ( GitHubTextualPath, S3TextualPath, + SFTPTextualPath, ) from textual_universal_directorytree.universal_directory_tree import ( UniversalDirectoryTree, @@ -16,6 +17,8 @@ registry.register_implementation(protocol="github", cls=GitHubTextualPath, clobber=True) registry.register_implementation(protocol="s3", cls=S3TextualPath, clobber=True) registry.register_implementation(protocol="s3a", cls=S3TextualPath, clobber=True) +registry.register_implementation(protocol="ssh", cls=SFTPTextualPath, clobber=True) +registry.register_implementation(protocol="sftp", cls=SFTPTextualPath, clobber=True) __all__ = [ "UniversalDirectoryTree", @@ -24,4 +27,5 @@ "GitHubTextualPath", "S3TextualPath", "UPath", + "SFTPTextualPath", ] diff --git a/textual_universal_directorytree/alternate_paths.py b/textual_universal_directorytree/alternate_paths.py index 0d24471..4b28950 100644 --- a/textual_universal_directorytree/alternate_paths.py +++ b/textual_universal_directorytree/alternate_paths.py @@ -8,6 +8,7 @@ import re from typing import Any +from upath import UPath from upath.implementations.cloud import S3Path from upath.implementations.github import GitHubPath @@ -116,3 +117,25 @@ def name(self) -> str: return f"{self._url.scheme}://{self._url.netloc}" else: return super().name + + +class SFTPTextualPath(UPath): + """ + SFTPTextualPath + """ + + def __str__(self) -> str: + """ + String representation of the SFTPPath + """ + string_representation = f"{self.protocol}://" + if "username" in self.storage_options: + string_representation += f"{self.storage_options['username']}@" + string_representation += f"{self.storage_options['host']}" + if "port" in self.storage_options: + string_representation += f":{self.storage_options['port']}" + if self.path == ".": + string_representation += "/" + else: + string_representation += f"/{self.path}" + return string_representation