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
31 def __init__(self
, writer
=None):
35 def end_paragraph(self
, blankline
): pass
36 def add_line_break(self
): pass
37 def add_hor_rule(self
, *args
, **kw
): pass
38 def add_label_data(self
, format
, counter
, blankline
=None): pass
39 def add_flowing_data(self
, data
): pass
40 def add_literal_data(self
, data
): pass
41 def flush_softspace(self
): pass
42 def push_alignment(self
, align
): pass
43 def pop_alignment(self
): pass
44 def push_font(self
, x
): pass
45 def pop_font(self
): pass
46 def push_margin(self
, margin
): pass
47 def pop_margin(self
): pass
48 def set_spacing(self
, spacing
): pass
49 def push_style(self
, *styles
): pass
50 def pop_style(self
, n
=1): pass
51 def assert_line_data(self
, flag
=1): pass
54 class AbstractFormatter
:
56 # Space handling policy: blank spaces at the boundary between elements
57 # are handled by the outermost context. "Literal" data is not checked
58 # to determine context, so spaces in literal data are handled directly
59 # in all circumstances.
61 def __init__(self
, writer
):
62 self
.writer
= writer
# Output device
63 self
.align
= None # Current alignment
64 self
.align_stack
= [] # Alignment stack
65 self
.font_stack
= [] # Font state
66 self
.margin_stack
= [] # Margin state
67 self
.spacing
= None # Vertical spacing state
68 self
.style_stack
= [] # Other state, e.g. color
69 self
.nospace
= 1 # Should leading space be suppressed
70 self
.softspace
= 0 # Should a space be inserted
71 self
.para_end
= 1 # Just ended a paragraph
72 self
.parskip
= 0 # Skipped space between paragraphs?
73 self
.hard_break
= 1 # Have a hard break
76 def end_paragraph(self
, blankline
):
77 if not self
.hard_break
:
78 self
.writer
.send_line_break()
80 if self
.parskip
< blankline
and not self
.have_label
:
81 self
.writer
.send_paragraph(blankline
- self
.parskip
)
82 self
.parskip
= blankline
84 self
.hard_break
= self
.nospace
= self
.para_end
= 1
87 def add_line_break(self
):
88 if not (self
.hard_break
or self
.para_end
):
89 self
.writer
.send_line_break()
90 self
.have_label
= self
.parskip
= 0
91 self
.hard_break
= self
.nospace
= 1
94 def add_hor_rule(self
, *args
, **kw
):
95 if not self
.hard_break
:
96 self
.writer
.send_line_break()
97 apply(self
.writer
.send_hor_rule
, args
, kw
)
98 self
.hard_break
= self
.nospace
= 1
99 self
.have_label
= self
.para_end
= self
.softspace
= self
.parskip
= 0
101 def add_label_data(self
, format
, counter
, blankline
= None):
102 if self
.have_label
or not self
.hard_break
:
103 self
.writer
.send_line_break()
104 if not self
.para_end
:
105 self
.writer
.send_paragraph((blankline
and 1) or 0)
106 if type(format
) is StringType
:
107 self
.writer
.send_label_data(self
.format_counter(format
, counter
))
109 self
.writer
.send_label_data(format
)
110 self
.nospace
= self
.have_label
= self
.hard_break
= self
.para_end
= 1
111 self
.softspace
= self
.parskip
= 0
113 def format_counter(self
, format
, counter
):
118 label
= label
+ ('%d' % counter
)
121 label
= label
+ self
.format_letter(c
, counter
)
124 label
= label
+ self
.format_roman(c
, counter
)
131 def format_letter(self
, case
, counter
):
134 counter
, x
= divmod(counter
-1, 26)
135 s
= chr(ord(case
) + x
)
139 def format_roman(self
, case
, counter
):
140 ones
= ['i', 'x', 'c', 'm']
141 fives
= ['v', 'l', 'd']
143 # This will die of IndexError when counter is too big
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
160 return string
.upper(label
)
163 def add_flowing_data(self
, data
,
164 # These are only here to load them into locals:
165 whitespace
= string
.whitespace
,
166 join
= string
.join
, split
= string
.split
):
168 # The following looks a bit convoluted but is a great improvement over
169 # data = regsub.gsub('[' + string.whitespace + ']+', ' ', data)
170 prespace
= data
[:1] in whitespace
171 postspace
= data
[-1:] in whitespace
172 data
= join(split(data
))
173 if self
.nospace
and not data
:
175 elif prespace
or self
.softspace
:
183 self
.hard_break
= self
.nospace
= self
.para_end
= \
184 self
.parskip
= self
.have_label
= 0
185 self
.softspace
= postspace
186 self
.writer
.send_flowing_data(data
)
188 def add_literal_data(self
, data
):
191 self
.writer
.send_flowing_data(" ")
192 self
.hard_break
= data
[-1:] == '\n'
193 self
.nospace
= self
.para_end
= self
.softspace
= \
194 self
.parskip
= self
.have_label
= 0
195 self
.writer
.send_literal_data(data
)
197 def flush_softspace(self
):
199 self
.hard_break
= self
.para_end
= self
.parskip
= \
200 self
.have_label
= self
.softspace
= 0
202 self
.writer
.send_flowing_data(' ')
204 def push_alignment(self
, align
):
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
)
212 def pop_alignment(self
):
214 del self
.align_stack
[-1]
216 self
.align
= align
= self
.align_stack
[-1]
217 self
.writer
.new_alignment(align
)
220 self
.writer
.new_alignment(None)
222 def push_font(self
, (size
, i
, b
, tt
)):
224 self
.hard_break
= self
.para_end
= self
.softspace
= 0
226 self
.writer
.send_flowing_data(' ')
228 csize
, ci
, cb
, ctt
= self
.font_stack
[-1]
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
)
239 del self
.font_stack
[-1]
241 font
= self
.font_stack
[-1]
244 self
.writer
.new_font(font
)
246 def push_margin(self
, margin
):
247 self
.margin_stack
.append(margin
)
248 fstack
= filter(None, self
.margin_stack
)
249 if not margin
and fstack
:
251 self
.writer
.new_margin(margin
, len(fstack
))
253 def pop_margin(self
):
254 if self
.margin_stack
:
255 del self
.margin_stack
[-1]
256 fstack
= filter(None, self
.margin_stack
)
261 self
.writer
.new_margin(margin
, len(fstack
))
263 def set_spacing(self
, spacing
):
264 self
.spacing
= spacing
265 self
.writer
.new_spacing(spacing
)
267 def push_style(self
, *styles
):
269 self
.hard_break
= self
.para_end
= self
.softspace
= 0
271 self
.writer
.send_flowing_data(' ')
273 self
.style_stack
.append(style
)
274 self
.writer
.new_styles(tuple(self
.style_stack
))
276 def pop_style(self
, n
=1):
277 del self
.style_stack
[-n
:]
278 self
.writer
.new_styles(tuple(self
.style_stack
))
280 def assert_line_data(self
, flag
=1):
281 self
.nospace
= self
.hard_break
= not flag
282 self
.para_end
= self
.parskip
= self
.have_label
= 0
286 """Minimal writer interface to use in testing & inheritance."""
287 def __init__(self
): pass
288 def flush(self
): pass
289 def new_alignment(self
, align
): pass
290 def new_font(self
, font
): pass
291 def new_margin(self
, margin
, level
): pass
292 def new_spacing(self
, spacing
): pass
293 def new_styles(self
, styles
): pass
294 def send_paragraph(self
, blankline
): pass
295 def send_line_break(self
): pass
296 def send_hor_rule(self
, *args
, **kw
): pass
297 def send_label_data(self
, data
): pass
298 def send_flowing_data(self
, data
): pass
299 def send_literal_data(self
, data
): pass
302 class AbstractWriter(NullWriter
):
307 def new_alignment(self
, align
):
308 print "new_alignment(%s)" % `align`
310 def new_font(self
, font
):
311 print "new_font(%s)" % `font`
313 def new_margin(self
, margin
, level
):
314 print "new_margin(%s, %d)" % (`margin`
, level
)
316 def new_spacing(self
, spacing
):
317 print "new_spacing(%s)" % `spacing`
319 def new_styles(self
, styles
):
320 print "new_styles(%s)" % `styles`
322 def send_paragraph(self
, blankline
):
323 print "send_paragraph(%s)" % `blankline`
325 def send_line_break(self
):
326 print "send_line_break()"
328 def send_hor_rule(self
, *args
, **kw
):
329 print "send_hor_rule()"
331 def send_label_data(self
, data
):
332 print "send_label_data(%s)" % `data`
334 def send_flowing_data(self
, data
):
335 print "send_flowing_data(%s)" % `data`
337 def send_literal_data(self
, data
):
338 print "send_literal_data(%s)" % `data`
341 class DumbWriter(NullWriter
):
343 def __init__(self
, file=None, maxcol
=72):
344 self
.file = file or sys
.stdout
346 NullWriter
.__init
__(self
)
353 def send_paragraph(self
, blankline
):
354 self
.file.write('\n'*blankline
)
358 def send_line_break(self
):
359 self
.file.write('\n')
363 def send_hor_rule(self
, *args
, **kw
):
364 self
.file.write('\n')
365 self
.file.write('-'*self
.maxcol
)
366 self
.file.write('\n')
370 def send_literal_data(self
, data
):
371 self
.file.write(data
)
372 i
= string
.rfind(data
, '\n')
376 data
= string
.expandtabs(data
)
377 self
.col
= self
.col
+ len(data
)
380 def send_flowing_data(self
, data
):
382 atbreak
= self
.atbreak
or data
[0] in string
.whitespace
385 write
= self
.file.write
386 for word
in string
.split(data
):
388 if col
+ len(word
) >= maxcol
:
395 col
= col
+ len(word
)
398 self
.atbreak
= data
[-1] in string
.whitespace
401 def test(file = None):
403 f
= AbstractFormatter(w
)
407 fp
= open(sys
.argv
[1])
417 f
.add_flowing_data(line
)
421 if __name__
== '__main__':