diff options
author | Erik Rose <erik@mozilla.com> | 2013-09-09 23:06:59 -0400 |
---|---|---|
committer | Erik Rose <erik@mozilla.com> | 2013-09-09 23:06:59 -0400 |
commit | c3d7ccbe3b76b791f7611e3c82ab9e0d8175cf42 (patch) | |
tree | f32ec63847d7bf6e1ddf95fd6fcc8ba2430a86ca | |
parent | ffbe0244bc88e4365bd544bfc0056e3c5be5fdfd (diff) | |
parent | 8a5d9976160869dd00bd1d3943dee467d946f0cc (diff) | |
download | blessings-c3d7ccbe3b76b791f7611e3c82ab9e0d8175cf42.tar.gz |
Merge the property-ization of is_a_tty and does_styling, some PEP-8 fixes, and the removal of some unused imports. Close #37.
-rw-r--r-- | blessings/__init__.py | 54 |
1 files changed, 32 insertions, 22 deletions
diff --git a/blessings/__init__.py b/blessings/__init__.py index c03e105..d558907 100644 --- a/blessings/__init__.py +++ b/blessings/__init__.py @@ -1,15 +1,15 @@ -from collections import defaultdict from contextlib import contextmanager import curses -from curses import tigetstr, tigetnum, setupterm, tparm +from curses import setupterm, tigetnum, tigetstr, tparm from fcntl import ioctl + try: from io import UnsupportedOperation as IOUnsupportedOperation except ImportError: class IOUnsupportedOperation(Exception): """A dummy exception to take the place of Python 3's ``io.UnsupportedOperation`` in Python 2""" -import os + from os import isatty, environ from platform import python_version_tuple import struct @@ -42,13 +42,6 @@ class Terminal(object): around with the terminal; it's almost always needed when the terminal is and saves sticking lots of extra args on client functions in practice. - ``does_styling`` - Whether this ``Terminal`` attempts to emit capabilities. This is - influenced by ``is_a_tty`` and by the ``force_styling`` arg to the - constructor. You can examine this value to decide whether to draw - progress bars or other frippery. - ``is_a_tty`` - Whether ``stream`` appears to be a terminal. """ def __init__(self, kind=None, stream=None, force_styling=False): @@ -83,15 +76,15 @@ class Terminal(object): stream = sys.__stdout__ try: stream_descriptor = (stream.fileno() if hasattr(stream, 'fileno') - and callable(stream.fileno) + and callable(stream.fileno) else None) except IOUnsupportedOperation: stream_descriptor = None - self.is_a_tty = (stream_descriptor is not None and + self._is_a_tty = (stream_descriptor is not None and isatty(stream_descriptor)) - self.does_styling = ((self.is_a_tty or force_styling) and - force_styling is not None) + self._does_styling = ((self.is_a_tty or force_styling) and + force_styling is not None) # The desciptor to direct terminal initialization sequences to. # sys.__stdout__ seems to always have a descriptor of 1, even if output @@ -175,11 +168,28 @@ class Terminal(object): Return values are always Unicode. """ - resolution = self._resolve_formatter(attr) if self.does_styling else NullCallableString() + resolution = (self._resolve_formatter(attr) if self.does_styling + else NullCallableString()) setattr(self, attr, resolution) # Cache capability codes. return resolution @property + def does_styling(self): + """Whether attempt to emit capabilities + + This is influenced by the ``is_a_tty`` property and by the + ``force_styling`` argument to the constructor. You can examine + this value to decide whether to draw progress bars or other frippery. + + """ + return self._does_styling + + @property + def is_a_tty(self): + """Whether my ``stream`` appears to be associated with a terminal""" + return self._is_a_tty + + @property def height(self): """The height of the terminal in characters @@ -188,8 +198,8 @@ class Terminal(object): piping to things that eventually display on the terminal (like ``less -R``) work. If a stream representing a terminal was passed in, return the dimensions of that terminal. If there somehow is no controlling - terminal, return ``None``. (Thus, you should check that ``is_a_tty`` is - true before doing any math on the result.) + terminal, return ``None``. (Thus, you should check that the property + ``is_a_tty`` is true before doing any math on the result.) """ return self._height_and_width()[0] @@ -314,8 +324,8 @@ class Terminal(object): # access to it. colors = tigetnum('colors') # Returns -1 if no color support, -2 if no # such cap. - #self.__dict__['colors'] = ret # Cache it. It's not changing. (Doesn't - # work.) + # self.__dict__['colors'] = ret # Cache it. It's not changing. + # (Doesn't work.) return colors if colors >= 0 else 0 def _resolve_formatter(self, attr): @@ -506,9 +516,9 @@ class NullCallableString(unicode): # NullParametrizableString or NullFormattingString, to return, and # retire this one. return u'' - return args[0] # Should we force even strs in Python 2.x to be - # unicodes? No. How would I know what encoding to use - # to convert it? + return args[0] # Should we force even strs in Python 2.x to be + # unicodes? No. How would I know what encoding to use + # to convert it? def split_into_formatters(compound): |