Skip to content

Commit

Permalink
✨ Python 3.12
Browse files Browse the repository at this point in the history
  • Loading branch information
juftin committed Mar 14, 2024
1 parent 412ac18 commit 8d1cd95
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 48 deletions.
4 changes: 2 additions & 2 deletions browsr/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ class TextualAppContext:
kwargs: dict[str, Any] | None = None

@property
def path(self) -> pathlib.Path:
def path(self) -> UPath:
"""
Resolve `file_path` to a upath.UPath object
Resolve `file_path` to a UPath object
"""
if "github" in str(self.file_path).lower():
file_path = str(self.file_path)
Expand Down
6 changes: 2 additions & 4 deletions browsr/screens/code_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from __future__ import annotations

import pathlib
from typing import ClassVar, Iterable, cast

from rich import traceback
Expand All @@ -15,6 +14,7 @@
from textual.screen import Screen
from textual.widget import Widget
from textual.widgets import Footer, Header
from textual_universal_directorytree import UPath

from browsr.base import TextualAppContext
from browsr.utils import get_file_info
Expand Down Expand Up @@ -157,9 +157,7 @@ def action_reload(self) -> None:
f"[italic]{self.config_object.path.name}[/italic]"
)
if reload_file:
selected_file_path = cast(
pathlib.Path, self.code_browser.selected_file_path
)
selected_file_path = cast(UPath, self.code_browser.selected_file_path)
file_name = selected_file_path.name
self.code_browser.window_switcher.render_file(
file_path=selected_file_path,
Expand Down
11 changes: 5 additions & 6 deletions browsr/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import datetime
import os
import pathlib
from dataclasses import dataclass
from typing import Any, BinaryIO, Dict, Optional, Union

Expand All @@ -13,7 +12,7 @@
from fitz import Pixmap
from PIL import Image
from rich_pixels import Pixels
from textual_universal_directorytree import is_remote_path
from textual_universal_directorytree import UPath, is_remote_path


def _open_pdf_as_image(buf: BinaryIO) -> Image.Image:
Expand All @@ -33,7 +32,7 @@ def _open_pdf_as_image(buf: BinaryIO) -> Image.Image:
return Image.frombytes(size=(pix.width, pix.height), data=pix.samples, mode=mode)


def open_image(document: pathlib.Path, screen_width: float) -> Pixels:
def open_image(document: UPath, screen_width: float) -> Pixels:
"""
Open an image file and return a rich_pixels.Pixels object
"""
Expand All @@ -57,7 +56,7 @@ class FileInfo:
File Information Object
"""

file: pathlib.Path
file: UPath
size: int
last_modified: Optional[datetime.datetime]
stat: Union[Dict[str, Any], os.stat_result]
Expand All @@ -68,7 +67,7 @@ class FileInfo:
is_cloudpath: bool


def get_file_info(file_path: pathlib.Path) -> FileInfo:
def get_file_info(file_path: UPath) -> FileInfo:
"""
Get File Information, Regardless of the FileSystem
"""
Expand Down Expand Up @@ -127,7 +126,7 @@ def get_file_info(file_path: pathlib.Path) -> FileInfo:
)


def handle_duplicate_filenames(file_path: pathlib.Path) -> pathlib.Path:
def handle_duplicate_filenames(file_path: UPath) -> UPath:
"""
Handle Duplicate Filenames
Expand Down
11 changes: 7 additions & 4 deletions browsr/widgets/code_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@
from typing import Any

import pyperclip
import upath
from rich.markdown import Markdown
from textual import on, work
from textual.app import ComposeResult
from textual.containers import Container
from textual.events import Mount
from textual.reactive import var
from textual.widgets import DirectoryTree
from textual_universal_directorytree import UniversalDirectoryTree, is_remote_path
from textual_universal_directorytree import (
UniversalDirectoryTree,
UPath,
is_remote_path,
)

from browsr.base import (
TextualAppContext,
Expand Down Expand Up @@ -55,7 +58,7 @@ class CodeBrowser(Container):
rich_themes = favorite_themes
show_tree = var(True)
force_show_tree = var(False)
selected_file_path: upath.UPath | pathlib.Path | None | var[None] = var(None)
selected_file_path: UPath | None | var[None] = var(None)

hidden_table_view = var(False)
table_view_status = var(False)
Expand Down Expand Up @@ -272,7 +275,7 @@ def download_selected_file(self) -> None:
timeout=2,
)

def _get_download_file_name(self) -> pathlib.Path:
def _get_download_file_name(self) -> UPath:
"""
Get the download file name.
"""
Expand Down
11 changes: 6 additions & 5 deletions browsr/widgets/universal_directory_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
from textual.binding import BindingType
from textual.widgets._directory_tree import DirEntry
from textual.widgets._tree import TreeNode
from textual_universal_directorytree import UniversalDirectoryTree
from upath import UPath as Path
from textual_universal_directorytree import UniversalDirectoryTree, UPath

from browsr.widgets.double_click_directory_tree import DoubleClickDirectoryTree
from browsr.widgets.vim import vim_cursor_bindings
Expand All @@ -27,7 +26,7 @@ class BrowsrDirectoryTree(UniversalDirectoryTree, DoubleClickDirectoryTree):
]

@classmethod
def _handle_top_level_bucket(cls, dir_path: Path) -> Iterable[Path] | None:
def _handle_top_level_bucket(cls, dir_path: UPath) -> Iterable[UPath] | None:
"""
Handle scenarios when someone wants to browse all of s3
Expand All @@ -36,12 +35,14 @@ def _handle_top_level_bucket(cls, dir_path: Path) -> Iterable[Path] | None:
"""
if str(dir_path) == "s3:/":
sub_buckets = sorted(
Path(f"s3:https://{bucket.name}") for bucket in dir_path.iterdir()
UPath(f"s3:https://{bucket.name}") for bucket in dir_path.iterdir()
)
return sub_buckets
return None

def _populate_node(self, node: TreeNode[DirEntry], content: Iterable[Path]) -> None:
def _populate_node(
self, node: TreeNode[DirEntry], content: Iterable[UPath]
) -> None:
"""
Populate the given tree node with the given directory content.
Expand Down
24 changes: 9 additions & 15 deletions browsr/widgets/windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from __future__ import annotations

import json
import pathlib
from json import JSONDecodeError
from typing import Any, ClassVar

Expand All @@ -21,6 +20,7 @@
from textual.reactive import Reactive, reactive
from textual.widget import Widget
from textual.widgets import Static
from textual_universal_directorytree import UPath

from browsr.base import TextualAppContext
from browsr.config import favorite_themes, image_file_extensions
Expand Down Expand Up @@ -50,9 +50,7 @@ def __init__(self, window: type[BaseCodeWindow], scroll_home: bool = False):
self.scroll_home: bool = scroll_home
super().__init__()

def file_to_string(
self, file_path: pathlib.Path, max_lines: int | None = None
) -> str:
def file_to_string(self, file_path: UPath, max_lines: int | None = None) -> str:
"""
Load a file into a string
"""
Expand All @@ -67,17 +65,15 @@ def file_to_string(
text = "\n".join(text.split("\n")[:max_lines])
return text

def file_to_image(self, file_path: pathlib.Path) -> Pixels:
def file_to_image(self, file_path: UPath) -> Pixels:
"""
Load a file into an image
"""
screen_width = self.app.size.width / 4
content = open_image(document=file_path, screen_width=screen_width)
return content

def file_to_json(
self, file_path: pathlib.Path, max_lines: int | None = None
) -> str:
def file_to_json(self, file_path: UPath, max_lines: int | None = None) -> str:
"""
Load a file into a JSON object
"""
Expand Down Expand Up @@ -169,7 +165,7 @@ def __init__(
self.config_object = config_object

def file_to_markdown(
self, file_path: pathlib.Path, max_lines: int | None = None
self, file_path: UPath, max_lines: int | None = None
) -> Markdown:
"""
Load a file into a Markdown
Expand All @@ -180,7 +176,7 @@ def file_to_markdown(
hyperlinks=True,
)

def text_to_syntax(self, text: str, file_path: str | pathlib.Path) -> Syntax:
def text_to_syntax(self, text: str, file_path: str | UPath) -> Syntax:
"""
Convert text to syntax
"""
Expand Down Expand Up @@ -235,9 +231,7 @@ class DataTableWindow(VimDataTable, BaseCodeWindow):
A DataTable widget for displaying code.
"""

def refresh_from_file(
self, file_path: pathlib.Path, max_lines: int | None = None
) -> None:
def refresh_from_file(self, file_path: UPath, max_lines: int | None = None) -> None:
"""
Load a file into a DataTable
"""
Expand Down Expand Up @@ -312,7 +306,7 @@ def __init__(
)
self.datatable_window.display = False
self.vim_scroll = VimScroll(self.static_window)
self.rendered_file: pathlib.Path | None = None
self.rendered_file: UPath | None = None

def compose(self) -> ComposeResult:
"""
Expand Down Expand Up @@ -344,7 +338,7 @@ def switch_window(self, window: BaseCodeWindow) -> None:
else:
screens[window_screen].display = False

def render_file(self, file_path: pathlib.Path, scroll_home: bool = True) -> None:
def render_file(self, file_path: UPath, scroll_home: bool = True) -> None:
"""
Render a file
"""
Expand Down
9 changes: 4 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
Pytest Fixtures Shared Across all Unit Tests
"""

import pathlib
from typing import Any, Dict, List

import pyperclip
import pytest
from click.testing import CliRunner
from textual_universal_directorytree import GitHubTextualPath
from textual_universal_directorytree import GitHubTextualPath, UPath


@pytest.fixture
Expand All @@ -20,15 +19,15 @@ def runner() -> CliRunner:


@pytest.fixture
def repo_dir() -> pathlib.Path:
def repo_dir() -> UPath:
"""
Return the path to the repository root
"""
return pathlib.Path(__file__).parent.parent.resolve()
return UPath(__file__).parent.parent.resolve()


@pytest.fixture
def screenshot_dir(repo_dir: pathlib.Path) -> pathlib.Path:
def screenshot_dir(repo_dir: UPath) -> UPath:
"""
Return the path to the screenshot directory
"""
Expand Down
4 changes: 2 additions & 2 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pathlib
from dataclasses import is_dataclass

from textual_universal_directorytree import GitHubTextualPath
from textual_universal_directorytree import GitHubTextualPath, UPath

from browsr.base import TextualAppContext
from browsr.config import favorite_themes
Expand Down Expand Up @@ -32,7 +32,7 @@ def test_textual_app_context_path() -> None:
Test that default TextualAppContext.path is CWD
"""
context = TextualAppContext()
assert isinstance(context.path, pathlib.Path)
assert isinstance(context.path, UPath)
assert context.path == pathlib.Path.cwd().resolve()


Expand Down
9 changes: 4 additions & 5 deletions tests/test_screenshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
Screenshot Testing Using Cassettes!
"""

import pathlib
from textwrap import dedent
from typing import Callable, Tuple

import pytest
from textual_universal_directorytree import GitHubTextualPath
from textual_universal_directorytree import GitHubTextualPath, UPath

from tests.conftest import cassette

Expand All @@ -33,7 +32,7 @@ def terminal_size() -> Tuple[int, int]:
@cassette
def test_github_screenshot(
snap_compare: Callable[..., bool],
tmp_path: pathlib.Path,
tmp_path: UPath,
app_file: str,
github_release_path: GitHubTextualPath,
terminal_size: Tuple[int, int],
Expand All @@ -49,7 +48,7 @@ def test_github_screenshot(
@cassette
def test_github_screenshot_license(
snap_compare: Callable[..., bool],
tmp_path: pathlib.Path,
tmp_path: UPath,
app_file: str,
github_release_path: GitHubTextualPath,
terminal_size: Tuple[int, int],
Expand All @@ -66,7 +65,7 @@ def test_github_screenshot_license(
@cassette
def test_mkdocs_screenshot(
snap_compare: Callable[..., bool],
tmp_path: pathlib.Path,
tmp_path: UPath,
app_file: str,
terminal_size: Tuple[int, int],
github_release_path: GitHubTextualPath,
Expand Down

0 comments on commit 8d1cd95

Please sign in to comment.