From: David G. <go...@py...> - 2010-06-02 22:41:20
|
On Wed, Jun 2, 2010 at 18:30, Ben Finney <ben...@be...> wrote: > David Goodger <go...@py...> writes: > >> On Wed, Jun 2, 2010 at 10:03, Ben Finney <ben...@be...> wrote: >> > My reading of the Docutils API documentation indicates that a Writer >> > usually lives inside a separate module. > > For reference, I got this impression from PEP 258: > > Each writer is a module or package exporting a "Writer" class with a > "write" method. > > <URL:http://www.python.org/dev/peps/pep-0258/#writers> That's for the general case of library code that's available to client code. Your case has a specific, single-purpose writer. > I couldn't find a more up-to-date description of the architecture where > it might have said writers can simply be classes and not necessarily > entire modules. You have to read between the lines, or better yet: use the source, Luke! ;-) >> > Is it possible to define a Writer class and use it directly within >> > the same program module? >> >> Sure, no problem: >> >> """ >> from docutils.core import publish_cmdline, default_description >> from docutils import writers >> >> description = ('[custom text] ' + default_description) >> >> class MyWriter(writers.Writer): >> # custom writer code here >> >> publish_cmdline(writer=MyWriter(), description=description) >> """ >> >> See existing front-end tools, >> http://docutils.sourceforge.net/docs/api/cmdline-tool.html, and >> http://docutils.sourceforge.net/docs/api/publisher.html for more >> details. > > Thanks. I kept seeing reference only to the ‘writer_name’ argument, so > that gives the impression that writers can only be referenced by the > name of a writer module. The docutils.core module has lots of API docs in the docstrings (hint, hint). -- David Goodger <http://python.net/~goodger> |