2003-07-04 14:24:44 +00:00
|
|
|
# -*- coding: iso-8859-1 -*-
|
2001-03-15 01:19:35 +00:00
|
|
|
"""various string utils"""
|
2004-01-03 14:59:33 +00:00
|
|
|
# Copyright (C) 2000-2004 Bastian Kleineidam
|
2001-03-15 01:19:35 +00:00
|
|
|
#
|
2001-05-23 21:20:44 +00:00
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
# (at your option) any later version.
|
2001-03-15 01:19:35 +00:00
|
|
|
#
|
2001-05-23 21:20:44 +00:00
|
|
|
# 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.
|
2001-03-15 01:19:35 +00:00
|
|
|
#
|
2001-05-23 21:20:44 +00:00
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
|
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
2000-05-01 11:35:08 +00:00
|
|
|
|
2004-07-20 14:50:00 +00:00
|
|
|
import re
|
|
|
|
|
import sys
|
2001-01-07 13:28:38 +00:00
|
|
|
|
2001-04-28 18:37:10 +00:00
|
|
|
markup_re = re.compile("<.*?>", re.DOTALL)
|
2000-02-26 10:24:46 +00:00
|
|
|
|
2002-11-24 19:53:37 +00:00
|
|
|
|
2002-05-04 13:27:02 +00:00
|
|
|
def stripQuotes (s):
|
2000-02-26 10:24:46 +00:00
|
|
|
"Strip optional quotes"
|
|
|
|
|
if len(s)<2:
|
|
|
|
|
return s
|
|
|
|
|
if s[0]=="\"" or s[0]=="'":
|
|
|
|
|
s = s[1:]
|
|
|
|
|
if s[-1]=="\"" or s[-1]=="'":
|
|
|
|
|
s = s[:-1]
|
|
|
|
|
return s
|
2000-04-24 22:07:48 +00:00
|
|
|
|
2002-11-24 19:53:37 +00:00
|
|
|
|
2002-05-04 13:27:02 +00:00
|
|
|
def indent (s, level):
|
2000-04-24 22:07:48 +00:00
|
|
|
"indent each line of s with <level> spaces"
|
2000-02-26 10:24:46 +00:00
|
|
|
return indentWith(s, level * " ")
|
2000-04-24 22:07:48 +00:00
|
|
|
|
2002-11-24 19:53:37 +00:00
|
|
|
|
2002-05-04 13:27:02 +00:00
|
|
|
def indentWith (s, indent):
|
2000-04-24 22:07:48 +00:00
|
|
|
"indent each line of s with given indent argument"
|
2000-02-26 10:24:46 +00:00
|
|
|
i = 0
|
|
|
|
|
while i < len(s):
|
|
|
|
|
if s[i]=="\n" and (i+1) < len(s):
|
|
|
|
|
s = s[0:(i+1)] + indent + s[(i+1):]
|
2001-01-22 23:02:54 +00:00
|
|
|
i += 1
|
2000-02-26 10:24:46 +00:00
|
|
|
return s
|
2000-04-24 22:07:48 +00:00
|
|
|
|
2002-11-24 19:53:37 +00:00
|
|
|
|
2002-05-04 13:27:02 +00:00
|
|
|
def blocktext (s, width):
|
2000-02-26 10:24:46 +00:00
|
|
|
"Adjust lines of s to be not wider than width"
|
|
|
|
|
# split into lines
|
2001-12-01 11:12:55 +00:00
|
|
|
s = s.split("\n")
|
2000-03-28 14:43:50 +00:00
|
|
|
s.reverse()
|
|
|
|
|
line = None
|
2000-02-26 10:24:46 +00:00
|
|
|
ret = ""
|
|
|
|
|
while len(s):
|
2000-03-28 14:43:50 +00:00
|
|
|
if line:
|
2001-01-22 23:02:54 +00:00
|
|
|
line += "\n"+s.pop()
|
2000-03-28 14:43:50 +00:00
|
|
|
else:
|
|
|
|
|
line = s.pop()
|
2000-02-26 10:24:46 +00:00
|
|
|
while len(line) > width:
|
|
|
|
|
i = getLastWordBoundary(line, width)
|
2001-12-01 11:12:55 +00:00
|
|
|
ret += line[0:i].strip() + "\n"
|
|
|
|
|
line = line[i:].strip()
|
2000-02-26 10:24:46 +00:00
|
|
|
return ret + line
|
2000-04-24 22:07:48 +00:00
|
|
|
|
2002-11-24 19:53:37 +00:00
|
|
|
|
2002-05-04 13:27:02 +00:00
|
|
|
def getLastWordBoundary (s, width):
|
2000-02-26 10:24:46 +00:00
|
|
|
"""Get maximal index i of a whitespace char in s with 0 < i < width.
|
|
|
|
|
Note: if s contains no whitespace this returns width-1"""
|
|
|
|
|
match = re.compile(".*\s").match(s[0:width])
|
|
|
|
|
if match:
|
|
|
|
|
return match.end()
|
|
|
|
|
return width-1
|
2000-04-24 22:07:48 +00:00
|
|
|
|
2002-11-24 19:53:37 +00:00
|
|
|
|
2002-05-04 13:27:02 +00:00
|
|
|
def getLineNumber (s, index):
|
2000-04-24 22:07:48 +00:00
|
|
|
"return the line number of str[index]"
|
2000-02-26 10:24:46 +00:00
|
|
|
i=0
|
|
|
|
|
if index<0: index=0
|
|
|
|
|
line=1
|
|
|
|
|
while i<index:
|
2002-05-04 13:27:02 +00:00
|
|
|
if s[i]=='\n':
|
2001-01-22 23:02:54 +00:00
|
|
|
line += 1
|
|
|
|
|
i += 1
|
2000-02-26 10:24:46 +00:00
|
|
|
return line
|
2000-04-24 22:07:48 +00:00
|
|
|
|
2002-11-24 19:53:37 +00:00
|
|
|
|
2002-05-04 13:27:02 +00:00
|
|
|
def paginate (text, lines=22):
|
2000-04-24 22:07:48 +00:00
|
|
|
"""print text in pages of lines size"""
|
2001-12-01 11:12:55 +00:00
|
|
|
textlines = text.split("\n")
|
2000-04-24 22:07:48 +00:00
|
|
|
curline = 1
|
|
|
|
|
for line in textlines:
|
|
|
|
|
print line
|
2001-01-22 23:02:54 +00:00
|
|
|
curline += 1
|
2000-04-24 22:07:48 +00:00
|
|
|
if curline >= lines and sys.stdin.isatty():
|
|
|
|
|
curline = 1
|
|
|
|
|
print "press return to continue..."
|
|
|
|
|
sys.stdin.read(1)
|
|
|
|
|
|
2002-11-24 19:53:37 +00:00
|
|
|
|
2002-05-04 13:27:02 +00:00
|
|
|
def remove_markup (s):
|
2001-04-28 18:37:10 +00:00
|
|
|
mo = markup_re.search(s)
|
|
|
|
|
while mo:
|
|
|
|
|
s = s[0:mo.start()] + s[mo.end():]
|
|
|
|
|
mo = markup_re.search(s)
|
|
|
|
|
return s
|
2002-11-24 19:53:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def unquote (s):
|
|
|
|
|
if not s:
|
|
|
|
|
return ''
|
2004-07-26 11:18:12 +00:00
|
|
|
return stripQuotes(s)
|
2002-11-24 19:53:37 +00:00
|
|
|
|
2002-12-06 18:16:46 +00:00
|
|
|
|
2002-12-06 17:57:57 +00:00
|
|
|
def strsize (b):
|
2002-12-06 18:16:46 +00:00
|
|
|
"""return human representation of bytes b"""
|
2002-12-06 17:57:57 +00:00
|
|
|
if b<1024:
|
|
|
|
|
return "%d Byte"%b
|
2002-12-06 18:16:46 +00:00
|
|
|
b /= 1024.0
|
2002-12-06 17:57:57 +00:00
|
|
|
if b<1024:
|
2002-12-06 18:16:46 +00:00
|
|
|
return "%.2f kB"%b
|
|
|
|
|
b /= 1024.0
|
2002-12-06 17:57:57 +00:00
|
|
|
if b<1024:
|
2002-12-06 18:16:46 +00:00
|
|
|
return "%.2f MB"%b
|
|
|
|
|
b /= 1024.0
|
|
|
|
|
return "%.2f GB"
|
2003-03-05 01:16:18 +00:00
|
|
|
|