1 """Generic output formatting.
3 Formatter objects transform an abstract flow of formatting events into
4 specific output events on writer objects. Formatters manage several stack
5 structures to allow various properties of a writer object to be changed and
6 restored; writers need not be able to handle relative changes nor any sort
7 of ``change back'' operation. Specific writer properties which may be
8 controlled via formatter objects are horizontal alignment, font, and left
9 margin indentations. A mechanism is provided which supports providing
10 arbitrary, non-exclusive style settings to a writer as well. Additional
11 interfaces facilitate formatting events which are not reversible, such as
14 Writer objects encapsulate device interfaces. Abstract devices, such as
15 file formats, are supported as well as physical devices. The provided
16 implementations all work with abstract devices. The interface makes
17 available mechanisms for setting the properties which formatter objects
18 manage and inserting data into the output.
23 from types
import StringType
78 self.writer.send_line_break()
81 self.writer.send_paragraph(blankline - self.
parskip)
89 self.writer.send_line_break()
96 self.writer.send_line_break()
97 apply(self.writer.send_hor_rule, args, kw)
103 self.writer.send_line_break()
105 self.writer.send_paragraph((blankline
and 1)
or 0)
106 if type(format)
is StringType:
109 self.writer.send_label_data(format)
117 label = label + (
'%d' % counter)
131 counter, x = divmod(counter-1, 26)
135 s = chr(ord(case) + x)
140 ones = [
'i',
'x',
'c',
'm']
141 fives = [
'v',
'l',
'd']
145 counter, x = divmod(counter, 10)
147 label = ones[index] + ones[index+1] + label
149 label = ones[index] + fives[index] + label
156 s = s + ones[index]*x
165 whitespace = string.whitespace,
166 join = string.join, split = string.split):
170 prespace = data[:1]
in whitespace
171 postspace = data[-1:]
in whitespace
186 self.writer.send_flowing_data(data)
191 self.writer.send_flowing_data(
" ")
195 self.writer.send_literal_data(data)
202 self.writer.send_flowing_data(
' ')
205 if align
and align != self.
align:
206 self.writer.new_alignment(align)
208 self.align_stack.append(align)
210 self.align_stack.append(self.
align)
217 self.writer.new_alignment(align)
220 self.writer.new_alignment(
None)
226 self.writer.send_flowing_data(
' ')
229 if size
is AS_IS: size = csize
230 if i
is AS_IS: i = ci
231 if b
is AS_IS: b = cb
232 if tt
is AS_IS: tt = ctt
233 font = (size, i, b, tt)
234 self.font_stack.append(font)
235 self.writer.new_font(font)
244 self.writer.new_font(font)
247 self.margin_stack.append(margin)
249 if not margin
and fstack:
251 self.writer.new_margin(margin, len(fstack))
261 self.writer.new_margin(margin, len(fstack))
265 self.writer.new_spacing(spacing)
271 self.writer.send_flowing_data(
' ')
273 self.style_stack.append(style)
286 """Minimal writer interface to use in testing & inheritance."""
305 print "new_alignment(%s)" % `align`
308 print "new_font(%s)" % `font`
311 print "new_margin(%s, %d)" % (`margin`, level)
314 print "new_spacing(%s)" % `spacing`
317 print "new_styles(%s)" % `styles`
320 print "send_paragraph(%s)" % `blankline`
323 print "send_line_break()"
326 print "send_hor_rule()"
329 print "send_label_data(%s)" % `data`
332 print "send_flowing_data(%s)" % `data`
335 print "send_literal_data(%s)" % `data`
343 NullWriter.__init__(self)
351 self.file.write(
'\n'*blankline)
356 self.file.write(
'\n')
361 self.file.write(
'\n')
362 self.file.write(
'-'*self.
maxcol)
363 self.file.write(
'\n')
368 self.file.write(data)
373 data = data.expandtabs()
374 self.
col = self.
col + len(data)
379 atbreak = self.
atbreak or data[0]
in string.whitespace
382 write = self.file.write
383 for word
in data.split():
385 if col + len(word) >= maxcol:
392 col = col + len(word)
395 self.
atbreak = data[-1]
in string.whitespace
404 fp =
open(sys.argv[1])
414 f.add_flowing_data(line)
418 if __name__ ==
'__main__':