2
# Twisted, the Framework of Your Internet
3
# Copyright (C) 2001 Matthew W. Lefkowitz
5
# This library is free software; you can redistribute it and/or
6
# modify it under the terms of version 2.1 of the GNU Lesser General Public
7
# License as published by the Free Software Foundation.
9
# This library is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
# Lesser General Public License for more details.
14
# You should have received a copy of the GNU Lesser General Public
15
# License along with this library; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
"""Miscellany of text-munging functions.
23
def stringyString(object, indentation=''):
24
"""Expansive string formatting for sequence types.
26
list.__str__ and dict.__str__ use repr() to display their
27
elements. This function also turns these sequence types
28
into strings, but uses str() on their elements instead.
30
Sequence elements are also displayed on seperate lines,
31
and nested sequences have nested indentation.
36
if type(object) is types.DictType:
38
for key, value in object.items():
39
value = stringyString(value, indentation + ' ')
40
if isMultiline(value):
41
if endsInNewline(value):
42
value = value[:-len('\n')]
43
sl.append("%s %s:\n%s" % (indentation, key, value))
45
# Oops. Will have to move that indentation.
46
sl.append("%s %s: %s" % (indentation, key,
47
value[len(indentation) + 3:]))
49
elif type(object) in (types.TupleType, types.ListType):
50
if type(object) is types.TupleType:
55
for element in object:
56
element = stringyString(element, indentation + ' ')
57
sl.append(string.rstrip(element) + ',')
59
sl[:] = map(lambda s, i=indentation: i+s,
60
string.split(str(object),'\n'))
63
sl.append(indentation)
66
sl[0] = indentation + braces[0] + sl[0][len(indentation) + 1:]
67
sl[-1] = sl[-1] + braces[-1]
69
s = string.join(sl, "\n")
71
if isMultiline(s) and not endsInNewline(s):
77
"""Returns True if this string has a newline in it."""
78
return (string.find(s, '\n') != -1)
81
"""Returns True if this string ends in a newline."""
82
return (s[-len('\n'):] == '\n')
84
def docstringLStrip(docstring):
85
"""Gets rid of unsightly lefthand docstring whitespace residue.
87
You'd think someone would have done this already, but apparently
94
docstring = string.replace(docstring, '\t', ' ' * 8)
95
lines = string.split(docstring,'\n')
98
for l in xrange(1,len(lines)):
100
if string.strip(line):
102
if line[leading] == ' ':
103
leading = leading + 1
109
outlines = lines[0:1]
110
for l in xrange(1,len(lines)):
111
outlines.append(lines[l][leading:])
113
return string.join(outlines, '\n')
115
def greedyWrap(inString, width=80):
116
"""Given a string and a column width, return a list of lines.
118
Caveat: I'm use a stupid greedy word-wrapping
119
algorythm. I won't put two spaces at the end
120
of a sentence. I don't do full justification.
121
And no, I've never even *heard* of hypenation.
126
inWords = string.split(inString)
131
column = column + len(inWords[ptr_line])
132
ptr_line = ptr_line + 1
136
# This single word is too long, it will be the whole line.
139
# We've gone too far, stop the line one word back.
140
ptr_line = ptr_line - 1
141
(l, inWords) = (inWords[0:ptr_line], inWords[ptr_line:])
142
outLines.append(string.join(l,' '))
146
elif not (len(inWords) > ptr_line):
147
# Clean up the last bit.
148
outLines.append(string.join(inWords, ' '))
158
wordWrap = greedyWrap