__init__.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # $Id: __init__.py 9026 2022-03-04 15:57:13Z milde $
  2. # Authors: David Goodger <goodger@python.org>; Ueli Schlaepfer
  3. # Copyright: This module has been placed in the public domain.
  4. """
  5. This package contains Docutils Reader modules.
  6. """
  7. __docformat__ = 'reStructuredText'
  8. from importlib import import_module
  9. from docutils import utils, parsers, Component
  10. from docutils.transforms import universal
  11. class Reader(Component):
  12. """
  13. Abstract base class for docutils Readers.
  14. Each reader module or package must export a subclass also called 'Reader'.
  15. The two steps of a Reader's responsibility are to read data from the
  16. source Input object and parse the data with the Parser object.
  17. Call `read()` to process a document.
  18. """
  19. component_type = 'reader'
  20. config_section = 'readers'
  21. def get_transforms(self):
  22. return Component.get_transforms(self) + [universal.Decorations,
  23. universal.ExposeInternals,
  24. universal.StripComments]
  25. def __init__(self, parser=None, parser_name=None):
  26. """
  27. Initialize the Reader instance.
  28. Several instance attributes are defined with dummy initial values.
  29. Subclasses may use these attributes as they wish.
  30. """
  31. self.parser = parser
  32. """A `parsers.Parser` instance shared by all doctrees. May be left
  33. unspecified if the document source determines the parser."""
  34. if parser is None and parser_name:
  35. self.set_parser(parser_name)
  36. self.source = None
  37. """`docutils.io` IO object, source of input data."""
  38. self.input = None
  39. """Raw text input; either a single string or, for more complex cases,
  40. a collection of strings."""
  41. def set_parser(self, parser_name):
  42. """Set `self.parser` by name."""
  43. parser_class = parsers.get_parser_class(parser_name)
  44. self.parser = parser_class()
  45. def read(self, source, parser, settings):
  46. self.source = source
  47. if not self.parser:
  48. self.parser = parser
  49. self.settings = settings
  50. self.input = self.source.read()
  51. self.parse()
  52. return self.document
  53. def parse(self):
  54. """Parse `self.input` into a document tree."""
  55. self.document = document = self.new_document()
  56. self.parser.parse(self.input, document)
  57. document.current_source = document.current_line = None
  58. def new_document(self):
  59. """Create and return a new empty document tree (root node)."""
  60. return utils.new_document(self.source.source_path, self.settings)
  61. class ReReader(Reader):
  62. """
  63. A reader which rereads an existing document tree (e.g. a
  64. deserializer).
  65. Often used in conjunction with `writers.UnfilteredWriter`.
  66. """
  67. def get_transforms(self):
  68. # Do not add any transforms. They have already been applied
  69. # by the reader which originally created the document.
  70. return Component.get_transforms(self)
  71. _reader_aliases = {}
  72. def get_reader_class(reader_name):
  73. """Return the Reader class from the `reader_name` module."""
  74. name = reader_name.lower()
  75. name = _reader_aliases.get(name, name)
  76. try:
  77. module = import_module('docutils.readers.'+name)
  78. except ImportError:
  79. try:
  80. module = import_module(name)
  81. except ImportError as err:
  82. raise ImportError(f'Reader "{reader_name}" not found. {err}')
  83. return module.Reader