Skip to content

Commit 8d1cd95

Browse files
committed
✨ Python 3.12
1 parent 412ac18 commit 8d1cd95

File tree

9 files changed

+41
-48
lines changed

9 files changed

+41
-48
lines changed

browsr/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ class TextualAppContext:
3131
kwargs: dict[str, Any] | None = None
3232

3333
@property
34-
def path(self) -> pathlib.Path:
34+
def path(self) -> UPath:
3535
"""
36-
Resolve `file_path` to a upath.UPath object
36+
Resolve `file_path` to a UPath object
3737
"""
3838
if "github" in str(self.file_path).lower():
3939
file_path = str(self.file_path)

browsr/screens/code_browser.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
from __future__ import annotations
66

7-
import pathlib
87
from typing import ClassVar, Iterable, cast
98

109
from rich import traceback
@@ -15,6 +14,7 @@
1514
from textual.screen import Screen
1615
from textual.widget import Widget
1716
from textual.widgets import Footer, Header
17+
from textual_universal_directorytree import UPath
1818

1919
from browsr.base import TextualAppContext
2020
from browsr.utils import get_file_info
@@ -157,9 +157,7 @@ def action_reload(self) -> None:
157157
f"[italic]{self.config_object.path.name}[/italic]"
158158
)
159159
if reload_file:
160-
selected_file_path = cast(
161-
pathlib.Path, self.code_browser.selected_file_path
162-
)
160+
selected_file_path = cast(UPath, self.code_browser.selected_file_path)
163161
file_name = selected_file_path.name
164162
self.code_browser.window_switcher.render_file(
165163
file_path=selected_file_path,

browsr/utils.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import datetime
66
import os
7-
import pathlib
87
from dataclasses import dataclass
98
from typing import Any, BinaryIO, Dict, Optional, Union
109

@@ -13,7 +12,7 @@
1312
from fitz import Pixmap
1413
from PIL import Image
1514
from rich_pixels import Pixels
16-
from textual_universal_directorytree import is_remote_path
15+
from textual_universal_directorytree import UPath, is_remote_path
1716

1817

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

3534

36-
def open_image(document: pathlib.Path, screen_width: float) -> Pixels:
35+
def open_image(document: UPath, screen_width: float) -> Pixels:
3736
"""
3837
Open an image file and return a rich_pixels.Pixels object
3938
"""
@@ -57,7 +56,7 @@ class FileInfo:
5756
File Information Object
5857
"""
5958

60-
file: pathlib.Path
59+
file: UPath
6160
size: int
6261
last_modified: Optional[datetime.datetime]
6362
stat: Union[Dict[str, Any], os.stat_result]
@@ -68,7 +67,7 @@ class FileInfo:
6867
is_cloudpath: bool
6968

7069

71-
def get_file_info(file_path: pathlib.Path) -> FileInfo:
70+
def get_file_info(file_path: UPath) -> FileInfo:
7271
"""
7372
Get File Information, Regardless of the FileSystem
7473
"""
@@ -127,7 +126,7 @@ def get_file_info(file_path: pathlib.Path) -> FileInfo:
127126
)
128127

129128

130-
def handle_duplicate_filenames(file_path: pathlib.Path) -> pathlib.Path:
129+
def handle_duplicate_filenames(file_path: UPath) -> UPath:
131130
"""
132131
Handle Duplicate Filenames
133132

browsr/widgets/code_browser.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,18 @@
1111
from typing import Any
1212

1313
import pyperclip
14-
import upath
1514
from rich.markdown import Markdown
1615
from textual import on, work
1716
from textual.app import ComposeResult
1817
from textual.containers import Container
1918
from textual.events import Mount
2019
from textual.reactive import var
2120
from textual.widgets import DirectoryTree
22-
from textual_universal_directorytree import UniversalDirectoryTree, is_remote_path
21+
from textual_universal_directorytree import (
22+
UniversalDirectoryTree,
23+
UPath,
24+
is_remote_path,
25+
)
2326

2427
from browsr.base import (
2528
TextualAppContext,
@@ -55,7 +58,7 @@ class CodeBrowser(Container):
5558
rich_themes = favorite_themes
5659
show_tree = var(True)
5760
force_show_tree = var(False)
58-
selected_file_path: upath.UPath | pathlib.Path | None | var[None] = var(None)
61+
selected_file_path: UPath | None | var[None] = var(None)
5962

6063
hidden_table_view = var(False)
6164
table_view_status = var(False)
@@ -272,7 +275,7 @@ def download_selected_file(self) -> None:
272275
timeout=2,
273276
)
274277

275-
def _get_download_file_name(self) -> pathlib.Path:
278+
def _get_download_file_name(self) -> UPath:
276279
"""
277280
Get the download file name.
278281
"""

browsr/widgets/universal_directory_tree.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
from textual.binding import BindingType
1010
from textual.widgets._directory_tree import DirEntry
1111
from textual.widgets._tree import TreeNode
12-
from textual_universal_directorytree import UniversalDirectoryTree
13-
from upath import UPath as Path
12+
from textual_universal_directorytree import UniversalDirectoryTree, UPath
1413

1514
from browsr.widgets.double_click_directory_tree import DoubleClickDirectoryTree
1615
from browsr.widgets.vim import vim_cursor_bindings
@@ -27,7 +26,7 @@ class BrowsrDirectoryTree(UniversalDirectoryTree, DoubleClickDirectoryTree):
2726
]
2827

2928
@classmethod
30-
def _handle_top_level_bucket(cls, dir_path: Path) -> Iterable[Path] | None:
29+
def _handle_top_level_bucket(cls, dir_path: UPath) -> Iterable[UPath] | None:
3130
"""
3231
Handle scenarios when someone wants to browse all of s3
3332
@@ -36,12 +35,14 @@ def _handle_top_level_bucket(cls, dir_path: Path) -> Iterable[Path] | None:
3635
"""
3736
if str(dir_path) == "s3:/":
3837
sub_buckets = sorted(
39-
Path(f"s3://{bucket.name}") for bucket in dir_path.iterdir()
38+
UPath(f"s3://{bucket.name}") for bucket in dir_path.iterdir()
4039
)
4140
return sub_buckets
4241
return None
4342

44-
def _populate_node(self, node: TreeNode[DirEntry], content: Iterable[Path]) -> None:
43+
def _populate_node(
44+
self, node: TreeNode[DirEntry], content: Iterable[UPath]
45+
) -> None:
4546
"""
4647
Populate the given tree node with the given directory content.
4748

browsr/widgets/windows.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from __future__ import annotations
66

77
import json
8-
import pathlib
98
from json import JSONDecodeError
109
from typing import Any, ClassVar
1110

@@ -21,6 +20,7 @@
2120
from textual.reactive import Reactive, reactive
2221
from textual.widget import Widget
2322
from textual.widgets import Static
23+
from textual_universal_directorytree import UPath
2424

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

53-
def file_to_string(
54-
self, file_path: pathlib.Path, max_lines: int | None = None
55-
) -> str:
53+
def file_to_string(self, file_path: UPath, max_lines: int | None = None) -> str:
5654
"""
5755
Load a file into a string
5856
"""
@@ -67,17 +65,15 @@ def file_to_string(
6765
text = "\n".join(text.split("\n")[:max_lines])
6866
return text
6967

70-
def file_to_image(self, file_path: pathlib.Path) -> Pixels:
68+
def file_to_image(self, file_path: UPath) -> Pixels:
7169
"""
7270
Load a file into an image
7371
"""
7472
screen_width = self.app.size.width / 4
7573
content = open_image(document=file_path, screen_width=screen_width)
7674
return content
7775

78-
def file_to_json(
79-
self, file_path: pathlib.Path, max_lines: int | None = None
80-
) -> str:
76+
def file_to_json(self, file_path: UPath, max_lines: int | None = None) -> str:
8177
"""
8278
Load a file into a JSON object
8379
"""
@@ -169,7 +165,7 @@ def __init__(
169165
self.config_object = config_object
170166

171167
def file_to_markdown(
172-
self, file_path: pathlib.Path, max_lines: int | None = None
168+
self, file_path: UPath, max_lines: int | None = None
173169
) -> Markdown:
174170
"""
175171
Load a file into a Markdown
@@ -180,7 +176,7 @@ def file_to_markdown(
180176
hyperlinks=True,
181177
)
182178

183-
def text_to_syntax(self, text: str, file_path: str | pathlib.Path) -> Syntax:
179+
def text_to_syntax(self, text: str, file_path: str | UPath) -> Syntax:
184180
"""
185181
Convert text to syntax
186182
"""
@@ -235,9 +231,7 @@ class DataTableWindow(VimDataTable, BaseCodeWindow):
235231
A DataTable widget for displaying code.
236232
"""
237233

238-
def refresh_from_file(
239-
self, file_path: pathlib.Path, max_lines: int | None = None
240-
) -> None:
234+
def refresh_from_file(self, file_path: UPath, max_lines: int | None = None) -> None:
241235
"""
242236
Load a file into a DataTable
243237
"""
@@ -312,7 +306,7 @@ def __init__(
312306
)
313307
self.datatable_window.display = False
314308
self.vim_scroll = VimScroll(self.static_window)
315-
self.rendered_file: pathlib.Path | None = None
309+
self.rendered_file: UPath | None = None
316310

317311
def compose(self) -> ComposeResult:
318312
"""
@@ -344,7 +338,7 @@ def switch_window(self, window: BaseCodeWindow) -> None:
344338
else:
345339
screens[window_screen].display = False
346340

347-
def render_file(self, file_path: pathlib.Path, scroll_home: bool = True) -> None:
341+
def render_file(self, file_path: UPath, scroll_home: bool = True) -> None:
348342
"""
349343
Render a file
350344
"""

tests/conftest.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22
Pytest Fixtures Shared Across all Unit Tests
33
"""
44

5-
import pathlib
65
from typing import Any, Dict, List
76

87
import pyperclip
98
import pytest
109
from click.testing import CliRunner
11-
from textual_universal_directorytree import GitHubTextualPath
10+
from textual_universal_directorytree import GitHubTextualPath, UPath
1211

1312

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

2120

2221
@pytest.fixture
23-
def repo_dir() -> pathlib.Path:
22+
def repo_dir() -> UPath:
2423
"""
2524
Return the path to the repository root
2625
"""
27-
return pathlib.Path(__file__).parent.parent.resolve()
26+
return UPath(__file__).parent.parent.resolve()
2827

2928

3029
@pytest.fixture
31-
def screenshot_dir(repo_dir: pathlib.Path) -> pathlib.Path:
30+
def screenshot_dir(repo_dir: UPath) -> UPath:
3231
"""
3332
Return the path to the screenshot directory
3433
"""

tests/test_config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import pathlib
55
from dataclasses import is_dataclass
66

7-
from textual_universal_directorytree import GitHubTextualPath
7+
from textual_universal_directorytree import GitHubTextualPath, UPath
88

99
from browsr.base import TextualAppContext
1010
from browsr.config import favorite_themes
@@ -32,7 +32,7 @@ def test_textual_app_context_path() -> None:
3232
Test that default TextualAppContext.path is CWD
3333
"""
3434
context = TextualAppContext()
35-
assert isinstance(context.path, pathlib.Path)
35+
assert isinstance(context.path, UPath)
3636
assert context.path == pathlib.Path.cwd().resolve()
3737

3838

tests/test_screenshots.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22
Screenshot Testing Using Cassettes!
33
"""
44

5-
import pathlib
65
from textwrap import dedent
76
from typing import Callable, Tuple
87

98
import pytest
10-
from textual_universal_directorytree import GitHubTextualPath
9+
from textual_universal_directorytree import GitHubTextualPath, UPath
1110

1211
from tests.conftest import cassette
1312

@@ -33,7 +32,7 @@ def terminal_size() -> Tuple[int, int]:
3332
@cassette
3433
def test_github_screenshot(
3534
snap_compare: Callable[..., bool],
36-
tmp_path: pathlib.Path,
35+
tmp_path: UPath,
3736
app_file: str,
3837
github_release_path: GitHubTextualPath,
3938
terminal_size: Tuple[int, int],
@@ -49,7 +48,7 @@ def test_github_screenshot(
4948
@cassette
5049
def test_github_screenshot_license(
5150
snap_compare: Callable[..., bool],
52-
tmp_path: pathlib.Path,
51+
tmp_path: UPath,
5352
app_file: str,
5453
github_release_path: GitHubTextualPath,
5554
terminal_size: Tuple[int, int],
@@ -66,7 +65,7 @@ def test_github_screenshot_license(
6665
@cassette
6766
def test_mkdocs_screenshot(
6867
snap_compare: Callable[..., bool],
69-
tmp_path: pathlib.Path,
68+
tmp_path: UPath,
7069
app_file: str,
7170
terminal_size: Tuple[int, int],
7271
github_release_path: GitHubTextualPath,

0 commit comments

Comments
 (0)