Package logilab :: Package common :: Module textutils
[show private | hide private]
[frames | no frames]

Module logilab.common.textutils

Some text manipulation utility functions.

:version:   $Revision: 1.25 $  
:author:    Logilab
:copyright: 2003-2005 LOGILAB S.A. (Paris, FRANCE)
:contact:   http://www.logilab.fr/ -- mailto:python-projects@logilab.org

:group text formatting: normalize_text, normalize_paragraph, pretty_match,unquote, colorize_ansi
:group text manipulation: searchall, get_csv
:sort: text formatting, text manipulation



:type ANSI_STYLES: dict(str)
:var ANSI_STYLES: dictionary mapping style identifier to ANSI terminal code

:type ANSI_COLORS: dict(str)
:var ANSI_COLORS: dictionary mapping color identifier to ANSI terminal code

:type ANSI_PREFIX: str
:var ANSI_PREFIX:
  ANSI terminal code notifing the start of an ANSI escape sequence
  
:type ANSI_END: str
:var ANSI_END:
  ANSI terminal code notifing the end of an ANSI escape sequence
  
:type ANSI_RESET: str
:var ANSI_RESET:
  ANSI terminal code reseting format defined by a previous ANSI escape sequence

Function Summary
  colorize_ansi(msg, color, style)
colorize message by wrapping it with ansi escape codes :type msg: str or unicode :param msg: the message string to colorize :type color: str or None :param color: the color identifier (see `ANSI_COLORS` for available values) :type style: str or None :param style: style string (see `ANSI_COLORS` for available values).
  get_csv(string, sep)
return a list of string in from a csv formatted line...
  normalize_paragraph(text, line_len, indent)
normalize a text to display it with a maximum line size and optionaly arbitrary indentation.
  normalize_text(text, line_len, indent)
normalize a text to display it with a maximum line size and optionally arbitrary indentation.
  pretty_match(match, string, underline_char)
return a string with the match location underlined:...
  searchall(rgx, data)
apply a regexp using "search" until no more match is found This function is deprecated, use re.finditer() instead.
  unquote(string)
remove optional quotes (simple or double) from the string...

Variable Summary
str __revision__ = '$Id: textutils.py,v 1.25 2005/09/06 08:5...
dict ANSI_COLORS = {'reset': '0', 'blue': '34', 'yellow': '33...
str ANSI_END = 'm'
str ANSI_PREFIX = '\x1b['
str ANSI_RESET = '\x1b[0m'
dict ANSI_STYLES = {'reset': '0', 'inverse': '7', 'bold': '1'...

Function Details

colorize_ansi(msg, color=None, style=None)

colorize message by wrapping it with ansi escape codes

:type msg: str or unicode
:param msg: the message string to colorize

:type color: str or None
:param color:
  the color identifier (see `ANSI_COLORS` for available values)

:type style: str or None
:param style:
  style string (see `ANSI_COLORS` for available values). To get
  several style effects at the same time, use a coma as separator.

:raise KeyError: if an unexistant color or style identifier is given

:rtype: str or unicode
:return: the ansi escaped string

get_csv(string, sep=',')

return a list of string in from a csv formatted line

>>> get_csv('a, b, c   ,  4')
['a', 'b', 'c', '4']
>>> get_csv('a')
['a']
>>>

:type string: str or unicode
:param string: a csv line

:type sep: str or unicode
:param sep: field separator, default to the comma (',')

:rtype: str or unicode
:return: the unquoted string (or the input string if it wasn't quoted)

normalize_paragraph(text, line_len=80, indent='')

normalize a text to display it with a maximum line size and
optionaly arbitrary indentation. Line jumps are normalized. The
indentation string may be used top insert a comment mark for
instance.


:type text: str or unicode
:param text: the input text to normalize

:type line_len: int
:param line_len: expected maximum line's length, default to 80

:type indent: str or unicode
:param indent: optional string to use as indentation

:rtype: str or unicode
:return:
  the input text normalized to fit on lines with a maximized size
  inferior to `line_len`, and optionally prefixed by an
  indentation string

normalize_text(text, line_len=80, indent='')

normalize a text to display it with a maximum line size and
optionally arbitrary indentation. Line jumps are normalized but blank
lines are kept. The indentation string may be used to insert a
comment (#) or a quoting (>) mark  for instance.

:type text: str or unicode
:param text: the input text to normalize

:type line_len: int
:param line_len: expected maximum line's length, default to 80

:type indent: str or unicode
:param indent: optional string to use as indentation

:rtype: str or unicode
:return:
  the input text normalized to fit on lines with a maximized size
  inferior to `line_len`, and optionally prefixed by an
  indentation string

pretty_match(match, string, underline_char='^')

return a string with the match location underlined:

>>> import re
>>> print pretty_match(re.search('mange', 'il mange du bacon'), 'il mange du bacon')
il mange du bacon
   ^^^^^
>>>

:type match: _sre.SRE_match
:param match: object returned by re.match, re.search or re.finditer

:type string: str or unicode
:param string:
  the string on which the regular expression has been applied to
  obtain the `match` object

:type underline_char: str or unicode
:param underline_char:
  character to use to underline the matched section, default to the
  carret '^'

:rtype: str or unicode
:return:
  the original string with an inserted line to underline the match
  location

searchall(rgx, data)

apply a regexp using "search" until no more match is found

This function is deprecated, use re.finditer() instead.

unquote(string)

remove optional quotes (simple or double) from the string

:type string: str or unicode
:param string: an optionaly quoted string

:rtype: str or unicode
:return: the unquoted string (or the input string if it wasn't quoted)

Variable Details

__revision__

Type:
str
Value:
'$Id: textutils.py,v 1.25 2005/09/06 08:51:01 alf Exp $'               

ANSI_COLORS

Type:
dict
Value:
{'black': '30',
 'blue': '34',
 'cyan': '36',
 'green': '32',
 'magenta': '35',
 'red': '31',
 'reset': '0',
 'white': '37',
...                                                                    

ANSI_END

Type:
str
Value:
'm'                                                                    

ANSI_PREFIX

Type:
str
Value:
'\x1b['                                                                

ANSI_RESET

Type:
str
Value:
'\x1b[0m'                                                              

ANSI_STYLES

Type:
dict
Value:
{'blink': '5',
 'bold': '1',
 'inverse': '7',
 'italic': '3',
 'reset': '0',
 'strike': '9',
 'underline': '4'}                                                     

Generated by Epydoc 2.1 on Fri Jan 6 10:52:59 2006 http://epydoc.sf.net