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.2 $

Author:

Logilab

Contact:

http://www.logilab.fr/ -- mailto:python-projects@logilab.org

Copyright:

2003-2005 LOGILAB S.A. (Paris, FRANCE)

Function Summary

str

_get_ansi_code(color, style)
return ansi escape code corresponding to color and style
    text formatting

str or unicode

normalize_text(text, line_len, indent)
normalize a text to display it with a maximum line size and optionaly arbitrary indentation.

str or unicode

normalize_paragraph(text, line_len, indent)
normalize a text to display it with a maximum line size and optionaly arbitrary indentation.

str or unicode

pretty_match(match, string, underline_char)
return a string with the match location underlined:

str or unicode

unquote(string)
remove optional quotes (simple or double) from the string

str or unicode

colorize_ansi(msg, color, style)
colorize message by wrapping it with ansi escape codes
    text manipulation
  searchall(rgx, data)
apply a regexp using "search" until no more match is found

str or unicode

get_csv(string, sep)
return a list of string in from a csv formatted line

Variable Summary
str __revision__ = '$Id: textutils.py,v 1.23 2005/02/08 17:2...

dict(str)

ANSI_COLORS: dictionary mapping color identifier to ANSI terminal code

str

ANSI_END: ANSI terminal code notifing the end of an ANSI escape sequence

str

ANSI_PREFIX: ANSI terminal code notifing the start of an ANSI escape sequence

str

ANSI_RESET: ANSI terminal code reseting format defined by a previous ANSI escape sequence

dict(str)

ANSI_STYLES: dictionary mapping style identifier to ANSI terminal code
SRE_Pattern _BLANKLINES_RGX = \r?\n\r?\n
SRE_Pattern _LINE_RGX = \r\n|\r+|\n
SRE_Pattern _NORM_SPACES_RGX = \s+

Function Details

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

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

Parameters:
text -

the input text to normalize


           (type=

str or unicode

)
line_len -

expected maximum line's length, default to 80


           (type=

int

)
indent -

optional string to use as indentation


           (type=

str or unicode

)
Returns:

the input text normalized to fit on lines with a maximized size inferior to line_len, and optionally prefixed by an indentation string


           (type=

str or unicode

)

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.

Parameters:
text -

the input text to normalize


           (type=

str or unicode

)
line_len -

expected maximum line's length, default to 80


           (type=

int

)
indent -

optional string to use as indentation


           (type=

str or unicode

)
Returns:

the input text normalized to fit on lines with a maximized size inferior to line_len, and optionally prefixed by an indentation string


           (type=

str or unicode

)

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
   ^^^^^
>>>
Parameters:
match -

object returned by re.match, re.search or re.finditer


           (type=

_sre.SRE_match

)
string -

the string on which the regular expression has been applied to obtain the match object


           (type=

str or unicode

)
underline_char -

character to use to underline the matched section, default to the carret '^'


           (type=

str or unicode

)
Returns:

the original string with an inserted line to underline the match location


           (type=

str or unicode

)

unquote(string)

remove optional quotes (simple or double) from the string

Parameters:
string -

an optionaly quoted string


           (type=

str or unicode

)
Returns:

the unquoted string (or the input string if it wasn't quoted)


           (type=

str or unicode

)

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

colorize message by wrapping it with ansi escape codes

Parameters:
msg -

the message string to colorize


           (type=

str or unicode

)
color -

the color identifier (see ANSI_COLORS for available values)


           (type=

str or None

)
style -

style string (see ANSI_COLORS for available values). To get several style effects at the same time, use a coma as separator.


           (type=

str or None

)
Returns:

the ansi escaped string


           (type=

str or unicode

)
Raises:
KeyError -

if an unexistant color or style identifier is given

searchall(rgx, data)

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

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

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']
>>>
Parameters:
string -

a csv line


           (type=

str or unicode

)
sep -

field separator, default to the comma (',')


           (type=

str or unicode

)
Returns:

the unquoted string (or the input string if it wasn't quoted)


           (type=

str or unicode

)

_get_ansi_code(color=None, style=None)

return ansi escape code corresponding to color and style

Parameters:
color -

the color identifier (see ANSI_COLORS for available values)


           (type=

str or None

)
style -

style string (see ANSI_COLORS for available values). To get several style effects at the same time, use a coma as separator.


           (type=

str or None

)
Returns:

the built escape code


           (type=

str

)
Raises:
KeyError -

if an unexistant color or style identifier is given


Variable Details

__revision__

Type:
str
Value:
'$Id: logilab.common.textutils-module.html,v 1.2 2005/04/19 14:39:10 fabioz Exp $'               

ANSI_COLORS

dictionary mapping color identifier to ANSI terminal code

Type:

dict(str)

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

ANSI_END

ANSI terminal code notifing the end of an ANSI escape sequence

Type:

str

Value:
'm'                                                                    

ANSI_PREFIX

ANSI terminal code notifing the start of an ANSI escape sequence

Type:

str

Value:
'\x1b['                                                                

ANSI_RESET

ANSI terminal code reseting format defined by a previous ANSI escape sequence

Type:

str

Value:
'\x1b[0m'                                                              

ANSI_STYLES

dictionary mapping style identifier to ANSI terminal code

Type:

dict(str)

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

_BLANKLINES_RGX

Type:
SRE_Pattern
Value:
\r?\n\r?\n                                                             

_LINE_RGX

Type:
SRE_Pattern
Value:
\r\n|\r+|\n                                                            

_NORM_SPACES_RGX

Type:
SRE_Pattern
Value:
\s+                                                                    

Generated by Epydoc 2.1 on Thu Apr 14 11:37:31 2005 http://epydoc.sf.net