Skip to content

Commit b998f65

Browse files
committed
Remove wrap and associated functions from roundup.support
The wrap function duplicates functionality that is already provided by the inbuilt textwrap module since python v2.3. The wrap function is only used in one place so lets just replace it with the equivalent in-built function.
1 parent f6e3fd3 commit b998f65

File tree

3 files changed

+3
-107
lines changed

3 files changed

+3
-107
lines changed

roundup/cgi/templating.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121

2222

2323
import cgi, urllib, re, os.path, mimetypes, csv
24-
import calendar, textwrap
24+
import calendar
25+
import textwrap
2526

2627
from roundup import hyperdb, date, support
2728
from roundup import i18n
@@ -1426,7 +1427,7 @@ def wrapped(self, escape=1, hyperlink=1):
14261427

14271428
if self._value is None:
14281429
return ''
1429-
s = support.wrap(str(self._value), width=80)
1430+
s = '\n'.join(textwrap.wrap(str(self._value), 80))
14301431
if escape:
14311432
s = cgi.escape(s)
14321433
if hyperlink:

roundup/support.py

Lines changed: 0 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -118,97 +118,4 @@ def display(self):
118118
sys.stdout.write(s + ' '*(75-len(s)) + '\r')
119119
sys.stdout.flush()
120120

121-
LEFT = 'left'
122-
LEFTN = 'left no strip'
123-
RIGHT = 'right'
124-
CENTER = 'center'
125-
126-
def align(line, width=70, alignment=LEFTN):
127-
''' Code from http://www.faqts.com/knowledge_base/view.phtml/aid/4476 '''
128-
if alignment == CENTER:
129-
line = line.strip()
130-
space = width - len(line)
131-
return ' '*(space/2) + line + ' '*(space/2 + space%2)
132-
elif alignment == RIGHT:
133-
line = line.rstrip()
134-
space = width - len(line)
135-
return ' '*space + line
136-
else:
137-
if alignment == LEFT:
138-
line = line.lstrip()
139-
space = width - len(line)
140-
return line + ' '*space
141-
142-
143-
def format_line(columns, positions, contents, spacer=' | ',
144-
collapse_whitespace=True, wsre=re.compile(r'\s+')):
145-
''' Fill up a single row with data from the contents '''
146-
l = []
147-
data = 0
148-
for i in range(len(columns)):
149-
width, alignment = columns[i]
150-
content = contents[i]
151-
col = ''
152-
while positions[i] < len(content):
153-
word = content[positions[i]]
154-
# if we hit a newline, honor it
155-
if '\n' in word:
156-
# chomp
157-
positions[i] += 1
158-
break
159-
160-
# make sure this word fits
161-
if col and len(word) + len(col) > width:
162-
break
163-
164-
# no whitespace at start-of-line
165-
if collapse_whitespace and wsre.match(word) and not col:
166-
# chomp
167-
positions[i] += 1
168-
continue
169-
170-
col += word
171-
# chomp
172-
positions[i] += 1
173-
if col:
174-
data = 1
175-
col = align(col, width, alignment)
176-
l.append(col)
177-
178-
if not data:
179-
return ''
180-
return spacer.join(l).rstrip()
181-
182-
183-
def format_columns(columns, contents, spacer=' | ', collapse_whitespace=True,
184-
splitre=re.compile(r'(\n|\r\n|\r|[ \t]+|\S+)')):
185-
''' Format the contents into columns, with 'spacing' between the
186-
columns
187-
'''
188-
assert len(columns) == len(contents), \
189-
'columns and contents must be same length'
190-
191-
# split the text into words, spaces/tabs and newlines
192-
for i in range(len(contents)):
193-
contents[i] = splitre.findall(contents[i])
194-
195-
# now process line by line
196-
l = []
197-
positions = [0]*len(contents)
198-
while 1:
199-
l.append(format_line(columns, positions, contents, spacer,
200-
collapse_whitespace))
201-
202-
# are we done?
203-
for i in range(len(contents)):
204-
if positions[i] < len(contents[i]):
205-
break
206-
else:
207-
break
208-
return '\n'.join(l)
209-
210-
def wrap(text, width=75, alignment=LEFTN):
211-
return format_columns(((width, alignment),), [text],
212-
collapse_whitespace=False)
213-
214121
# vim: set et sts=4 sw=4 :

test/test_textfmt.py

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)