diff options
author | Alexey Stepanov <penguinolog@users.noreply.github.com> | 2023-05-01 15:26:37 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-01 15:26:37 +0200 |
commit | bac20db87eaeb0b7a0026807d7e6dbe391b08580 (patch) | |
tree | fdf051284fdae2933a6d792abfba22d0f4c3bde7 | |
parent | 1cc1d63358f7759785b39079eca069ed41078b6d (diff) | |
download | urwid-bac20db87eaeb0b7a0026807d7e6dbe391b08580.tar.gz |
Return original code to the deprecated getters and setters (#549)
* Return original code to the deprecated getters and setters
* Make more aggressive deprecation warning
Fix: #548
* Return original code to the deprecated getters and setters
* Make more aggressive deprecation warning
Fix: #548
---------
Co-authored-by: Aleksei Stepanov <alekseis@nvidia.com>
-rwxr-xr-x | urwid/container.py | 230 | ||||
-rw-r--r-- | urwid/listbox.py | 24 | ||||
-rwxr-xr-x | urwid/monitored_list.py | 4 | ||||
-rw-r--r-- | urwid/tests/test_vterm.py | 2 |
4 files changed, 237 insertions, 23 deletions
diff --git a/urwid/container.py b/urwid/container.py index 64a6e35..00f1719 100755 --- a/urwid/container.py +++ b/urwid/container.py @@ -152,7 +152,7 @@ class WidgetContainerMixin: f"method `{self.__class__.__name__}._get_focus` is deprecated, " f"please use `{self.__class__.__name__}.focus` property", DeprecationWarning, - stacklevel=2, + stacklevel=3, ) return self.focus @@ -224,7 +224,7 @@ class WidgetContainerListContentsMixin: f"method `{self.__class__.__name__}._get_focus_position` is deprecated, " f"please use `{self.__class__.__name__}.focus_position` property", DeprecationWarning, - stacklevel=2, + stacklevel=3, ) return self.focus_position @@ -238,7 +238,7 @@ class WidgetContainerListContentsMixin: f"method `{self.__class__.__name__}._set_focus_position` is deprecated, " f"please use `{self.__class__.__name__}.focus_position` property", DeprecationWarning, - stacklevel=2, + stacklevel=3, ) self.focus_position = position @@ -340,7 +340,7 @@ class GridFlow(WidgetWrap, WidgetContainerMixin, WidgetContainerListContentsMixi "only for backwards compatibility." "You should use the new standard container property `contents` to modify GridFlow", DeprecationWarning, - stacklevel=2, + stacklevel=3, ) return self.cells @@ -349,7 +349,7 @@ class GridFlow(WidgetWrap, WidgetContainerMixin, WidgetContainerListContentsMixi "only for backwards compatibility." "You should use the new standard container property `contents` to modify GridFlow", DeprecationWarning, - stacklevel=2, + stacklevel=3, ) self.cells = widgets @@ -374,7 +374,7 @@ class GridFlow(WidgetWrap, WidgetContainerMixin, WidgetContainerListContentsMixi f"Method `{self.__class__.__name__}._get_cell_width` is deprecated, " f"please use property `{self.__class__.__name__}.cell_width`", DeprecationWarning, - stacklevel=2, + stacklevel=3, ) return self.cell_width @@ -383,7 +383,7 @@ class GridFlow(WidgetWrap, WidgetContainerMixin, WidgetContainerListContentsMixi f"Method `{self.__class__.__name__}._set_cell_width` is deprecated, " f"please use property `{self.__class__.__name__}.cell_width`", DeprecationWarning, - stacklevel=2, + stacklevel=3, ) self.cell_width = width @@ -442,9 +442,19 @@ class GridFlow(WidgetWrap, WidgetContainerMixin, WidgetContainerListContentsMixi stacklevel=2, ) if isinstance(cell, int): - self.focus_position = cell + try: + if cell < 0 or cell >= len(self.contents): + raise IndexError + except (TypeError, IndexError): + raise IndexError(f"No GridFlow child widget at position {cell}") + self.contents.focus = cell return - self.focus_cell = cell + + for i, (w, options) in enumerate(self.contents): + if cell == w: + self.focus_position = i + return + raise ValueError(f"Widget not found in GridFlow contents: {cell!r}") @property def focus(self) -> Widget | None: @@ -453,6 +463,17 @@ class GridFlow(WidgetWrap, WidgetContainerMixin, WidgetContainerListContentsMixi return None return self.contents[self.focus_position][0] + def _get_focus(self) -> Widget: + warnings.warn( + f"method `{self.__class__.__name__}._get_focus` is deprecated, " + f"please use `{self.__class__.__name__}.focus` property", + DeprecationWarning, + stacklevel=3, + ) + if not self.contents: + return None + return self.contents[self.focus_position][0] + def get_focus(self): """ Return the widget in focus, for backwards compatibility. @@ -466,7 +487,9 @@ class GridFlow(WidgetWrap, WidgetContainerMixin, WidgetContainerListContentsMixi PendingDeprecationWarning, stacklevel=2, ) - return self.focus + if not self.contents: + return None + return self.contents[self.focus_position][0] @property def focus_cell(self): @@ -500,9 +523,13 @@ class GridFlow(WidgetWrap, WidgetContainerMixin, WidgetContainerListContentsMixi "You may also use the new standard container property" "`focus` to get the focus and `focus_position` to get/set the cell in focus by index", DeprecationWarning, - stacklevel=2, + stacklevel=3, ) - self.focus_cell = cell + for i, (w, options) in enumerate(self.contents): + if cell == w: + self.focus_position = i + return + raise ValueError(f"Widget not found in GridFlow contents: {cell!r}") @property def focus_position(self) -> int | None: @@ -528,6 +555,36 @@ class GridFlow(WidgetWrap, WidgetContainerMixin, WidgetContainerListContentsMixi raise IndexError(f"No GridFlow child widget at position {position}") self.contents.focus = position + def _get_focus_position(self) -> int | None: + warnings.warn( + f"method `{self.__class__.__name__}._get_focus_position` is deprecated, " + f"please use `{self.__class__.__name__}.focus_position` property", + DeprecationWarning, + stacklevel=3, + ) + if not self.contents: + raise IndexError("No focus_position, GridFlow is empty") + return self.contents.focus + + def _set_focus_position(self, position: int) -> None: + """ + Set the widget in focus. + + position -- index of child widget to be made focus + """ + warnings.warn( + f"method `{self.__class__.__name__}._set_focus_position` is deprecated, " + f"please use `{self.__class__.__name__}.focus_position` property", + DeprecationWarning, + stacklevel=3, + ) + try: + if position < 0 or position >= len(self.contents): + raise IndexError + except (TypeError, IndexError): + raise IndexError(f"No GridFlow child widget at position {position}") + self.contents.focus = position + def get_display_widget(self, size: tuple[int]) -> Divider | Pile: """ Arrange the cells into columns (and possibly a pile) for @@ -882,6 +939,15 @@ class Overlay(Widget, WidgetContainerMixin, WidgetContainerListContentsMixin): """ return self.top_w + def _get_focus(self) -> Widget: + warnings.warn( + f"method `{self.__class__.__name__}._get_focus` is deprecated, " + f"please use `{self.__class__.__name__}.focus` property", + DeprecationWarning, + stacklevel=3, + ) + return self.top_w + @property def focus_position(self) -> Literal[1]: """ @@ -899,6 +965,30 @@ class Overlay(Widget, WidgetContainerMixin, WidgetContainerListContentsMixin): if position != 1: raise IndexError(f"Overlay widget focus_position currently must always be set to 1, not {position}") + def _get_focus_position(self) -> int | None: + warnings.warn( + f"method `{self.__class__.__name__}._get_focus_position` is deprecated, " + f"please use `{self.__class__.__name__}.focus_position` property", + DeprecationWarning, + stacklevel=3, + ) + return 1 + + def _set_focus_position(self, position: int) -> None: + """ + Set the widget in focus. + + position -- index of child widget to be made focus + """ + warnings.warn( + f"method `{self.__class__.__name__}._set_focus_position` is deprecated, " + f"please use `{self.__class__.__name__}.focus_position` property", + DeprecationWarning, + stacklevel=3, + ) + if position != 1: + raise IndexError(f"Overlay widget focus_position currently must always be set to 1, not {position}") + @property def contents(self): """ @@ -1277,6 +1367,19 @@ class Frame(Widget, WidgetContainerMixin): 'body': self._body }[self.focus_part] + def _get_focus(self) -> Widget: + warnings.warn( + f"method `{self.__class__.__name__}._get_focus` is deprecated, " + f"please use `{self.__class__.__name__}.focus` property", + DeprecationWarning, + stacklevel=3, + ) + return { + 'header': self._header, + 'footer': self._footer, + 'body': self._body + }[self.focus_part] + @property def contents(self): """ @@ -1374,7 +1477,7 @@ class Frame(Widget, WidgetContainerMixin): f"method `{self.__class__.__name__}._contents` is deprecated, " f"please use property `{self.__class__.__name__}.contents`", DeprecationWarning, - stacklevel=2, + stacklevel=3, ) return self.contents @@ -1830,6 +1933,17 @@ class Pile(Widget, WidgetContainerMixin, WidgetContainerListContentsMixin): return raise ValueError(f"Widget not found in Pile contents: {item!r}") + def _get_focus(self) -> Widget: + warnings.warn( + f"method `{self.__class__.__name__}._get_focus` is deprecated, " + f"please use `{self.__class__.__name__}.focus` property", + DeprecationWarning, + stacklevel=3, + ) + if not self.contents: + return None + return self.contents[self.focus_position][0] + def get_focus(self) -> Widget | None: """ Return the widget in focus, for backwards compatibility. You may @@ -1842,7 +1956,9 @@ class Pile(Widget, WidgetContainerMixin, WidgetContainerListContentsMixin): PendingDeprecationWarning, stacklevel=2, ) - return self.focus + if not self.contents: + return None + return self.contents[self.focus_position][0] def set_focus(self, item: Widget | int) -> None: warnings.warn( @@ -1851,7 +1967,14 @@ class Pile(Widget, WidgetContainerMixin, WidgetContainerListContentsMixin): PendingDeprecationWarning, stacklevel=2, ) - self.focus = item + if isinstance(item, int): + self.focus_position = item + return + for i, (w, options) in enumerate(self.contents): + if item == w: + self.focus_position = i + return + raise ValueError(f"Widget not found in Pile contents: {item!r}") @property def focus_item(self): @@ -1899,6 +2022,36 @@ class Pile(Widget, WidgetContainerMixin, WidgetContainerListContentsMixin): raise IndexError(f"No Pile child widget at position {position}") self.contents.focus = position + def _get_focus_position(self) -> int | None: + warnings.warn( + f"method `{self.__class__.__name__}._get_focus_position` is deprecated, " + f"please use `{self.__class__.__name__}.focus_position` property", + DeprecationWarning, + stacklevel=3, + ) + if not self.contents: + raise IndexError("No focus_position, Pile is empty") + return self.contents.focus + + def _set_focus_position(self, position: int) -> None: + """ + Set the widget in focus. + + position -- index of child widget to be made focus + """ + warnings.warn( + f"method `{self.__class__.__name__}._set_focus_position` is deprecated, " + f"please use `{self.__class__.__name__}.focus_position` property", + DeprecationWarning, + stacklevel=3, + ) + try: + if position < 0 or position >= len(self.contents): + raise IndexError + except (TypeError, IndexError): + raise IndexError(f"No Pile child widget at position {position}") + self.contents.focus = position + def get_pref_col(self, size): """Return the preferred column for the cursor, or None.""" if not self.selectable(): @@ -2522,6 +2675,17 @@ class Columns(Widget, WidgetContainerMixin, WidgetContainerListContentsMixin): return None return self.contents[self.focus_position][0] + def _get_focus(self) -> Widget: + warnings.warn( + f"method `{self.__class__.__name__}._get_focus` is deprecated, " + f"please use `{self.__class__.__name__}.focus` property", + DeprecationWarning, + stacklevel=3, + ) + if not self.contents: + return None + return self.contents[self.focus_position][0] + def get_focus(self): """ Return the widget in focus, for backwards compatibility. @@ -2535,7 +2699,9 @@ class Columns(Widget, WidgetContainerMixin, WidgetContainerListContentsMixin): PendingDeprecationWarning, stacklevel=2, ) - return self.focus + if not self.contents: + return None + return self.contents[self.focus_position][0] @property def focus_position(self) -> int | None: @@ -2543,7 +2709,7 @@ class Columns(Widget, WidgetContainerMixin, WidgetContainerListContentsMixin): index of child widget in focus. Raises :exc:`IndexError` if read when Columns is empty, or when set to an invalid index. """ - if not self.widget_list: + if not self.contents: raise IndexError("No focus_position, Columns is empty") return self.contents.focus @@ -2561,6 +2727,36 @@ class Columns(Widget, WidgetContainerMixin, WidgetContainerListContentsMixin): raise IndexError(f"No Columns child widget at position {position}") self.contents.focus = position + def _get_focus_position(self) -> int | None: + warnings.warn( + f"method `{self.__class__.__name__}._get_focus_position` is deprecated, " + f"please use `{self.__class__.__name__}.focus_position` property", + DeprecationWarning, + stacklevel=3, + ) + if not self.contents: + raise IndexError("No focus_position, Columns is empty") + return self.contents.focus + + def _set_focus_position(self, position: int) -> None: + """ + Set the widget in focus. + + position -- index of child widget to be made focus + """ + warnings.warn( + f"method `{self.__class__.__name__}._set_focus_position` is deprecated, " + f"please use `{self.__class__.__name__}.focus_position` property", + DeprecationWarning, + stacklevel=3, + ) + try: + if position < 0 or position >= len(self.contents): + raise IndexError + except (TypeError, IndexError): + raise IndexError(f"No Columns child widget at position {position}") + self.contents.focus = position + @property def focus_col(self): """ diff --git a/urwid/listbox.py b/urwid/listbox.py index 47b8376..ddb9bf0 100644 --- a/urwid/listbox.py +++ b/urwid/listbox.py @@ -133,9 +133,9 @@ class SimpleListWalker(MonitoredList, ListWalker): f"Method `{self.__class__.__name__}._get_contents` is deprecated, " f"please use property`{self.__class__.__name__}.contents`", DeprecationWarning, - stacklevel=2, + stacklevel=3, ) - return self.contents + return self def _modified(self): if self.focus >= len(self): @@ -332,7 +332,7 @@ class ListBox(Widget, WidgetContainerMixin): f"Method `{self.__class__.__name__}._get_body` is deprecated, " f"please use property `{self.__class__.__name__}.body`", DeprecationWarning, - stacklevel=2, + stacklevel=3, ) return self.body @@ -341,7 +341,7 @@ class ListBox(Widget, WidgetContainerMixin): f"Method `{self.__class__.__name__}._set_body` is deprecated, " f"please use property `{self.__class__.__name__}.body`", DeprecationWarning, - stacklevel=2, + stacklevel=3, ) self.body = body @@ -628,6 +628,13 @@ class ListBox(Widget, WidgetContainerMixin): compatibility. You may also use the new standard container properties :attr:`focus` and :attr:`focus_position` to read these values. """ + warnings.warn( + "only for backwards compatibility." + "You may also use the new standard container property `focus` to get the focus " + "and property `focus_position` to read these values.", + PendingDeprecationWarning, + stacklevel=2, + ) return self._body.get_focus() @property @@ -639,6 +646,15 @@ class ListBox(Widget, WidgetContainerMixin): """ return self._body.get_focus()[0] + def _get_focus(self) -> Widget: + warnings.warn( + f"method `{self.__class__.__name__}._get_focus` is deprecated, " + f"please use `{self.__class__.__name__}.focus` property", + DeprecationWarning, + stacklevel=3, + ) + return self.focus + def _get_focus_position(self): """ Return the list walker position of the widget in focus. The type diff --git a/urwid/monitored_list.py b/urwid/monitored_list.py index 1a91495..5c390b5 100755 --- a/urwid/monitored_list.py +++ b/urwid/monitored_list.py @@ -190,7 +190,7 @@ class MonitoredFocusList(MonitoredList): f"method `{self.__class__.__name__}._get_focus` is deprecated, " f"please use `{self.__class__.__name__}.focus` property", DeprecationWarning, - stacklevel=2, + stacklevel=3, ) return self.focus @@ -199,7 +199,7 @@ class MonitoredFocusList(MonitoredList): f"method `{self.__class__.__name__}._set_focus` is deprecated, " f"please use `{self.__class__.__name__}.focus` property", DeprecationWarning, - stacklevel=2, + stacklevel=3, ) self.focus = index diff --git a/urwid/tests/test_vterm.py b/urwid/tests/test_vterm.py index c75136a..44d5d0c 100644 --- a/urwid/tests/test_vterm.py +++ b/urwid/tests/test_vterm.py @@ -26,6 +26,7 @@ import sys import typing import unittest from itertools import dropwhile +from time import sleep from urwid import Widget, signals, vterm from urwid.decoration import BoxAdapter @@ -60,6 +61,7 @@ class DummyCommand: def write(self, data: bytes) -> None: os.write(self.writer, data) + sleep(0.025) def quit(self) -> None: self.write(self.QUITSTRING) |