small refactoring
This commit is contained in:
@@ -31,11 +31,11 @@ class delimiters:
|
|||||||
|
|
||||||
|
|
||||||
SUPPORTS_ANSI = False
|
SUPPORTS_ANSI = False
|
||||||
|
ANSI_OK = ('TERM' in os.environ and os.environ['TERM'] == 'ANSI')
|
||||||
|
|
||||||
for handle in [sys.stdout, sys.stderr]:
|
for handle in [sys.stdout, sys.stderr]:
|
||||||
if (hasattr(handle, "isatty") and handle.isatty()) or \
|
if (hasattr(handle, "isatty") and handle.isatty()) or ANSI_OK:
|
||||||
('TERM' in os.environ and os.environ['TERM'] == 'ANSI'):
|
if platform.system() == 'Windows' and not ANSI_OK:
|
||||||
if platform.system() == 'Windows' and not (
|
|
||||||
'TERM' in os.environ and os.environ['TERM'] == 'ANSI'):
|
|
||||||
SUPPORTS_ANSI = False
|
SUPPORTS_ANSI = False
|
||||||
else:
|
else:
|
||||||
SUPPORTS_ANSI = True
|
SUPPORTS_ANSI = True
|
||||||
@@ -67,8 +67,8 @@ def wrapper_factory(delimiter):
|
|||||||
delimiter_in, delimiter_out = delimiter
|
delimiter_in, delimiter_out = delimiter
|
||||||
|
|
||||||
def wrap_escaped(middle_part):
|
def wrap_escaped(middle_part):
|
||||||
din = "".join([r"[{0}]".format(d) for d in delimiter_in])
|
din = r"".join([r"\{0}".format(d) for d in delimiter_in])
|
||||||
dout = "".join([r"[{0}]".format(d) for d in delimiter_out])
|
dout = r"".join([r"\{0}".format(d) for d in delimiter_out])
|
||||||
return r"".join([din, middle_part, dout])
|
return r"".join([din, middle_part, dout])
|
||||||
|
|
||||||
def wrap_normal(middle_part):
|
def wrap_normal(middle_part):
|
||||||
@@ -82,17 +82,17 @@ def translate_colors(string, delimiter):
|
|||||||
|
|
||||||
for attr in re.findall(wrap_escaped("on[:](\w+)"), string):
|
for attr in re.findall(wrap_escaped("on[:](\w+)"), string):
|
||||||
string = string.replace(
|
string = string.replace(
|
||||||
wrap_normal(u"on:%s") % unicode(attr),
|
wrap_normal(u"on:" + unicode(attr)),
|
||||||
getattr(backcolors, attr)
|
getattr(backcolors, attr)
|
||||||
)
|
)
|
||||||
|
|
||||||
for attr in re.findall(wrap_escaped("(\w+)"), string):
|
for attr in re.findall(wrap_escaped("(\w+)"), string):
|
||||||
string = string.replace(
|
string = string.replace(
|
||||||
wrap_normal(u"%s") % unicode(attr),
|
wrap_normal(attr),
|
||||||
getattr(forecolors, attr, wrap_normal("%s") % attr)
|
getattr(forecolors, attr, wrap_normal(attr))
|
||||||
).replace(
|
).replace(
|
||||||
wrap_normal(u"%s") % unicode(attr),
|
wrap_normal(attr),
|
||||||
getattr(modifiers, attr, wrap_normal("%s") % attr)
|
getattr(modifiers, attr, wrap_normal(attr))
|
||||||
)
|
)
|
||||||
|
|
||||||
return minify(string)
|
return minify(string)
|
||||||
@@ -110,12 +110,12 @@ def ignore_colors(string, delimiter):
|
|||||||
string = up_supress_regex.sub('\g<start>', string)
|
string = up_supress_regex.sub('\g<start>', string)
|
||||||
|
|
||||||
for attr in re.findall(wrap_escaped("on[:](\w+)"), string):
|
for attr in re.findall(wrap_escaped("on[:](\w+)"), string):
|
||||||
string = string.replace(wrap_normal(u"on:%s") % unicode(attr), u"")
|
string = string.replace(wrap_normal(u"on:" + unicode(attr)), u"")
|
||||||
|
|
||||||
for attr in re.findall(wrap_escaped("(\w+)"), string):
|
for attr in re.findall(wrap_escaped("(\w+)"), string):
|
||||||
string = (string
|
string = (string
|
||||||
.replace(wrap_normal(u"%s") % unicode(attr), u"")
|
.replace(wrap_normal(attr), u"")
|
||||||
.replace(wrap_normal(u"%s") % unicode(attr), u""))
|
.replace(wrap_normal(attr), u""))
|
||||||
|
|
||||||
return string
|
return string
|
||||||
|
|
||||||
@@ -124,6 +124,10 @@ class Writer(StringIO):
|
|||||||
original = None
|
original = None
|
||||||
translate = True
|
translate = True
|
||||||
|
|
||||||
|
def __init__(self, delimiter=delimiters.DEFAULT):
|
||||||
|
self.delimiter = delimiter
|
||||||
|
StringIO.__init__(self)
|
||||||
|
|
||||||
def write(self, string):
|
def write(self, string):
|
||||||
f = self.translate and translate_colors or ignore_colors
|
f = self.translate and translate_colors or ignore_colors
|
||||||
self.original.write(f(string, self.delimiter))
|
self.original.write(f(string, self.delimiter))
|
||||||
|
@@ -20,6 +20,7 @@ from nose.tools import with_setup, assert_equals
|
|||||||
|
|
||||||
import couleur
|
import couleur
|
||||||
|
|
||||||
|
|
||||||
def prepare_stderr():
|
def prepare_stderr():
|
||||||
if isinstance(sys.stderr, StringIO):
|
if isinstance(sys.stderr, StringIO):
|
||||||
del sys.stderr
|
del sys.stderr
|
||||||
@@ -27,12 +28,14 @@ def prepare_stderr():
|
|||||||
std = StringIO()
|
std = StringIO()
|
||||||
sys.stderr = std
|
sys.stderr = std
|
||||||
|
|
||||||
|
|
||||||
def assert_stderr(expected):
|
def assert_stderr(expected):
|
||||||
string = sys.stderr.getvalue()
|
string = sys.stderr.getvalue()
|
||||||
sys.stderr.seek(0)
|
sys.stderr.seek(0)
|
||||||
sys.stderr.truncate()
|
sys.stderr.truncate()
|
||||||
assert_equals(string, expected)
|
assert_equals(string, expected)
|
||||||
|
|
||||||
|
|
||||||
@with_setup(prepare_stderr)
|
@with_setup(prepare_stderr)
|
||||||
def test_output_black_foreground():
|
def test_output_black_foreground():
|
||||||
"STDERR filter output: black foreground"
|
"STDERR filter output: black foreground"
|
||||||
@@ -44,6 +47,7 @@ def test_output_black_foreground():
|
|||||||
sys.stderr.write("#{black}should not be translated\n")
|
sys.stderr.write("#{black}should not be translated\n")
|
||||||
assert_stderr('#{black}should not be translated\n')
|
assert_stderr('#{black}should not be translated\n')
|
||||||
|
|
||||||
|
|
||||||
@with_setup(prepare_stderr)
|
@with_setup(prepare_stderr)
|
||||||
def test_output_black_on_white_foreground():
|
def test_output_black_on_white_foreground():
|
||||||
"STDERR filter output: black foreground on white background"
|
"STDERR filter output: black foreground on white background"
|
||||||
@@ -55,6 +59,7 @@ def test_output_black_on_white_foreground():
|
|||||||
sys.stderr.write("#{black}should not be translated\n")
|
sys.stderr.write("#{black}should not be translated\n")
|
||||||
assert_stderr('#{black}should not be translated\n')
|
assert_stderr('#{black}should not be translated\n')
|
||||||
|
|
||||||
|
|
||||||
@with_setup(prepare_stderr)
|
@with_setup(prepare_stderr)
|
||||||
def test_output_green_foreground():
|
def test_output_green_foreground():
|
||||||
"STDERR filter output: green foreground"
|
"STDERR filter output: green foreground"
|
||||||
@@ -66,6 +71,7 @@ def test_output_green_foreground():
|
|||||||
sys.stderr.write("#{black}should not be translated\n")
|
sys.stderr.write("#{black}should not be translated\n")
|
||||||
assert_stderr('#{black}should not be translated\n')
|
assert_stderr('#{black}should not be translated\n')
|
||||||
|
|
||||||
|
|
||||||
@with_setup(prepare_stderr)
|
@with_setup(prepare_stderr)
|
||||||
def test_output_green_and_red_on_white_foreground():
|
def test_output_green_and_red_on_white_foreground():
|
||||||
"STDERR filter output: green foreground and white on red background"
|
"STDERR filter output: green foreground and white on red background"
|
||||||
@@ -77,9 +83,10 @@ def test_output_green_and_red_on_white_foreground():
|
|||||||
sys.stderr.write("#{black}should not be translated\n")
|
sys.stderr.write("#{black}should not be translated\n")
|
||||||
assert_stderr('#{black}should not be translated\n')
|
assert_stderr('#{black}should not be translated\n')
|
||||||
|
|
||||||
|
|
||||||
@with_setup(prepare_stderr)
|
@with_setup(prepare_stderr)
|
||||||
def test_errput_stderr_ignoring_errput():
|
def test_output_stderr_ignoring_output():
|
||||||
"STDERR filter errput: ignoring output"
|
"STDERR filter output: ignoring output"
|
||||||
|
|
||||||
couleur.proxy(sys.stderr).enable()
|
couleur.proxy(sys.stderr).enable()
|
||||||
couleur.proxy(sys.stderr).ignore()
|
couleur.proxy(sys.stderr).ignore()
|
||||||
@@ -92,6 +99,7 @@ def test_errput_stderr_ignoring_errput():
|
|||||||
sys.stderr.write("#{black}should not be translated\n")
|
sys.stderr.write("#{black}should not be translated\n")
|
||||||
assert_stderr('#{black}should not be translated\n')
|
assert_stderr('#{black}should not be translated\n')
|
||||||
|
|
||||||
|
|
||||||
def test_integration_with_stderr():
|
def test_integration_with_stderr():
|
||||||
"STDERR filter integration"
|
"STDERR filter integration"
|
||||||
|
|
||||||
@@ -100,3 +108,19 @@ def test_integration_with_stderr():
|
|||||||
assert sys.stderr is not sys.__stderr__
|
assert sys.stderr is not sys.__stderr__
|
||||||
couleur.proxy(sys.stderr).disable()
|
couleur.proxy(sys.stderr).disable()
|
||||||
assert sys.stderr is sys.__stderr__
|
assert sys.stderr is sys.__stderr__
|
||||||
|
|
||||||
|
|
||||||
|
@with_setup(prepare_stderr)
|
||||||
|
def test_output_stderr_ignoring_output_square_brackets():
|
||||||
|
"STDERR filter output: ignoring output"
|
||||||
|
|
||||||
|
couleur.proxy(sys.stderr, delimiter=couleur.delimiters.SQUARE_BRACKETS).enable()
|
||||||
|
couleur.proxy(sys.stderr, delimiter=couleur.delimiters.SQUARE_BRACKETS).ignore()
|
||||||
|
sys.stderr.write("[green]Hello [white][on:blue]World![reset]\n")
|
||||||
|
assert_stderr('Hello World!\n')
|
||||||
|
couleur.proxy(sys.stderr, delimiter=couleur.delimiters.SQUARE_BRACKETS).enable()
|
||||||
|
sys.stderr.write("[green]Hi There!\n")
|
||||||
|
assert_stderr('\033[32mHi There!\n')
|
||||||
|
couleur.proxy(sys.stderr, delimiter=couleur.delimiters.SQUARE_BRACKETS).disable()
|
||||||
|
sys.stderr.write("[black]should not be translated\n")
|
||||||
|
assert_stderr('[black]should not be translated\n')
|
||||||
|
@@ -120,3 +120,19 @@ def test_output_different_delimiters():
|
|||||||
couleur.proxy(sys.stdout).disable()
|
couleur.proxy(sys.stdout).disable()
|
||||||
print "[black]should not be translated"
|
print "[black]should not be translated"
|
||||||
assert_stdout('[black]should not be translated\n')
|
assert_stdout('[black]should not be translated\n')
|
||||||
|
|
||||||
|
|
||||||
|
@with_setup(prepare_stdout)
|
||||||
|
def test_output_stdout_ignoring_output_square_brackets():
|
||||||
|
"STDOUT filter output: ignoring output"
|
||||||
|
|
||||||
|
couleur.proxy(sys.stdout, delimiter=couleur.delimiters.SQUARE_BRACKETS).enable()
|
||||||
|
couleur.proxy(sys.stdout, delimiter=couleur.delimiters.SQUARE_BRACKETS).ignore()
|
||||||
|
sys.stdout.write("[green]Hello [white][on:blue]World![reset]\n")
|
||||||
|
assert_stdout('Hello World!\n')
|
||||||
|
couleur.proxy(sys.stdout, delimiter=couleur.delimiters.SQUARE_BRACKETS).enable()
|
||||||
|
sys.stdout.write("[green]Hi There!\n")
|
||||||
|
assert_stdout('\033[32mHi There!\n')
|
||||||
|
couleur.proxy(sys.stdout, delimiter=couleur.delimiters.SQUARE_BRACKETS).disable()
|
||||||
|
sys.stdout.write("[black]should not be translated\n")
|
||||||
|
assert_stdout('[black]should not be translated\n')
|
||||||
|
Reference in New Issue
Block a user