textual-fspicker 0.0.11__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- textual_fspicker-0.0.11/PKG-INFO +82 -0
- textual_fspicker-0.0.11/README.md +50 -0
- textual_fspicker-0.0.11/pyproject.toml +5 -0
- textual_fspicker-0.0.11/setup.cfg +48 -0
- textual_fspicker-0.0.11/textual_fspicker/__init__.py +25 -0
- textual_fspicker-0.0.11/textual_fspicker/__main__.py +96 -0
- textual_fspicker-0.0.11/textual_fspicker/base_dialog.py +142 -0
- textual_fspicker-0.0.11/textual_fspicker/file_dialog.py +177 -0
- textual_fspicker-0.0.11/textual_fspicker/file_open.py +50 -0
- textual_fspicker-0.0.11/textual_fspicker/file_save.py +50 -0
- textual_fspicker-0.0.11/textual_fspicker/parts/__init__.py +11 -0
- textual_fspicker-0.0.11/textual_fspicker/parts/directory_navigation.py +461 -0
- textual_fspicker-0.0.11/textual_fspicker/path_filters.py +59 -0
- textual_fspicker-0.0.11/textual_fspicker/path_maker.py +45 -0
- textual_fspicker-0.0.11/textual_fspicker/py.typed +0 -0
- textual_fspicker-0.0.11/textual_fspicker/safe_tests.py +77 -0
- textual_fspicker-0.0.11/textual_fspicker/select_directory.py +85 -0
- textual_fspicker-0.0.11/textual_fspicker.egg-info/PKG-INFO +82 -0
- textual_fspicker-0.0.11/textual_fspicker.egg-info/SOURCES.txt +21 -0
- textual_fspicker-0.0.11/textual_fspicker.egg-info/dependency_links.txt +1 -0
- textual_fspicker-0.0.11/textual_fspicker.egg-info/requires.txt +1 -0
- textual_fspicker-0.0.11/textual_fspicker.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: textual-fspicker
|
|
3
|
+
Version: 0.0.11
|
|
4
|
+
Summary: A simple Textual filesystem picker dialog library.
|
|
5
|
+
Home-page: https://github.com/davep/textual-fspicker
|
|
6
|
+
Author: Dave Pearson
|
|
7
|
+
Author-email: davep@davep.org
|
|
8
|
+
Maintainer: Dave Pearson
|
|
9
|
+
Maintainer-email: davep@davep.org
|
|
10
|
+
License: License :: OSI Approved :: MIT License
|
|
11
|
+
Project-URL: Documentation, https://github.com/davep/textual-fspicker/blob/main/README.md
|
|
12
|
+
Project-URL: Source, https://github.com/davep/textual-fspicker
|
|
13
|
+
Project-URL: Issues, https://github.com/davep/textual-fspicker/issues
|
|
14
|
+
Project-URL: Discussions, https://github.com/davep/textual-fspicker/discussions
|
|
15
|
+
Keywords: terminal,library,file system,filesystem,dialog
|
|
16
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
17
|
+
Classifier: Environment :: Console
|
|
18
|
+
Classifier: Development Status :: 3 - Alpha
|
|
19
|
+
Classifier: Intended Audience :: Developers
|
|
20
|
+
Classifier: Operating System :: OS Independent
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
24
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
25
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
26
|
+
Classifier: Topic :: Terminals
|
|
27
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
28
|
+
Classifier: Typing :: Typed
|
|
29
|
+
Requires-Python: >=3.8
|
|
30
|
+
Description-Content-Type: text/markdown
|
|
31
|
+
Requires-Dist: textual>=0.31.0
|
|
32
|
+
|
|
33
|
+
# textual-fspicker
|
|
34
|
+
|
|
35
|
+

|
|
36
|
+
*An example of `textual-fspicker` being used in a Textual application*
|
|
37
|
+
|
|
38
|
+
## Introduction
|
|
39
|
+
|
|
40
|
+
This library provides a simple set of filesystem navigation and picking
|
|
41
|
+
dialogs (actually, as of the time of writing, it only provides two, this is
|
|
42
|
+
a very early WiP you're seeing). The aim is to provide "ready to go" dialogs
|
|
43
|
+
that should also be fairly easy to tailor to your own applications.
|
|
44
|
+
|
|
45
|
+
## Installing
|
|
46
|
+
|
|
47
|
+
The package can be installed with `pip` or related tools, for example:
|
|
48
|
+
|
|
49
|
+
```sh
|
|
50
|
+
$ pip install textual-fspicker
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## The library
|
|
54
|
+
|
|
55
|
+
Right at the moment there's just two dialogs available:
|
|
56
|
+
|
|
57
|
+
- `FileOpen` -- For selecting a file from the filesystem.
|
|
58
|
+
- `FileSave` -- For selecting a file for saving in the filesystem.
|
|
59
|
+
- `SelectDirectory` -- For selecting a directory from the filesysrem.
|
|
60
|
+
|
|
61
|
+
You can see them in action in the [demo/test
|
|
62
|
+
code](https://github.com/davep/textual-fspicker/blob/main/textual_fspicker/__main__.py).
|
|
63
|
+
|
|
64
|
+
Yes, I know that documentation is lacking right now -- I'm still fleshing
|
|
65
|
+
out how this will work and what it will provide -- so the best place to see
|
|
66
|
+
how the code can be used is in that demo/test code.
|
|
67
|
+
|
|
68
|
+
## TODO
|
|
69
|
+
|
|
70
|
+
- [ ] Flesh out what's displayed for directory entries
|
|
71
|
+
- [ ] Add lots of styling options for directory entries
|
|
72
|
+
- [ ] Settle on a final set of default styles for the dialogs
|
|
73
|
+
- [X] Add a more file-save-oriented dialog
|
|
74
|
+
- [X] Add a directory picking dialog
|
|
75
|
+
- [X] Add file filtering (extensions using `Select`)
|
|
76
|
+
- [X] Expose the hidden show/hide facility of the navigator in the dialogs
|
|
77
|
+
- [ ] Better documentation
|
|
78
|
+
- [ ] Test on Windows
|
|
79
|
+
- [ ] Add custom mtime formatting
|
|
80
|
+
- [ ] Add support for showing different times
|
|
81
|
+
|
|
82
|
+
[//]: # (README.md ends here)
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# textual-fspicker
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
*An example of `textual-fspicker` being used in a Textual application*
|
|
5
|
+
|
|
6
|
+
## Introduction
|
|
7
|
+
|
|
8
|
+
This library provides a simple set of filesystem navigation and picking
|
|
9
|
+
dialogs (actually, as of the time of writing, it only provides two, this is
|
|
10
|
+
a very early WiP you're seeing). The aim is to provide "ready to go" dialogs
|
|
11
|
+
that should also be fairly easy to tailor to your own applications.
|
|
12
|
+
|
|
13
|
+
## Installing
|
|
14
|
+
|
|
15
|
+
The package can be installed with `pip` or related tools, for example:
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
$ pip install textual-fspicker
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## The library
|
|
22
|
+
|
|
23
|
+
Right at the moment there's just two dialogs available:
|
|
24
|
+
|
|
25
|
+
- `FileOpen` -- For selecting a file from the filesystem.
|
|
26
|
+
- `FileSave` -- For selecting a file for saving in the filesystem.
|
|
27
|
+
- `SelectDirectory` -- For selecting a directory from the filesysrem.
|
|
28
|
+
|
|
29
|
+
You can see them in action in the [demo/test
|
|
30
|
+
code](https://github.com/davep/textual-fspicker/blob/main/textual_fspicker/__main__.py).
|
|
31
|
+
|
|
32
|
+
Yes, I know that documentation is lacking right now -- I'm still fleshing
|
|
33
|
+
out how this will work and what it will provide -- so the best place to see
|
|
34
|
+
how the code can be used is in that demo/test code.
|
|
35
|
+
|
|
36
|
+
## TODO
|
|
37
|
+
|
|
38
|
+
- [ ] Flesh out what's displayed for directory entries
|
|
39
|
+
- [ ] Add lots of styling options for directory entries
|
|
40
|
+
- [ ] Settle on a final set of default styles for the dialogs
|
|
41
|
+
- [X] Add a more file-save-oriented dialog
|
|
42
|
+
- [X] Add a directory picking dialog
|
|
43
|
+
- [X] Add file filtering (extensions using `Select`)
|
|
44
|
+
- [X] Expose the hidden show/hide facility of the navigator in the dialogs
|
|
45
|
+
- [ ] Better documentation
|
|
46
|
+
- [ ] Test on Windows
|
|
47
|
+
- [ ] Add custom mtime formatting
|
|
48
|
+
- [ ] Add support for showing different times
|
|
49
|
+
|
|
50
|
+
[//]: # (README.md ends here)
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
[metadata]
|
|
2
|
+
name = textual-fspicker
|
|
3
|
+
description = A simple Textual filesystem picker dialog library.
|
|
4
|
+
version = attr: textual_fspicker.__version__
|
|
5
|
+
long_description = file: README.md
|
|
6
|
+
long_description_content_type = text/markdown
|
|
7
|
+
url = https://github.com/davep/textual-fspicker
|
|
8
|
+
author = Dave Pearson
|
|
9
|
+
author_email = davep@davep.org
|
|
10
|
+
maintainer = Dave Pearson
|
|
11
|
+
maintainer_email = davep@davep.org
|
|
12
|
+
license = License :: OSI Approved :: MIT License
|
|
13
|
+
license_files = LICENCE
|
|
14
|
+
keywords = terminal, library, file system, filesystem, dialog
|
|
15
|
+
classifiers =
|
|
16
|
+
License :: OSI Approved :: MIT License
|
|
17
|
+
Environment :: Console
|
|
18
|
+
Development Status :: 3 - Alpha
|
|
19
|
+
Intended Audience :: Developers
|
|
20
|
+
Operating System :: OS Independent
|
|
21
|
+
Programming Language :: Python :: 3.8
|
|
22
|
+
Programming Language :: Python :: 3.9
|
|
23
|
+
Programming Language :: Python :: 3.10
|
|
24
|
+
Programming Language :: Python :: 3.11
|
|
25
|
+
Programming Language :: Python :: 3.12
|
|
26
|
+
Topic :: Terminals
|
|
27
|
+
Topic :: Software Development :: Libraries
|
|
28
|
+
Typing :: Typed
|
|
29
|
+
project_urls =
|
|
30
|
+
Documentation = https://github.com/davep/textual-fspicker/blob/main/README.md
|
|
31
|
+
Source = https://github.com/davep/textual-fspicker
|
|
32
|
+
Issues = https://github.com/davep/textual-fspicker/issues
|
|
33
|
+
Discussions = https://github.com/davep/textual-fspicker/discussions
|
|
34
|
+
|
|
35
|
+
[options]
|
|
36
|
+
packages = find:
|
|
37
|
+
platforms = any
|
|
38
|
+
include_package_data = True
|
|
39
|
+
install_requires = textual>=0.31.0
|
|
40
|
+
python_requires = >=3.8
|
|
41
|
+
|
|
42
|
+
[options.package_data]
|
|
43
|
+
textual_fspicker = py.typed
|
|
44
|
+
|
|
45
|
+
[egg_info]
|
|
46
|
+
tag_build =
|
|
47
|
+
tag_date = 0
|
|
48
|
+
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"""A library that provides a widgets for selecting things from the filesystem."""
|
|
2
|
+
|
|
3
|
+
######################################################################
|
|
4
|
+
# Main app information.
|
|
5
|
+
__author__ = "Dave Pearson"
|
|
6
|
+
__copyright__ = "Copyright 2023, Dave Pearson"
|
|
7
|
+
__credits__ = ["Dave Pearson"]
|
|
8
|
+
__maintainer__ = "Dave Pearson"
|
|
9
|
+
__email__ = "davep@davep.org"
|
|
10
|
+
__version__ = "0.0.11"
|
|
11
|
+
__licence__ = "MIT"
|
|
12
|
+
|
|
13
|
+
##############################################################################
|
|
14
|
+
# Local imports.
|
|
15
|
+
from .file_open import FileOpen
|
|
16
|
+
from .file_save import FileSave
|
|
17
|
+
from .path_filters import Filters
|
|
18
|
+
from .path_maker import MakePath
|
|
19
|
+
from .select_directory import SelectDirectory
|
|
20
|
+
|
|
21
|
+
##############################################################################
|
|
22
|
+
# Export the imports.
|
|
23
|
+
__all__ = ["FileOpen", "FileSave", "SelectDirectory", "Filters", "MakePath"]
|
|
24
|
+
|
|
25
|
+
### __init__.py ends here
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"""Main entry point for testing the library."""
|
|
2
|
+
|
|
3
|
+
##############################################################################
|
|
4
|
+
# Python imports.
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
##############################################################################
|
|
9
|
+
# Textual imports.
|
|
10
|
+
from textual import on
|
|
11
|
+
from textual.app import App, ComposeResult
|
|
12
|
+
from textual.containers import Center, Horizontal
|
|
13
|
+
from textual.widgets import Label, Button, Footer
|
|
14
|
+
|
|
15
|
+
##############################################################################
|
|
16
|
+
# Local imports.
|
|
17
|
+
from textual_fspicker import FileOpen, FileSave, Filters, SelectDirectory
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
##############################################################################
|
|
21
|
+
class TestApp(App[None]):
|
|
22
|
+
"""A simple test application."""
|
|
23
|
+
|
|
24
|
+
CSS = """
|
|
25
|
+
Screen#_default {
|
|
26
|
+
align: center middle;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
Screen#_default Horizontal {
|
|
30
|
+
align: center middle;
|
|
31
|
+
height: auto;
|
|
32
|
+
margin-bottom: 1;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
Screen#_default Horizontal Button {
|
|
36
|
+
margin-left: 1;
|
|
37
|
+
margin-right: 1;
|
|
38
|
+
}
|
|
39
|
+
"""
|
|
40
|
+
|
|
41
|
+
BINDINGS = [("d", "toggle_dark", "Light/Dark")]
|
|
42
|
+
|
|
43
|
+
def compose(self) -> ComposeResult:
|
|
44
|
+
"""Compose the layout of the test application."""
|
|
45
|
+
with Horizontal():
|
|
46
|
+
yield Button("Open a file", id="open")
|
|
47
|
+
yield Button("Save a file", id="save")
|
|
48
|
+
yield Button("Select a directory", id="directory")
|
|
49
|
+
with Center():
|
|
50
|
+
yield Label("Press the button to pick something")
|
|
51
|
+
yield Footer()
|
|
52
|
+
|
|
53
|
+
def show_selected(self, to_show: Path | None) -> None:
|
|
54
|
+
"""Show the file that was selected by the user.
|
|
55
|
+
|
|
56
|
+
Args:
|
|
57
|
+
to_show: The file to show.
|
|
58
|
+
"""
|
|
59
|
+
self.query_one(Label).update("Cancelled" if to_show is None else str(to_show))
|
|
60
|
+
|
|
61
|
+
@on(Button.Pressed, "#open")
|
|
62
|
+
def open_file(self) -> None:
|
|
63
|
+
"""Show the `FileOpen` dialog when the button is pushed."""
|
|
64
|
+
self.push_screen(
|
|
65
|
+
FileOpen(
|
|
66
|
+
".",
|
|
67
|
+
filters=Filters(
|
|
68
|
+
("Python", lambda p: p.suffix.lower() == ".py"),
|
|
69
|
+
("Any", lambda _: True),
|
|
70
|
+
("Emacs", lambda p: p.suffix.lower() == ".el"),
|
|
71
|
+
("Lisp", lambda p: p.suffix.lower() in (".lisp", ".lsp", ".cl")),
|
|
72
|
+
("Pascal", lambda p: p.suffix.lower() == ".pas"),
|
|
73
|
+
("Clipper", lambda p: p.suffix.lower() in (".prg", ".ch")),
|
|
74
|
+
("C", lambda p: p.suffix.lower() in (".c", ".h")),
|
|
75
|
+
("C++", lambda p: p.suffix.lower() in (".cpp", ".cc", ".h")),
|
|
76
|
+
),
|
|
77
|
+
),
|
|
78
|
+
callback=self.show_selected,
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
@on(Button.Pressed, "#save")
|
|
82
|
+
def save_file(self) -> None:
|
|
83
|
+
"""Show the `FileSave` dialog when the button is pushed."""
|
|
84
|
+
self.push_screen(FileSave(can_overwrite=False), callback=self.show_selected)
|
|
85
|
+
|
|
86
|
+
@on(Button.Pressed, "#directory")
|
|
87
|
+
def select_directory(self) -> None:
|
|
88
|
+
"""show the `SelectDirectory` dialog when the button is pushed."""
|
|
89
|
+
self.push_screen(SelectDirectory(), callback=self.show_selected)
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
##############################################################################
|
|
93
|
+
if __name__ == "__main__":
|
|
94
|
+
TestApp().run()
|
|
95
|
+
|
|
96
|
+
### __main__.py ends here
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
"""The base dialog code for the other dialogs in the library."""
|
|
2
|
+
|
|
3
|
+
##############################################################################
|
|
4
|
+
# Python imports.
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from typing import Optional
|
|
8
|
+
|
|
9
|
+
##############################################################################
|
|
10
|
+
# Textual imports.
|
|
11
|
+
from textual import on
|
|
12
|
+
from textual.app import ComposeResult
|
|
13
|
+
from textual.binding import Binding
|
|
14
|
+
from textual.containers import Horizontal, Vertical
|
|
15
|
+
from textual.screen import ModalScreen
|
|
16
|
+
from textual.widgets import Button
|
|
17
|
+
|
|
18
|
+
##############################################################################
|
|
19
|
+
# Local imports.
|
|
20
|
+
from .parts import DirectoryNavigation
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
##############################################################################
|
|
24
|
+
class Dialog(Vertical):
|
|
25
|
+
"""Layout class for the main dialog area."""
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
##############################################################################
|
|
29
|
+
class InputBar(Horizontal):
|
|
30
|
+
"""The input bar area of the dialog."""
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
##############################################################################
|
|
34
|
+
class FileSystemPickerScreen(ModalScreen[Optional[Path]]):
|
|
35
|
+
"""Base screen for the dialogs in this library."""
|
|
36
|
+
|
|
37
|
+
DEFAULT_CSS = """
|
|
38
|
+
FileSystemPickerScreen {
|
|
39
|
+
align: center middle;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
FileSystemPickerScreen Dialog {
|
|
43
|
+
width: 80%;
|
|
44
|
+
height: 80%;
|
|
45
|
+
border: panel $panel-lighten-2;
|
|
46
|
+
background: $panel-lighten-1;
|
|
47
|
+
border-title-color: $text;
|
|
48
|
+
border-title-background: $panel-lighten-2;
|
|
49
|
+
border-subtitle-color: $text;
|
|
50
|
+
border-subtitle-background: $error;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
FileSystemPickerScreen DirectoryNavigation {
|
|
54
|
+
height: 1fr;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
FileSystemPickerScreen InputBar {
|
|
58
|
+
height: auto;
|
|
59
|
+
align: right middle;
|
|
60
|
+
padding-top: 1;
|
|
61
|
+
padding-right: 1;
|
|
62
|
+
padding-bottom: 1;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
FileSystemPickerScreen InputBar Button {
|
|
66
|
+
margin-left: 1;
|
|
67
|
+
}
|
|
68
|
+
"""
|
|
69
|
+
|
|
70
|
+
BINDINGS = [Binding("full_stop", "hidden"), Binding("escape", "dismiss(None)")]
|
|
71
|
+
"""The bindings for the dialog."""
|
|
72
|
+
|
|
73
|
+
def __init__(
|
|
74
|
+
self, location: str | Path = ".", title: str = "", select_button: str = ""
|
|
75
|
+
) -> None:
|
|
76
|
+
"""Initialise the dialog.
|
|
77
|
+
|
|
78
|
+
Args:
|
|
79
|
+
location: Optional starting location.
|
|
80
|
+
title: Optional title.
|
|
81
|
+
select_button: Label for the select button.
|
|
82
|
+
"""
|
|
83
|
+
super().__init__()
|
|
84
|
+
self._location = location
|
|
85
|
+
"""The starting location."""
|
|
86
|
+
self._title = title
|
|
87
|
+
"""The title for the dialog."""
|
|
88
|
+
self._select_button = select_button or "Select"
|
|
89
|
+
"""The text prompt for the select button."""
|
|
90
|
+
|
|
91
|
+
def _input_bar(self) -> ComposeResult:
|
|
92
|
+
"""Provide any widgets for the input bar, before the buttons."""
|
|
93
|
+
yield from ()
|
|
94
|
+
|
|
95
|
+
def compose(self) -> ComposeResult:
|
|
96
|
+
"""Compose the child widgets.
|
|
97
|
+
|
|
98
|
+
Returns:
|
|
99
|
+
The widgets to compose.
|
|
100
|
+
"""
|
|
101
|
+
with Dialog() as dialog:
|
|
102
|
+
dialog.border_title = self._title
|
|
103
|
+
yield DirectoryNavigation(self._location)
|
|
104
|
+
with InputBar():
|
|
105
|
+
yield from self._input_bar()
|
|
106
|
+
yield Button(self._select_button, id="select")
|
|
107
|
+
yield Button("Cancel", id="cancel")
|
|
108
|
+
|
|
109
|
+
def _set_error(self, message: str = "") -> None:
|
|
110
|
+
"""Set or clear the error message.
|
|
111
|
+
|
|
112
|
+
Args:
|
|
113
|
+
message: Optional message to show as an error.
|
|
114
|
+
"""
|
|
115
|
+
self.query_one(Dialog).border_subtitle = message
|
|
116
|
+
|
|
117
|
+
@on(DirectoryNavigation.Changed)
|
|
118
|
+
def _clear_error(self) -> None:
|
|
119
|
+
"""Clear any error that might be showing."""
|
|
120
|
+
self._set_error()
|
|
121
|
+
|
|
122
|
+
@on(DirectoryNavigation.PermissionError)
|
|
123
|
+
def _show_permission_error(self) -> None:
|
|
124
|
+
"""Show any permission error bubbled up from the directory navigator."""
|
|
125
|
+
self._set_error("Permission error")
|
|
126
|
+
|
|
127
|
+
@on(Button.Pressed, "#cancel")
|
|
128
|
+
def _cancel(self, event: Button.Pressed) -> None:
|
|
129
|
+
"""Cancel the dialog.
|
|
130
|
+
|
|
131
|
+
Args:
|
|
132
|
+
event: The even to handle.
|
|
133
|
+
"""
|
|
134
|
+
event.stop()
|
|
135
|
+
self.dismiss(None)
|
|
136
|
+
|
|
137
|
+
def action_hidden(self) -> None:
|
|
138
|
+
"""Action for toggling the display of hidden entries."""
|
|
139
|
+
self.query_one(DirectoryNavigation).toggle_hidden()
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
### base_dialog.py ends here
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
"""Base file-oriented dialog."""
|
|
2
|
+
|
|
3
|
+
##############################################################################
|
|
4
|
+
# Python imports.
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
##############################################################################
|
|
9
|
+
# Textual imports.
|
|
10
|
+
from textual import on
|
|
11
|
+
from textual.app import ComposeResult
|
|
12
|
+
from textual.events import Mount
|
|
13
|
+
from textual.widgets import Button, Input, Select
|
|
14
|
+
|
|
15
|
+
##############################################################################
|
|
16
|
+
# Local imports.
|
|
17
|
+
from .base_dialog import FileSystemPickerScreen
|
|
18
|
+
from .parts import DirectoryNavigation
|
|
19
|
+
from .path_filters import Filters
|
|
20
|
+
from .path_maker import MakePath
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
##############################################################################
|
|
24
|
+
class FileFilter(Select[int]):
|
|
25
|
+
"""The file filter."""
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
##############################################################################
|
|
29
|
+
class BaseFileDialog(FileSystemPickerScreen):
|
|
30
|
+
"""The base dialog for file-oriented picking dialogs."""
|
|
31
|
+
|
|
32
|
+
DEFAULT_CSS = """
|
|
33
|
+
BaseFileDialog InputBar Input {
|
|
34
|
+
width: 2fr;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
BaseFileDialog InputBar Select {
|
|
38
|
+
width: 1fr;
|
|
39
|
+
}
|
|
40
|
+
"""
|
|
41
|
+
|
|
42
|
+
def __init__(
|
|
43
|
+
self,
|
|
44
|
+
location: str | Path = ".",
|
|
45
|
+
title: str = "Open",
|
|
46
|
+
select_button: str = "",
|
|
47
|
+
*,
|
|
48
|
+
filters: Filters | None = None,
|
|
49
|
+
) -> None:
|
|
50
|
+
"""Initialise the base dialog dialog.
|
|
51
|
+
|
|
52
|
+
Args:
|
|
53
|
+
location: Optional starting location.
|
|
54
|
+
title: Optional title.
|
|
55
|
+
select_button: The label for the selection button.
|
|
56
|
+
filters: Optional filters to show in the dialog.
|
|
57
|
+
"""
|
|
58
|
+
super().__init__(location, title, select_button=select_button)
|
|
59
|
+
self._filters = filters
|
|
60
|
+
"""The filters for the dialog."""
|
|
61
|
+
|
|
62
|
+
def _input_bar(self) -> ComposeResult:
|
|
63
|
+
"""Provide any widgets for the input before, before the buttons."""
|
|
64
|
+
yield Input()
|
|
65
|
+
if self._filters:
|
|
66
|
+
yield FileFilter(
|
|
67
|
+
self._filters.selections,
|
|
68
|
+
prompt="File filter",
|
|
69
|
+
value=0,
|
|
70
|
+
allow_blank=False,
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
@on(Mount)
|
|
74
|
+
def _initial_filter(self) -> None:
|
|
75
|
+
"""Set the initial filter once the DOM is ready."""
|
|
76
|
+
if self._filters:
|
|
77
|
+
self.query_one(DirectoryNavigation).file_filter = self._filters[0]
|
|
78
|
+
|
|
79
|
+
@on(DirectoryNavigation.Selected)
|
|
80
|
+
def _select_file(self, event: DirectoryNavigation.Selected) -> None:
|
|
81
|
+
"""Handle a file being selected in the picker.
|
|
82
|
+
|
|
83
|
+
Args:
|
|
84
|
+
event: The event to handle.
|
|
85
|
+
"""
|
|
86
|
+
file_name = self.query_one(Input)
|
|
87
|
+
file_name.value = str(event.path.name)
|
|
88
|
+
file_name.focus()
|
|
89
|
+
|
|
90
|
+
@on(Input.Changed)
|
|
91
|
+
def _clear_error(self) -> None:
|
|
92
|
+
"""Clear any error that might be showing."""
|
|
93
|
+
super()._clear_error()
|
|
94
|
+
|
|
95
|
+
@on(Select.Changed)
|
|
96
|
+
def _change_filter(self, event: Select.Changed) -> None:
|
|
97
|
+
"""Handle a change in the filter.
|
|
98
|
+
|
|
99
|
+
Args:
|
|
100
|
+
event: The event to handle.
|
|
101
|
+
"""
|
|
102
|
+
if self._filters is not None and isinstance(event.value, int):
|
|
103
|
+
self.query_one(DirectoryNavigation).file_filter = self._filters[event.value]
|
|
104
|
+
else:
|
|
105
|
+
self.query_one(DirectoryNavigation).file_filter = None
|
|
106
|
+
self.query_one(DirectoryNavigation).focus()
|
|
107
|
+
|
|
108
|
+
def _should_return(self, candidate: Path) -> bool:
|
|
109
|
+
"""Final check on a picked file before returning it to the caller.
|
|
110
|
+
|
|
111
|
+
Args:
|
|
112
|
+
candidate: The file to check.
|
|
113
|
+
|
|
114
|
+
Returns:
|
|
115
|
+
`True` if the file should be returned, `False` if not.
|
|
116
|
+
|
|
117
|
+
Note:
|
|
118
|
+
This method is designed to be called as a final check; this is a
|
|
119
|
+
good place to set up the display of an error before returning
|
|
120
|
+
`False`, for example.
|
|
121
|
+
"""
|
|
122
|
+
del candidate
|
|
123
|
+
return True
|
|
124
|
+
|
|
125
|
+
@on(Input.Submitted)
|
|
126
|
+
@on(Button.Pressed, "#select")
|
|
127
|
+
def _confirm_file(self, event: Input.Submitted | Button.Pressed) -> None:
|
|
128
|
+
"""Confirm the selection of the file in the input box.
|
|
129
|
+
|
|
130
|
+
Args:
|
|
131
|
+
event: The event to handle.
|
|
132
|
+
"""
|
|
133
|
+
event.stop()
|
|
134
|
+
file_name = self.query_one(Input)
|
|
135
|
+
|
|
136
|
+
# Only even try and process this if there's some input.
|
|
137
|
+
if not file_name.value:
|
|
138
|
+
self._set_error("A file must be chosen")
|
|
139
|
+
return
|
|
140
|
+
|
|
141
|
+
# If it looks like the user is typing in some sort of home
|
|
142
|
+
# directory path... (does pathlib let me test for this, or at
|
|
143
|
+
# least ask what the home character is? Docs don't mention this;
|
|
144
|
+
# so for now I'm going to hard-code this).
|
|
145
|
+
if file_name.value.startswith("~"):
|
|
146
|
+
# ...let's simply expand and go with that.
|
|
147
|
+
try:
|
|
148
|
+
chosen = MakePath.of(file_name.value).expanduser()
|
|
149
|
+
except RuntimeError as error:
|
|
150
|
+
self._set_error(str(error))
|
|
151
|
+
return
|
|
152
|
+
else:
|
|
153
|
+
# It's not a home directory path, so let's combine with the
|
|
154
|
+
# location of the directory navigator widget.
|
|
155
|
+
chosen = (
|
|
156
|
+
self.query_one(DirectoryNavigation).location / file_name.value
|
|
157
|
+
).resolve()
|
|
158
|
+
|
|
159
|
+
# If it's a directory, approach it like it's the user simply
|
|
160
|
+
# doing a "cd".
|
|
161
|
+
try:
|
|
162
|
+
if chosen.is_dir():
|
|
163
|
+
self.query_one(Input).value = ""
|
|
164
|
+
self.query_one(DirectoryNavigation).location = chosen
|
|
165
|
+
self.query_one(DirectoryNavigation).focus()
|
|
166
|
+
return
|
|
167
|
+
except PermissionError:
|
|
168
|
+
self._set_error("Permission error")
|
|
169
|
+
return
|
|
170
|
+
|
|
171
|
+
# If the chosen file passes the final tests...
|
|
172
|
+
if self._should_return(chosen):
|
|
173
|
+
# ...return it.
|
|
174
|
+
self.dismiss(result=chosen)
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
### file_dialog.py ends here
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"""Provides a file opening dialog."""
|
|
2
|
+
|
|
3
|
+
##############################################################################
|
|
4
|
+
# Python imports.
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
##############################################################################
|
|
9
|
+
# Local imports.
|
|
10
|
+
from .file_dialog import BaseFileDialog
|
|
11
|
+
from .path_filters import Filters
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
##############################################################################
|
|
15
|
+
class FileOpen(BaseFileDialog):
|
|
16
|
+
"""A file opening dialog."""
|
|
17
|
+
|
|
18
|
+
def __init__(
|
|
19
|
+
self,
|
|
20
|
+
location: str | Path = ".",
|
|
21
|
+
title: str = "Open",
|
|
22
|
+
*,
|
|
23
|
+
filters: Filters | None = None,
|
|
24
|
+
must_exist: bool = True,
|
|
25
|
+
) -> None:
|
|
26
|
+
"""Initialise the `FileOpen` dialog.
|
|
27
|
+
|
|
28
|
+
Args:
|
|
29
|
+
location: Optional starting location.
|
|
30
|
+
title: Optional title.
|
|
31
|
+
filters: Optional filters to show in the dialog.
|
|
32
|
+
must_exist: Flag to say if the file must exist.
|
|
33
|
+
"""
|
|
34
|
+
super().__init__(location, title, select_button="Open", filters=filters)
|
|
35
|
+
self._must_exist = must_exist
|
|
36
|
+
"""Must the file exist?"""
|
|
37
|
+
|
|
38
|
+
def _should_return(self, candidate: Path) -> bool:
|
|
39
|
+
"""Perform the final checks on the chosen file.
|
|
40
|
+
|
|
41
|
+
Args:
|
|
42
|
+
candidate: The file to check.
|
|
43
|
+
"""
|
|
44
|
+
if self._must_exist and not candidate.exists():
|
|
45
|
+
self._set_error("The file must exist")
|
|
46
|
+
return False
|
|
47
|
+
return True
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
### file_open.py ends here
|