Mercurial > cpython-lru_cache-weakref
annotate Lib/formatter.py @ 25838:8540887bcec6 2.2
Backport of 2.183:
Fix memory leak in add_subclass() found by NealN with valgrind.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Fri, 18 Oct 2002 13:42:21 +0000 |
| parents | 99df257142f6 |
| children | 3caee2958387 |
| rev | line source |
|---|---|
|
11708
4a23cc49e2b0
Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents:
9850
diff
changeset
|
1 """Generic output formatting. |
|
4a23cc49e2b0
Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents:
9850
diff
changeset
|
2 |
|
4a23cc49e2b0
Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents:
9850
diff
changeset
|
3 Formatter objects transform an abstract flow of formatting events into |
|
4a23cc49e2b0
Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents:
9850
diff
changeset
|
4 specific output events on writer objects. Formatters manage several stack |
|
4a23cc49e2b0
Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents:
9850
diff
changeset
|
5 structures to allow various properties of a writer object to be changed and |
|
4a23cc49e2b0
Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents:
9850
diff
changeset
|
6 restored; writers need not be able to handle relative changes nor any sort |
|
4a23cc49e2b0
Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents:
9850
diff
changeset
|
7 of ``change back'' operation. Specific writer properties which may be |
|
4a23cc49e2b0
Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents:
9850
diff
changeset
|
8 controlled via formatter objects are horizontal alignment, font, and left |
|
4a23cc49e2b0
Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents:
9850
diff
changeset
|
9 margin indentations. A mechanism is provided which supports providing |
|
4a23cc49e2b0
Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents:
9850
diff
changeset
|
10 arbitrary, non-exclusive style settings to a writer as well. Additional |
|
4a23cc49e2b0
Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents:
9850
diff
changeset
|
11 interfaces facilitate formatting events which are not reversible, such as |
|
15867
f5a3d98a35bc
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11708
diff
changeset
|
12 paragraph separation. |
|
11708
4a23cc49e2b0
Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents:
9850
diff
changeset
|
13 |
|
4a23cc49e2b0
Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents:
9850
diff
changeset
|
14 Writer objects encapsulate device interfaces. Abstract devices, such as |
|
4a23cc49e2b0
Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents:
9850
diff
changeset
|
15 file formats, are supported as well as physical devices. The provided |
|
4a23cc49e2b0
Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents:
9850
diff
changeset
|
16 implementations all work with abstract devices. The interface makes |
|
4a23cc49e2b0
Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents:
9850
diff
changeset
|
17 available mechanisms for setting the properties which formatter objects |
|
15867
f5a3d98a35bc
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11708
diff
changeset
|
18 manage and inserting data into the output. |
|
11708
4a23cc49e2b0
Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents:
9850
diff
changeset
|
19 """ |
|
4a23cc49e2b0
Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents:
9850
diff
changeset
|
20 |
| 2916 | 21 import string |
| 22 import sys | |
|
3650
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
23 from types import StringType |
| 2916 | 24 |
| 25 | |
| 26 AS_IS = None | |
| 27 | |
| 28 | |
| 3150 | 29 class NullFormatter: |
|
23575
99df257142f6
Backport change to 1.18 adding docstrings
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
30 """A formatter which does nothing. |
|
99df257142f6
Backport change to 1.18 adding docstrings
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
31 |
|
99df257142f6
Backport change to 1.18 adding docstrings
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
32 If the writer parameter is omitted, a NullWriter instance is created. |
|
99df257142f6
Backport change to 1.18 adding docstrings
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
33 No methods of the writer are called by NullFormatter instances. |
|
99df257142f6
Backport change to 1.18 adding docstrings
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
34 |
|
99df257142f6
Backport change to 1.18 adding docstrings
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
35 Implementations should inherit from this class if implementing a writer |
|
99df257142f6
Backport change to 1.18 adding docstrings
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
36 interface but don't need to inherit any implementation. |
|
99df257142f6
Backport change to 1.18 adding docstrings
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
37 |
|
99df257142f6
Backport change to 1.18 adding docstrings
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
38 """ |
| 3150 | 39 |
|
4360
dbf2737d2c81
Add provisions to set the (to be documented!) instance variable
Guido van Rossum <guido@python.org>
parents:
4147
diff
changeset
|
40 def __init__(self, writer=None): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
41 if not writer: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
42 writer = NullWriter() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
43 self.writer = writer |
| 3150 | 44 def end_paragraph(self, blankline): pass |
| 45 def add_line_break(self): pass | |
|
4385
70cc238d563e
(formatter.py): Simplify NullFormatter definition of add_hor_rule() to match
Fred Drake <fdrake@acm.org>
parents:
4360
diff
changeset
|
46 def add_hor_rule(self, *args, **kw): pass |
|
70cc238d563e
(formatter.py): Simplify NullFormatter definition of add_hor_rule() to match
Fred Drake <fdrake@acm.org>
parents:
4360
diff
changeset
|
47 def add_label_data(self, format, counter, blankline=None): pass |
| 3150 | 48 def add_flowing_data(self, data): pass |
| 49 def add_literal_data(self, data): pass | |
| 50 def flush_softspace(self): pass | |
|
3650
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
51 def push_alignment(self, align): pass |
|
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
52 def pop_alignment(self): pass |
| 3150 | 53 def push_font(self, x): pass |
| 54 def pop_font(self): pass | |
| 55 def push_margin(self, margin): pass | |
| 56 def pop_margin(self): pass | |
| 57 def set_spacing(self, spacing): pass | |
|
3650
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
58 def push_style(self, *styles): pass |
|
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
59 def pop_style(self, n=1): pass |
|
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
60 def assert_line_data(self, flag=1): pass |
| 3150 | 61 |
| 62 | |
| 2916 | 63 class AbstractFormatter: |
|
23575
99df257142f6
Backport change to 1.18 adding docstrings
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
64 """The standard formatter. |
|
99df257142f6
Backport change to 1.18 adding docstrings
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
65 |
|
99df257142f6
Backport change to 1.18 adding docstrings
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
66 This implementation has demonstrated wide applicability to many writers, |
|
99df257142f6
Backport change to 1.18 adding docstrings
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
67 and may be used directly in most circumstances. It has been used to |
|
99df257142f6
Backport change to 1.18 adding docstrings
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
68 implement a full-featured World Wide Web browser. |
|
99df257142f6
Backport change to 1.18 adding docstrings
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
69 |
|
99df257142f6
Backport change to 1.18 adding docstrings
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
70 """ |
| 2916 | 71 |
|
4147
30f1c01f3865
Three sets of changes from Grail:
Guido van Rossum <guido@python.org>
parents:
3653
diff
changeset
|
72 # Space handling policy: blank spaces at the boundary between elements |
|
30f1c01f3865
Three sets of changes from Grail:
Guido van Rossum <guido@python.org>
parents:
3653
diff
changeset
|
73 # are handled by the outermost context. "Literal" data is not checked |
|
30f1c01f3865
Three sets of changes from Grail:
Guido van Rossum <guido@python.org>
parents:
3653
diff
changeset
|
74 # to determine context, so spaces in literal data are handled directly |
|
30f1c01f3865
Three sets of changes from Grail:
Guido van Rossum <guido@python.org>
parents:
3653
diff
changeset
|
75 # in all circumstances. |
|
30f1c01f3865
Three sets of changes from Grail:
Guido van Rossum <guido@python.org>
parents:
3653
diff
changeset
|
76 |
| 2916 | 77 def __init__(self, writer): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
78 self.writer = writer # Output device |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
79 self.align = None # Current alignment |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
80 self.align_stack = [] # Alignment stack |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
81 self.font_stack = [] # Font state |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
82 self.margin_stack = [] # Margin state |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
83 self.spacing = None # Vertical spacing state |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
84 self.style_stack = [] # Other state, e.g. color |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
85 self.nospace = 1 # Should leading space be suppressed |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
86 self.softspace = 0 # Should a space be inserted |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
87 self.para_end = 1 # Just ended a paragraph |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
88 self.parskip = 0 # Skipped space between paragraphs? |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
89 self.hard_break = 1 # Have a hard break |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
90 self.have_label = 0 |
| 2916 | 91 |
| 92 def end_paragraph(self, blankline): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
93 if not self.hard_break: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
94 self.writer.send_line_break() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
95 self.have_label = 0 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
96 if self.parskip < blankline and not self.have_label: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
97 self.writer.send_paragraph(blankline - self.parskip) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
98 self.parskip = blankline |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
99 self.have_label = 0 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
100 self.hard_break = self.nospace = self.para_end = 1 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
101 self.softspace = 0 |
| 2916 | 102 |
| 103 def add_line_break(self): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
104 if not (self.hard_break or self.para_end): |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
105 self.writer.send_line_break() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
106 self.have_label = self.parskip = 0 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
107 self.hard_break = self.nospace = 1 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
108 self.softspace = 0 |
| 2916 | 109 |
|
3650
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
110 def add_hor_rule(self, *args, **kw): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
111 if not self.hard_break: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
112 self.writer.send_line_break() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
113 apply(self.writer.send_hor_rule, args, kw) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
114 self.hard_break = self.nospace = 1 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
115 self.have_label = self.para_end = self.softspace = self.parskip = 0 |
| 2916 | 116 |
|
3650
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
117 def add_label_data(self, format, counter, blankline = None): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
118 if self.have_label or not self.hard_break: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
119 self.writer.send_line_break() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
120 if not self.para_end: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
121 self.writer.send_paragraph((blankline and 1) or 0) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
122 if type(format) is StringType: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
123 self.writer.send_label_data(self.format_counter(format, counter)) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
124 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
125 self.writer.send_label_data(format) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
126 self.nospace = self.have_label = self.hard_break = self.para_end = 1 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
127 self.softspace = self.parskip = 0 |
| 2916 | 128 |
| 129 def format_counter(self, format, counter): | |
| 130 label = '' | |
| 131 for c in format: | |
|
17801
5597d4bcde07
Remove a bare try/except completely -- it just did not make sense!
Fred Drake <fdrake@acm.org>
parents:
16514
diff
changeset
|
132 if c == '1': |
|
5597d4bcde07
Remove a bare try/except completely -- it just did not make sense!
Fred Drake <fdrake@acm.org>
parents:
16514
diff
changeset
|
133 label = label + ('%d' % counter) |
|
5597d4bcde07
Remove a bare try/except completely -- it just did not make sense!
Fred Drake <fdrake@acm.org>
parents:
16514
diff
changeset
|
134 elif c in 'aA': |
|
5597d4bcde07
Remove a bare try/except completely -- it just did not make sense!
Fred Drake <fdrake@acm.org>
parents:
16514
diff
changeset
|
135 if counter > 0: |
|
5597d4bcde07
Remove a bare try/except completely -- it just did not make sense!
Fred Drake <fdrake@acm.org>
parents:
16514
diff
changeset
|
136 label = label + self.format_letter(c, counter) |
|
5597d4bcde07
Remove a bare try/except completely -- it just did not make sense!
Fred Drake <fdrake@acm.org>
parents:
16514
diff
changeset
|
137 elif c in 'iI': |
|
5597d4bcde07
Remove a bare try/except completely -- it just did not make sense!
Fred Drake <fdrake@acm.org>
parents:
16514
diff
changeset
|
138 if counter > 0: |
|
5597d4bcde07
Remove a bare try/except completely -- it just did not make sense!
Fred Drake <fdrake@acm.org>
parents:
16514
diff
changeset
|
139 label = label + self.format_roman(c, counter) |
|
5597d4bcde07
Remove a bare try/except completely -- it just did not make sense!
Fred Drake <fdrake@acm.org>
parents:
16514
diff
changeset
|
140 else: |
|
3650
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
141 label = label + c |
| 2916 | 142 return label |
| 143 | |
| 144 def format_letter(self, case, counter): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
145 label = '' |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
146 while counter > 0: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
147 counter, x = divmod(counter-1, 26) |
|
17801
5597d4bcde07
Remove a bare try/except completely -- it just did not make sense!
Fred Drake <fdrake@acm.org>
parents:
16514
diff
changeset
|
148 # This makes a strong assumption that lowercase letters |
|
5597d4bcde07
Remove a bare try/except completely -- it just did not make sense!
Fred Drake <fdrake@acm.org>
parents:
16514
diff
changeset
|
149 # and uppercase letters form two contiguous blocks, with |
|
5597d4bcde07
Remove a bare try/except completely -- it just did not make sense!
Fred Drake <fdrake@acm.org>
parents:
16514
diff
changeset
|
150 # letters in order! |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
151 s = chr(ord(case) + x) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
152 label = s + label |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
153 return label |
| 2916 | 154 |
| 155 def format_roman(self, case, counter): | |
| 156 ones = ['i', 'x', 'c', 'm'] | |
| 157 fives = ['v', 'l', 'd'] | |
|
3650
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
158 label, index = '', 0 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
159 # This will die of IndexError when counter is too big |
| 2916 | 160 while counter > 0: |
| 161 counter, x = divmod(counter, 10) | |
| 162 if x == 9: | |
|
3650
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
163 label = ones[index] + ones[index+1] + label |
| 2916 | 164 elif x == 4: |
|
3650
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
165 label = ones[index] + fives[index] + label |
| 2916 | 166 else: |
| 167 if x >= 5: | |
| 168 s = fives[index] | |
| 169 x = x-5 | |
| 170 else: | |
| 171 s = '' | |
| 172 s = s + ones[index]*x | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
173 label = s + label |
| 2916 | 174 index = index + 1 |
|
3650
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
175 if case == 'I': |
|
16514
90c600a819e9
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
15867
diff
changeset
|
176 return label.upper() |
| 2916 | 177 return label |
| 178 | |
|
3650
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
179 def add_flowing_data(self, data, |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
180 # These are only here to load them into locals: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
181 whitespace = string.whitespace, |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
182 join = string.join, split = string.split): |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
183 if not data: return |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
184 # The following looks a bit convoluted but is a great improvement over |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
185 # data = regsub.gsub('[' + string.whitespace + ']+', ' ', data) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
186 prespace = data[:1] in whitespace |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
187 postspace = data[-1:] in whitespace |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
188 data = join(split(data)) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
189 if self.nospace and not data: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
190 return |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
191 elif prespace or self.softspace: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
192 if not data: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
193 if not self.nospace: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
194 self.softspace = 1 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
195 self.parskip = 0 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
196 return |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
197 if not self.nospace: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
198 data = ' ' + data |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
199 self.hard_break = self.nospace = self.para_end = \ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
200 self.parskip = self.have_label = 0 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
201 self.softspace = postspace |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
202 self.writer.send_flowing_data(data) |
| 2916 | 203 |
| 204 def add_literal_data(self, data): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
205 if not data: return |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
206 if self.softspace: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
207 self.writer.send_flowing_data(" ") |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
208 self.hard_break = data[-1:] == '\n' |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
209 self.nospace = self.para_end = self.softspace = \ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
210 self.parskip = self.have_label = 0 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
211 self.writer.send_literal_data(data) |
| 2916 | 212 |
|
3130
dcf2a37e73d9
add flush_softspace() interface
Guido van Rossum <guido@python.org>
parents:
2916
diff
changeset
|
213 def flush_softspace(self): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
214 if self.softspace: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
215 self.hard_break = self.para_end = self.parskip = \ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
216 self.have_label = self.softspace = 0 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
217 self.nospace = 1 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
218 self.writer.send_flowing_data(' ') |
|
3130
dcf2a37e73d9
add flush_softspace() interface
Guido van Rossum <guido@python.org>
parents:
2916
diff
changeset
|
219 |
|
3650
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
220 def push_alignment(self, align): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
221 if align and align != self.align: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
222 self.writer.new_alignment(align) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
223 self.align = align |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
224 self.align_stack.append(align) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
225 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
226 self.align_stack.append(self.align) |
|
3650
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
227 |
|
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
228 def pop_alignment(self): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
229 if self.align_stack: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
230 del self.align_stack[-1] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
231 if self.align_stack: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
232 self.align = align = self.align_stack[-1] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
233 self.writer.new_alignment(align) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
234 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
235 self.align = None |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
236 self.writer.new_alignment(None) |
|
3650
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
237 |
| 2916 | 238 def push_font(self, (size, i, b, tt)): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
239 if self.softspace: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
240 self.hard_break = self.para_end = self.softspace = 0 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
241 self.nospace = 1 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
242 self.writer.send_flowing_data(' ') |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
243 if self.font_stack: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
244 csize, ci, cb, ctt = self.font_stack[-1] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
245 if size is AS_IS: size = csize |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
246 if i is AS_IS: i = ci |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
247 if b is AS_IS: b = cb |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
248 if tt is AS_IS: tt = ctt |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
249 font = (size, i, b, tt) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
250 self.font_stack.append(font) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
251 self.writer.new_font(font) |
| 2916 | 252 |
| 253 def pop_font(self): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
254 if self.font_stack: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
255 del self.font_stack[-1] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
256 if self.font_stack: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
257 font = self.font_stack[-1] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
258 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
259 font = None |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
260 self.writer.new_font(font) |
| 2916 | 261 |
| 262 def push_margin(self, margin): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
263 self.margin_stack.append(margin) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
264 fstack = filter(None, self.margin_stack) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
265 if not margin and fstack: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
266 margin = fstack[-1] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
267 self.writer.new_margin(margin, len(fstack)) |
| 2916 | 268 |
| 269 def pop_margin(self): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
270 if self.margin_stack: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
271 del self.margin_stack[-1] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
272 fstack = filter(None, self.margin_stack) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
273 if fstack: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
274 margin = fstack[-1] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
275 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
276 margin = None |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
277 self.writer.new_margin(margin, len(fstack)) |
| 2916 | 278 |
| 279 def set_spacing(self, spacing): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
280 self.spacing = spacing |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
281 self.writer.new_spacing(spacing) |
| 2916 | 282 |
|
3650
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
283 def push_style(self, *styles): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
284 if self.softspace: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
285 self.hard_break = self.para_end = self.softspace = 0 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
286 self.nospace = 1 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
287 self.writer.send_flowing_data(' ') |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
288 for style in styles: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
289 self.style_stack.append(style) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
290 self.writer.new_styles(tuple(self.style_stack)) |
|
3650
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
291 |
|
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
292 def pop_style(self, n=1): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
293 del self.style_stack[-n:] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
294 self.writer.new_styles(tuple(self.style_stack)) |
| 2916 | 295 |
|
3650
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
296 def assert_line_data(self, flag=1): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
297 self.nospace = self.hard_break = not flag |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
298 self.para_end = self.parskip = self.have_label = 0 |
|
3650
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
299 |
|
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
300 |
|
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
301 class NullWriter: |
|
23575
99df257142f6
Backport change to 1.18 adding docstrings
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
302 """Minimal writer interface to use in testing & inheritance. |
|
99df257142f6
Backport change to 1.18 adding docstrings
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
303 |
|
99df257142f6
Backport change to 1.18 adding docstrings
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
304 A writer which only provides the interface definition; no actions are |
|
99df257142f6
Backport change to 1.18 adding docstrings
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
305 taken on any methods. This should be the base class for all writers |
|
99df257142f6
Backport change to 1.18 adding docstrings
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
306 which do not need to inherit any implementation methods. |
|
99df257142f6
Backport change to 1.18 adding docstrings
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
307 |
|
99df257142f6
Backport change to 1.18 adding docstrings
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
308 """ |
|
3653
5309cef8c169
change DumbWriter to derive from NullWriter
Guido van Rossum <guido@python.org>
parents:
3650
diff
changeset
|
309 def __init__(self): pass |
|
4848
52695efdf22c
(formatter.py): Add a flush() method to the writer interface. This really
Fred Drake <fdrake@acm.org>
parents:
4385
diff
changeset
|
310 def flush(self): pass |
|
3650
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
311 def new_alignment(self, align): pass |
|
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
312 def new_font(self, font): pass |
|
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
313 def new_margin(self, margin, level): pass |
|
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
314 def new_spacing(self, spacing): pass |
|
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
315 def new_styles(self, styles): pass |
|
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
316 def send_paragraph(self, blankline): pass |
|
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
317 def send_line_break(self): pass |
|
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
318 def send_hor_rule(self, *args, **kw): pass |
|
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
319 def send_label_data(self, data): pass |
|
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
320 def send_flowing_data(self, data): pass |
|
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
321 def send_literal_data(self, data): pass |
| 2916 | 322 |
| 323 | |
|
3653
5309cef8c169
change DumbWriter to derive from NullWriter
Guido van Rossum <guido@python.org>
parents:
3650
diff
changeset
|
324 class AbstractWriter(NullWriter): |
|
23575
99df257142f6
Backport change to 1.18 adding docstrings
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
325 """A writer which can be used in debugging formatters, but not much else. |
|
99df257142f6
Backport change to 1.18 adding docstrings
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
326 |
|
99df257142f6
Backport change to 1.18 adding docstrings
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
327 Each method simply announces itself by printing its name and |
|
99df257142f6
Backport change to 1.18 adding docstrings
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
328 arguments on standard output. |
|
99df257142f6
Backport change to 1.18 adding docstrings
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
329 |
|
99df257142f6
Backport change to 1.18 adding docstrings
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
330 """ |
| 2916 | 331 |
|
3650
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
332 def new_alignment(self, align): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
333 print "new_alignment(%s)" % `align` |
|
3650
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
334 |
| 2916 | 335 def new_font(self, font): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
336 print "new_font(%s)" % `font` |
| 2916 | 337 |
| 338 def new_margin(self, margin, level): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
339 print "new_margin(%s, %d)" % (`margin`, level) |
| 2916 | 340 |
| 341 def new_spacing(self, spacing): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
342 print "new_spacing(%s)" % `spacing` |
| 2916 | 343 |
| 344 def new_styles(self, styles): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
345 print "new_styles(%s)" % `styles` |
| 2916 | 346 |
| 347 def send_paragraph(self, blankline): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
348 print "send_paragraph(%s)" % `blankline` |
| 2916 | 349 |
| 350 def send_line_break(self): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
351 print "send_line_break()" |
| 2916 | 352 |
|
3650
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
353 def send_hor_rule(self, *args, **kw): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
354 print "send_hor_rule()" |
| 2916 | 355 |
| 356 def send_label_data(self, data): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
357 print "send_label_data(%s)" % `data` |
| 2916 | 358 |
| 359 def send_flowing_data(self, data): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
360 print "send_flowing_data(%s)" % `data` |
| 2916 | 361 |
| 362 def send_literal_data(self, data): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
363 print "send_literal_data(%s)" % `data` |
| 2916 | 364 |
| 365 | |
|
3653
5309cef8c169
change DumbWriter to derive from NullWriter
Guido van Rossum <guido@python.org>
parents:
3650
diff
changeset
|
366 class DumbWriter(NullWriter): |
|
23575
99df257142f6
Backport change to 1.18 adding docstrings
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
367 """Simple writer class which writes output on the file object passed in |
|
99df257142f6
Backport change to 1.18 adding docstrings
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
368 as the file parameter or, if file is omitted, on standard output. The |
|
99df257142f6
Backport change to 1.18 adding docstrings
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
369 output is simply word-wrapped to the number of columns specified by |
|
99df257142f6
Backport change to 1.18 adding docstrings
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
370 the maxcol parameter. This class is suitable for reflowing a sequence |
|
99df257142f6
Backport change to 1.18 adding docstrings
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
371 of paragraphs. |
|
99df257142f6
Backport change to 1.18 adding docstrings
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
372 |
|
99df257142f6
Backport change to 1.18 adding docstrings
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
373 """ |
| 2916 | 374 |
| 375 def __init__(self, file=None, maxcol=72): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
376 self.file = file or sys.stdout |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
377 self.maxcol = maxcol |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
378 NullWriter.__init__(self) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
379 self.reset() |
| 2916 | 380 |
| 381 def reset(self): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
382 self.col = 0 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
383 self.atbreak = 0 |
| 2916 | 384 |
| 385 def send_paragraph(self, blankline): | |
|
9850
ec80ad25ca86
DumbWriter.send_paragraph(): Fix problem with DumbWriter reported to
Fred Drake <fdrake@acm.org>
parents:
7755
diff
changeset
|
386 self.file.write('\n'*blankline) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
387 self.col = 0 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
388 self.atbreak = 0 |
| 2916 | 389 |
| 390 def send_line_break(self): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
391 self.file.write('\n') |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
392 self.col = 0 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
393 self.atbreak = 0 |
| 2916 | 394 |
|
3650
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
395 def send_hor_rule(self, *args, **kw): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
396 self.file.write('\n') |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
397 self.file.write('-'*self.maxcol) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
398 self.file.write('\n') |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
399 self.col = 0 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
400 self.atbreak = 0 |
| 2916 | 401 |
| 402 def send_literal_data(self, data): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
403 self.file.write(data) |
|
16514
90c600a819e9
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
15867
diff
changeset
|
404 i = data.rfind('\n') |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
405 if i >= 0: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
406 self.col = 0 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
407 data = data[i+1:] |
|
16514
90c600a819e9
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
15867
diff
changeset
|
408 data = data.expandtabs() |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
409 self.col = self.col + len(data) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
410 self.atbreak = 0 |
| 2916 | 411 |
| 412 def send_flowing_data(self, data): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
413 if not data: return |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
414 atbreak = self.atbreak or data[0] in string.whitespace |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
415 col = self.col |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
416 maxcol = self.maxcol |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
417 write = self.file.write |
|
16514
90c600a819e9
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
15867
diff
changeset
|
418 for word in data.split(): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
419 if atbreak: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
420 if col + len(word) >= maxcol: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
421 write('\n') |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
422 col = 0 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
423 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
424 write(' ') |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
425 col = col + 1 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
426 write(word) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
427 col = col + len(word) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
428 atbreak = 1 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
429 self.col = col |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
430 self.atbreak = data[-1] in string.whitespace |
| 2916 | 431 |
| 432 | |
| 433 def test(file = None): | |
| 434 w = DumbWriter() | |
| 435 f = AbstractFormatter(w) | |
| 436 if file: | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
437 fp = open(file) |
| 2916 | 438 elif sys.argv[1:]: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
439 fp = open(sys.argv[1]) |
| 2916 | 440 else: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
441 fp = sys.stdin |
| 2916 | 442 while 1: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
443 line = fp.readline() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
444 if not line: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
445 break |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
446 if line == '\n': |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
447 f.end_paragraph(1) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
448 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
449 f.add_flowing_data(line) |
| 2916 | 450 f.end_paragraph(0) |
| 451 | |
| 452 | |
| 453 if __name__ == '__main__': | |
| 454 test() |
