Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/docutils/readers/__init__.py @ 0:4f3585e2f14b draft default tip
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author | shellac |
---|---|
date | Mon, 22 Mar 2021 18:12:50 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4f3585e2f14b |
---|---|
1 # $Id: __init__.py 8239 2018-11-21 21:46:00Z milde $ | |
2 # Authors: David Goodger <goodger@python.org>; Ueli Schlaepfer | |
3 # Copyright: This module has been placed in the public domain. | |
4 | |
5 """ | |
6 This package contains Docutils Reader modules. | |
7 """ | |
8 | |
9 __docformat__ = 'reStructuredText' | |
10 | |
11 import sys | |
12 | |
13 from docutils import utils, parsers, Component | |
14 from docutils.transforms import universal | |
15 | |
16 | |
17 class Reader(Component): | |
18 | |
19 """ | |
20 Abstract base class for docutils Readers. | |
21 | |
22 Each reader module or package must export a subclass also called 'Reader'. | |
23 | |
24 The two steps of a Reader's responsibility are to read data from the | |
25 source Input object and parse the data with the Parser object. | |
26 Call `read()` to process a document. | |
27 """ | |
28 | |
29 component_type = 'reader' | |
30 config_section = 'readers' | |
31 | |
32 def get_transforms(self): | |
33 return Component.get_transforms(self) + [ | |
34 universal.Decorations, | |
35 universal.ExposeInternals, | |
36 universal.StripComments,] | |
37 | |
38 def __init__(self, parser=None, parser_name=None): | |
39 """ | |
40 Initialize the Reader instance. | |
41 | |
42 Several instance attributes are defined with dummy initial values. | |
43 Subclasses may use these attributes as they wish. | |
44 """ | |
45 | |
46 self.parser = parser | |
47 """A `parsers.Parser` instance shared by all doctrees. May be left | |
48 unspecified if the document source determines the parser.""" | |
49 | |
50 if parser is None and parser_name: | |
51 self.set_parser(parser_name) | |
52 | |
53 self.source = None | |
54 """`docutils.io` IO object, source of input data.""" | |
55 | |
56 self.input = None | |
57 """Raw text input; either a single string or, for more complex cases, | |
58 a collection of strings.""" | |
59 | |
60 def set_parser(self, parser_name): | |
61 """Set `self.parser` by name.""" | |
62 parser_class = parsers.get_parser_class(parser_name) | |
63 self.parser = parser_class() | |
64 | |
65 def read(self, source, parser, settings): | |
66 self.source = source | |
67 if not self.parser: | |
68 self.parser = parser | |
69 self.settings = settings | |
70 self.input = self.source.read() | |
71 self.parse() | |
72 return self.document | |
73 | |
74 def parse(self): | |
75 """Parse `self.input` into a document tree.""" | |
76 self.document = document = self.new_document() | |
77 self.parser.parse(self.input, document) | |
78 document.current_source = document.current_line = None | |
79 | |
80 def new_document(self): | |
81 """Create and return a new empty document tree (root node).""" | |
82 document = utils.new_document(self.source.source_path, self.settings) | |
83 return document | |
84 | |
85 | |
86 class ReReader(Reader): | |
87 | |
88 """ | |
89 A reader which rereads an existing document tree (e.g. a | |
90 deserializer). | |
91 | |
92 Often used in conjunction with `writers.UnfilteredWriter`. | |
93 """ | |
94 | |
95 def get_transforms(self): | |
96 # Do not add any transforms. They have already been applied | |
97 # by the reader which originally created the document. | |
98 return Component.get_transforms(self) | |
99 | |
100 | |
101 _reader_aliases = {} | |
102 | |
103 def get_reader_class(reader_name): | |
104 """Return the Reader class from the `reader_name` module.""" | |
105 reader_name = reader_name.lower() | |
106 if reader_name in _reader_aliases: | |
107 reader_name = _reader_aliases[reader_name] | |
108 try: | |
109 module = __import__(reader_name, globals(), locals(), level=1) | |
110 except ImportError: | |
111 module = __import__(reader_name, globals(), locals(), level=0) | |
112 return module.Reader |