Fix [3541369] Relative __import__ also with Python 3.3.
[docutils.git] / docutils / parsers / __init__.py
blob341e358f539ea9a4dd7697e0c8ae74a14aefe8b3
1 # $Id$
2 # Author: David Goodger <goodger@python.org>
3 # Copyright: This module has been placed in the public domain.
5 """
6 This package contains Docutils parser modules.
7 """
9 __docformat__ = 'reStructuredText'
11 import sys
12 from docutils import Component
13 if sys.version_info < (2,5):
14 from docutils._compat import __import__
17 class Parser(Component):
19 component_type = 'parser'
20 config_section = 'parsers'
22 def parse(self, inputstring, document):
23 """Override to parse `inputstring` into document tree `document`."""
24 raise NotImplementedError('subclass must override this method')
26 def setup_parse(self, inputstring, document):
27 """Initial parse setup. Call at start of `self.parse()`."""
28 self.inputstring = inputstring
29 self.document = document
30 document.reporter.attach_observer(document.note_parse_message)
32 def finish_parse(self):
33 """Finalize parse details. Call at end of `self.parse()`."""
34 self.document.reporter.detach_observer(
35 self.document.note_parse_message)
38 _parser_aliases = {
39 'restructuredtext': 'rst',
40 'rest': 'rst',
41 'restx': 'rst',
42 'rtxt': 'rst',}
44 def get_parser_class(parser_name):
45 """Return the Parser class from the `parser_name` module."""
46 parser_name = parser_name.lower()
47 if parser_name in _parser_aliases:
48 parser_name = _parser_aliases[parser_name]
49 module = __import__(parser_name, globals(), locals(), level=1)
50 return module.Parser