Skip to content

Commit

Permalink
Get rid of filedialog_kwargs
Browse files Browse the repository at this point in the history
  • Loading branch information
Akuli committed Jul 8, 2023
1 parent b0dacde commit b6850ba
Show file tree
Hide file tree
Showing 7 changed files with 4 additions and 68 deletions.
14 changes: 0 additions & 14 deletions docs/porcupine.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,6 @@ Version Information
# display an error message or do things without the new feature


File Dialogs
------------

.. data:: filedialog_kwargs
:type: dict[str, Any]

Porcupine uses :mod:`tkinter.filedialog` functions similarly to this::

path = filedialog.asksaveasfilename(**porcupine.filedialog_kwargs)

The :source:`filetypes plugin <porcupine/plugins/filetypes.py>` uses this
for displaying known filetypes in the dialogs.


Directories
-----------

Expand Down
1 change: 0 additions & 1 deletion porcupine/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,5 @@ def user_log_dir(self) -> str:
get_horizontal_panedwindow = _state.get_horizontal_panedwindow # TODO: document this
get_vertical_panedwindow = _state.get_vertical_panedwindow # TODO: document this
get_tab_manager = _state.get_tab_manager
filedialog_kwargs = _state.filedialog_kwargs
add_quit_callback = _state.add_quit_callback
quit = _state.quit
1 change: 0 additions & 1 deletion porcupine/_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ class _State:

# global state makes some things a lot easier (I'm sorry)
_global_state: _State | None = None
filedialog_kwargs: dict[str, Any] = {}


def _log_tkinter_error(
Expand Down
4 changes: 2 additions & 2 deletions porcupine/menubar.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from typing_extensions import Literal

from porcupine import pluginmanager, settings, tabs, utils
from porcupine._state import filedialog_kwargs, get_main_window, get_tab_manager, quit
from porcupine._state import get_main_window, get_tab_manager, quit
from porcupine.settings import global_settings

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -341,7 +341,7 @@ def new_file() -> None:

def open_files() -> None:
# paths is "" or tuple
paths = filedialog.askopenfilenames(**filedialog_kwargs)
paths = filedialog.askopenfilenames()
for path in map(Path, paths):
get_tab_manager().open_file(path)

Expand Down
23 changes: 0 additions & 23 deletions porcupine/plugins/filetypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

from porcupine import (
dirs,
filedialog_kwargs,
get_parsed_args,
get_tab_manager,
menubar,
Expand Down Expand Up @@ -101,27 +100,6 @@ def load_filetypes() -> None:
filetype["filetype_name"] = name


def set_filedialog_kwargs() -> None:
filedialog_kwargs["filetypes"] = []

for name, filetype in filetypes.items():
# "*.py" doesn't work on windows, but ".py" works and does the same thing
# See "SPECIFYING EXTENSIONS" in tk_getOpenFile manual page
file_patterns = [
pattern.split("/")[-1].lstrip("*") for pattern in filetype["filename_patterns"]
]

filedialog_kwargs["filetypes"].append((name, file_patterns))

# File types without an extension seem to cause crashes on Mac in certain cases (See issue #1092).
if sys.platform != "darwin":
filedialog_kwargs["filetypes"].insert(0, ("All Files", ["*"]))
else:
filedialog_kwargs["filetypes"].remove(
("Makefile", ["Makefile", "makefile", "Makefile.*", "makefile.*"])
)


def get_filetype_from_matches(
matches: dict[str, FileType], they_match_what: str
) -> FileType | None:
Expand Down Expand Up @@ -290,7 +268,6 @@ def setup() -> None:
"Default filetype for new files:",
values=sorted(filetypes.keys(), key=str.casefold),
)
set_filedialog_kwargs()
global filetypes_var
filetypes_var = tkinter.StringVar()
for name in sorted(filetypes.keys(), key=str.casefold):
Expand Down
2 changes: 1 addition & 1 deletion porcupine/tabs.py
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ def save_as(self, path: Path | None = None) -> bool:
asking the user.
"""
if path is None:
path_string = filedialog.asksaveasfilename(**_state.filedialog_kwargs)
path_string = filedialog.asksaveasfilename()
if not path_string: # it may be '' because tkinter
return False
path = Path(path_string)
Expand Down
27 changes: 1 addition & 26 deletions tests/test_filetypes_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
import shutil
import sys
from pathlib import Path
from tkinter import filedialog

import pytest

from porcupine import dirs, filedialog_kwargs, get_main_window
from porcupine import dirs
from porcupine.plugins import filetypes


Expand All @@ -33,30 +32,11 @@ def custom_filetypes():
)
filetypes.filetypes.clear()
filetypes.load_filetypes()
filetypes.set_filedialog_kwargs()

yield
(dirs.user_config_path / "filetypes.toml").unlink()
filetypes.filetypes.clear()
filetypes.load_filetypes()
filetypes.set_filedialog_kwargs()


def test_filedialog_patterns_got_stripped():
python_patterns = dict(filedialog_kwargs["filetypes"])["Python"]
assert "*.py" not in python_patterns
assert ".py" in python_patterns


@pytest.mark.skipif(sys.platform != "linux", reason="don't know how filedialog works on non-Linux")
def test_actually_running_filedialog(custom_filetypes):
# Wait and then press Esc. That's done as Tcl code because the Tk widget
# representing the dialog can't be used with tkinter.
root = get_main_window().nametowidget(".")
root.after(1000, root.eval, "event generate [focus] <Escape>")

# If filedialog_kwargs are wrong, then this errors.
filedialog.askopenfilename(**filedialog_kwargs)


def test_bad_filetype_on_command_line(run_porcupine):
Expand Down Expand Up @@ -98,11 +78,6 @@ def lexer_name(path):
assert str(tmp_path) in caplog.records[0].message
assert "HTML, Mako template" in caplog.records[0].message

# filedialog doesn't support slashes in patterns
for filetype_name, patterns in filedialog_kwargs["filetypes"]:
for pattern in patterns:
assert "/" not in pattern


@pytest.mark.skipif(shutil.which("clangd") is None, reason="example config uses clangd")
def test_cplusplus_toml_bug(tmp_path, tabmanager, custom_filetypes):
Expand Down

0 comments on commit b6850ba

Please sign in to comment.