notesmgr 0.1.0__py3-none-any.whl
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.
- notesmgr/__init__.py +5 -0
- notesmgr/__main__.py +10 -0
- notesmgr/actions.py +86 -0
- notesmgr/application.py +93 -0
- notesmgr/button_row.py +149 -0
- notesmgr/clipboard_linux.py +58 -0
- notesmgr/clipboard_macos.py +78 -0
- notesmgr/clipboard_tool.py +84 -0
- notesmgr/clipboard_windows.py +198 -0
- notesmgr/cmd_line.py +63 -0
- notesmgr/commands.py +307 -0
- notesmgr/config.py +152 -0
- notesmgr/config_defaults.py +41 -0
- notesmgr/config_editor.py +62 -0
- notesmgr/config_files.py +147 -0
- notesmgr/console.py +62 -0
- notesmgr/descriptions.py +27 -0
- notesmgr/dialogs.py +389 -0
- notesmgr/editor_command.py +138 -0
- notesmgr/errors.py +14 -0
- notesmgr/explorer_drag.py +222 -0
- notesmgr/explorer_drop.py +195 -0
- notesmgr/explorer_font.py +81 -0
- notesmgr/explorer_tree.py +148 -0
- notesmgr/failure_report.py +92 -0
- notesmgr/file_watch.py +115 -0
- notesmgr/folder_ops.py +173 -0
- notesmgr/main_window.py +474 -0
- notesmgr/markdown_render.py +78 -0
- notesmgr/menu_bar.py +114 -0
- notesmgr/note_blocks.py +511 -0
- notesmgr/note_file.py +203 -0
- notesmgr/note_fonts.py +189 -0
- notesmgr/note_image.py +75 -0
- notesmgr/note_ops.py +316 -0
- notesmgr/note_panel.py +227 -0
- notesmgr/note_table.py +145 -0
- notesmgr/note_tags.py +241 -0
- notesmgr/note_text.py +62 -0
- notesmgr/note_view.py +273 -0
- notesmgr/order_file.py +150 -0
- notesmgr/project.py +168 -0
- notesmgr/project_ops.py +302 -0
- notesmgr/py.typed +0 -0
- notesmgr/rich_clipboard.py +107 -0
- notesmgr/session.py +71 -0
- notesmgr/shortcuts.py +131 -0
- notesmgr/trash.py +31 -0
- notesmgr/version_info.py +78 -0
- notesmgr-0.1.0.dist-info/METADATA +299 -0
- notesmgr-0.1.0.dist-info/RECORD +55 -0
- notesmgr-0.1.0.dist-info/WHEEL +5 -0
- notesmgr-0.1.0.dist-info/entry_points.txt +2 -0
- notesmgr-0.1.0.dist-info/licenses/LICENSE.txt +22 -0
- notesmgr-0.1.0.dist-info/top_level.txt +1 -0
notesmgr/main_window.py
ADDED
|
@@ -0,0 +1,474 @@
|
|
|
1
|
+
#! /usr/local/bin/python3
|
|
2
|
+
"""The main window of the notesmgr application."""
|
|
3
|
+
|
|
4
|
+
# Copyright (c) 2026 Tom Björkholm
|
|
5
|
+
# MIT License
|
|
6
|
+
|
|
7
|
+
import io
|
|
8
|
+
import tkinter
|
|
9
|
+
from functools import partial
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
from tkinter import ttk
|
|
12
|
+
from typing import AbstractSet, Callable, Optional, Sequence, Union
|
|
13
|
+
from edit_cfg_json import ConfigLoadError
|
|
14
|
+
from edit_cfg_json_tk import TkEditorPanel
|
|
15
|
+
from notesmgr.actions import DELETE_FOLDER, NEW_FOLDER, RENAME_FOLDER
|
|
16
|
+
from notesmgr.commands import Commands, WindowHooks
|
|
17
|
+
from notesmgr.config_editor import open_config_editor
|
|
18
|
+
from notesmgr.config_files import copy_to_user_wide
|
|
19
|
+
from notesmgr.dialogs import ask_choice, ask_folder, ask_yes_no, \
|
|
20
|
+
busy_cursor, show_error, show_info, show_text
|
|
21
|
+
from notesmgr.errors import NotesmgrError
|
|
22
|
+
from notesmgr.explorer_drop import Drop
|
|
23
|
+
from notesmgr.explorer_tree import ExplorerTree
|
|
24
|
+
from notesmgr.menu_bar import MenuEntry, MenuSpec, build_menu_bar, \
|
|
25
|
+
entry_labels, set_enabled
|
|
26
|
+
from notesmgr.note_panel import NotePanel
|
|
27
|
+
from notesmgr.project import is_project
|
|
28
|
+
from notesmgr.project_ops import OpenReport, changed_message, \
|
|
29
|
+
create_project, open_project
|
|
30
|
+
from notesmgr.session import Session
|
|
31
|
+
from notesmgr.shortcuts import TREE_EDIT_KEYS, Shortcut, Shortcuts, \
|
|
32
|
+
tk_window_system, window_shortcuts
|
|
33
|
+
from notesmgr.version_info import version_report
|
|
34
|
+
|
|
35
|
+
APPLICATION_NAME = 'notesmgr'
|
|
36
|
+
INITIAL_WIDTH = 1000
|
|
37
|
+
INITIAL_HEIGHT = 650
|
|
38
|
+
SCREEN_SHARE = 90
|
|
39
|
+
MINIMUM_WIDTH = 640
|
|
40
|
+
MINIMUM_HEIGHT = 400
|
|
41
|
+
FILE_MENU = 'File'
|
|
42
|
+
NOTE_MENU = 'Note'
|
|
43
|
+
FOLDER_MENU = 'Folder'
|
|
44
|
+
VIEW_MENU = 'View'
|
|
45
|
+
CONFIG_MENU = 'Configuration'
|
|
46
|
+
HELP_MENU = 'Help'
|
|
47
|
+
NEW_PROJECT_ENTRY = 'New project…'
|
|
48
|
+
OPEN_PROJECT_ENTRY = 'Open project…'
|
|
49
|
+
QUIT_ENTRY = 'Quit'
|
|
50
|
+
ZOOM_IN_ENTRY = 'Larger text'
|
|
51
|
+
ZOOM_OUT_ENTRY = 'Smaller text'
|
|
52
|
+
NORMAL_SIZE_ENTRY = 'Normal text size'
|
|
53
|
+
ZOOM_STEP = 1
|
|
54
|
+
EDIT_CONFIG_ENTRY = 'Edit configuration…'
|
|
55
|
+
USER_WIDE_ENTRY = 'Save configuration as user wide…'
|
|
56
|
+
USER_WIDE_SAVED = 'The configuration of the project is now the user wide\n' \
|
|
57
|
+
'configuration, in the file {path}.'
|
|
58
|
+
VERSION_ENTRY = 'Version information…'
|
|
59
|
+
VERSION_TITLE = 'notesmgr versions'
|
|
60
|
+
CONFIG_TITLE = 'Configuration'
|
|
61
|
+
PROJECT_TITLE = 'Project'
|
|
62
|
+
TEMPLATE_TITLE = 'Templates'
|
|
63
|
+
NEW_PROJECT_TITLE = 'Folder to make into a notesmgr project'
|
|
64
|
+
OPEN_PROJECT_TITLE = 'notesmgr project folder to open'
|
|
65
|
+
OFFER_OPEN = 'The folder {folder} is a notesmgr project already.\n' \
|
|
66
|
+
'Open that project?'
|
|
67
|
+
TEMPLATE_QUESTION = 'The folder {folder} holds more than one template.\n' \
|
|
68
|
+
'Which one is to be kept? The others are moved to the trash, and\n' \
|
|
69
|
+
'the one that is kept is given the file extension of the project.'
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def fitted(wanted: int, screen: int) -> int:
|
|
73
|
+
"""Return a length of the window that the screen has room for.
|
|
74
|
+
|
|
75
|
+
A window larger than its screen has its edges and its title bar
|
|
76
|
+
out of reach, so a small screen gives nine tenths of itself
|
|
77
|
+
rather than the length that was wanted.
|
|
78
|
+
|
|
79
|
+
Args:
|
|
80
|
+
wanted: Pixels the window would like to have.
|
|
81
|
+
screen: Pixels the screen has in the same direction.
|
|
82
|
+
|
|
83
|
+
Returns:
|
|
84
|
+
The pixels that the window is given.
|
|
85
|
+
"""
|
|
86
|
+
return min(wanted, screen * SCREEN_SHARE // 100)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def accelerator(keys: Shortcuts, action: str) -> Optional[str]:
|
|
90
|
+
"""Return what a menu entry shows of its keys, None for no keys."""
|
|
91
|
+
shortcut = keys.actions.get(action)
|
|
92
|
+
return None if shortcut is None else shortcut.label
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def handled(command: Callable[[], None],
|
|
96
|
+
_event: 'tkinter.Event[tkinter.Misc]') -> str:
|
|
97
|
+
"""Run the command of a key, and end the handling of that key."""
|
|
98
|
+
command()
|
|
99
|
+
return 'break'
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
class MainWindow:
|
|
103
|
+
"""The notesmgr main window with its explorer and its note panel.
|
|
104
|
+
|
|
105
|
+
The window is given to the constructor instead of created by it, so
|
|
106
|
+
that the application can use the Tk root window while tests can use
|
|
107
|
+
a hidden toplevel window under one shared Tk root.
|
|
108
|
+
"""
|
|
109
|
+
|
|
110
|
+
def __init__(self, window: Union[tkinter.Tk, tkinter.Toplevel]) -> None:
|
|
111
|
+
"""Fill the given toplevel window with the notesmgr main window."""
|
|
112
|
+
self.window = window
|
|
113
|
+
self.session = Session()
|
|
114
|
+
self.panes = ttk.PanedWindow(window, orient=tkinter.HORIZONTAL)
|
|
115
|
+
self.explorer = ExplorerTree(self.panes, self.show_selected,
|
|
116
|
+
self.dropped)
|
|
117
|
+
hooks = WindowHooks(self.reopen, self.offer_actions)
|
|
118
|
+
commands = Commands(window, self.session, hooks)
|
|
119
|
+
self.note_panel = NotePanel(self.panes, self.session, commands)
|
|
120
|
+
self.config_panel: Optional[TkEditorPanel] = None
|
|
121
|
+
keys = window_shortcuts(tk_window_system(window))
|
|
122
|
+
self.menu_bar = build_menu_bar(window, self._menu_specs(keys))
|
|
123
|
+
self._shape_window()
|
|
124
|
+
self._bind_shortcuts(keys)
|
|
125
|
+
self.show_project(None)
|
|
126
|
+
self.explorer.tree.focus_set()
|
|
127
|
+
|
|
128
|
+
def _menu_specs(self, keys: Shortcuts) -> list[MenuSpec]:
|
|
129
|
+
"""Return the menus of the main window and what they hold.
|
|
130
|
+
|
|
131
|
+
The entries that act on a note need a note to act on, the
|
|
132
|
+
entries that act on a folder need a folder, and copying the
|
|
133
|
+
configuration to the user wide location asks for a project
|
|
134
|
+
configuration file to copy, so all of them are greyed out
|
|
135
|
+
until what they need is selected.
|
|
136
|
+
"""
|
|
137
|
+
new_entry = MenuEntry(NEW_PROJECT_ENTRY, self.new_project_dialog)
|
|
138
|
+
open_entry = MenuEntry(OPEN_PROJECT_ENTRY, self.open_project_dialog)
|
|
139
|
+
quit_entry = MenuEntry(QUIT_ENTRY, self.quit, keys.quit.label)
|
|
140
|
+
edit_entry = MenuEntry(EDIT_CONFIG_ENTRY, self.edit_configuration)
|
|
141
|
+
user_wide = MenuEntry(USER_WIDE_ENTRY, self.save_user_wide,
|
|
142
|
+
enabled=False)
|
|
143
|
+
versions = MenuEntry(VERSION_ENTRY, self.show_version)
|
|
144
|
+
return [MenuSpec(FILE_MENU, [new_entry, open_entry, quit_entry]),
|
|
145
|
+
MenuSpec(NOTE_MENU, self._note_entries(keys)),
|
|
146
|
+
MenuSpec(FOLDER_MENU, self._folder_entries(keys)),
|
|
147
|
+
MenuSpec(VIEW_MENU, self._view_entries(keys)),
|
|
148
|
+
MenuSpec(CONFIG_MENU, [edit_entry, user_wide]),
|
|
149
|
+
MenuSpec(HELP_MENU, [versions])]
|
|
150
|
+
|
|
151
|
+
def _note_entries(self, keys: Shortcuts) -> list[MenuEntry]:
|
|
152
|
+
"""Return one entry of the note menu for every working button.
|
|
153
|
+
|
|
154
|
+
The note menu and the button row do the same things, so they
|
|
155
|
+
are described in one place, which is the panel that holds the
|
|
156
|
+
buttons. A button that was described with no command has
|
|
157
|
+
nothing to do, and is left out of the menu until it has.
|
|
158
|
+
"""
|
|
159
|
+
return [MenuEntry(spec.label, spec.command,
|
|
160
|
+
accelerator(keys, spec.label), enabled=False)
|
|
161
|
+
for spec in self.note_panel.actions()
|
|
162
|
+
if spec.command is not None]
|
|
163
|
+
|
|
164
|
+
def _folder_entries(self, keys: Shortcuts) -> list[MenuEntry]:
|
|
165
|
+
"""Return the entries that act on a folder of the project."""
|
|
166
|
+
commands = self.note_panel.commands
|
|
167
|
+
return [MenuEntry(NEW_FOLDER, commands.new_folder,
|
|
168
|
+
accelerator(keys, NEW_FOLDER), enabled=False),
|
|
169
|
+
MenuEntry(RENAME_FOLDER, commands.rename_folder,
|
|
170
|
+
enabled=False),
|
|
171
|
+
MenuEntry(DELETE_FOLDER, commands.delete_folder,
|
|
172
|
+
enabled=False)]
|
|
173
|
+
|
|
174
|
+
def _view_entries(self, keys: Shortcuts) -> list[MenuEntry]:
|
|
175
|
+
"""Return the entries that say how large a note is drawn.
|
|
176
|
+
|
|
177
|
+
A note is read on whatever screen the user has, so how large
|
|
178
|
+
it is drawn is theirs to say, and it can always be said
|
|
179
|
+
however small or large the note itself is.
|
|
180
|
+
"""
|
|
181
|
+
return [MenuEntry(ZOOM_IN_ENTRY, self._zoom_command(ZOOM_STEP),
|
|
182
|
+
keys.larger.label),
|
|
183
|
+
MenuEntry(ZOOM_OUT_ENTRY, self._zoom_command(-ZOOM_STEP),
|
|
184
|
+
keys.smaller.label),
|
|
185
|
+
MenuEntry(NORMAL_SIZE_ENTRY, self._normal_command(),
|
|
186
|
+
keys.normal.label)]
|
|
187
|
+
|
|
188
|
+
def _zoom_command(self, step: int) -> Callable[[], None]:
|
|
189
|
+
"""Return what draws the window so many steps larger or smaller."""
|
|
190
|
+
return partial(self.zoom, step)
|
|
191
|
+
|
|
192
|
+
def _normal_command(self) -> Callable[[], None]:
|
|
193
|
+
"""Return what draws the window in the size it started out in."""
|
|
194
|
+
return self.zoom_normal
|
|
195
|
+
|
|
196
|
+
def zoom(self, step: int) -> None:
|
|
197
|
+
"""Draw the note and the tree so many steps larger or smaller.
|
|
198
|
+
|
|
199
|
+
The note and the names of the notes are read on one screen
|
|
200
|
+
and at one distance from it, so they are made larger and
|
|
201
|
+
smaller together rather than each for itself.
|
|
202
|
+
"""
|
|
203
|
+
self.note_panel.zoom(step)
|
|
204
|
+
self.explorer.zoom(step)
|
|
205
|
+
|
|
206
|
+
def zoom_normal(self) -> None:
|
|
207
|
+
"""Draw the note and the tree in the size they started out in."""
|
|
208
|
+
self.note_panel.zoom_normal()
|
|
209
|
+
self.explorer.zoom_normal()
|
|
210
|
+
|
|
211
|
+
def _bind_shortcuts(self, keys: Shortcuts) -> None:
|
|
212
|
+
"""Let the shortcuts shown on the menu entries be typed.
|
|
213
|
+
|
|
214
|
+
Tk installs no binding for a menu accelerator, so every key
|
|
215
|
+
sequence has to be bound as well. They are bound on this
|
|
216
|
+
window only, so that a dialog which happens to have the
|
|
217
|
+
keyboard focus cannot reach the main window with them. The
|
|
218
|
+
Return key of the tree edits the selected note, and still
|
|
219
|
+
opens and closes a selected folder as the tree itself does.
|
|
220
|
+
"""
|
|
221
|
+
self._bind(keys.quit, self.quit)
|
|
222
|
+
self._bind(keys.larger, self._zoom_command(ZOOM_STEP))
|
|
223
|
+
self._bind(keys.smaller, self._zoom_command(-ZOOM_STEP))
|
|
224
|
+
self._bind(keys.normal, self._normal_command())
|
|
225
|
+
for action, command in self.note_panel.key_commands().items():
|
|
226
|
+
self._bind(keys.actions[action], command)
|
|
227
|
+
for sequence in TREE_EDIT_KEYS:
|
|
228
|
+
self.explorer.tree.bind(sequence, self._edit_key)
|
|
229
|
+
|
|
230
|
+
def _bind(self, shortcut: Shortcut, command: Callable[[], None]) \
|
|
231
|
+
-> None:
|
|
232
|
+
"""Let every key sequence of one shortcut run a command.
|
|
233
|
+
|
|
234
|
+
The tree moves its selection on an arrow key whatever key is
|
|
235
|
+
held down with it, so the sequences are bound on the tree as
|
|
236
|
+
well. A binding of the tree itself comes before what the tree
|
|
237
|
+
does, and ends the handling of the key once it has run.
|
|
238
|
+
"""
|
|
239
|
+
for widget in (self.window, self.explorer.tree):
|
|
240
|
+
for sequence in shortcut.sequences:
|
|
241
|
+
widget.bind(sequence, partial(handled, command))
|
|
242
|
+
|
|
243
|
+
def _edit_key(self, _event: 'tkinter.Event[ttk.Treeview]') -> None:
|
|
244
|
+
"""Open the note that is shown in the editor of the project."""
|
|
245
|
+
self.note_panel.edit_note()
|
|
246
|
+
|
|
247
|
+
def _shape_window(self) -> None:
|
|
248
|
+
"""Lay out the panes and give the window its size.
|
|
249
|
+
|
|
250
|
+
The window starts out in its own size and is kept above its
|
|
251
|
+
smallest size, except that neither is larger than the screen.
|
|
252
|
+
"""
|
|
253
|
+
self.panes.add(self.explorer.frame, weight=0)
|
|
254
|
+
self.panes.add(self.note_panel.frame, weight=1)
|
|
255
|
+
self.panes.pack(fill=tkinter.BOTH, expand=True)
|
|
256
|
+
width = self.window.winfo_screenwidth()
|
|
257
|
+
height = self.window.winfo_screenheight()
|
|
258
|
+
self.window.geometry(f'{fitted(INITIAL_WIDTH, width)}x'
|
|
259
|
+
f'{fitted(INITIAL_HEIGHT, height)}')
|
|
260
|
+
self.window.minsize(fitted(MINIMUM_WIDTH, width),
|
|
261
|
+
fitted(MINIMUM_HEIGHT, height))
|
|
262
|
+
|
|
263
|
+
def show_project(self, project_name: Optional[str]) -> None:
|
|
264
|
+
"""Name the open project in the window title, None meaning none."""
|
|
265
|
+
if project_name is None:
|
|
266
|
+
self.window.title(APPLICATION_NAME)
|
|
267
|
+
else:
|
|
268
|
+
self.window.title(f'{APPLICATION_NAME} — {project_name}')
|
|
269
|
+
|
|
270
|
+
def show_selected(self, path: Optional[Path]) -> None:
|
|
271
|
+
"""Show what the explorer has selected in the note panel."""
|
|
272
|
+
self.note_panel.show_path(path)
|
|
273
|
+
|
|
274
|
+
def dropped(self, item: Path, drop: Drop) -> None:
|
|
275
|
+
"""Move what was dragged in the explorer to where it was dropped.
|
|
276
|
+
|
|
277
|
+
Args:
|
|
278
|
+
item: The note or the folder that was dragged.
|
|
279
|
+
drop: Where it was dropped, as the tree worked it out.
|
|
280
|
+
"""
|
|
281
|
+
self.note_panel.commands.drop(item, drop)
|
|
282
|
+
|
|
283
|
+
def offer_actions(self, labels: AbstractSet[str]) -> None:
|
|
284
|
+
"""Offer what can be done now, and grey out what cannot.
|
|
285
|
+
|
|
286
|
+
The buttons of the panel and the entries of the note and
|
|
287
|
+
folder menus do the same things, so they are offered and
|
|
288
|
+
taken back together, whenever another item is selected and
|
|
289
|
+
whenever the note that is shown is taken away.
|
|
290
|
+
|
|
291
|
+
Args:
|
|
292
|
+
labels: What the actions that can be done now are called.
|
|
293
|
+
"""
|
|
294
|
+
self.note_panel.offer(labels)
|
|
295
|
+
for title in (NOTE_MENU, FOLDER_MENU):
|
|
296
|
+
menu = self.menu_bar.menus[title]
|
|
297
|
+
for label in entry_labels(menu):
|
|
298
|
+
set_enabled(menu, label, label in labels)
|
|
299
|
+
|
|
300
|
+
def new_project_dialog(self) -> None:
|
|
301
|
+
"""Ask for a folder and make a notesmgr project of it.
|
|
302
|
+
|
|
303
|
+
A folder that is a project already is not made into one twice,
|
|
304
|
+
and opening it is what the user is offered instead.
|
|
305
|
+
"""
|
|
306
|
+
folder = ask_folder(self.window, NEW_PROJECT_TITLE,
|
|
307
|
+
self.session.chooser_folder())
|
|
308
|
+
if folder is None:
|
|
309
|
+
return
|
|
310
|
+
if not is_project(folder):
|
|
311
|
+
self.make_project(folder)
|
|
312
|
+
elif ask_yes_no(self.window, PROJECT_TITLE,
|
|
313
|
+
OFFER_OPEN.format(folder=folder)):
|
|
314
|
+
self.load_project(folder)
|
|
315
|
+
|
|
316
|
+
def open_project_dialog(self) -> None:
|
|
317
|
+
"""Ask for a project folder and open the project in it."""
|
|
318
|
+
folder = ask_folder(self.window, OPEN_PROJECT_TITLE,
|
|
319
|
+
self.session.chooser_folder())
|
|
320
|
+
if folder is not None:
|
|
321
|
+
self.load_project(folder)
|
|
322
|
+
|
|
323
|
+
def load_project(self, root: Path,
|
|
324
|
+
selected: Optional[Path] = None) -> None:
|
|
325
|
+
"""Open an existing project and show what it holds.
|
|
326
|
+
|
|
327
|
+
Args:
|
|
328
|
+
root: The root folder of the project to open.
|
|
329
|
+
selected: What to select in it, None for nothing at all.
|
|
330
|
+
"""
|
|
331
|
+
self.opened(partial(open_project, root, self.choose_template),
|
|
332
|
+
selected)
|
|
333
|
+
|
|
334
|
+
def make_project(self, root: Path) -> None:
|
|
335
|
+
"""Make a folder into a project, then open it and show it."""
|
|
336
|
+
self.opened(partial(create_project, root, self.choose_template))
|
|
337
|
+
|
|
338
|
+
def reopen(self, selected: Optional[Path]) -> None:
|
|
339
|
+
"""Show the open project again, selecting one item of it.
|
|
340
|
+
|
|
341
|
+
Every command changes the files of the project, so the whole
|
|
342
|
+
project is read again rather than the tree being mended item
|
|
343
|
+
by item. That way the tree says what the folders really hold,
|
|
344
|
+
whatever another program did to them meanwhile.
|
|
345
|
+
|
|
346
|
+
Args:
|
|
347
|
+
selected: What to select once it is shown again, None to
|
|
348
|
+
select nothing at all.
|
|
349
|
+
"""
|
|
350
|
+
project = self.session.project
|
|
351
|
+
if project is not None:
|
|
352
|
+
self.load_project(project.root, selected)
|
|
353
|
+
|
|
354
|
+
def opened(self, opening: Callable[[], OpenReport],
|
|
355
|
+
selected: Optional[Path] = None) -> None:
|
|
356
|
+
"""Show what an opening gave, or say why it gave nothing."""
|
|
357
|
+
try:
|
|
358
|
+
report = opening()
|
|
359
|
+
except NotesmgrError as error:
|
|
360
|
+
show_error(self.window, PROJECT_TITLE, str(error))
|
|
361
|
+
return
|
|
362
|
+
self.show_opened(report, selected)
|
|
363
|
+
|
|
364
|
+
def choose_template(self, folder: Path,
|
|
365
|
+
templates: Sequence[Path]) -> Optional[Path]:
|
|
366
|
+
"""Ask which of the templates of a folder is the one to keep.
|
|
367
|
+
|
|
368
|
+
Args:
|
|
369
|
+
folder: The folder that holds more than one template.
|
|
370
|
+
templates: The templates that it holds.
|
|
371
|
+
|
|
372
|
+
Returns:
|
|
373
|
+
The template to keep, None when the user chose none.
|
|
374
|
+
"""
|
|
375
|
+
question = TEMPLATE_QUESTION.format(folder=folder)
|
|
376
|
+
names = [path.name for path in templates]
|
|
377
|
+
chosen = ask_choice(self.window, TEMPLATE_TITLE, question, names)
|
|
378
|
+
return None if chosen is None else folder / chosen
|
|
379
|
+
|
|
380
|
+
def show_opened(self, report: OpenReport,
|
|
381
|
+
selected: Optional[Path]) -> None:
|
|
382
|
+
"""Show a project that was opened, and what opening it did."""
|
|
383
|
+
self.session.opened(report.project)
|
|
384
|
+
set_enabled(self.menu_bar.menus[CONFIG_MENU], USER_WIDE_ENTRY, True)
|
|
385
|
+
self.show_project(report.project.root.name)
|
|
386
|
+
self.explorer.show(report.project)
|
|
387
|
+
self.select(selected)
|
|
388
|
+
self.tell_about_opening(report)
|
|
389
|
+
|
|
390
|
+
def select(self, path: Optional[Path]) -> None:
|
|
391
|
+
"""Select one item of the tree, and show what is selected.
|
|
392
|
+
|
|
393
|
+
Tk tells of a selection it was given only once it comes to
|
|
394
|
+
handle its own events, which is too late for a command that
|
|
395
|
+
wants to see the project as it now stands, so the panel is
|
|
396
|
+
told here rather than waiting for the event.
|
|
397
|
+
|
|
398
|
+
Args:
|
|
399
|
+
path: What to select, None to select nothing at all.
|
|
400
|
+
"""
|
|
401
|
+
self.show_selected(self.explorer.select(path))
|
|
402
|
+
|
|
403
|
+
def tell_about_opening(self, report: OpenReport) -> None:
|
|
404
|
+
"""Tell what opening a project changed and what it could not do."""
|
|
405
|
+
changed = changed_message(report)
|
|
406
|
+
if changed:
|
|
407
|
+
show_info(self.window, PROJECT_TITLE, changed)
|
|
408
|
+
if report.problems:
|
|
409
|
+
show_error(self.window, PROJECT_TITLE, '\n'.join(report.problems))
|
|
410
|
+
|
|
411
|
+
def edit_configuration(self) -> None:
|
|
412
|
+
"""Open the editor of the configuration that is in use.
|
|
413
|
+
|
|
414
|
+
That is the configuration of the open project, and the user
|
|
415
|
+
wide configuration while no project is open. One session at a
|
|
416
|
+
time is enough, and the editor holds the application while it
|
|
417
|
+
is open, so a second one is not started.
|
|
418
|
+
"""
|
|
419
|
+
if self.config_panel is not None:
|
|
420
|
+
return
|
|
421
|
+
try:
|
|
422
|
+
self.config_panel = open_config_editor(self.window,
|
|
423
|
+
self._config_closed,
|
|
424
|
+
self.session.config_file())
|
|
425
|
+
except ConfigLoadError as error:
|
|
426
|
+
show_error(self.window, CONFIG_TITLE, str(error))
|
|
427
|
+
|
|
428
|
+
def _config_closed(self) -> None:
|
|
429
|
+
"""Take up again what the configuration editor may have changed.
|
|
430
|
+
|
|
431
|
+
The configuration says what the notes and the templates of a
|
|
432
|
+
project are called, so an open project is opened once more
|
|
433
|
+
when an editing session has ended.
|
|
434
|
+
"""
|
|
435
|
+
self.config_panel = None
|
|
436
|
+
if self.session.project is not None:
|
|
437
|
+
self.load_project(self.session.project.root)
|
|
438
|
+
|
|
439
|
+
def save_user_wide(self) -> None:
|
|
440
|
+
"""Copy the project's configuration to the user wide file.
|
|
441
|
+
|
|
442
|
+
The user wide file is found in more than one way, so the user
|
|
443
|
+
is told which file it was written to.
|
|
444
|
+
"""
|
|
445
|
+
source = self.session.config_file()
|
|
446
|
+
if source is None:
|
|
447
|
+
return
|
|
448
|
+
try:
|
|
449
|
+
target = copy_to_user_wide(source)
|
|
450
|
+
except OSError as error:
|
|
451
|
+
show_error(self.window, CONFIG_TITLE, str(error))
|
|
452
|
+
return
|
|
453
|
+
show_info(self.window, CONFIG_TITLE, USER_WIDE_SAVED.format(
|
|
454
|
+
path=target))
|
|
455
|
+
|
|
456
|
+
def show_version(self) -> None:
|
|
457
|
+
"""Show what notesmgr and the packages below it are.
|
|
458
|
+
|
|
459
|
+
Gathering the report asks PyPI whether there are newer
|
|
460
|
+
releases, which takes a moment, so the window says that it is
|
|
461
|
+
working while that is going on.
|
|
462
|
+
"""
|
|
463
|
+
report = io.StringIO()
|
|
464
|
+
try:
|
|
465
|
+
with busy_cursor(self.window):
|
|
466
|
+
version_report(report)
|
|
467
|
+
except NotesmgrError as error:
|
|
468
|
+
show_error(self.window, VERSION_TITLE, str(error))
|
|
469
|
+
return
|
|
470
|
+
show_text(self.window, VERSION_TITLE, report.getvalue())
|
|
471
|
+
|
|
472
|
+
def quit(self) -> None:
|
|
473
|
+
"""Destroy the main window, which ends the application."""
|
|
474
|
+
self.window.destroy()
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
#! /usr/local/bin/python3
|
|
2
|
+
"""Turning the markdown of a note into the HTML it is drawn from."""
|
|
3
|
+
|
|
4
|
+
# Copyright (c) 2026 Tom Björkholm
|
|
5
|
+
# MIT License
|
|
6
|
+
|
|
7
|
+
import markdown
|
|
8
|
+
from markdown.inlinepatterns import SimpleTagInlineProcessor
|
|
9
|
+
|
|
10
|
+
EXTENSIONS = ['fenced_code', 'tables', 'sane_lists']
|
|
11
|
+
"""The markdown that a note may be written in, beyond the plain kind.
|
|
12
|
+
|
|
13
|
+
Fenced code is how a note holds a program or a command line, tables
|
|
14
|
+
are what a note of comparisons is written as, and sane lists keeps a
|
|
15
|
+
list from beginning in the middle of a paragraph.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
STRIKE_PATTERN = r'(~{2})(.+?)~{2}'
|
|
19
|
+
"""What a note marks text that is struck through with.
|
|
20
|
+
|
|
21
|
+
Two tildes around the text is how it is written everywhere it is
|
|
22
|
+
written, and Python-Markdown knows nothing of it, so notesmgr says
|
|
23
|
+
what it means rather than taking a dependency for one pattern.
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
STRIKE_TAG = 'del'
|
|
27
|
+
"""The HTML that text struck through in a note is turned into."""
|
|
28
|
+
|
|
29
|
+
STRIKE_NAME = 'strike'
|
|
30
|
+
"""What the pattern of text struck through is registered as."""
|
|
31
|
+
|
|
32
|
+
STRIKE_PRIORITY = 65
|
|
33
|
+
"""Where the pattern belongs among the patterns of Python-Markdown.
|
|
34
|
+
|
|
35
|
+
Just above the emphasis patterns, which is where the marking of a
|
|
36
|
+
run of text belongs, and well below the ones for code and links,
|
|
37
|
+
so that two tildes inside them are left as the text they are.
|
|
38
|
+
"""
|
|
39
|
+
|
|
40
|
+
TAB_LENGTH = 2
|
|
41
|
+
"""How many spaces one step of indentation is taken to be.
|
|
42
|
+
|
|
43
|
+
Python-Markdown counts four, which leaves a list nested with two
|
|
44
|
+
spaces, as most editors and markdown linters write one, read as a
|
|
45
|
+
list of its own rather than as a nested one. Notes are written by
|
|
46
|
+
hand, so the smaller step is the one that draws what was meant.
|
|
47
|
+
"""
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def markdown_converter() -> markdown.Markdown:
|
|
51
|
+
"""Return the converter that the HTML of a note is made with.
|
|
52
|
+
|
|
53
|
+
Python-Markdown passes markup that a note holds straight through
|
|
54
|
+
into its output, and a note is read rather than obeyed, so the
|
|
55
|
+
two processors that let it through are taken out and the markup
|
|
56
|
+
is drawn as the text that it is. Text struck through is added,
|
|
57
|
+
which Python-Markdown knows nothing of on its own.
|
|
58
|
+
"""
|
|
59
|
+
converter = markdown.Markdown(extensions=EXTENSIONS, tab_length=TAB_LENGTH)
|
|
60
|
+
converter.preprocessors.deregister('html_block')
|
|
61
|
+
converter.inlinePatterns.deregister('html')
|
|
62
|
+
strike = SimpleTagInlineProcessor(STRIKE_PATTERN, STRIKE_TAG)
|
|
63
|
+
patterns = converter.inlinePatterns
|
|
64
|
+
patterns.register(strike, STRIKE_NAME, STRIKE_PRIORITY)
|
|
65
|
+
return converter
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def note_html(text: str) -> str:
|
|
69
|
+
"""Return the HTML of a note that is written in markdown.
|
|
70
|
+
|
|
71
|
+
Args:
|
|
72
|
+
text: The markdown of the note, as much of it as is shown.
|
|
73
|
+
|
|
74
|
+
Returns:
|
|
75
|
+
The HTML that the note is drawn from, and that a formatted
|
|
76
|
+
copy of the note is taken from.
|
|
77
|
+
"""
|
|
78
|
+
return markdown_converter().convert(text)
|
notesmgr/menu_bar.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
#! /usr/local/bin/python3
|
|
2
|
+
"""The menu bar of a notesmgr window."""
|
|
3
|
+
|
|
4
|
+
# Copyright (c) 2026 Tom Björkholm
|
|
5
|
+
# MIT License
|
|
6
|
+
|
|
7
|
+
import tkinter
|
|
8
|
+
from typing import Callable, Literal, Mapping, NamedTuple, Optional, \
|
|
9
|
+
Sequence, Union
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class MenuEntry(NamedTuple):
|
|
13
|
+
"""One command of a menu, and whether it can be chosen."""
|
|
14
|
+
|
|
15
|
+
label: str
|
|
16
|
+
command: Callable[[], None]
|
|
17
|
+
accelerator: Optional[str] = None
|
|
18
|
+
enabled: bool = True
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class MenuSpec(NamedTuple):
|
|
22
|
+
"""One menu of a menu bar and the commands it holds."""
|
|
23
|
+
|
|
24
|
+
title: str
|
|
25
|
+
entries: Sequence[MenuEntry]
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class MenuBar(NamedTuple):
|
|
29
|
+
"""The menu bar widget of a window and its menus by title.
|
|
30
|
+
|
|
31
|
+
The menus are kept by title so that an entry which only makes
|
|
32
|
+
sense in some states can be greyed out and offered again later.
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
widget: tkinter.Menu
|
|
36
|
+
menus: Mapping[str, tkinter.Menu]
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def entry_state(enabled: bool) -> Literal['normal', 'disabled']:
|
|
40
|
+
"""Return the Tk state of an entry that can or cannot be chosen.
|
|
41
|
+
|
|
42
|
+
Args:
|
|
43
|
+
enabled: Whether the entry can be chosen.
|
|
44
|
+
|
|
45
|
+
Returns:
|
|
46
|
+
The state that Tk knows that by.
|
|
47
|
+
"""
|
|
48
|
+
return 'normal' if enabled else 'disabled'
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def build_menu(menu_bar: tkinter.Menu, spec: MenuSpec) -> tkinter.Menu:
|
|
52
|
+
"""Create one described menu and add it to a menu bar.
|
|
53
|
+
|
|
54
|
+
Args:
|
|
55
|
+
menu_bar: The menu bar that the menu is added to.
|
|
56
|
+
spec: What the menu is called and what it holds.
|
|
57
|
+
|
|
58
|
+
Returns:
|
|
59
|
+
The menu that was made.
|
|
60
|
+
"""
|
|
61
|
+
menu = tkinter.Menu(menu_bar, tearoff=False)
|
|
62
|
+
for entry in spec.entries:
|
|
63
|
+
menu.add_command(label=entry.label, command=entry.command,
|
|
64
|
+
accelerator=entry.accelerator or '',
|
|
65
|
+
state=entry_state(entry.enabled))
|
|
66
|
+
menu_bar.add_cascade(label=spec.title, menu=menu)
|
|
67
|
+
return menu
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def build_menu_bar(window: Union[tkinter.Tk, tkinter.Toplevel],
|
|
71
|
+
menus: Sequence[MenuSpec]) -> MenuBar:
|
|
72
|
+
"""Build the described menus and install them on a window.
|
|
73
|
+
|
|
74
|
+
An application says what its menus hold and gets them, instead of
|
|
75
|
+
calling Tk for every entry of every menu.
|
|
76
|
+
|
|
77
|
+
Args:
|
|
78
|
+
window: The window that gets the menu bar.
|
|
79
|
+
menus: The menus of the menu bar, in the order they are shown.
|
|
80
|
+
|
|
81
|
+
Returns:
|
|
82
|
+
The menu bar that was made.
|
|
83
|
+
"""
|
|
84
|
+
widget = tkinter.Menu(window)
|
|
85
|
+
built = {spec.title: build_menu(widget, spec) for spec in menus}
|
|
86
|
+
window.configure(menu=widget)
|
|
87
|
+
return MenuBar(widget=widget, menus=built)
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def set_enabled(menu: tkinter.Menu, label: str, enabled: bool) -> None:
|
|
91
|
+
"""Let one entry of a menu be chosen, or grey it out.
|
|
92
|
+
|
|
93
|
+
Args:
|
|
94
|
+
menu: The menu that the entry is in.
|
|
95
|
+
label: Which entry of that menu it is.
|
|
96
|
+
enabled: Whether the entry can now be chosen.
|
|
97
|
+
"""
|
|
98
|
+
menu.entryconfigure(label, state=entry_state(enabled))
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def entry_labels(menu: tkinter.Menu) -> list[str]:
|
|
102
|
+
"""Return what the entries of a menu say, in the order they are in.
|
|
103
|
+
|
|
104
|
+
Args:
|
|
105
|
+
menu: The menu to look through.
|
|
106
|
+
|
|
107
|
+
Returns:
|
|
108
|
+
The label of every entry, and nothing at all for a menu that
|
|
109
|
+
holds no entries.
|
|
110
|
+
"""
|
|
111
|
+
last = menu.index(tkinter.END)
|
|
112
|
+
if last is None:
|
|
113
|
+
return []
|
|
114
|
+
return [str(menu.entrycget(index, 'label')) for index in range(last + 1)]
|