diff options
Diffstat (limited to 'urwid/display_common.py')
-rwxr-xr-x | urwid/display_common.py | 38 |
1 files changed, 35 insertions, 3 deletions
diff --git a/urwid/display_common.py b/urwid/display_common.py index ca71e0b..c3b9c5d 100755 --- a/urwid/display_common.py +++ b/urwid/display_common.py @@ -26,7 +26,7 @@ try: except ImportError: pass # windows -from urwid.util import int_scale +from urwid.util import StoppingContext, int_scale from urwid import signals from urwid.compat import B, bytes3 @@ -718,12 +718,44 @@ class BaseScreen(object): started = property(lambda self: self._started) - def start(self): + def start(self, *args, **kwargs): + """Set up the screen. If the screen has already been started, does + nothing. + + May be used as a context manager, in which case :meth:`stop` will + automatically be called at the end of the block: + + with screen.start(): + ... + + You shouldn't override this method in a subclass; instead, override + :meth:`_start`. + """ + if not self._started: + self._start(*args, **kwargs) self._started = True + return StoppingContext(self) + + def _start(self): + pass def stop(self): + if self._started: + self._stop() self._started = False + def _stop(self): + pass + + def run_wrapper(self, fn, *args, **kwargs): + """Start the screen, call a function, then stop the screen. Extra + arguments are passed to `start`. + + Deprecated in favor of calling `start` as a context manager. + """ + with self.start(*args, **kwargs): + return fn() + def register_palette(self, palette): """Register a set of palette entries. @@ -750,7 +782,7 @@ class BaseScreen(object): raise ScreenError("Invalid register_palette entry: %s" % repr(item)) name, like_name = item - if not self._palette.has_key(like_name): + if like_name not in self._palette: raise ScreenError("palette entry '%s' doesn't exist"%like_name) self._palette[name] = self._palette[like_name] |