Skip to content

Commit

Permalink
Pylint and PEP8 fixes for color.py
Browse files Browse the repository at this point in the history
Change-Id: I1a676e25957a7b5dd800d2585a2ec7fe75295668
  • Loading branch information
Anthony King committed Mar 28, 2015
1 parent 9c76f67 commit bdf7ed2
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions color.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,43 @@

import pager

COLORS = {None :-1,
'normal' :-1,
'black' : 0,
'red' : 1,
'green' : 2,
'yellow' : 3,
'blue' : 4,
COLORS = {None: -1,
'normal': -1,
'black': 0,
'red': 1,
'green': 2,
'yellow': 3,
'blue': 4,
'magenta': 5,
'cyan' : 6,
'white' : 7}

ATTRS = {None :-1,
'bold' : 1,
'dim' : 2,
'ul' : 4,
'blink' : 5,
'cyan': 6,
'white': 7}

ATTRS = {None: -1,
'bold': 1,
'dim': 2,
'ul': 4,
'blink': 5,
'reverse': 7}

RESET = "\033[m" # pylint: disable=W1401
# backslash is not anomalous
RESET = "\033[m"


def is_color(s):
return s in COLORS


def is_attr(s):
return s in ATTRS

def _Color(fg = None, bg = None, attr = None):

def _Color(fg=None, bg=None, attr=None):
fg = COLORS[fg]
bg = COLORS[bg]
attr = ATTRS[attr]

if attr >= 0 or fg >= 0 or bg >= 0:
need_sep = False
code = "\033[" #pylint: disable=W1401
code = "\033["

if attr >= 0:
code += chr(ord('0') + attr)
Expand All @@ -71,7 +73,6 @@ def _Color(fg = None, bg = None, attr = None):
if bg >= 0:
if need_sep:
code += ';'
need_sep = True

if bg < 8:
code += '4%c' % (ord('0') + bg)
Expand All @@ -82,9 +83,9 @@ def _Color(fg = None, bg = None, attr = None):
code = ''
return code


DEFAULT = None


def SetDefaultColoring(state):
"""Set coloring behavior to |state|.
Expand Down Expand Up @@ -145,32 +146,37 @@ def nl(self):
def printer(self, opt=None, fg=None, bg=None, attr=None):
s = self
c = self.colorer(opt, fg, bg, attr)

def f(fmt, *args):
s._out.write(c(fmt, *args))
return f

def nofmt_printer(self, opt=None, fg=None, bg=None, attr=None):
s = self
c = self.nofmt_colorer(opt, fg, bg, attr)

def f(fmt):
s._out.write(c(fmt))
return f

def colorer(self, opt=None, fg=None, bg=None, attr=None):
if self._on:
c = self._parse(opt, fg, bg, attr)

def f(fmt, *args):
output = fmt % args
return ''.join([c, output, RESET])
return f
else:

def f(fmt, *args):
return fmt % args
return f

def nofmt_colorer(self, opt=None, fg=None, bg=None, attr=None):
if self._on:
c = self._parse(opt, fg, bg, attr)

def f(fmt):
return ''.join([c, fmt, RESET])
return f
Expand Down

0 comments on commit bdf7ed2

Please sign in to comment.