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/dialogs.py
ADDED
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
#! /usr/local/bin/python3
|
|
2
|
+
"""Windows that notesmgr shows over a window of its own."""
|
|
3
|
+
|
|
4
|
+
# Copyright (c) 2026 Tom Björkholm
|
|
5
|
+
# MIT License
|
|
6
|
+
|
|
7
|
+
import tkinter
|
|
8
|
+
from contextlib import contextmanager
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
from tkinter import filedialog, messagebox, simpledialog, ttk
|
|
11
|
+
from typing import Iterator, NamedTuple, Optional, Sequence, Union
|
|
12
|
+
|
|
13
|
+
MIN_TEXT_WIDTH = 40
|
|
14
|
+
"""Narrowest that a window showing a text is made, in characters."""
|
|
15
|
+
|
|
16
|
+
MAX_TEXT_WIDTH = 100
|
|
17
|
+
"""Widest that a window showing a text is made, in characters."""
|
|
18
|
+
|
|
19
|
+
MAX_TEXT_HEIGHT = 30
|
|
20
|
+
"""Tallest that a window showing a text is made, in lines."""
|
|
21
|
+
|
|
22
|
+
CLOSE_LABEL = 'Close'
|
|
23
|
+
"""What the button that closes a shown text says."""
|
|
24
|
+
|
|
25
|
+
PADDING = 8
|
|
26
|
+
"""Space in pixels left around the button of a shown text."""
|
|
27
|
+
|
|
28
|
+
BUSY_CURSOR = 'watch'
|
|
29
|
+
"""Mouse cursor shown while an answer is being gathered."""
|
|
30
|
+
|
|
31
|
+
CHOOSE_LABEL = 'Keep'
|
|
32
|
+
"""What the button that takes the chosen option says."""
|
|
33
|
+
|
|
34
|
+
CANCEL_LABEL = 'Cancel'
|
|
35
|
+
"""What the button that answers nothing at all says."""
|
|
36
|
+
|
|
37
|
+
ACCEPT_LABEL = 'OK'
|
|
38
|
+
"""What the button that takes what was filled in says."""
|
|
39
|
+
|
|
40
|
+
NAME_LABEL = 'Name'
|
|
41
|
+
"""What the field holding a name is called."""
|
|
42
|
+
|
|
43
|
+
FOLDER_LABEL = 'Folder'
|
|
44
|
+
"""What the field holding a folder of the project is called."""
|
|
45
|
+
|
|
46
|
+
ENTRY_WIDTH = 40
|
|
47
|
+
"""Width in characters of the field that a name is typed into."""
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def text_size(text: str) -> tuple[int, int]:
|
|
51
|
+
"""Return the width and height in characters that a text needs.
|
|
52
|
+
|
|
53
|
+
A window is never made narrower than a short line nor wider or
|
|
54
|
+
taller than a screen comfortably holds, so a very long line or a
|
|
55
|
+
very long text is scrolled to instead of being shown whole.
|
|
56
|
+
|
|
57
|
+
Args:
|
|
58
|
+
text: The text that is going to be shown.
|
|
59
|
+
|
|
60
|
+
Returns:
|
|
61
|
+
The width in characters and the height in lines.
|
|
62
|
+
"""
|
|
63
|
+
lines = text.splitlines() or ['']
|
|
64
|
+
widest = max(len(line) for line in lines)
|
|
65
|
+
width = min(max(widest, MIN_TEXT_WIDTH), MAX_TEXT_WIDTH)
|
|
66
|
+
return width, min(len(lines), MAX_TEXT_HEIGHT)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def show_text(parent: Union[tkinter.Tk, tkinter.Toplevel], title: str,
|
|
70
|
+
text: str) -> tkinter.Toplevel:
|
|
71
|
+
"""Show a text that cannot be edited, over another window.
|
|
72
|
+
|
|
73
|
+
Args:
|
|
74
|
+
parent: The window that the new window is shown over.
|
|
75
|
+
title: What the new window is called.
|
|
76
|
+
text: What the new window shows.
|
|
77
|
+
|
|
78
|
+
Returns:
|
|
79
|
+
The window that was made, which its own button destroys.
|
|
80
|
+
"""
|
|
81
|
+
window = tkinter.Toplevel(parent)
|
|
82
|
+
window.title(title)
|
|
83
|
+
window.transient(parent)
|
|
84
|
+
_fill_with_text(window, text)
|
|
85
|
+
return window
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def _fill_with_text(window: tkinter.Toplevel, text: str) -> None:
|
|
89
|
+
"""Fill a window with a text, a scroll bar and a close button."""
|
|
90
|
+
width, height = text_size(text)
|
|
91
|
+
area = tkinter.Text(window, width=width, height=height, wrap='none',
|
|
92
|
+
font='TkFixedFont')
|
|
93
|
+
scroll = ttk.Scrollbar(window, orient=tkinter.VERTICAL, command=area.yview)
|
|
94
|
+
area.configure(yscrollcommand=scroll.set)
|
|
95
|
+
area.insert('1.0', text)
|
|
96
|
+
area.configure(state=tkinter.DISABLED)
|
|
97
|
+
button = ttk.Button(window, text=CLOSE_LABEL, command=window.destroy)
|
|
98
|
+
button.pack(side=tkinter.BOTTOM, pady=PADDING)
|
|
99
|
+
scroll.pack(side=tkinter.RIGHT, fill=tkinter.Y)
|
|
100
|
+
area.pack(side=tkinter.LEFT, fill=tkinter.BOTH, expand=True)
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def show_error(parent: Union[tkinter.Tk, tkinter.Toplevel], title: str,
|
|
104
|
+
message: str) -> None:
|
|
105
|
+
"""Tell the user what went wrong, in a window of its own.
|
|
106
|
+
|
|
107
|
+
Args:
|
|
108
|
+
parent: The window that the message is shown over.
|
|
109
|
+
title: What the message window is called.
|
|
110
|
+
message: What went wrong.
|
|
111
|
+
"""
|
|
112
|
+
messagebox.showerror(title=title, message=message, parent=parent)
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
@contextmanager
|
|
116
|
+
def busy_cursor(window: Union[tkinter.Tk, tkinter.Toplevel]) -> Iterator[None]:
|
|
117
|
+
"""Show the waiting cursor while something slow is being done.
|
|
118
|
+
|
|
119
|
+
Args:
|
|
120
|
+
window: The window that is going to be busy.
|
|
121
|
+
|
|
122
|
+
Yields:
|
|
123
|
+
Nothing. The cursor is put back when the block has ended,
|
|
124
|
+
whether it ended by finishing or by raising.
|
|
125
|
+
"""
|
|
126
|
+
window.configure(cursor=BUSY_CURSOR)
|
|
127
|
+
window.update_idletasks()
|
|
128
|
+
try:
|
|
129
|
+
yield
|
|
130
|
+
finally:
|
|
131
|
+
window.configure(cursor='')
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def ask_folder(parent: Union[tkinter.Tk, tkinter.Toplevel], title: str,
|
|
135
|
+
folder: Path) -> Optional[Path]:
|
|
136
|
+
"""Ask the user for a folder that is there.
|
|
137
|
+
|
|
138
|
+
Args:
|
|
139
|
+
parent: The window that the question is asked over.
|
|
140
|
+
title: What the window asking is called.
|
|
141
|
+
folder: The folder that the chooser starts in.
|
|
142
|
+
|
|
143
|
+
Returns:
|
|
144
|
+
The folder that was chosen, None when none was.
|
|
145
|
+
"""
|
|
146
|
+
chosen = filedialog.askdirectory(parent=parent, title=title,
|
|
147
|
+
mustexist=True, initialdir=folder)
|
|
148
|
+
return Path(chosen) if chosen else None
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def ask_yes_no(parent: Union[tkinter.Tk, tkinter.Toplevel], title: str,
|
|
152
|
+
question: str) -> bool:
|
|
153
|
+
"""Ask the user something that is answered with yes or no.
|
|
154
|
+
|
|
155
|
+
Args:
|
|
156
|
+
parent: The window that the question is asked over.
|
|
157
|
+
title: What the window asking is called.
|
|
158
|
+
question: What the user is asked.
|
|
159
|
+
|
|
160
|
+
Returns:
|
|
161
|
+
Whether the user answered yes.
|
|
162
|
+
"""
|
|
163
|
+
return messagebox.askyesno(title=title, message=question, parent=parent)
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
def show_info(parent: Union[tkinter.Tk, tkinter.Toplevel], title: str,
|
|
167
|
+
message: str) -> None:
|
|
168
|
+
"""Tell the user something that is no cause for worry.
|
|
169
|
+
|
|
170
|
+
Args:
|
|
171
|
+
parent: The window that the message is shown over.
|
|
172
|
+
title: What the message window is called.
|
|
173
|
+
message: What the user is told.
|
|
174
|
+
"""
|
|
175
|
+
messagebox.showinfo(title=title, message=message, parent=parent)
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
class AskingWindow:
|
|
179
|
+
"""A window that asks the user something and waits for an answer.
|
|
180
|
+
|
|
181
|
+
The window is built by the constructor, and the answer is waited
|
|
182
|
+
for by answered(), so that a test can look at the window and
|
|
183
|
+
answer it by pressing its buttons without a main loop of its own.
|
|
184
|
+
"""
|
|
185
|
+
|
|
186
|
+
def __init__(self, parent: Union[tkinter.Tk, tkinter.Toplevel],
|
|
187
|
+
title: str) -> None:
|
|
188
|
+
"""Build the window, which is empty until it is filled in.
|
|
189
|
+
|
|
190
|
+
Args:
|
|
191
|
+
parent: The window that the question is asked over.
|
|
192
|
+
title: What the window asking is called.
|
|
193
|
+
"""
|
|
194
|
+
self.taken = False
|
|
195
|
+
self.window = tkinter.Toplevel(parent)
|
|
196
|
+
self.window.title(title)
|
|
197
|
+
self.window.transient(parent)
|
|
198
|
+
self.window.protocol('WM_DELETE_WINDOW', self.cancel)
|
|
199
|
+
|
|
200
|
+
def ask(self, question: str) -> None:
|
|
201
|
+
"""Put what the user is asked at the top of the window."""
|
|
202
|
+
asked = ttk.Label(self.window, text=question, justify=tkinter.LEFT)
|
|
203
|
+
asked.pack(side=tkinter.TOP, anchor=tkinter.W, padx=PADDING,
|
|
204
|
+
pady=PADDING)
|
|
205
|
+
|
|
206
|
+
def add_buttons(self, accept_label: str) -> None:
|
|
207
|
+
"""Put the button that answers and the one that does not.
|
|
208
|
+
|
|
209
|
+
Args:
|
|
210
|
+
accept_label: What the button taking the answer says.
|
|
211
|
+
"""
|
|
212
|
+
cancel = ttk.Button(self.window, text=CANCEL_LABEL,
|
|
213
|
+
command=self.cancel)
|
|
214
|
+
accept = ttk.Button(self.window, text=accept_label,
|
|
215
|
+
command=self.accept)
|
|
216
|
+
cancel.pack(side=tkinter.RIGHT, padx=PADDING, pady=PADDING)
|
|
217
|
+
accept.pack(side=tkinter.RIGHT, pady=PADDING)
|
|
218
|
+
|
|
219
|
+
def accept(self) -> None:
|
|
220
|
+
"""Take what the user filled in as the answer."""
|
|
221
|
+
self.taken = True
|
|
222
|
+
self.window.destroy()
|
|
223
|
+
|
|
224
|
+
def cancel(self) -> None:
|
|
225
|
+
"""Answer nothing at all."""
|
|
226
|
+
self.taken = False
|
|
227
|
+
self.window.destroy()
|
|
228
|
+
|
|
229
|
+
def answered(self) -> bool:
|
|
230
|
+
"""Wait for the answer, holding the rest of the application.
|
|
231
|
+
|
|
232
|
+
Returns:
|
|
233
|
+
Whether the user answered rather than answering nothing.
|
|
234
|
+
"""
|
|
235
|
+
self.window.grab_set()
|
|
236
|
+
self.window.wait_window()
|
|
237
|
+
return self.taken
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
class ChoiceDialog(AskingWindow):
|
|
241
|
+
"""Asks the user to choose one of several named things."""
|
|
242
|
+
|
|
243
|
+
def __init__(self, parent: Union[tkinter.Tk, tkinter.Toplevel], title: str,
|
|
244
|
+
question: str, options: Sequence[str]) -> None:
|
|
245
|
+
"""Build the window that asks the question.
|
|
246
|
+
|
|
247
|
+
Args:
|
|
248
|
+
parent: The window that the question is asked over.
|
|
249
|
+
title: What the window asking is called.
|
|
250
|
+
question: What the user is asked.
|
|
251
|
+
options: What the user chooses between.
|
|
252
|
+
"""
|
|
253
|
+
super().__init__(parent, title)
|
|
254
|
+
self.picked = tkinter.StringVar(self.window,
|
|
255
|
+
value=options[0] if options else '')
|
|
256
|
+
self.ask(question)
|
|
257
|
+
for option in options:
|
|
258
|
+
offered = ttk.Radiobutton(self.window, text=option, value=option,
|
|
259
|
+
variable=self.picked)
|
|
260
|
+
offered.pack(side=tkinter.TOP, anchor=tkinter.W, padx=PADDING)
|
|
261
|
+
self.add_buttons(CHOOSE_LABEL)
|
|
262
|
+
|
|
263
|
+
@property
|
|
264
|
+
def chosen(self) -> Optional[str]:
|
|
265
|
+
"""Return what was chosen, None while nothing was."""
|
|
266
|
+
return self.picked.get() if self.taken else None
|
|
267
|
+
|
|
268
|
+
def choose(self) -> Optional[str]:
|
|
269
|
+
"""Wait for the answer, holding the rest of the application.
|
|
270
|
+
|
|
271
|
+
Returns:
|
|
272
|
+
What the user chose, None when the user chose nothing.
|
|
273
|
+
"""
|
|
274
|
+
self.answered()
|
|
275
|
+
return self.chosen
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
def ask_choice(parent: Union[tkinter.Tk, tkinter.Toplevel], title: str,
|
|
279
|
+
question: str, options: Sequence[str]) -> Optional[str]:
|
|
280
|
+
"""Ask the user to choose one of several named things.
|
|
281
|
+
|
|
282
|
+
Args:
|
|
283
|
+
parent: The window that the question is asked over.
|
|
284
|
+
title: What the window asking is called.
|
|
285
|
+
question: What the user is asked.
|
|
286
|
+
options: What the user chooses between.
|
|
287
|
+
|
|
288
|
+
Returns:
|
|
289
|
+
What the user chose, None when the user chose nothing.
|
|
290
|
+
"""
|
|
291
|
+
return ChoiceDialog(parent, title, question, options).choose()
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
def ask_name(parent: Union[tkinter.Tk, tkinter.Toplevel], title: str,
|
|
295
|
+
question: str, given: str = '') -> Optional[str]:
|
|
296
|
+
"""Ask the user for the name of a note or a folder.
|
|
297
|
+
|
|
298
|
+
What the name may be is the model's to say, so anything at all
|
|
299
|
+
can be typed here and is refused, if it is to be refused, where
|
|
300
|
+
the file is made.
|
|
301
|
+
|
|
302
|
+
Args:
|
|
303
|
+
parent: The window that the question is asked over.
|
|
304
|
+
title: What the window asking is called.
|
|
305
|
+
question: What the user is asked.
|
|
306
|
+
given: What the field holds before anything is typed.
|
|
307
|
+
|
|
308
|
+
Returns:
|
|
309
|
+
What the user typed, None when the user typed nothing at all.
|
|
310
|
+
"""
|
|
311
|
+
return simpledialog.askstring(title, question, parent=parent,
|
|
312
|
+
initialvalue=given)
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
class NameFolder(NamedTuple):
|
|
316
|
+
"""A name, and the folder of the project that it is to be in."""
|
|
317
|
+
|
|
318
|
+
name: str
|
|
319
|
+
folder: str
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
class NameFolderDialog(AskingWindow):
|
|
323
|
+
"""Asks the user for a name and for a folder of the project.
|
|
324
|
+
|
|
325
|
+
The folders are offered to be chosen rather than to be typed, so
|
|
326
|
+
that what is asked for is always a folder of the project, and
|
|
327
|
+
the file chooser of the platform is not let anywhere near it.
|
|
328
|
+
"""
|
|
329
|
+
|
|
330
|
+
def __init__(self, parent: Union[tkinter.Tk, tkinter.Toplevel], title: str,
|
|
331
|
+
given: NameFolder, folders: Sequence[str]) -> None:
|
|
332
|
+
"""Build the window that asks for the name and the folder.
|
|
333
|
+
|
|
334
|
+
Args:
|
|
335
|
+
parent: The window that the question is asked over.
|
|
336
|
+
title: What the window asking is called.
|
|
337
|
+
given: What the two fields hold to begin with.
|
|
338
|
+
folders: The folders of the project, as they are named.
|
|
339
|
+
"""
|
|
340
|
+
super().__init__(parent, title)
|
|
341
|
+
self.typed = tkinter.StringVar(self.window, value=given.name)
|
|
342
|
+
self.picked = tkinter.StringVar(self.window, value=given.folder)
|
|
343
|
+
self._fill_in(folders)
|
|
344
|
+
self.add_buttons(ACCEPT_LABEL)
|
|
345
|
+
|
|
346
|
+
def _fill_in(self, folders: Sequence[str]) -> None:
|
|
347
|
+
"""Put the field for the name above the one for the folder."""
|
|
348
|
+
self.ask(NAME_LABEL)
|
|
349
|
+
entry = ttk.Entry(self.window, textvariable=self.typed,
|
|
350
|
+
width=ENTRY_WIDTH)
|
|
351
|
+
entry.pack(side=tkinter.TOP, fill=tkinter.X, padx=PADDING)
|
|
352
|
+
self.ask(FOLDER_LABEL)
|
|
353
|
+
chooser = ttk.Combobox(self.window, textvariable=self.picked,
|
|
354
|
+
values=list(folders), state='readonly')
|
|
355
|
+
chooser.pack(side=tkinter.TOP, fill=tkinter.X, padx=PADDING)
|
|
356
|
+
entry.focus_set()
|
|
357
|
+
|
|
358
|
+
@property
|
|
359
|
+
def given(self) -> Optional[NameFolder]:
|
|
360
|
+
"""Return what was filled in, None while nothing was taken."""
|
|
361
|
+
if not self.taken:
|
|
362
|
+
return None
|
|
363
|
+
return NameFolder(name=self.typed.get(), folder=self.picked.get())
|
|
364
|
+
|
|
365
|
+
def ask_for(self) -> Optional[NameFolder]:
|
|
366
|
+
"""Wait for the answer, holding the rest of the application.
|
|
367
|
+
|
|
368
|
+
Returns:
|
|
369
|
+
The name and the folder, None when the user gave none.
|
|
370
|
+
"""
|
|
371
|
+
self.answered()
|
|
372
|
+
return self.given
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
def ask_name_folder(parent: Union[tkinter.Tk, tkinter.Toplevel], title: str,
|
|
376
|
+
given: NameFolder,
|
|
377
|
+
folders: Sequence[str]) -> Optional[NameFolder]:
|
|
378
|
+
"""Ask the user for a name and for a folder of the project.
|
|
379
|
+
|
|
380
|
+
Args:
|
|
381
|
+
parent: The window that the question is asked over.
|
|
382
|
+
title: What the window asking is called.
|
|
383
|
+
given: What the two fields hold to begin with.
|
|
384
|
+
folders: The folders of the project, as they are named.
|
|
385
|
+
|
|
386
|
+
Returns:
|
|
387
|
+
The name and the folder, None when the user gave none.
|
|
388
|
+
"""
|
|
389
|
+
return NameFolderDialog(parent, title, given, folders).ask_for()
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
#! /usr/local/bin/python3
|
|
2
|
+
"""Starting the editor that a project is configured with."""
|
|
3
|
+
|
|
4
|
+
# Copyright (c) 2026 Tom Björkholm
|
|
5
|
+
# MIT License
|
|
6
|
+
|
|
7
|
+
import os
|
|
8
|
+
import shlex
|
|
9
|
+
import subprocess
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
from typing import Callable
|
|
12
|
+
from notesmgr.errors import NotesmgrError
|
|
13
|
+
|
|
14
|
+
PLACEHOLDER = '{file}'
|
|
15
|
+
"""What the name of the note replaces in the editor command."""
|
|
16
|
+
|
|
17
|
+
ON_WINDOWS = os.name == 'nt'
|
|
18
|
+
"""Whether a command line is to be read the way Windows reads one."""
|
|
19
|
+
|
|
20
|
+
QUOTES = ('"', "'")
|
|
21
|
+
"""What a Windows command line may have around one of its words."""
|
|
22
|
+
|
|
23
|
+
UNREADABLE = 'The editor command cannot be read.\n{command}\n{reason}'
|
|
24
|
+
"""What is said about a command whose quotes do not match."""
|
|
25
|
+
|
|
26
|
+
NO_PROGRAM = 'The editor command {command} names no program to start.'
|
|
27
|
+
"""What is said about a command that holds no word at all."""
|
|
28
|
+
|
|
29
|
+
NOT_STARTED = 'The editor cannot be started.\n{command}\n{reason}'
|
|
30
|
+
"""What is said about an editor that the system did not start."""
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def unquoted(word: str) -> str:
|
|
34
|
+
"""Return one word of a command line without the quotes around it.
|
|
35
|
+
|
|
36
|
+
Args:
|
|
37
|
+
word: A word as a Windows command line holds it.
|
|
38
|
+
|
|
39
|
+
Returns:
|
|
40
|
+
The word itself, which is what is to be passed on.
|
|
41
|
+
"""
|
|
42
|
+
for quote in QUOTES:
|
|
43
|
+
if len(word) > 1 and word.startswith(quote) and word.endswith(quote):
|
|
44
|
+
return word[1:-1]
|
|
45
|
+
return word
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def split_command(command: str, windows: bool = ON_WINDOWS) -> list[str]:
|
|
49
|
+
"""Return the words that an editor command is made of.
|
|
50
|
+
|
|
51
|
+
A backslash is an escape character to a POSIX shell and a path
|
|
52
|
+
separator on Windows, so a Windows command line is split by the
|
|
53
|
+
rules that keep its paths whole, and the quotes that splitting
|
|
54
|
+
leaves behind are then taken off.
|
|
55
|
+
|
|
56
|
+
Args:
|
|
57
|
+
command: The editor command as the configuration holds it.
|
|
58
|
+
windows: Whether to split it the way Windows does.
|
|
59
|
+
|
|
60
|
+
Returns:
|
|
61
|
+
The words of the command, quotes taken off.
|
|
62
|
+
|
|
63
|
+
Raises:
|
|
64
|
+
ValueError: The quotes of the command do not match.
|
|
65
|
+
"""
|
|
66
|
+
if windows:
|
|
67
|
+
return [unquoted(word) for word in shlex.split(command, posix=False)]
|
|
68
|
+
return shlex.split(command)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def editor_argv(command: str, path: Path) -> list[str]:
|
|
72
|
+
"""Return the command line that opens one note in the editor.
|
|
73
|
+
|
|
74
|
+
The name of the note takes the place of every {file} in the
|
|
75
|
+
command, and is added at the end of a command that holds none,
|
|
76
|
+
so that 'code' and 'code {file}' mean the same thing.
|
|
77
|
+
|
|
78
|
+
Args:
|
|
79
|
+
command: The editor command as the configuration holds it.
|
|
80
|
+
path: The note to open.
|
|
81
|
+
|
|
82
|
+
Returns:
|
|
83
|
+
The program to start and the arguments to give it.
|
|
84
|
+
|
|
85
|
+
Raises:
|
|
86
|
+
NotesmgrError: The command cannot be read, or names nothing
|
|
87
|
+
to start.
|
|
88
|
+
"""
|
|
89
|
+
try:
|
|
90
|
+
words = split_command(command)
|
|
91
|
+
except ValueError as error:
|
|
92
|
+
raise NotesmgrError(UNREADABLE.format(command=command,
|
|
93
|
+
reason=error)) from error
|
|
94
|
+
if not words:
|
|
95
|
+
raise NotesmgrError(NO_PROGRAM.format(command=command))
|
|
96
|
+
name = str(path)
|
|
97
|
+
if not any(PLACEHOLDER in word for word in words):
|
|
98
|
+
return words + [name]
|
|
99
|
+
return [word.replace(PLACEHOLDER, name) for word in words]
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def start_detached(argv: list[str]) -> None:
|
|
103
|
+
"""Start a program and leave it running on its own.
|
|
104
|
+
|
|
105
|
+
The editor outlives the command that started it and is not to be
|
|
106
|
+
stopped by what stops notesmgr, so it is put in a session of its
|
|
107
|
+
own. Windows knows no sessions and ignores that, which is right
|
|
108
|
+
there, where a started program is independent already.
|
|
109
|
+
|
|
110
|
+
Args:
|
|
111
|
+
argv: The program to start and the arguments to give it.
|
|
112
|
+
|
|
113
|
+
Raises:
|
|
114
|
+
OSError: The program cannot be started.
|
|
115
|
+
"""
|
|
116
|
+
# pylint: disable=consider-using-with
|
|
117
|
+
subprocess.Popen(argv, start_new_session=True)
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def launch_editor(command: str, path: Path,
|
|
121
|
+
run: Callable[[list[str]], None] = start_detached) -> None:
|
|
122
|
+
"""Open one note in the editor that the project is configured with.
|
|
123
|
+
|
|
124
|
+
Args:
|
|
125
|
+
command: The editor command as the configuration holds it.
|
|
126
|
+
path: The note to open.
|
|
127
|
+
run: What starts the editor, for a test to stand in for.
|
|
128
|
+
|
|
129
|
+
Raises:
|
|
130
|
+
NotesmgrError: The command cannot be read, names nothing to
|
|
131
|
+
start, or names a program that the system did not start.
|
|
132
|
+
"""
|
|
133
|
+
argv = editor_argv(command, path)
|
|
134
|
+
try:
|
|
135
|
+
run(argv)
|
|
136
|
+
except OSError as error:
|
|
137
|
+
raise NotesmgrError(NOT_STARTED.format(command=' '.join(argv),
|
|
138
|
+
reason=error)) from error
|
notesmgr/errors.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#! /usr/local/bin/python3
|
|
2
|
+
"""What notesmgr raises when it cannot do what it was asked to do."""
|
|
3
|
+
|
|
4
|
+
# Copyright (c) 2026 Tom Björkholm
|
|
5
|
+
# MIT License
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class NotesmgrError(Exception):
|
|
9
|
+
"""Something cannot be done, said in words meant for the user.
|
|
10
|
+
|
|
11
|
+
The message is shown in a window as it stands, so it names the
|
|
12
|
+
file or the folder it is about and says what is wrong with it,
|
|
13
|
+
rather than saying what the code was doing at the time.
|
|
14
|
+
"""
|