You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

346 lines
13 KiB

Merged revisions 57152-57220 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r57155 | brett.cannon | 2007-08-17 11:51:57 -0700 (Fri, 17 Aug 2007) | 2 lines Clarify the comment about setting the PYTHON variable for the Doc Makefile. ........ r57156 | brett.cannon | 2007-08-17 11:59:58 -0700 (Fri, 17 Aug 2007) | 3 lines Remove news entry for test.test_support.guard_warnings_filter as it has been removed. ........ r57158 | facundo.batista | 2007-08-17 12:16:44 -0700 (Fri, 17 Aug 2007) | 9 lines Added a flag (_send_traceback_header) to the SimpleXMLRPCServer class that allows sending back exception/stack trace information about internal server errors (this flag defaults to False to avoid sending such information unless explicitly enabled). Added tests to verify behavior of this new feature (these tests are skipped on win32 because of problems with WSAEWOULDBLOCK). Renamed HTTPTestCase to SimpleServerTestCase. [GSoC - Alan McIntyre] ........ r57160 | brett.cannon | 2007-08-17 13:16:15 -0700 (Fri, 17 Aug 2007) | 2 lines Make test_warnings re-entrant. ........ r57175 | georg.brandl | 2007-08-17 23:05:56 -0700 (Fri, 17 Aug 2007) | 2 lines Fix PDB command descriptions. ........ r57182 | brett.cannon | 2007-08-18 11:30:36 -0700 (Sat, 18 Aug 2007) | 3 lines Remove Mikael as the maintainer of BeOS at his request. With no maintainer should the port be deprecated in 2.6? ........ r57199 | georg.brandl | 2007-08-19 11:43:50 -0700 (Sun, 19 Aug 2007) | 2 lines Fix stray backticks. ........ r57211 | hyeshik.chang | 2007-08-19 23:49:18 -0700 (Sun, 19 Aug 2007) | 2 lines Add cheot-ga-keut composed make-up sequence support in EUC-KR codec. ........ r57216 | guido.van.rossum | 2007-08-20 08:18:04 -0700 (Mon, 20 Aug 2007) | 4 lines Add a hack (originally devised in a slightly different form by Thomas Wouters) to prevent spurious tracebacks when a daemon thread's cleanup happens to wake up when the world around it has already been destroyed. ........
19 years ago
  1. :mod:`formatter` --- Generic output formatting
  2. ==============================================
  3. .. module:: formatter
  4. :synopsis: Generic output formatter and device interface.
  5. This module supports two interface definitions, each with multiple
  6. implementations: The *formatter* interface, and the *writer* interface which is
  7. required by the formatter interface.
  8. Formatter objects transform an abstract flow of formatting events into specific
  9. output events on writer objects. Formatters manage several stack structures to
  10. allow various properties of a writer object to be changed and restored; writers
  11. need not be able to handle relative changes nor any sort of "change back"
  12. operation. Specific writer properties which may be controlled via formatter
  13. objects are horizontal alignment, font, and left margin indentations. A
  14. mechanism is provided which supports providing arbitrary, non-exclusive style
  15. settings to a writer as well. Additional interfaces facilitate formatting
  16. events which are not reversible, such as paragraph separation.
  17. Writer objects encapsulate device interfaces. Abstract devices, such as file
  18. formats, are supported as well as physical devices. The provided
  19. implementations all work with abstract devices. The interface makes available
  20. mechanisms for setting the properties which formatter objects manage and
  21. inserting data into the output.
  22. .. _formatter-interface:
  23. The Formatter Interface
  24. -----------------------
  25. Interfaces to create formatters are dependent on the specific formatter class
  26. being instantiated. The interfaces described below are the required interfaces
  27. which all formatters must support once initialized.
  28. One data element is defined at the module level:
  29. .. data:: AS_IS
  30. Value which can be used in the font specification passed to the ``push_font()``
  31. method described below, or as the new value to any other ``push_property()``
  32. method. Pushing the ``AS_IS`` value allows the corresponding ``pop_property()``
  33. method to be called without having to track whether the property was changed.
  34. The following attributes are defined for formatter instance objects:
  35. .. attribute:: formatter.writer
  36. The writer instance with which the formatter interacts.
  37. .. method:: formatter.end_paragraph(blanklines)
  38. Close any open paragraphs and insert at least *blanklines* before the next
  39. paragraph.
  40. .. method:: formatter.add_line_break()
  41. Add a hard line break if one does not already exist. This does not break the
  42. logical paragraph.
  43. .. method:: formatter.add_hor_rule(*args, **kw)
  44. Insert a horizontal rule in the output. A hard break is inserted if there is
  45. data in the current paragraph, but the logical paragraph is not broken. The
  46. arguments and keywords are passed on to the writer's :meth:`send_line_break`
  47. method.
  48. .. method:: formatter.add_flowing_data(data)
  49. Provide data which should be formatted with collapsed whitespace. Whitespace
  50. from preceding and successive calls to :meth:`add_flowing_data` is considered as
  51. well when the whitespace collapse is performed. The data which is passed to
  52. this method is expected to be word-wrapped by the output device. Note that any
  53. word-wrapping still must be performed by the writer object due to the need to
  54. rely on device and font information.
  55. .. method:: formatter.add_literal_data(data)
  56. Provide data which should be passed to the writer unchanged. Whitespace,
  57. including newline and tab characters, are considered legal in the value of
  58. *data*.
  59. .. method:: formatter.add_label_data(format, counter)
  60. Insert a label which should be placed to the left of the current left margin.
  61. This should be used for constructing bulleted or numbered lists. If the
  62. *format* value is a string, it is interpreted as a format specification for
  63. *counter*, which should be an integer. The result of this formatting becomes the
  64. value of the label; if *format* is not a string it is used as the label value
  65. directly. The label value is passed as the only argument to the writer's
  66. :meth:`send_label_data` method. Interpretation of non-string label values is
  67. dependent on the associated writer.
  68. Format specifications are strings which, in combination with a counter value,
  69. are used to compute label values. Each character in the format string is copied
  70. to the label value, with some characters recognized to indicate a transform on
  71. the counter value. Specifically, the character ``'1'`` represents the counter
  72. value formatter as an Arabic number, the characters ``'A'`` and ``'a'``
  73. represent alphabetic representations of the counter value in upper and lower
  74. case, respectively, and ``'I'`` and ``'i'`` represent the counter value in Roman
  75. numerals, in upper and lower case. Note that the alphabetic and roman
  76. transforms require that the counter value be greater than zero.
  77. .. method:: formatter.flush_softspace()
  78. Send any pending whitespace buffered from a previous call to
  79. :meth:`add_flowing_data` to the associated writer object. This should be called
  80. before any direct manipulation of the writer object.
  81. .. method:: formatter.push_alignment(align)
  82. Push a new alignment setting onto the alignment stack. This may be
  83. :const:`AS_IS` if no change is desired. If the alignment value is changed from
  84. the previous setting, the writer's :meth:`new_alignment` method is called with
  85. the *align* value.
  86. .. method:: formatter.pop_alignment()
  87. Restore the previous alignment.
  88. .. method:: formatter.push_font((size, italic, bold, teletype))
  89. Change some or all font properties of the writer object. Properties which are
  90. not set to :const:`AS_IS` are set to the values passed in while others are
  91. maintained at their current settings. The writer's :meth:`new_font` method is
  92. called with the fully resolved font specification.
  93. .. method:: formatter.pop_font()
  94. Restore the previous font.
  95. .. method:: formatter.push_margin(margin)
  96. Increase the number of left margin indentations by one, associating the logical
  97. tag *margin* with the new indentation. The initial margin level is ``0``.
  98. Changed values of the logical tag must be true values; false values other than
  99. :const:`AS_IS` are not sufficient to change the margin.
  100. .. method:: formatter.pop_margin()
  101. Restore the previous margin.
  102. .. method:: formatter.push_style(*styles)
  103. Push any number of arbitrary style specifications. All styles are pushed onto
  104. the styles stack in order. A tuple representing the entire stack, including
  105. :const:`AS_IS` values, is passed to the writer's :meth:`new_styles` method.
  106. .. method:: formatter.pop_style(n=1)
  107. Pop the last *n* style specifications passed to :meth:`push_style`. A tuple
  108. representing the revised stack, including :const:`AS_IS` values, is passed to
  109. the writer's :meth:`new_styles` method.
  110. .. method:: formatter.set_spacing(spacing)
  111. Set the spacing style for the writer.
  112. .. method:: formatter.assert_line_data(flag=1)
  113. Inform the formatter that data has been added to the current paragraph
  114. out-of-band. This should be used when the writer has been manipulated
  115. directly. The optional *flag* argument can be set to false if the writer
  116. manipulations produced a hard line break at the end of the output.
  117. .. _formatter-impls:
  118. Formatter Implementations
  119. -------------------------
  120. Two implementations of formatter objects are provided by this module. Most
  121. applications may use one of these classes without modification or subclassing.
  122. .. class:: NullFormatter(writer=None)
  123. A formatter which does nothing. If *writer* is omitted, a :class:`NullWriter`
  124. instance is created. No methods of the writer are called by
  125. :class:`NullFormatter` instances. Implementations should inherit from this
  126. class if implementing a writer interface but don't need to inherit any
  127. implementation.
  128. .. class:: AbstractFormatter(writer)
  129. The standard formatter. This implementation has demonstrated wide applicability
  130. to many writers, and may be used directly in most circumstances. It has been
  131. used to implement a full-featured World Wide Web browser.
  132. .. _writer-interface:
  133. The Writer Interface
  134. --------------------
  135. Interfaces to create writers are dependent on the specific writer class being
  136. instantiated. The interfaces described below are the required interfaces which
  137. all writers must support once initialized. Note that while most applications can
  138. use the :class:`AbstractFormatter` class as a formatter, the writer must
  139. typically be provided by the application.
  140. .. method:: writer.flush()
  141. Flush any buffered output or device control events.
  142. .. method:: writer.new_alignment(align)
  143. Set the alignment style. The *align* value can be any object, but by convention
  144. is a string or ``None``, where ``None`` indicates that the writer's "preferred"
  145. alignment should be used. Conventional *align* values are ``'left'``,
  146. ``'center'``, ``'right'``, and ``'justify'``.
  147. .. method:: writer.new_font(font)
  148. Set the font style. The value of *font* will be ``None``, indicating that the
  149. device's default font should be used, or a tuple of the form ``(size,
  150. italic, bold, teletype)``. Size will be a string indicating the size of
  151. font that should be used; specific strings and their interpretation must be
  152. defined by the application. The *italic*, *bold*, and *teletype* values are
  153. Boolean values specifying which of those font attributes should be used.
  154. .. method:: writer.new_margin(margin, level)
  155. Set the margin level to the integer *level* and the logical tag to *margin*.
  156. Interpretation of the logical tag is at the writer's discretion; the only
  157. restriction on the value of the logical tag is that it not be a false value for
  158. non-zero values of *level*.
  159. .. method:: writer.new_spacing(spacing)
  160. Set the spacing style to *spacing*.
  161. .. method:: writer.new_styles(styles)
  162. Set additional styles. The *styles* value is a tuple of arbitrary values; the
  163. value :const:`AS_IS` should be ignored. The *styles* tuple may be interpreted
  164. either as a set or as a stack depending on the requirements of the application
  165. and writer implementation.
  166. .. method:: writer.send_line_break()
  167. Break the current line.
  168. .. method:: writer.send_paragraph(blankline)
  169. Produce a paragraph separation of at least *blankline* blank lines, or the
  170. equivalent. The *blankline* value will be an integer. Note that the
  171. implementation will receive a call to :meth:`send_line_break` before this call
  172. if a line break is needed; this method should not include ending the last line
  173. of the paragraph. It is only responsible for vertical spacing between
  174. paragraphs.
  175. .. method:: writer.send_hor_rule(*args, **kw)
  176. Display a horizontal rule on the output device. The arguments to this method
  177. are entirely application- and writer-specific, and should be interpreted with
  178. care. The method implementation may assume that a line break has already been
  179. issued via :meth:`send_line_break`.
  180. .. method:: writer.send_flowing_data(data)
  181. Output character data which may be word-wrapped and re-flowed as needed. Within
  182. any sequence of calls to this method, the writer may assume that spans of
  183. multiple whitespace characters have been collapsed to single space characters.
  184. .. method:: writer.send_literal_data(data)
  185. Output character data which has already been formatted for display. Generally,
  186. this should be interpreted to mean that line breaks indicated by newline
  187. characters should be preserved and no new line breaks should be introduced. The
  188. data may contain embedded newline and tab characters, unlike data provided to
  189. the :meth:`send_formatted_data` interface.
  190. .. method:: writer.send_label_data(data)
  191. Set *data* to the left of the current left margin, if possible. The value of
  192. *data* is not restricted; treatment of non-string values is entirely
  193. application- and writer-dependent. This method will only be called at the
  194. beginning of a line.
  195. .. _writer-impls:
  196. Writer Implementations
  197. ----------------------
  198. Three implementations of the writer object interface are provided as examples by
  199. this module. Most applications will need to derive new writer classes from the
  200. :class:`NullWriter` class.
  201. .. class:: NullWriter()
  202. A writer which only provides the interface definition; no actions are taken on
  203. any methods. This should be the base class for all writers which do not need to
  204. inherit any implementation methods.
  205. .. class:: AbstractWriter()
  206. A writer which can be used in debugging formatters, but not much else. Each
  207. method simply announces itself by printing its name and arguments on standard
  208. output.
  209. .. class:: DumbWriter(file=None, maxcol=72)
  210. Simple writer class which writes output on the :term:`file object` passed
  211. in as *file* or, if *file* is omitted, on standard output. The output is
  212. simply word-wrapped to the number of columns specified by *maxcol*. This
  213. class is suitable for reflowing a sequence of paragraphs.