urwid 2.6.0.post0__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.

Potentially problematic release.


This version of urwid might be problematic. Click here for more details.

Files changed (75) hide show
  1. urwid/__init__.py +333 -0
  2. urwid/canvas.py +1413 -0
  3. urwid/command_map.py +137 -0
  4. urwid/container.py +59 -0
  5. urwid/decoration.py +65 -0
  6. urwid/display/__init__.py +97 -0
  7. urwid/display/_posix_raw_display.py +413 -0
  8. urwid/display/_raw_display_base.py +914 -0
  9. urwid/display/_web.css +12 -0
  10. urwid/display/_web.js +462 -0
  11. urwid/display/_win32.py +171 -0
  12. urwid/display/_win32_raw_display.py +269 -0
  13. urwid/display/common.py +1219 -0
  14. urwid/display/curses.py +690 -0
  15. urwid/display/escape.py +624 -0
  16. urwid/display/html_fragment.py +251 -0
  17. urwid/display/lcd.py +518 -0
  18. urwid/display/raw.py +37 -0
  19. urwid/display/web.py +636 -0
  20. urwid/event_loop/__init__.py +55 -0
  21. urwid/event_loop/abstract_loop.py +175 -0
  22. urwid/event_loop/asyncio_loop.py +231 -0
  23. urwid/event_loop/glib_loop.py +294 -0
  24. urwid/event_loop/main_loop.py +721 -0
  25. urwid/event_loop/select_loop.py +230 -0
  26. urwid/event_loop/tornado_loop.py +206 -0
  27. urwid/event_loop/trio_loop.py +302 -0
  28. urwid/event_loop/twisted_loop.py +269 -0
  29. urwid/event_loop/zmq_loop.py +275 -0
  30. urwid/font.py +695 -0
  31. urwid/graphics.py +96 -0
  32. urwid/highlight.css +19 -0
  33. urwid/listbox.py +1899 -0
  34. urwid/monitored_list.py +522 -0
  35. urwid/numedit.py +376 -0
  36. urwid/signals.py +330 -0
  37. urwid/split_repr.py +130 -0
  38. urwid/str_util.py +358 -0
  39. urwid/text_layout.py +632 -0
  40. urwid/treetools.py +515 -0
  41. urwid/util.py +557 -0
  42. urwid/version.py +16 -0
  43. urwid/vterm.py +1806 -0
  44. urwid/widget/__init__.py +181 -0
  45. urwid/widget/attr_map.py +161 -0
  46. urwid/widget/attr_wrap.py +140 -0
  47. urwid/widget/bar_graph.py +649 -0
  48. urwid/widget/big_text.py +77 -0
  49. urwid/widget/box_adapter.py +126 -0
  50. urwid/widget/columns.py +1145 -0
  51. urwid/widget/constants.py +574 -0
  52. urwid/widget/container.py +227 -0
  53. urwid/widget/divider.py +110 -0
  54. urwid/widget/edit.py +718 -0
  55. urwid/widget/filler.py +403 -0
  56. urwid/widget/frame.py +539 -0
  57. urwid/widget/grid_flow.py +539 -0
  58. urwid/widget/line_box.py +194 -0
  59. urwid/widget/overlay.py +829 -0
  60. urwid/widget/padding.py +597 -0
  61. urwid/widget/pile.py +971 -0
  62. urwid/widget/popup.py +170 -0
  63. urwid/widget/progress_bar.py +141 -0
  64. urwid/widget/scrollable.py +597 -0
  65. urwid/widget/solid_fill.py +44 -0
  66. urwid/widget/text.py +354 -0
  67. urwid/widget/widget.py +852 -0
  68. urwid/widget/widget_decoration.py +166 -0
  69. urwid/widget/wimp.py +792 -0
  70. urwid/wimp.py +23 -0
  71. urwid-2.6.0.post0.dist-info/COPYING +504 -0
  72. urwid-2.6.0.post0.dist-info/METADATA +332 -0
  73. urwid-2.6.0.post0.dist-info/RECORD +75 -0
  74. urwid-2.6.0.post0.dist-info/WHEEL +5 -0
  75. urwid-2.6.0.post0.dist-info/top_level.txt +1 -0
urwid/treetools.py ADDED
@@ -0,0 +1,515 @@
1
+ # Generic TreeWidget/TreeWalker class
2
+ # Copyright (c) 2010 Rob Lanphier
3
+ # Copyright (C) 2004-2010 Ian Ward
4
+ #
5
+ # This library is free software; you can redistribute it and/or
6
+ # modify it under the terms of the GNU Lesser General Public
7
+ # License as published by the Free Software Foundation; either
8
+ # version 2.1 of the License, or (at your option) any later version.
9
+ #
10
+ # This library is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
+ # Lesser General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU Lesser General Public
16
+ # License along with this library; if not, write to the Free Software
17
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
+ #
19
+ # Urwid web site: https://urwid.org/
20
+
21
+
22
+ """
23
+ Urwid tree view
24
+
25
+ Features:
26
+ - custom selectable widgets for trees
27
+ - custom list walker for displaying widgets in a tree fashion
28
+ """
29
+
30
+
31
+ from __future__ import annotations
32
+
33
+ import typing
34
+
35
+ import urwid
36
+
37
+ if typing.TYPE_CHECKING:
38
+ from collections.abc import Hashable
39
+
40
+
41
+ class TreeWidgetError(RuntimeError):
42
+ pass
43
+
44
+
45
+ class TreeWidget(urwid.WidgetWrap):
46
+ """A widget representing something in a nested tree display."""
47
+
48
+ indent_cols = 3
49
+ unexpanded_icon = urwid.SelectableIcon("+", 0)
50
+ expanded_icon = urwid.SelectableIcon("-", 0)
51
+
52
+ def __init__(self, node: TreeNode) -> None:
53
+ self._node = node
54
+ self._innerwidget = None
55
+ self.is_leaf = not hasattr(node, "get_first_child")
56
+ self.expanded = True
57
+ widget = self.get_indented_widget()
58
+ super().__init__(widget)
59
+
60
+ def selectable(self) -> bool:
61
+ """
62
+ Allow selection of non-leaf nodes so children may be (un)expanded
63
+ """
64
+ return not self.is_leaf
65
+
66
+ def get_indented_widget(self) -> urwid.Widget:
67
+ widget = self.get_inner_widget()
68
+ if not self.is_leaf:
69
+ widget = urwid.Columns(
70
+ [("fixed", 1, [self.unexpanded_icon, self.expanded_icon][self.expanded]), widget],
71
+ dividechars=1,
72
+ )
73
+ indent_cols = self.get_indent_cols()
74
+ return urwid.Padding(widget, width=("relative", 100), left=indent_cols)
75
+
76
+ def update_expanded_icon(self) -> None:
77
+ """Update display widget text for parent widgets"""
78
+ # icon is first element in columns indented widget
79
+ self._w.base_widget.widget_list[0] = [self.unexpanded_icon, self.expanded_icon][self.expanded]
80
+
81
+ def get_indent_cols(self):
82
+ return self.indent_cols * self.get_node().get_depth()
83
+
84
+ def get_inner_widget(self):
85
+ if self._innerwidget is None:
86
+ self._innerwidget = self.load_inner_widget()
87
+ return self._innerwidget
88
+
89
+ def load_inner_widget(self) -> urwid.Widget:
90
+ return urwid.Text(self.get_display_text())
91
+
92
+ def get_node(self):
93
+ return self._node
94
+
95
+ def get_display_text(self) -> str | tuple[Hashable, str] | list[str | tuple[Hashable, str]]:
96
+ return f"{self.get_node().get_key()}: {self.get_node().get_value()!s}"
97
+
98
+ def next_inorder(self) -> TreeWidget | None:
99
+ """Return the next TreeWidget depth first from this one."""
100
+ # first check if there's a child widget
101
+ firstchild = self.first_child()
102
+ if firstchild is not None:
103
+ return firstchild
104
+
105
+ # now we need to hunt for the next sibling
106
+ thisnode = self.get_node()
107
+ nextnode = thisnode.next_sibling()
108
+ depth = thisnode.get_depth()
109
+ while nextnode is None and depth > 0:
110
+ # keep going up the tree until we find an ancestor next sibling
111
+ thisnode = thisnode.get_parent()
112
+ nextnode = thisnode.next_sibling()
113
+ depth -= 1
114
+ if depth != thisnode.get_depth():
115
+ raise ValueError(depth)
116
+ if nextnode is None:
117
+ # we're at the end of the tree
118
+ return None
119
+
120
+ return nextnode.get_widget()
121
+
122
+ def prev_inorder(self) -> TreeWidget | None:
123
+ """Return the previous TreeWidget depth first from this one."""
124
+ thisnode = self._node
125
+ prevnode = thisnode.prev_sibling()
126
+ if prevnode is not None:
127
+ # we need to find the last child of the previous widget if its
128
+ # expanded
129
+ prevwidget = prevnode.get_widget()
130
+ lastchild = prevwidget.last_child()
131
+ if lastchild is None:
132
+ return prevwidget
133
+
134
+ return lastchild
135
+
136
+ # need to hunt for the parent
137
+ depth = thisnode.get_depth()
138
+ if prevnode is None and depth == 0:
139
+ return None
140
+ if prevnode is None:
141
+ prevnode = thisnode.get_parent()
142
+ return prevnode.get_widget()
143
+
144
+ def keypress(self, size, key: str) -> str | None:
145
+ """Handle expand & collapse requests (non-leaf nodes)"""
146
+ if self.is_leaf:
147
+ return key
148
+
149
+ if key in {"+", "right"}:
150
+ self.expanded = True
151
+ self.update_expanded_icon()
152
+ return None
153
+ if key == "-":
154
+ self.expanded = False
155
+ self.update_expanded_icon()
156
+ return None
157
+ if self._w.selectable():
158
+ return super().keypress(size, key)
159
+
160
+ return key
161
+
162
+ def mouse_event(
163
+ self,
164
+ size,
165
+ event: str,
166
+ button: int,
167
+ col: int,
168
+ row: int,
169
+ focus: bool,
170
+ ) -> bool:
171
+ if self.is_leaf or event != "mouse press" or button != 1:
172
+ return False
173
+
174
+ if row == 0 and col == self.get_indent_cols():
175
+ self.expanded = not self.expanded
176
+ self.update_expanded_icon()
177
+ return True
178
+
179
+ return False
180
+
181
+ def first_child(self) -> TreeWidget | None:
182
+ """Return first child if expanded."""
183
+ if self.is_leaf or not self.expanded:
184
+ return None
185
+
186
+ if self._node.has_children():
187
+ firstnode = self._node.get_first_child()
188
+ return firstnode.get_widget()
189
+
190
+ return None
191
+
192
+ def last_child(self) -> TreeWidget | None:
193
+ """Return last child if expanded."""
194
+ if self.is_leaf or not self.expanded:
195
+ return None
196
+
197
+ if self._node.has_children():
198
+ lastchild = self._node.get_last_child().get_widget()
199
+ else:
200
+ return None
201
+ # recursively search down for the last descendant
202
+ lastdescendant = lastchild.last_child()
203
+ if lastdescendant is None:
204
+ return lastchild
205
+
206
+ return lastdescendant
207
+
208
+
209
+ class TreeNode:
210
+ """
211
+ Store tree contents and cache TreeWidget objects.
212
+ A TreeNode consists of the following elements:
213
+ * key: accessor token for parent nodes
214
+ * value: subclass-specific data
215
+ * parent: a TreeNode which contains a pointer back to this object
216
+ * widget: The widget used to render the object
217
+ """
218
+
219
+ def __init__(
220
+ self,
221
+ value,
222
+ parent: ParentNode | None = None,
223
+ key=None,
224
+ depth: int | None = None,
225
+ ) -> None:
226
+ self._key = key
227
+ self._parent = parent
228
+ self._value = value
229
+ self._depth = depth
230
+ self._widget = None
231
+
232
+ def get_widget(self, reload: bool = False) -> TreeWidget:
233
+ """Return the widget for this node."""
234
+ if self._widget is None or reload:
235
+ self._widget = self.load_widget()
236
+ return self._widget
237
+
238
+ def load_widget(self) -> TreeWidget:
239
+ return TreeWidget(self)
240
+
241
+ def get_depth(self) -> int:
242
+ if self._depth is self._parent is None:
243
+ self._depth = 0
244
+ elif self._depth is None:
245
+ self._depth = self._parent.get_depth() + 1
246
+ return self._depth
247
+
248
+ def get_index(self):
249
+ if self.get_depth() == 0:
250
+ return None
251
+
252
+ return self.get_parent().get_child_index(self.get_key())
253
+
254
+ def get_key(self):
255
+ return self._key
256
+
257
+ def set_key(self, key) -> None:
258
+ self._key = key
259
+
260
+ def change_key(self, key) -> None:
261
+ self.get_parent().change_child_key(self._key, key)
262
+
263
+ def get_parent(self) -> ParentNode:
264
+ if self._parent is None and self.get_depth() > 0:
265
+ self._parent = self.load_parent()
266
+ return self._parent
267
+
268
+ def load_parent(self):
269
+ """Provide TreeNode with a parent for the current node. This function
270
+ is only required if the tree was instantiated from a child node
271
+ (virtual function)"""
272
+ raise TreeWidgetError("virtual function. Implement in subclass")
273
+
274
+ def get_value(self):
275
+ return self._value
276
+
277
+ def is_root(self) -> bool:
278
+ return self.get_depth() == 0
279
+
280
+ def next_sibling(self) -> TreeNode | None:
281
+ if self.get_depth() > 0:
282
+ return self.get_parent().next_child(self.get_key())
283
+
284
+ return None
285
+
286
+ def prev_sibling(self) -> TreeNode | None:
287
+ if self.get_depth() > 0:
288
+ return self.get_parent().prev_child(self.get_key())
289
+
290
+ return None
291
+
292
+ def get_root(self) -> ParentNode:
293
+ root = self
294
+ while root.get_parent() is not None:
295
+ root = root.get_parent()
296
+ return root
297
+
298
+
299
+ class ParentNode(TreeNode):
300
+ """Maintain sort order for TreeNodes."""
301
+
302
+ def __init__(
303
+ self,
304
+ value,
305
+ parent: ParentNode | None = None,
306
+ key=None,
307
+ depth: int | None = None,
308
+ ) -> None:
309
+ super().__init__(value, parent=parent, key=key, depth=depth)
310
+
311
+ self._child_keys = None
312
+ self._children = {}
313
+
314
+ def get_child_keys(self, reload: bool = False):
315
+ """Return a possibly ordered list of child keys"""
316
+ if self._child_keys is None or reload:
317
+ self._child_keys = self.load_child_keys()
318
+ return self._child_keys
319
+
320
+ def load_child_keys(self):
321
+ """Provide ParentNode with an ordered list of child keys (virtual function)"""
322
+ raise TreeWidgetError("virtual function. Implement in subclass")
323
+
324
+ def get_child_widget(self, key) -> TreeWidget:
325
+ """Return the widget for a given key. Create if necessary."""
326
+
327
+ return self.get_child_node(key).get_widget()
328
+
329
+ def get_child_node(self, key, reload: bool = False) -> TreeNode:
330
+ """Return the child node for a given key. Create if necessary."""
331
+ if key not in self._children or reload:
332
+ self._children[key] = self.load_child_node(key)
333
+ return self._children[key]
334
+
335
+ def load_child_node(self, key) -> TreeNode:
336
+ """Load the child node for a given key (virtual function)"""
337
+ raise TreeWidgetError("virtual function. Implement in subclass")
338
+
339
+ def set_child_node(self, key, node: TreeNode) -> None:
340
+ """Set the child node for a given key.
341
+
342
+ Useful for bottom-up, lazy population of a tree.
343
+ """
344
+ self._children[key] = node
345
+
346
+ def change_child_key(self, oldkey, newkey) -> None:
347
+ if newkey in self._children:
348
+ raise TreeWidgetError(f"{newkey} is already in use")
349
+ self._children[newkey] = self._children.pop(oldkey)
350
+ self._children[newkey].set_key(newkey)
351
+
352
+ def get_child_index(self, key):
353
+ try:
354
+ return self.get_child_keys().index(key)
355
+ except ValueError as exc:
356
+ raise TreeWidgetError(
357
+ f"Can't find key {key} in ParentNode {self.get_key()}\nParentNode items: {self.get_child_keys()!s}"
358
+ ).with_traceback(exc.__traceback__) from exc
359
+
360
+ def next_child(self, key) -> TreeNode | None:
361
+ """Return the next child node in index order from the given key."""
362
+
363
+ index = self.get_child_index(key)
364
+ # the given node may have just been deleted
365
+ if index is None:
366
+ return None
367
+ index += 1
368
+
369
+ child_keys = self.get_child_keys()
370
+ if index < len(child_keys):
371
+ # get the next item at same level
372
+ return self.get_child_node(child_keys[index])
373
+
374
+ return None
375
+
376
+ def prev_child(self, key) -> TreeNode | None:
377
+ """Return the previous child node in index order from the given key."""
378
+ index = self.get_child_index(key)
379
+ if index is None:
380
+ return None
381
+
382
+ child_keys = self.get_child_keys()
383
+ index -= 1
384
+
385
+ if index >= 0:
386
+ # get the previous item at same level
387
+ return self.get_child_node(child_keys[index])
388
+
389
+ return None
390
+
391
+ def get_first_child(self) -> TreeNode:
392
+ """Return the first TreeNode in the directory."""
393
+ child_keys = self.get_child_keys()
394
+ return self.get_child_node(child_keys[0])
395
+
396
+ def get_last_child(self) -> TreeNode:
397
+ """Return the last TreeNode in the directory."""
398
+ child_keys = self.get_child_keys()
399
+ return self.get_child_node(child_keys[-1])
400
+
401
+ def has_children(self) -> bool:
402
+ """Does this node have any children?"""
403
+ return len(self.get_child_keys()) > 0
404
+
405
+
406
+ class TreeWalker(urwid.ListWalker):
407
+ """ListWalker-compatible class for displaying TreeWidgets
408
+
409
+ positions are TreeNodes."""
410
+
411
+ def __init__(self, start_from) -> None:
412
+ """start_from: TreeNode with the initial focus."""
413
+ self.focus = start_from
414
+
415
+ def get_focus(self):
416
+ widget = self.focus.get_widget()
417
+ return widget, self.focus
418
+
419
+ def set_focus(self, focus) -> None:
420
+ self.focus = focus
421
+ self._modified()
422
+
423
+ # pylint: disable=arguments-renamed # its bad, but we should not change API
424
+ def get_next(self, start_from) -> tuple[TreeWidget, TreeNode] | tuple[None, None]:
425
+ target = start_from.get_widget().next_inorder()
426
+ if target is None:
427
+ return None, None
428
+
429
+ return target, target.get_node()
430
+
431
+ def get_prev(self, start_from) -> tuple[TreeWidget, TreeNode] | tuple[None, None]:
432
+ target = start_from.get_widget().prev_inorder()
433
+ if target is None:
434
+ return None, None
435
+
436
+ return target, target.get_node()
437
+
438
+ # pylint: enable=arguments-renamed
439
+
440
+
441
+ class TreeListBox(urwid.ListBox):
442
+ """A ListBox with special handling for navigation and
443
+ collapsing of TreeWidgets"""
444
+
445
+ def keypress(self, size: tuple[int, int], key: str) -> str | None:
446
+ key = super().keypress(size, key)
447
+ return self.unhandled_input(size, key)
448
+
449
+ def unhandled_input(self, size: tuple[int, int], data: str) -> str | None:
450
+ """Handle macro-navigation keys"""
451
+ if data == "left":
452
+ self.move_focus_to_parent(size)
453
+ return None
454
+ if data == "-":
455
+ self.collapse_focus_parent(size)
456
+ return None
457
+
458
+ return data
459
+
460
+ def collapse_focus_parent(self, size: tuple[int, int]) -> None:
461
+ """Collapse parent directory."""
462
+
463
+ _widget, pos = self.body.get_focus()
464
+ self.move_focus_to_parent(size)
465
+
466
+ _pwidget, ppos = self.body.get_focus()
467
+ if pos != ppos:
468
+ self.keypress(size, "-")
469
+
470
+ def move_focus_to_parent(self, size: tuple[int, int]) -> None:
471
+ """Move focus to parent of widget in focus."""
472
+
473
+ _widget, pos = self.body.get_focus()
474
+
475
+ parentpos = pos.get_parent()
476
+
477
+ if parentpos is None:
478
+ return
479
+
480
+ middle, top, _bottom = self.calculate_visible(size)
481
+
482
+ row_offset, _focus_widget, _focus_pos, _focus_rows, _cursor = middle
483
+ _trim_top, fill_above = top
484
+
485
+ for _widget, pos, rows in fill_above:
486
+ row_offset -= rows
487
+ if pos == parentpos:
488
+ self.change_focus(size, pos, row_offset)
489
+ return
490
+
491
+ self.change_focus(size, pos.get_parent())
492
+
493
+ def _keypress_max_left(self, size: tuple[int, int]) -> None:
494
+ self.focus_home(size)
495
+
496
+ def _keypress_max_right(self, size: tuple[int, int]) -> None:
497
+ self.focus_end(size)
498
+
499
+ def focus_home(self, size: tuple[int, int]) -> None:
500
+ """Move focus to very top."""
501
+
502
+ _widget, pos = self.body.get_focus()
503
+ rootnode = pos.get_root()
504
+ self.change_focus(size, rootnode)
505
+
506
+ def focus_end(self, size: tuple[int, int]) -> None:
507
+ """Move focus to far bottom."""
508
+
509
+ maxrow, _maxcol = size
510
+ _widget, pos = self.body.get_focus()
511
+ lastwidget = pos.get_root().get_widget().last_child()
512
+ if lastwidget:
513
+ lastnode = lastwidget.get_node()
514
+
515
+ self.change_focus(size, lastnode, maxrow - 1)