summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Matusiak <numerodix@gmail.com>2021-05-05 13:40:55 +1000
committerMartin Matusiak <numerodix@gmail.com>2021-05-05 13:40:55 +1000
commit06a2777a888b48156c045cf30f009b0b1bb0322b (patch)
tree7a8fdde788197b6b38abebb914f843fc5e7ac9e2
parentde0d5bb0b0ec7062a0469f91bbd931cc143d75d4 (diff)
downloadansicolor-06a2777a888b48156c045cf30f009b0b1bb0322b.tar.gz
flake8 fixes
-rw-r--r--ansicolor/__init__.py32
-rw-r--r--ansicolor/ansicolor.py50
-rw-r--r--setup.cfg2
-rw-r--r--tests/test_colors.py23
4 files changed, 75 insertions, 32 deletions
diff --git a/ansicolor/__init__.py b/ansicolor/__init__.py
index 3396f72..5b384ed 100644
--- a/ansicolor/__init__.py
+++ b/ansicolor/__init__.py
@@ -1,6 +1,32 @@
from __future__ import absolute_import
-from ansicolor.ansicolor import * # noqa
+from ansicolor.ansicolor import black
+from ansicolor.ansicolor import blue
+from ansicolor.ansicolor import cyan
+from ansicolor.ansicolor import green
+from ansicolor.ansicolor import magenta
+from ansicolor.ansicolor import red
+from ansicolor.ansicolor import white
+from ansicolor.ansicolor import yellow
+
+from ansicolor.ansicolor import colorize
+from ansicolor.ansicolor import colorize_v2
+from ansicolor.ansicolor import get_code
+from ansicolor.ansicolor import get_code_v2
+from ansicolor.ansicolor import wrap_string
+
+from ansicolor.ansicolor import highlight_string
+from ansicolor.ansicolor import get_highlighter
+
+from ansicolor.ansicolor import strip_escapes
+from ansicolor.ansicolor import justify_formatted
+
+from ansicolor.ansicolor import colordiff
+from ansicolor.ansicolor import set_term_title
+from ansicolor.ansicolor import write_out
+from ansicolor.ansicolor import write_err
+
+from ansicolor.ansicolor import Colors
__all__ = [
@@ -14,8 +40,10 @@ __all__ = [
'yellow',
'colorize',
- 'wrap_string',
+ 'colorize_v2',
'get_code',
+ 'get_code_v2',
+ 'wrap_string',
'highlight_string',
'get_highlighter',
diff --git a/ansicolor/ansicolor.py b/ansicolor/ansicolor.py
index f7003bb..1ac7cec 100644
--- a/ansicolor/ansicolor.py
+++ b/ansicolor/ansicolor.py
@@ -62,7 +62,8 @@ class Colors(object):
for color in cls._colorlist:
yield color
-## Define Colors members
+
+# Define Colors members
Colors.new("Black")
Colors.new("Red")
Colors.new("Green")
@@ -73,7 +74,7 @@ Colors.new("Cyan")
Colors.new("White")
-## Define coloring shorthands
+# Define coloring shorthands
def make_func(color):
def f(s, bold=False, reverse=False):
return colorize(s, color, bold=bold, reverse=reverse)
@@ -87,11 +88,12 @@ def make_func(color):
""" % color.__name__.lower()
return f
+
for color in Colors.iter():
globals()[color.__name__.lower()] = make_func(color)
-## Define highlighting colors
+# Define highlighting colors
highlights = [
Colors.Green,
Colors.Yellow,
@@ -106,7 +108,7 @@ for (n, h) in enumerate(highlights):
highlight_map[n] = [color for color in Colors.iter() if h == color].pop()
-## Coloring functions
+# Coloring functions
def get_highlighter(colorid):
"""
Map a color index to a highlighting color.
@@ -143,7 +145,8 @@ def get_code(color, bold=False, reverse=False):
return '\033[' + fmt + color + 'm'
-def get_code_v2(color, bold=False, reverse=False, underline=False, blink=False):
+def get_code_v2(color, bold=False, reverse=False, underline=False,
+ blink=False):
"""
Returns the escape code for styling with the given color,
in bold and/or reverse.
@@ -160,13 +163,17 @@ def get_code_v2(color, bold=False, reverse=False, underline=False, blink=False):
return ""
fmt = '0'
- l = []
- if bold: l.append('1')
- if underline: l.append('4')
- if blink: l.append('5')
- if reverse: l.append('7')
- if len(l) != 0:
- fmt = ';'.join(l)
+ items = []
+ if bold:
+ items.append('1')
+ if underline:
+ items.append('4')
+ if blink:
+ items.append('5')
+ if reverse:
+ items.append('7')
+ if len(items) != 0:
+ fmt = ';'.join(items)
color = (color is not None) and ';3%s' % color.id or ''
@@ -199,8 +206,8 @@ def colorize(s, color, bold=False, reverse=False, start=None, end=None):
get_code(None),
after))
-def colorize_v2(s, color, bold=False, reverse=False, underline=False, blink=False,
- start=None, end=None):
+def colorize_v2(s, color, bold=False, reverse=False, underline=False,
+ blink=False, start=None, end=None):
"""
Colorize a string with the color given.
:param string s: The string to colorize.
@@ -223,10 +230,13 @@ def colorize_v2(s, color, bold=False, reverse=False, underline=False, blink=Fals
after = s[end:]
return ("%s%s%s%s%s" % (before,
- get_code_v2(color, bold=bold,
- underline=underline,
- blink=blink,
- reverse=reverse),
+ get_code_v2(
+ color,
+ bold=bold,
+ underline=underline,
+ blink=blink,
+ reverse=reverse
+ ),
between,
get_code_v2(None),
after))
@@ -389,11 +399,13 @@ def colordiff(x, y, color_x=Colors.Cyan, color_y=Colors.Green, debug=False):
def make_generator(it):
g = ((i, e) for (i, e) in enumerate(it))
+
def f():
try:
return next(g)
except StopIteration:
return (-1, None)
+
return f
def log(s):
@@ -480,7 +492,7 @@ def strip_escapes(s):
return re.sub('\033\[(?:(?:[0-9]*;)*)(?:[0-9]*m)', '', s)
-## Output functions
+# Output functions
def set_term_title(s):
"""
Set the title of a terminal window.
diff --git a/setup.cfg b/setup.cfg
index 4c21470..0373d04 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -1,6 +1,6 @@
[flake8]
exclude = .tox/,build/,docs/
-ignore = E301,E302,E303
+ignore = E301,E302,E303,W503,W605
[wheel]
universal = 1
diff --git a/tests/test_colors.py b/tests/test_colors.py
index d52e818..ba43a5f 100644
--- a/tests/test_colors.py
+++ b/tests/test_colors.py
@@ -82,12 +82,12 @@ def test_codes_v2():
assert '\033[1;4;31m' == get_code_v2(Colors.Red, bold=True, underline=True)
assert '\033[1;5;31m' == get_code_v2(Colors.Red, bold=True, blink=True)
assert '\033[1;7;31m' == get_code_v2(Colors.Red, bold=True, reverse=True)
-
+
assert '\033[4;5;31m' == get_code_v2(Colors.Red, underline=True, blink=True)
assert '\033[4;7;31m' == get_code_v2(Colors.Red, underline=True, reverse=True)
-
+
assert '\033[5;7;31m' == get_code_v2(Colors.Red, blink=True, reverse=True)
-
+
assert '\033[1;4;5;31m' == get_code_v2(Colors.Red, bold=True, underline=True, blink=True)
assert '\033[1;4;7;31m' == get_code_v2(Colors.Red, bold=True, underline=True, reverse=True)
assert '\033[1;5;7;31m' == get_code_v2(Colors.Red, bold=True, blink=True, reverse=True)
@@ -272,9 +272,9 @@ def test_strip_escapes():
)
assert strip_escapes(
- colorize("Hi", None, bold=True) +
- " there, " +
- colorize("stranger", Colors.Green, bold=True)
+ colorize("Hi", None, bold=True)
+ + " there, "
+ + colorize("stranger", Colors.Green, bold=True)
) == "Hi there, stranger"
@@ -282,8 +282,11 @@ def test_colordiff_different():
x, y = colordiff("hi bob", "hi there",
color_x=Colors.Red, color_y=Colors.Blue)
- fx = lambda s: red(s, reverse=True)
- fy = lambda s: blue(s, reverse=True)
+ def fx(s):
+ return red(s, reverse=True)
+
+ def fy(s):
+ return blue(s, reverse=True)
assert x == "hi " + fx("b") + fx("o") + fx("b")
assert y == "hi " + fy("t") + fy("h") + fy("e") + fy("r") + fy("e")
@@ -292,8 +295,8 @@ def test_colordiff_edited():
x, y = colordiff("hi bobby", "hi bob",
color_x=Colors.Red, color_y=Colors.Blue)
- fx = lambda s: red(s, reverse=True)
- fy = lambda s: blue(s, reverse=True)
+ def fx(s):
+ return red(s, reverse=True)
assert x == "hi bob" + fx("b") + fx("y")
assert y == "hi bob"