Mercurial > cpython-lru_cache-weakref
annotate Lib/formatter.py @ 58925:eecf1bef052d 2.6
Merged revisions 78207 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line
#7930: fix stripid
........
| author | Ezio Melotti <ezio.melotti@gmail.com> |
|---|---|
| date | Tue, 16 Feb 2010 23:29:44 +0000 |
| parents | a6b190b0cf06 |
| children | 751c6dd71b5e |
| 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 sys |
| 22 | |
| 23 | |
| 24 AS_IS = None | |
| 25 | |
| 26 | |
| 3150 | 27 class NullFormatter: |
|
23565
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
28 """A formatter which does nothing. |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
29 |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
30 If the writer parameter is omitted, a NullWriter instance is created. |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
31 No methods of the writer are called by NullFormatter instances. |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
32 |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
33 Implementations should inherit from this class if implementing a writer |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
34 interface but don't need to inherit any implementation. |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
35 |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
36 """ |
| 3150 | 37 |
|
4360
dbf2737d2c81
Add provisions to set the (to be documented!) instance variable
Guido van Rossum <guido@python.org>
parents:
4147
diff
changeset
|
38 def __init__(self, writer=None): |
|
23612
3caee2958387
Replace boolean test with is None
Raymond Hettinger <python@rcn.com>
parents:
23565
diff
changeset
|
39 if writer is None: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
40 writer = NullWriter() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
41 self.writer = writer |
| 3150 | 42 def end_paragraph(self, blankline): pass |
| 43 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
|
44 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
|
45 def add_label_data(self, format, counter, blankline=None): pass |
| 3150 | 46 def add_flowing_data(self, data): pass |
| 47 def add_literal_data(self, data): pass | |
| 48 def flush_softspace(self): pass | |
|
3650
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
49 def push_alignment(self, align): pass |
|
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
50 def pop_alignment(self): pass |
| 3150 | 51 def push_font(self, x): pass |
| 52 def pop_font(self): pass | |
| 53 def push_margin(self, margin): pass | |
| 54 def pop_margin(self): pass | |
| 55 def set_spacing(self, spacing): pass | |
|
3650
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
56 def push_style(self, *styles): pass |
|
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
57 def pop_style(self, n=1): pass |
|
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
58 def assert_line_data(self, flag=1): pass |
| 3150 | 59 |
| 60 | |
| 2916 | 61 class AbstractFormatter: |
|
23565
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
62 """The standard formatter. |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
63 |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
64 This implementation has demonstrated wide applicability to many writers, |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
65 and may be used directly in most circumstances. It has been used to |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
66 implement a full-featured World Wide Web browser. |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
67 |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
68 """ |
| 2916 | 69 |
|
4147
30f1c01f3865
Three sets of changes from Grail:
Guido van Rossum <guido@python.org>
parents:
3653
diff
changeset
|
70 # 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
|
71 # 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
|
72 # 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
|
73 # in all circumstances. |
|
30f1c01f3865
Three sets of changes from Grail:
Guido van Rossum <guido@python.org>
parents:
3653
diff
changeset
|
74 |
| 2916 | 75 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
|
76 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
|
77 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
|
78 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
|
79 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
|
80 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
|
81 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
|
82 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
|
83 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
|
84 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
|
85 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
|
86 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
|
87 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
|
88 self.have_label = 0 |
| 2916 | 89 |
| 90 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
|
91 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
|
92 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
|
93 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
|
94 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
|
95 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
|
96 self.parskip = blankline |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
97 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
|
98 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
|
99 self.softspace = 0 |
| 2916 | 100 |
| 101 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
|
102 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
|
103 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
|
104 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
|
105 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
|
106 self.softspace = 0 |
| 2916 | 107 |
|
3650
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
108 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
|
109 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
|
110 self.writer.send_line_break() |
|
28089
14f65d661084
Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents:
23645
diff
changeset
|
111 self.writer.send_hor_rule(*args, **kw) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
112 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
|
113 self.have_label = self.para_end = self.softspace = self.parskip = 0 |
| 2916 | 114 |
|
3650
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
115 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
|
116 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
|
117 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
|
118 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
|
119 self.writer.send_paragraph((blankline and 1) or 0) |
|
23645
e1e90b0f1f45
Remove uses of the string and types modules:
Walter Dörwald <walter@livinglogic.de>
parents:
23612
diff
changeset
|
120 if isinstance(format, str): |
|
7755
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_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
|
122 else: |
|
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(format) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
124 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
|
125 self.softspace = self.parskip = 0 |
| 2916 | 126 |
| 127 def format_counter(self, format, counter): | |
| 128 label = '' | |
| 129 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
|
130 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
|
131 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
|
132 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
|
133 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
|
134 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
|
135 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
|
136 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
|
137 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
|
138 else: |
|
3650
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
139 label = label + c |
| 2916 | 140 return label |
| 141 | |
| 142 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
|
143 label = '' |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
144 while counter > 0: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
145 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
|
146 # 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
|
147 # 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
|
148 # 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
|
149 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
|
150 label = s + label |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
151 return label |
| 2916 | 152 |
| 153 def format_roman(self, case, counter): | |
| 154 ones = ['i', 'x', 'c', 'm'] | |
| 155 fives = ['v', 'l', 'd'] | |
|
3650
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
156 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
|
157 # This will die of IndexError when counter is too big |
| 2916 | 158 while counter > 0: |
| 159 counter, x = divmod(counter, 10) | |
| 160 if x == 9: | |
|
3650
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
161 label = ones[index] + ones[index+1] + label |
| 2916 | 162 elif x == 4: |
|
3650
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
163 label = ones[index] + fives[index] + label |
| 2916 | 164 else: |
| 165 if x >= 5: | |
| 166 s = fives[index] | |
| 167 x = x-5 | |
| 168 else: | |
| 169 s = '' | |
| 170 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
|
171 label = s + label |
| 2916 | 172 index = index + 1 |
|
3650
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
173 if case == 'I': |
|
16514
90c600a819e9
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
15867
diff
changeset
|
174 return label.upper() |
| 2916 | 175 return label |
| 176 | |
|
23645
e1e90b0f1f45
Remove uses of the string and types modules:
Walter Dörwald <walter@livinglogic.de>
parents:
23612
diff
changeset
|
177 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
|
178 if not data: return |
|
23645
e1e90b0f1f45
Remove uses of the string and types modules:
Walter Dörwald <walter@livinglogic.de>
parents:
23612
diff
changeset
|
179 prespace = data[:1].isspace() |
|
e1e90b0f1f45
Remove uses of the string and types modules:
Walter Dörwald <walter@livinglogic.de>
parents:
23612
diff
changeset
|
180 postspace = data[-1:].isspace() |
|
e1e90b0f1f45
Remove uses of the string and types modules:
Walter Dörwald <walter@livinglogic.de>
parents:
23612
diff
changeset
|
181 data = " ".join(data.split()) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
182 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
|
183 return |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
184 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
|
185 if not data: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
186 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
|
187 self.softspace = 1 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
188 self.parskip = 0 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
189 return |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
190 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
|
191 data = ' ' + data |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
192 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
|
193 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
|
194 self.softspace = postspace |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
195 self.writer.send_flowing_data(data) |
| 2916 | 196 |
| 197 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
|
198 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
|
199 if self.softspace: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
200 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
|
201 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
|
202 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
|
203 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
|
204 self.writer.send_literal_data(data) |
| 2916 | 205 |
|
3130
dcf2a37e73d9
add flush_softspace() interface
Guido van Rossum <guido@python.org>
parents:
2916
diff
changeset
|
206 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
|
207 if self.softspace: |
|
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 = 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
|
209 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
|
210 self.nospace = 1 |
|
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_flowing_data(' ') |
|
3130
dcf2a37e73d9
add flush_softspace() interface
Guido van Rossum <guido@python.org>
parents:
2916
diff
changeset
|
212 |
|
3650
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
213 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
|
214 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
|
215 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
|
216 self.align = align |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
217 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
|
218 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
219 self.align_stack.append(self.align) |
|
3650
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
220 |
|
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
221 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
|
222 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
|
223 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
|
224 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
|
225 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
|
226 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
|
227 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
228 self.align = None |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
229 self.writer.new_alignment(None) |
|
3650
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
230 |
| 2916 | 231 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
|
232 if self.softspace: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
233 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
|
234 self.nospace = 1 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
235 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
|
236 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
|
237 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
|
238 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
|
239 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
|
240 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
|
241 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
|
242 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
|
243 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
|
244 self.writer.new_font(font) |
| 2916 | 245 |
| 246 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
|
247 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
|
248 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
|
249 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
|
250 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
|
251 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
252 font = None |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
253 self.writer.new_font(font) |
| 2916 | 254 |
| 255 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
|
256 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
|
257 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
|
258 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
|
259 margin = fstack[-1] |
|
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_margin(margin, len(fstack)) |
| 2916 | 261 |
| 262 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
|
263 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
|
264 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
|
265 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
|
266 if fstack: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
267 margin = fstack[-1] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
268 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
269 margin = None |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
270 self.writer.new_margin(margin, len(fstack)) |
| 2916 | 271 |
| 272 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
|
273 self.spacing = spacing |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
274 self.writer.new_spacing(spacing) |
| 2916 | 275 |
|
3650
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
276 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
|
277 if self.softspace: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
278 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
|
279 self.nospace = 1 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
280 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
|
281 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
|
282 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
|
283 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
|
284 |
|
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
285 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
|
286 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
|
287 self.writer.new_styles(tuple(self.style_stack)) |
| 2916 | 288 |
|
3650
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
289 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
|
290 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
|
291 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
|
292 |
|
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
293 |
|
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
294 class NullWriter: |
|
23565
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
295 """Minimal writer interface to use in testing & inheritance. |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
296 |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
297 A writer which only provides the interface definition; no actions are |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
298 taken on any methods. This should be the base class for all writers |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
299 which do not need to inherit any implementation methods. |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
300 |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
301 """ |
|
3653
5309cef8c169
change DumbWriter to derive from NullWriter
Guido van Rossum <guido@python.org>
parents:
3650
diff
changeset
|
302 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
|
303 def flush(self): pass |
|
3650
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
304 def new_alignment(self, align): pass |
|
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
305 def new_font(self, font): pass |
|
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
306 def new_margin(self, margin, level): pass |
|
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
307 def new_spacing(self, spacing): pass |
|
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
308 def new_styles(self, styles): pass |
|
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
309 def send_paragraph(self, blankline): pass |
|
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
310 def send_line_break(self): pass |
|
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
311 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
|
312 def send_label_data(self, data): pass |
|
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
313 def send_flowing_data(self, data): pass |
|
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
314 def send_literal_data(self, data): pass |
| 2916 | 315 |
| 316 | |
|
3653
5309cef8c169
change DumbWriter to derive from NullWriter
Guido van Rossum <guido@python.org>
parents:
3650
diff
changeset
|
317 class AbstractWriter(NullWriter): |
|
23565
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
318 """A writer which can be used in debugging formatters, but not much else. |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
319 |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
320 Each method simply announces itself by printing its name and |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
321 arguments on standard output. |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
322 |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
323 """ |
| 2916 | 324 |
|
3650
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
325 def new_alignment(self, align): |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
28089
diff
changeset
|
326 print "new_alignment(%r)" % (align,) |
|
3650
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
327 |
| 2916 | 328 def new_font(self, font): |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
28089
diff
changeset
|
329 print "new_font(%r)" % (font,) |
| 2916 | 330 |
| 331 def new_margin(self, margin, level): | |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
28089
diff
changeset
|
332 print "new_margin(%r, %d)" % (margin, level) |
| 2916 | 333 |
| 334 def new_spacing(self, spacing): | |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
28089
diff
changeset
|
335 print "new_spacing(%r)" % (spacing,) |
| 2916 | 336 |
| 337 def new_styles(self, styles): | |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
28089
diff
changeset
|
338 print "new_styles(%r)" % (styles,) |
| 2916 | 339 |
| 340 def send_paragraph(self, blankline): | |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
28089
diff
changeset
|
341 print "send_paragraph(%r)" % (blankline,) |
| 2916 | 342 |
| 343 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
|
344 print "send_line_break()" |
| 2916 | 345 |
|
3650
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
346 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
|
347 print "send_hor_rule()" |
| 2916 | 348 |
| 349 def send_label_data(self, data): | |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
28089
diff
changeset
|
350 print "send_label_data(%r)" % (data,) |
| 2916 | 351 |
| 352 def send_flowing_data(self, data): | |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
28089
diff
changeset
|
353 print "send_flowing_data(%r)" % (data,) |
| 2916 | 354 |
| 355 def send_literal_data(self, data): | |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
28089
diff
changeset
|
356 print "send_literal_data(%r)" % (data,) |
| 2916 | 357 |
| 358 | |
|
3653
5309cef8c169
change DumbWriter to derive from NullWriter
Guido van Rossum <guido@python.org>
parents:
3650
diff
changeset
|
359 class DumbWriter(NullWriter): |
|
23565
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
360 """Simple writer class which writes output on the file object passed in |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
361 as the file parameter or, if file is omitted, on standard output. The |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
362 output is simply word-wrapped to the number of columns specified by |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
363 the maxcol parameter. This class is suitable for reflowing a sequence |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
364 of paragraphs. |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
365 |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
19218
diff
changeset
|
366 """ |
| 2916 | 367 |
| 368 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
|
369 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
|
370 self.maxcol = maxcol |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
371 NullWriter.__init__(self) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
372 self.reset() |
| 2916 | 373 |
| 374 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
|
375 self.col = 0 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
376 self.atbreak = 0 |
| 2916 | 377 |
| 378 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
|
379 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
|
380 self.col = 0 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
381 self.atbreak = 0 |
| 2916 | 382 |
| 383 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
|
384 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
|
385 self.col = 0 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
386 self.atbreak = 0 |
| 2916 | 387 |
|
3650
5d6fe145f773
Many improvements dure to Fred Drake
Guido van Rossum <guido@python.org>
parents:
3383
diff
changeset
|
388 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
|
389 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
|
390 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
|
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 |
| 395 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
|
396 self.file.write(data) |
|
16514
90c600a819e9
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
15867
diff
changeset
|
397 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
|
398 if i >= 0: |
|
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 data = data[i+1:] |
|
16514
90c600a819e9
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
15867
diff
changeset
|
401 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
|
402 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
|
403 self.atbreak = 0 |
| 2916 | 404 |
| 405 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
|
406 if not data: return |
|
23645
e1e90b0f1f45
Remove uses of the string and types modules:
Walter Dörwald <walter@livinglogic.de>
parents:
23612
diff
changeset
|
407 atbreak = self.atbreak or data[0].isspace() |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
408 col = self.col |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
409 maxcol = self.maxcol |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
410 write = self.file.write |
|
16514
90c600a819e9
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
15867
diff
changeset
|
411 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
|
412 if atbreak: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
413 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
|
414 write('\n') |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
415 col = 0 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
416 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
417 write(' ') |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
418 col = col + 1 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
419 write(word) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
420 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
|
421 atbreak = 1 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
422 self.col = col |
|
23645
e1e90b0f1f45
Remove uses of the string and types modules:
Walter Dörwald <walter@livinglogic.de>
parents:
23612
diff
changeset
|
423 self.atbreak = data[-1].isspace() |
| 2916 | 424 |
| 425 | |
| 426 def test(file = None): | |
| 427 w = DumbWriter() | |
| 428 f = AbstractFormatter(w) | |
|
23612
3caee2958387
Replace boolean test with is None
Raymond Hettinger <python@rcn.com>
parents:
23565
diff
changeset
|
429 if file is not None: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
430 fp = open(file) |
| 2916 | 431 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
|
432 fp = open(sys.argv[1]) |
| 2916 | 433 else: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
434 fp = sys.stdin |
|
44847
a6b190b0cf06
#1726198: replace while 1: fp.readline() with file iteration.
Georg Brandl <georg@python.org>
parents:
34130
diff
changeset
|
435 for line in fp: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
436 if line == '\n': |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
437 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
|
438 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6515
diff
changeset
|
439 f.add_flowing_data(line) |
| 2916 | 440 f.end_paragraph(0) |
| 441 | |
| 442 | |
| 443 if __name__ == '__main__': | |
| 444 test() |
