[Assorted-commits] SF.net SVN: assorted:[981] python-commons/trunk/src/commons/misc.py
Brought to you by:
yangzhang
From: <yan...@us...> - 2008-10-02 19:14:45
|
Revision: 981 http://assorted.svn.sourceforge.net/assorted/?rev=981&view=rev Author: yangzhang Date: 2008-10-02 19:14:28 +0000 (Thu, 02 Oct 2008) Log Message: ----------- added wrap_color, force for TerminalController, days 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-10-02 19:13:44 UTC (rev 980) +++ python-commons/trunk/src/commons/misc.py 2008-10-02 19:14:28 UTC (rev 981) @@ -8,10 +8,12 @@ from contextlib import * from subprocess import CalledProcessError, PIPE, Popen from time import * +from datetime import timedelta +import sys, re -# TerminalController -from sys import stdout -from re import sub +def days(td): + """Returns the ceil(days in the timedelta L{td}).""" + return td.days + (1 if td - timedelta(days = td.days) > timedelta() else 0) def generate_bit_fields(count): """ @@ -156,7 +158,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, force=False): + def __init__(self, term_stream=sys.stdout, force=False): """ Create a `TerminalController` and initialize its attributes with appropriate values for the current terminal. @@ -209,7 +211,7 @@ # these, so strip them out. import curses cap = curses.tigetstr(cap_name) or '' - return sub(r'\$<\d+>[/*]?', '', cap) + return re.sub(r'\$<\d+>[/*]?', '', cap) def render(self, template): """ @@ -217,20 +219,24 @@ the corresponding terminal control string (if it's defined) or '' (if it's not). """ - return sub(r'\$\$|\${\w+}', self._render_sub, template) + return re.sub(r'\$\$|\${\w+}', self._render_sub, template) def _render_sub(self, match): s = match.group() 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) +def wrap_color(s, color): + """ + Wraps L{s} in L{color} (resets color to NORMAL at the end). + """ + return '${%s}%s${NORMAL}' % (s, color) + import unittest class color_test( unittest.TestCase ): This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |