Pylint and PEP8 fixes for color.py

Change-Id: I1a676e25957a7b5dd800d2585a2ec7fe75295668
This commit is contained in:
Anthony King 2015-03-28 21:10:17 +00:00
parent 9c76f67f13
commit bdf7ed2301

View File

@ -18,41 +18,43 @@ import sys
import pager import pager
COLORS = {None :-1, COLORS = {None: -1,
'normal' :-1, 'normal': -1,
'black' : 0, 'black': 0,
'red' : 1, 'red': 1,
'green' : 2, 'green': 2,
'yellow' : 3, 'yellow': 3,
'blue' : 4, 'blue': 4,
'magenta': 5, 'magenta': 5,
'cyan' : 6, 'cyan': 6,
'white' : 7} 'white': 7}
ATTRS = {None :-1, ATTRS = {None: -1,
'bold' : 1, 'bold': 1,
'dim' : 2, 'dim': 2,
'ul' : 4, 'ul': 4,
'blink' : 5, 'blink': 5,
'reverse': 7} 'reverse': 7}
RESET = "\033[m" # pylint: disable=W1401 RESET = "\033[m"
# backslash is not anomalous
def is_color(s): def is_color(s):
return s in COLORS return s in COLORS
def is_attr(s): def is_attr(s):
return s in ATTRS return s in ATTRS
def _Color(fg = None, bg = None, attr = None):
def _Color(fg=None, bg=None, attr=None):
fg = COLORS[fg] fg = COLORS[fg]
bg = COLORS[bg] bg = COLORS[bg]
attr = ATTRS[attr] attr = ATTRS[attr]
if attr >= 0 or fg >= 0 or bg >= 0: if attr >= 0 or fg >= 0 or bg >= 0:
need_sep = False need_sep = False
code = "\033[" #pylint: disable=W1401 code = "\033["
if attr >= 0: if attr >= 0:
code += chr(ord('0') + attr) code += chr(ord('0') + attr)
@ -71,7 +73,6 @@ def _Color(fg = None, bg = None, attr = None):
if bg >= 0: if bg >= 0:
if need_sep: if need_sep:
code += ';' code += ';'
need_sep = True
if bg < 8: if bg < 8:
code += '4%c' % (ord('0') + bg) code += '4%c' % (ord('0') + bg)
@ -82,9 +83,9 @@ def _Color(fg = None, bg = None, attr = None):
code = '' code = ''
return code return code
DEFAULT = None DEFAULT = None
def SetDefaultColoring(state): def SetDefaultColoring(state):
"""Set coloring behavior to |state|. """Set coloring behavior to |state|.
@ -145,6 +146,7 @@ class Coloring(object):
def printer(self, opt=None, fg=None, bg=None, attr=None): def printer(self, opt=None, fg=None, bg=None, attr=None):
s = self s = self
c = self.colorer(opt, fg, bg, attr) c = self.colorer(opt, fg, bg, attr)
def f(fmt, *args): def f(fmt, *args):
s._out.write(c(fmt, *args)) s._out.write(c(fmt, *args))
return f return f
@ -152,6 +154,7 @@ class Coloring(object):
def nofmt_printer(self, opt=None, fg=None, bg=None, attr=None): def nofmt_printer(self, opt=None, fg=None, bg=None, attr=None):
s = self s = self
c = self.nofmt_colorer(opt, fg, bg, attr) c = self.nofmt_colorer(opt, fg, bg, attr)
def f(fmt): def f(fmt):
s._out.write(c(fmt)) s._out.write(c(fmt))
return f return f
@ -159,11 +162,13 @@ class Coloring(object):
def colorer(self, opt=None, fg=None, bg=None, attr=None): def colorer(self, opt=None, fg=None, bg=None, attr=None):
if self._on: if self._on:
c = self._parse(opt, fg, bg, attr) c = self._parse(opt, fg, bg, attr)
def f(fmt, *args): def f(fmt, *args):
output = fmt % args output = fmt % args
return ''.join([c, output, RESET]) return ''.join([c, output, RESET])
return f return f
else: else:
def f(fmt, *args): def f(fmt, *args):
return fmt % args return fmt % args
return f return f
@ -171,6 +176,7 @@ class Coloring(object):
def nofmt_colorer(self, opt=None, fg=None, bg=None, attr=None): def nofmt_colorer(self, opt=None, fg=None, bg=None, attr=None):
if self._on: if self._on:
c = self._parse(opt, fg, bg, attr) c = self._parse(opt, fg, bg, attr)
def f(fmt): def f(fmt):
return ''.join([c, fmt, RESET]) return ''.join([c, fmt, RESET])
return f return f