Socket
Book a DemoInstallSign in
Socket

textual-universal-directorytree

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

textual-universal-directorytree

A Textual Directory Tree for all File Systems

Source
pipPyPI
Version
1.2.1
Maintainers
1

textual-universal-directorytree

textual-universal-directorytree

DirectoryTree widget for textual, compatible with all filesystems

PyPI PyPI - Python Version Docker Image Version Conda Version GitHub License Testing Status task uv pre-commit docs semantic-release Gitmoji

Installation

pip install textual-universal-directorytree

Extra Dependencies

Some filesystems require additional dependencies to be installed. The remote extra includes all the known optional dependencies. See the Filesystems Supported section for more information.

pip install "textual-universal-directorytree[remote]"

Usage

The below example shows how to use textual-universal-directorytree in a Textual app. It uses the GitHub filesystem to display the contents of the textual GitHub repository. It requires the requests library to be installed (or the remote extra).

from __future__ import annotations

from typing import Any, ClassVar

from rich.syntax import Syntax
from textual import on
from textual.app import App, ComposeResult
from textual.binding import BindingType
from textual.containers import Horizontal, VerticalScroll
from textual.widgets import DirectoryTree, Footer, Header, Static

from textual_universal_directorytree import UniversalDirectoryTree, UPath


class UniversalDirectoryTreeApp(App):
    """
    The power of upath and fsspec in a Textual app
    """

    TITLE = "UniversalDirectoryTree"

    CSS = """
    UniversalDirectoryTree {
        max-width: 50%;
        width: auto;
        height: 100%;
        dock: left;
    }
    """

    BINDINGS: ClassVar[list[BindingType]] = [
        ("q", "quit", "Quit"),
    ]

    def __init__(self, path: str | UPath, *args: Any, **kwargs: Any):
        super().__init__(*args, **kwargs)
        self.universal_path = UPath(path).resolve()
        self.directory_tree = UniversalDirectoryTree(path=self.universal_path)
        self.file_content = Static(expand=True)

    def compose(self) -> ComposeResult:
        yield Header()
        yield Horizontal(self.directory_tree, VerticalScroll(self.file_content))
        yield Footer()

    @on(DirectoryTree.FileSelected)
    def handle_file_selected(self, message: DirectoryTree.FileSelected) -> None:
        """
        Do something with the selected file.

        Objects returned by the FileSelected event are upath.UPath objects and
        they are compatible with the familiar pathlib.Path API built into Python.
        """
        self.sub_title = str(message.path)
        try:
            file_content = message.path.read_text()
        except UnicodeDecodeError:
            self.file_content.update("")
            return None
        lexer = Syntax.guess_lexer(path=message.path.name, code=file_content)
        code = Syntax(code=file_content, lexer=lexer)
        self.file_content.update(code)

Run the above app in your terminal:

python -m textual_universal_directorytree github://juftin:textual-universal-directorytree@main/

Filesystems Supported

textual-universal-directorytree leverages fsspec and universal_pathlib to enable compatibility with local and remote filesystems.

In some cases you need to install a filesystem-specific library to enable compatibility with that filesystem. For example, to enable compatibility with AWS S3 you must install s3fs which is an fsspec implementation for S3.

The following filesystems are known to be supported by textual-universal-directorytree, but it's possible that others filesystems are supported as well and just haven't been tested. If you find a filesystem that works, please open an issue.

File SystemFormatOptional Dependencies
Localpath/to/fileNone
Localfile://path/to/fileNone
AWS S3s3://bucket/paths3fs
AWS S3s3a://bucket/paths3fs
Google GCSgs://bucket/pathgcsfs
Azure Data Lakeadl://bucket/pathadlfs
Azure Blobabfs://bucket/pathadlfs
Azure Blobaz://bucket/pathadlfs
GitHubgithub://owner:repo@branchrequests
GitHubgithub://owner:repo@branch/pathrequests
SSHssh://user@host:port/pathparamiko
SFTPsftp://user@host:port/pathparamiko

License

textual-universal-directorytree is distributed under the terms of the MIT license.

Keywords

fsspec

FAQs

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts