Menu

#272 get_parser_class problems loading parsers from other modules

closed-rejected
nobody
None
5
2020-03-03
2015-02-28
No

Due to the implementation of get_parser_class, it is impossible to load a parser from a package other than docutils, unless the parser is in the top level package. This makes it annoying to write custom parsers in some other package.

I would like to be able to do something like get_parser_class('foo.bar') to load foo.bar.Parser.

The fix is to add the desired name ('Parser') to the import, i.e.:

:::python
try:
    module = __import__(parser_name, globals(), locals(), ['Parser'], level=1)
except ImportError:
    module = __import__(parser_name, globals(), locals(), ['Parser'], level=0)

I'm also not crazy about forcing the name to lower case, since that prevents using a parser from any module with a non-lower-cast name. A better solution would seem to be to convert the name to lower case only when checking for an alias, and then add an alias 'rst': 'rst' so that 'rst' with any capitalization maps to 'rst' in all lower case.

Discussion

  • Günter Milde

    Günter Milde - 2015-03-01
    • status: open --> closed-rejected
     
  • Günter Milde

    Günter Milde - 2015-03-01

    The "get_*_class" methods of the "Publisher" as well as the "reader_name",
    "parser_name", and "writer_name" argument are intended for use with registered readers, writers, and parsers from the Docutils core.

    For custom readers, parsers, and writers, the "Publisher" class and the "publish_*" convenience functions provide the "reader", "writer" and "parser" arguments, that expect a class instance.
    This offers the flexibility to import from anywhere and without case folding.

    See for example the HTML4trans writer frontend in the sandbox:

    http://docutils.sourceforge.net/sandbox/html4trans/tools/rst2html_trans.py

     

Log in to post a comment.

Monday.com Logo