[Assorted-commits] SF.net SVN: assorted:[978] python-commons/trunk/src/commons/misc.py
Brought to you by:
yangzhang
From: <yan...@us...> - 2008-10-02 18:47:27
|
Revision: 978 http://assorted.svn.sourceforge.net/assorted/?rev=978&view=rev Author: yangzhang Date: 2008-10-02 18:46:58 +0000 (Thu, 02 Oct 2008) Log Message: ----------- added a force option to TerminalController; added remove_colors; added unit test for colors Modified Paths: -------------- python-commons/trunk/src/commons/misc.py Modified: python-commons/trunk/src/commons/misc.py =================================================================== --- python-commons/trunk/src/commons/misc.py 2008-09-30 07:04:34 UTC (rev 977) +++ python-commons/trunk/src/commons/misc.py 2008-10-02 18:46:58 UTC (rev 978) @@ -156,7 +156,7 @@ _COLORS = """BLACK BLUE GREEN CYAN RED MAGENTA YELLOW WHITE""".split() _ANSICOLORS = "BLACK RED GREEN YELLOW BLUE MAGENTA CYAN WHITE".split() - def __init__(self, term_stream=stdout): + def __init__(self, term_stream=stdout, force=False): """ Create a `TerminalController` and initialize its attributes with appropriate values for the current terminal. @@ -169,7 +169,7 @@ except: return # If the stream isn't a tty, then assume it has no capabilities. - if not term_stream.isatty(): return + if not force and not term_stream.isatty(): return # Check the terminal type. If we fail, then assume that the # terminal has no capabilities. @@ -224,3 +224,22 @@ if s == '$$': return s else: return getattr(self, s[2:-1]) +import re + +remove_colors_pat = re.compile('\033\\[[0-9;]*m') +def remove_colors(s): + 'Removes ANSI escape codes (e.g. those for terminal colors).' + return remove_colors_pat.sub('', s) + +import unittest + +class color_test( unittest.TestCase ): + def test_round_trip( self ): + tc = TerminalController() + template = '${GREEN}green${BLUE}blue${NORMAL}normal' + rendered = tc.render( template ) + removed = remove_colors( rendered ) + self.assertEqual( removed, 'greenbluenormal' ) + +if __name__ == '__main__': + unittest.main() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |