small refactoring

This commit is contained in:
Gabriel Falcão
2015-11-12 14:15:06 -05:00
parent c986da5465
commit 9e6fdf4793
3 changed files with 60 additions and 16 deletions

View File

@@ -31,11 +31,11 @@ class delimiters:
SUPPORTS_ANSI = False
ANSI_OK = ('TERM' in os.environ and os.environ['TERM'] == 'ANSI')
for handle in [sys.stdout, sys.stderr]:
if (hasattr(handle, "isatty") and handle.isatty()) or \
('TERM' in os.environ and os.environ['TERM'] == 'ANSI'):
if platform.system() == 'Windows' and not (
'TERM' in os.environ and os.environ['TERM'] == 'ANSI'):
if (hasattr(handle, "isatty") and handle.isatty()) or ANSI_OK:
if platform.system() == 'Windows' and not ANSI_OK:
SUPPORTS_ANSI = False
else:
SUPPORTS_ANSI = True
@@ -67,8 +67,8 @@ def wrapper_factory(delimiter):
delimiter_in, delimiter_out = delimiter
def wrap_escaped(middle_part):
din = "".join([r"[{0}]".format(d) for d in delimiter_in])
dout = "".join([r"[{0}]".format(d) for d in delimiter_out])
din = r"".join([r"\{0}".format(d) for d in delimiter_in])
dout = r"".join([r"\{0}".format(d) for d in delimiter_out])
return r"".join([din, middle_part, dout])
def wrap_normal(middle_part):
@@ -82,17 +82,17 @@ def translate_colors(string, delimiter):
for attr in re.findall(wrap_escaped("on[:](\w+)"), string):
string = string.replace(
wrap_normal(u"on:%s") % unicode(attr),
wrap_normal(u"on:" + unicode(attr)),
getattr(backcolors, attr)
)
for attr in re.findall(wrap_escaped("(\w+)"), string):
string = string.replace(
wrap_normal(u"%s") % unicode(attr),
getattr(forecolors, attr, wrap_normal("%s") % attr)
wrap_normal(attr),
getattr(forecolors, attr, wrap_normal(attr))
).replace(
wrap_normal(u"%s") % unicode(attr),
getattr(modifiers, attr, wrap_normal("%s") % attr)
wrap_normal(attr),
getattr(modifiers, attr, wrap_normal(attr))
)
return minify(string)
@@ -110,12 +110,12 @@ def ignore_colors(string, delimiter):
string = up_supress_regex.sub('\g<start>', 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):
string = (string
.replace(wrap_normal(u"%s") % unicode(attr), u"")
.replace(wrap_normal(u"%s") % unicode(attr), u""))
.replace(wrap_normal(attr), u"")
.replace(wrap_normal(attr), u""))
return string
@@ -124,6 +124,10 @@ class Writer(StringIO):
original = None
translate = True
def __init__(self, delimiter=delimiters.DEFAULT):
self.delimiter = delimiter
StringIO.__init__(self)
def write(self, string):
f = self.translate and translate_colors or ignore_colors
self.original.write(f(string, self.delimiter))

View File

@@ -20,6 +20,7 @@ from nose.tools import with_setup, assert_equals
import couleur
def prepare_stderr():
if isinstance(sys.stderr, StringIO):
del sys.stderr
@@ -27,12 +28,14 @@ def prepare_stderr():
std = StringIO()
sys.stderr = std
def assert_stderr(expected):
string = sys.stderr.getvalue()
sys.stderr.seek(0)
sys.stderr.truncate()
assert_equals(string, expected)
@with_setup(prepare_stderr)
def test_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")
assert_stderr('#{black}should not be translated\n')
@with_setup(prepare_stderr)
def test_output_black_on_white_foreground():
"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")
assert_stderr('#{black}should not be translated\n')
@with_setup(prepare_stderr)
def test_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")
assert_stderr('#{black}should not be translated\n')
@with_setup(prepare_stderr)
def test_output_green_and_red_on_white_foreground():
"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")
assert_stderr('#{black}should not be translated\n')
@with_setup(prepare_stderr)
def test_errput_stderr_ignoring_errput():
"STDERR filter errput: ignoring output"
def test_output_stderr_ignoring_output():
"STDERR filter output: ignoring output"
couleur.proxy(sys.stderr).enable()
couleur.proxy(sys.stderr).ignore()
@@ -92,6 +99,7 @@ def test_errput_stderr_ignoring_errput():
sys.stderr.write("#{black}should not be translated\n")
assert_stderr('#{black}should not be translated\n')
def test_integration_with_stderr():
"STDERR filter integration"
@@ -100,3 +108,19 @@ def test_integration_with_stderr():
assert sys.stderr is not sys.__stderr__
couleur.proxy(sys.stderr).disable()
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')

View File

@@ -120,3 +120,19 @@ def test_output_different_delimiters():
couleur.proxy(sys.stdout).disable()
print "[black]should not be translated"
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')