implementing the file proxy
This commit is contained in:
parent
cead6007e9
commit
9bc6b72e3a
68
couleur.py
68
couleur.py
|
@ -20,41 +20,65 @@ import uuid
|
|||
__version__ = '0.2'
|
||||
from StringIO import StringIO
|
||||
|
||||
class ColorWriter(StringIO):
|
||||
def __init__(self, original):
|
||||
self.original = original
|
||||
self.old_write = original.write
|
||||
def translate_colors(string):
|
||||
for attr in filter(lambda x: not x.startswith("_"), dir(forecolors)):
|
||||
string = string.replace(
|
||||
"#{%s}" % attr,
|
||||
getattr(forecolors, attr)
|
||||
)
|
||||
string = string.replace(
|
||||
"#{on:%s}" % attr,
|
||||
getattr(backcolors, attr)
|
||||
)
|
||||
|
||||
def translate_colors(self, string):
|
||||
for attr in filter(lambda x: not x.startswith("_"), dir(forecolors)):
|
||||
string = string.replace(
|
||||
"#{%s}" % attr,
|
||||
getattr(forecolors, attr)
|
||||
)
|
||||
string = string.replace(
|
||||
"#{on:%s}" % attr,
|
||||
getattr(backcolors, attr)
|
||||
)
|
||||
string = string.replace("\n", "%s\n" % modifiers.reset)
|
||||
return string
|
||||
|
||||
string = string.replace("\n", "%s\n" % modifiers.reset)
|
||||
return string
|
||||
class StdOutMocker(StringIO):
|
||||
original = sys.__stdout__
|
||||
def write(self, string):
|
||||
sys.__stdout__.write(translate_colors(string))
|
||||
|
||||
def write(self, what):
|
||||
self.old_write(self.translate_colors(what))
|
||||
class StdErrMocker(StringIO):
|
||||
original = sys.__stderr__
|
||||
def write(self, string):
|
||||
sys.__stderr__.write(translate_colors(string))
|
||||
|
||||
class Proxy(object):
|
||||
def __init__(self, output):
|
||||
self.old_write = output.write
|
||||
|
||||
if output is sys.__stdout__:
|
||||
output = StdOutMocker()
|
||||
|
||||
elif output is sys.__stderr__:
|
||||
output = StdErrMocker()
|
||||
|
||||
self.output = output
|
||||
self.writer = ColorWriter(output)
|
||||
|
||||
|
||||
def enable(self):
|
||||
self.output.write = self.writer.write
|
||||
if isinstance(self.output, StdOutMocker):
|
||||
sys.stdout = self.output
|
||||
elif isinstance(self.output, StdErrMocker):
|
||||
sys.stderr = self.output
|
||||
else:
|
||||
self.output.write = lambda x: self.old_write(translate_colors(x))
|
||||
|
||||
def disable(self):
|
||||
self.output.write = self.writer.old_write
|
||||
if isinstance(self.output, StdOutMocker):
|
||||
sys.stdout = self.output.original
|
||||
elif isinstance(self.output, StdErrMocker):
|
||||
sys.stderr = self.output.original
|
||||
else:
|
||||
self.output.write = self.old_write
|
||||
|
||||
_proxy_registry = {}
|
||||
def proxy(output):
|
||||
return Proxy(output)
|
||||
if output not in _proxy_registry.keys():
|
||||
_proxy_registry[output] = Proxy(output)
|
||||
|
||||
return _proxy_registry[output]
|
||||
|
||||
def ansify(number):
|
||||
"""Wraps the given ansi code to a proper escaped python output
|
||||
|
|
|
@ -1,69 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# <Couleur - fancy shell output for python>
|
||||
# Copyright (C) <2010> Gabriel Falcão <gabriel@nacaolivre.org>
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Lesser General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
import sys
|
||||
from StringIO import StringIO
|
||||
from nose.tools import with_setup, assert_equals
|
||||
|
||||
import couleur
|
||||
|
||||
def prepare_stdout():
|
||||
if isinstance(sys.stdout, StringIO):
|
||||
del sys.stdout
|
||||
|
||||
std = StringIO()
|
||||
sys.stdout = std
|
||||
|
||||
def assert_stdout(expected):
|
||||
string = sys.stdout.getvalue()
|
||||
assert_equals(string, expected)
|
||||
|
||||
@with_setup(prepare_stdout)
|
||||
def test_output_black_foreground():
|
||||
"Test stdout filter output: black foreground"
|
||||
|
||||
couleur.proxy(sys.stdout).enable()
|
||||
print "#{black}Hello Black!"
|
||||
assert_stdout('\033[30mHello Black!\033[0m\n')
|
||||
couleur.proxy(sys.stdout).disable()
|
||||
|
||||
@with_setup(prepare_stdout)
|
||||
def test_output_black_on_white_foreground():
|
||||
"Test stdout filter output: black foreground on white background"
|
||||
|
||||
couleur.proxy(sys.stdout).enable()
|
||||
print "#{black}#{on:white}Hello Black!"
|
||||
assert_stdout('\033[30m\033[47mHello Black!\033[0m\n')
|
||||
couleur.proxy(sys.stdout).disable()
|
||||
|
||||
@with_setup(prepare_stdout)
|
||||
def test_output_green_foreground():
|
||||
"Test stdout filter output: green foreground"
|
||||
|
||||
couleur.proxy(sys.stdout).enable()
|
||||
print "#{green}Hello Green!"
|
||||
assert_stdout('\033[32mHello Green!\033[0m\n')
|
||||
couleur.proxy(sys.stdout).disable()
|
||||
|
||||
@with_setup(prepare_stdout)
|
||||
def test_output_green_and_red_on_white_foreground():
|
||||
"Test stdout filter output: green foreground and white on red background"
|
||||
|
||||
couleur.proxy(sys.stdout).enable()
|
||||
print "#{green}Hello #{white}#{on:red}Italy!"
|
||||
assert_stdout('\033[32mHello \033[37m\033[41mItaly!\033[0m\n')
|
||||
couleur.proxy(sys.stdout).disable()
|
||||
|
Loading…
Reference in New Issue