From 101671ae44e1686680c80cd07b452aabeb88fb63 Mon Sep 17 00:00:00 2001 From: goodger Date: Sat, 20 Apr 2002 03:01:52 +0000 Subject: Initial revision git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@18 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 docutils/core.py (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py new file mode 100644 index 000000000..b553b07b7 --- /dev/null +++ b/docutils/core.py @@ -0,0 +1,85 @@ +#! /usr/bin/env python + +""" +:Authors: David Goodger +:Contact: goodger@users.sourceforge.net +:Revision: $Revision$ +:Date: $Date$ +:Copyright: This module has been placed in the public domain. + + +""" + +__docformat__ = 'reStructuredText' + + +import readers, parsers, writers, utils + + +class Publisher: + + """ + Publisher encapsulates the high-level logic of a Docutils system. + """ + + reporter = None + """A `utils.Reporter` instance used for all document processing.""" + + def __init__(self, reader=None, parser=None, writer=None, reporter=None, + languagecode='en', warninglevel=2, errorlevel=4, + warningstream=None, debug=0): + """ + Initial setup. If any of `reader`, `parser`, or `writer` are + not specified, the corresponding 'set*' method should be + called. + """ + self.reader = reader + self.parser = parser + self.writer = writer + if not reporter: + reporter = utils.Reporter(warninglevel, errorlevel, warningstream, + debug) + self.reporter = reporter + self.languagecode = languagecode + + def setreader(self, readername, languagecode=None): + """Set `self.reader` by name.""" + readerclass = readers.get_reader_class(readername) + self.reader = readerclass(self.reporter, + languagecode or self.languagecode) + + def setparser(self, parsername): + """Set `self.parser` by name.""" + parserclass = parsers.get_parser_class(parsername) + self.parser = parserclass() + + def setwriter(self, writername): + """Set `self.writer` by name.""" + writerclass = writers.get_writer_class(writername) + self.writer = writerclass() + + def publish(self, source, destination): + """ + Run `source` through `self.reader`, then through `self.writer` to + `destination`. + """ + document = self.reader.read(source, self.parser) + self.writer.write(document, destination) + + +def publish(source=None, destination=None, + reader=None, readername='standalone', + parser=None, parsername='restructuredtext', + writer=None, writername='pprint', + reporter=None, languagecode='en', + warninglevel=2, errorlevel=4, warningstream=None, debug=0): + """Set up & run a `Publisher`.""" + pub = Publisher(reader, parser, writer, reporter, languagecode, + warninglevel, errorlevel, warningstream, debug) + if reader is None: + pub.setreader(readername) + if parser is None: + pub.setparser(parsername) + if writer is None: + pub.setwriter(writername) + pub.publish(source, destination) -- cgit v1.2.1 From e13920cf12052d05709108e6c787817b8b36c42e Mon Sep 17 00:00:00 2001 From: goodger Date: Sun, 5 May 2002 15:19:18 +0000 Subject: refactored; improved compound names git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@73 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 63 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 33 insertions(+), 30 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index b553b07b7..a578fef5f 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -7,27 +7,30 @@ :Date: $Date$ :Copyright: This module has been placed in the public domain. - +Calling the `publish` convenience function (or instantiating a +`Publisher` object) with component names will result in default +behavior. For custom behavior (setting component options), create +custom component objects first, and pass *them* to +`publish`/`Publisher`. """ __docformat__ = 'reStructuredText' - import readers, parsers, writers, utils class Publisher: """ - Publisher encapsulates the high-level logic of a Docutils system. + A facade encapsulating the high-level logic of a Docutils system. """ reporter = None """A `utils.Reporter` instance used for all document processing.""" def __init__(self, reader=None, parser=None, writer=None, reporter=None, - languagecode='en', warninglevel=2, errorlevel=4, - warningstream=None, debug=0): + language_code='en', warning_level=2, error_level=4, + warning_stream=None, debug=0): """ Initial setup. If any of `reader`, `parser`, or `writer` are not specified, the corresponding 'set*' method should be @@ -37,26 +40,27 @@ class Publisher: self.parser = parser self.writer = writer if not reporter: - reporter = utils.Reporter(warninglevel, errorlevel, warningstream, - debug) + reporter = utils.Reporter(warning_level, error_level, + warning_stream, debug) self.reporter = reporter - self.languagecode = languagecode + self.language_code = language_code - def setreader(self, readername, languagecode=None): + def set_reader(self, reader_name, parser, parser_name, + language_code=None): """Set `self.reader` by name.""" - readerclass = readers.get_reader_class(readername) - self.reader = readerclass(self.reporter, - languagecode or self.languagecode) + reader_class = readers.get_reader_class(reader_name) + self.reader = reader_class(self.reporter, parser, parser_name, + language_code or self.language_code) - def setparser(self, parsername): + def set_parser(self, parser_name): """Set `self.parser` by name.""" - parserclass = parsers.get_parser_class(parsername) - self.parser = parserclass() + parser_class = parsers.get_parser_class(parser_name) + self.parser = parser_class() - def setwriter(self, writername): + def set_writer(self, writer_name): """Set `self.writer` by name.""" - writerclass = writers.get_writer_class(writername) - self.writer = writerclass() + writer_class = writers.get_writer_class(writer_name) + self.writer = writer_class() def publish(self, source, destination): """ @@ -68,18 +72,17 @@ class Publisher: def publish(source=None, destination=None, - reader=None, readername='standalone', - parser=None, parsername='restructuredtext', - writer=None, writername='pprint', - reporter=None, languagecode='en', - warninglevel=2, errorlevel=4, warningstream=None, debug=0): - """Set up & run a `Publisher`.""" - pub = Publisher(reader, parser, writer, reporter, languagecode, - warninglevel, errorlevel, warningstream, debug) + reader=None, reader_name='standalone', + parser=None, parser_name='restructuredtext', + writer=None, writer_name='pseudoxml', + reporter=None, language_code='en', + warning_level=2, error_level=4, warning_stream=None, debug=0): + """A convenience function; set up & run a `Publisher`.""" + pub = Publisher(reader, parser, writer, reporter, language_code, + warning_level, error_level, warning_stream, debug) if reader is None: - pub.setreader(readername) - if parser is None: - pub.setparser(parsername) + pub.set_reader(reader_name, parser, parser_name) if writer is None: - pub.setwriter(writername) + pub.set_writer(writer_name) pub.publish(source, destination) + -- cgit v1.2.1 From da1419a26009c34ea88c885bca2ef99f455c0708 Mon Sep 17 00:00:00 2001 From: goodger Date: Sat, 18 May 2002 02:57:27 +0000 Subject: docstring fix git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@132 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index a578fef5f..ffb86303c 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -10,7 +10,7 @@ Calling the `publish` convenience function (or instantiating a `Publisher` object) with component names will result in default behavior. For custom behavior (setting component options), create -custom component objects first, and pass *them* to +custom component objects first, and pass *them* to `publish`/`Publisher`. """ @@ -33,8 +33,8 @@ class Publisher: warning_stream=None, debug=0): """ Initial setup. If any of `reader`, `parser`, or `writer` are - not specified, the corresponding 'set*' method should be - called. + not specified, the corresponding ``set_...`` method should be + called with a component name. """ self.reader = reader self.parser = parser @@ -85,4 +85,3 @@ def publish(source=None, destination=None, if writer is None: pub.set_writer(writer_name) pub.publish(source, destination) - -- cgit v1.2.1 From 060ae1cc18c6362cbfc28c6ee525ff32616ad1fb Mon Sep 17 00:00:00 2001 From: goodger Date: Fri, 24 May 2002 03:08:09 +0000 Subject: - Changed names of Reporter's thresholds: warning_level -> report_level; error_level -> halt_level. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@143 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index ffb86303c..3cab9a5c1 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -29,7 +29,7 @@ class Publisher: """A `utils.Reporter` instance used for all document processing.""" def __init__(self, reader=None, parser=None, writer=None, reporter=None, - language_code='en', warning_level=2, error_level=4, + language_code='en', report_level=2, halt_level=4, warning_stream=None, debug=0): """ Initial setup. If any of `reader`, `parser`, or `writer` are @@ -40,7 +40,7 @@ class Publisher: self.parser = parser self.writer = writer if not reporter: - reporter = utils.Reporter(warning_level, error_level, + reporter = utils.Reporter(report_level, halt_level, warning_stream, debug) self.reporter = reporter self.language_code = language_code @@ -76,10 +76,10 @@ def publish(source=None, destination=None, parser=None, parser_name='restructuredtext', writer=None, writer_name='pseudoxml', reporter=None, language_code='en', - warning_level=2, error_level=4, warning_stream=None, debug=0): + report_level=2, halt_level=4, warning_stream=None, debug=0): """A convenience function; set up & run a `Publisher`.""" pub = Publisher(reader, parser, writer, reporter, language_code, - warning_level, error_level, warning_stream, debug) + report_level, halt_level, warning_stream, debug) if reader is None: pub.set_reader(reader_name, parser, parser_name) if writer is None: -- cgit v1.2.1 From 94bef8e11867100fe10a5daf83bde3db0019edd9 Mon Sep 17 00:00:00 2001 From: goodger Date: Thu, 30 May 2002 02:10:56 +0000 Subject: - Removed many keyword parameters to ``Publisher.__init__()`` and ``publish()``; bundled into an option values object. Added ``argv`` and ``usage`` parameters for command-line support. - Added ``Publisher.process_command_line()`` and ``.set_options()`` methods. - Added support for an option values object which carries default settings and overrides (from command-line options and library use). - Cleaned up imports: no more relative package imports or comma-separated lists of top-level modules. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@149 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 109 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 72 insertions(+), 37 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 3cab9a5c1..7c28d800a 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -16,7 +16,10 @@ custom component objects first, and pass *them* to __docformat__ = 'reStructuredText' -import readers, parsers, writers, utils +import sys +from docutils import Component +from docutils import readers, parsers, writers +from docutils.frontend import OptionParser class Publisher: @@ -25,63 +28,95 @@ class Publisher: A facade encapsulating the high-level logic of a Docutils system. """ - reporter = None - """A `utils.Reporter` instance used for all document processing.""" - - def __init__(self, reader=None, parser=None, writer=None, reporter=None, - language_code='en', report_level=2, halt_level=4, - warning_stream=None, debug=0): + def __init__(self, reader=None, parser=None, writer=None): """ - Initial setup. If any of `reader`, `parser`, or `writer` are - not specified, the corresponding ``set_...`` method should be - called with a component name. + Initial setup. If any of `reader`, `parser`, or `writer` are not + specified, the corresponding ``set_...`` method should be called with + a component name (`set_reader` sets the parser as well). """ + self.reader = reader + """A `readers.Reader` instance.""" + self.parser = parser + """A `parsers.Parser` instance.""" + self.writer = writer - if not reporter: - reporter = utils.Reporter(report_level, halt_level, - warning_stream, debug) - self.reporter = reporter - self.language_code = language_code - - def set_reader(self, reader_name, parser, parser_name, - language_code=None): + """A `writers.Writer` instance.""" + + self.options = None + """An object containing Docutils settings as instance attributes. + Set by `self.process_command_line()` or `self.set_options()`.""" + + self.source = None + """The source of input data.""" + + self.destination = None + """The destination for docutils output.""" + + def set_reader(self, reader_name, parser, parser_name): """Set `self.reader` by name.""" reader_class = readers.get_reader_class(reader_name) - self.reader = reader_class(self.reporter, parser, parser_name, - language_code or self.language_code) - - def set_parser(self, parser_name): - """Set `self.parser` by name.""" - parser_class = parsers.get_parser_class(parser_name) - self.parser = parser_class() + self.reader = reader_class(parser, parser_name) def set_writer(self, writer_name): """Set `self.writer` by name.""" writer_class = writers.get_writer_class(writer_name) self.writer = writer_class() - def publish(self, source, destination): + def set_options(self, **defaults): + """ + Set default option values (keyword arguments). + + Set components first (`self.set_reader` & `self.set_writer`). + Overrides the command line. + """ + option_parser = OptionParser( + components=(self.reader, self.parser, self.writer), + defaults=defaults) + self.options = option_parser.get_default_values() + + def process_command_line(self, argv=None, usage=None): + """ + Pass an empty list to `argv` to avoid reading `sys.argv` (the + default). + + Set components first (`self.set_reader` & `self.set_writer`). + """ + option_parser = OptionParser( + components=(self.reader, self.parser, self.writer), usage=usage) + if argv is None: + argv = sys.argv[1:] + self.options, self.source, self.destination \ + = option_parser.parse_args(argv) + """ + # For testing purposes: + from pprint import pformat + print 'options:' + print pformat(options.__dict__) + print 'source=%r, destination=%r' % (source, destination) + sys.exit(0) + """ + + def publish(self, argv=None, usage=None): """ - Run `source` through `self.reader`, then through `self.writer` to - `destination`. + Process command line options and arguments, run `self.reader` + and then `self.writer`. """ - document = self.reader.read(source, self.parser) - self.writer.write(document, destination) + if self.options is None: + self.process_command_line(argv, usage) + document = self.reader.read(self.source, self.parser, self.options) + self.writer.write(document, self.destination) -def publish(source=None, destination=None, - reader=None, reader_name='standalone', +def publish(reader=None, reader_name='standalone', parser=None, parser_name='restructuredtext', writer=None, writer_name='pseudoxml', - reporter=None, language_code='en', - report_level=2, halt_level=4, warning_stream=None, debug=0): + argv=None, usage=None): """A convenience function; set up & run a `Publisher`.""" - pub = Publisher(reader, parser, writer, reporter, language_code, - report_level, halt_level, warning_stream, debug) + pub = Publisher(reader, parser, writer) if reader is None: pub.set_reader(reader_name, parser, parser_name) if writer is None: pub.set_writer(writer_name) - pub.publish(source, destination) + pub.publish(argv, usage) -- cgit v1.2.1 From 0e41749d3da1e831a98aefb848c6a168894db738 Mon Sep 17 00:00:00 2001 From: goodger Date: Fri, 31 May 2002 00:50:04 +0000 Subject: docstring git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@163 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 7c28d800a..f64e00b8c 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -69,7 +69,8 @@ class Publisher: Set default option values (keyword arguments). Set components first (`self.set_reader` & `self.set_writer`). - Overrides the command line. + Explicitly setting options disables command line option processing + from `self.publish()`. """ option_parser = OptionParser( components=(self.reader, self.parser, self.writer), -- cgit v1.2.1 From 4c89d002f8f9017caa9ff690e0a82d1ec7556f5d Mon Sep 17 00:00:00 2001 From: goodger Date: Sat, 1 Jun 2002 01:42:51 +0000 Subject: Removed temporary testing code. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@171 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 8 -------- 1 file changed, 8 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index f64e00b8c..f46527a9f 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -90,14 +90,6 @@ class Publisher: argv = sys.argv[1:] self.options, self.source, self.destination \ = option_parser.parse_args(argv) - """ - # For testing purposes: - from pprint import pformat - print 'options:' - print pformat(options.__dict__) - print 'source=%r, destination=%r' % (source, destination) - sys.exit(0) - """ def publish(self, argv=None, usage=None): """ -- cgit v1.2.1 From 361fcaa4671d334ee92294de947ad99546469bd5 Mon Sep 17 00:00:00 2001 From: goodger Date: Thu, 13 Jun 2002 03:25:41 +0000 Subject: Updated for new Optik option group functionality. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@180 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index f46527a9f..1fe6af418 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -77,7 +77,7 @@ class Publisher: defaults=defaults) self.options = option_parser.get_default_values() - def process_command_line(self, argv=None, usage=None): + def process_command_line(self, argv=None, usage=None, description=None): """ Pass an empty list to `argv` to avoid reading `sys.argv` (the default). @@ -85,19 +85,20 @@ class Publisher: Set components first (`self.set_reader` & `self.set_writer`). """ option_parser = OptionParser( - components=(self.reader, self.parser, self.writer), usage=usage) + components=(self.reader, self.parser, self.writer), + usage=usage, description=description) if argv is None: argv = sys.argv[1:] self.options, self.source, self.destination \ = option_parser.parse_args(argv) - def publish(self, argv=None, usage=None): + def publish(self, argv=None, usage=None, description=None): """ Process command line options and arguments, run `self.reader` and then `self.writer`. """ if self.options is None: - self.process_command_line(argv, usage) + self.process_command_line(argv, usage, description) document = self.reader.read(self.source, self.parser, self.options) self.writer.write(document, self.destination) @@ -105,11 +106,11 @@ class Publisher: def publish(reader=None, reader_name='standalone', parser=None, parser_name='restructuredtext', writer=None, writer_name='pseudoxml', - argv=None, usage=None): + argv=None, usage=None, description=None): """A convenience function; set up & run a `Publisher`.""" pub = Publisher(reader, parser, writer) if reader is None: pub.set_reader(reader_name, parser, parser_name) if writer is None: pub.set_writer(writer_name) - pub.publish(argv, usage) + pub.publish(argv, usage, description) -- cgit v1.2.1 From 373cca31645a6460611519ee0ae27e02076d2495 Mon Sep 17 00:00:00 2001 From: goodger Date: Thu, 27 Jun 2002 01:16:03 +0000 Subject: - Added ``option_spec`` parameter to ``publish()`` etc. - Added "--dump-internal-document-attributes" support. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@204 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 1fe6af418..4a75a9508 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -37,7 +37,7 @@ class Publisher: self.reader = reader """A `readers.Reader` instance.""" - + self.parser = parser """A `parsers.Parser` instance.""" @@ -77,40 +77,45 @@ class Publisher: defaults=defaults) self.options = option_parser.get_default_values() - def process_command_line(self, argv=None, usage=None, description=None): + def process_command_line(self, argv=None, usage=None, description=None, + option_spec=None): """ Pass an empty list to `argv` to avoid reading `sys.argv` (the default). - + Set components first (`self.set_reader` & `self.set_writer`). """ option_parser = OptionParser( - components=(self.reader, self.parser, self.writer), + components=(option_spec, self.reader, self.parser, self.writer), usage=usage, description=description) if argv is None: argv = sys.argv[1:] self.options, self.source, self.destination \ = option_parser.parse_args(argv) - def publish(self, argv=None, usage=None, description=None): + def publish(self, argv=None, usage=None, description=None, + option_spec=None): """ Process command line options and arguments, run `self.reader` and then `self.writer`. """ if self.options is None: - self.process_command_line(argv, usage, description) + self.process_command_line(argv, usage, description, option_spec) document = self.reader.read(self.source, self.parser, self.options) self.writer.write(document, self.destination) + if self.options.dump_internal_document_attributes: + from pprint import pformat + print >>sys.stderr, pformat(document.__dict__) def publish(reader=None, reader_name='standalone', parser=None, parser_name='restructuredtext', writer=None, writer_name='pseudoxml', - argv=None, usage=None, description=None): + argv=None, usage=None, description=None, option_spec=None): """A convenience function; set up & run a `Publisher`.""" pub = Publisher(reader, parser, writer) if reader is None: pub.set_reader(reader_name, parser, parser_name) if writer is None: pub.set_writer(writer_name) - pub.publish(argv, usage, description) + pub.publish(argv, usage, description, option_spec) -- cgit v1.2.1 From 01e6d2ca28acbc89879acefd8e0baa3e6ca8c457 Mon Sep 17 00:00:00 2001 From: goodger Date: Fri, 28 Jun 2002 04:13:57 +0000 Subject: - Added support for the ``docutils.io.IO`` class & subclasses. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@218 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 53 +++++++++++++++++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 18 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 4a75a9508..c0f9f4375 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -18,7 +18,7 @@ __docformat__ = 'reStructuredText' import sys from docutils import Component -from docutils import readers, parsers, writers +from docutils import readers, parsers, writers, io from docutils.frontend import OptionParser @@ -28,7 +28,10 @@ class Publisher: A facade encapsulating the high-level logic of a Docutils system. """ - def __init__(self, reader=None, parser=None, writer=None): + def __init__(self, reader=None, parser=None, writer=None, + source=None, source_class=io.FileIO, + destination=None, destination_class=io.FileIO, + options=None): """ Initial setup. If any of `reader`, `parser`, or `writer` are not specified, the corresponding ``set_...`` method should be called with @@ -44,27 +47,34 @@ class Publisher: self.writer = writer """A `writers.Writer` instance.""" - self.options = None - """An object containing Docutils settings as instance attributes. - Set by `self.process_command_line()` or `self.set_options()`.""" + self.source = source + """The source of input data, an `io.IO` instance.""" + + self.source_class = source_class + """The class for dynamically created source objects.""" - self.source = None - """The source of input data.""" + self.destination = destination + """The destination for docutils output, an `io.IO` instance.""" - self.destination = None - """The destination for docutils output.""" + self.destination_class = destination_class + """The class for dynamically created destination objects.""" + + self.options = options + """An object containing Docutils settings as instance attributes. + Set by `self.process_command_line()` or `self.set_options()`.""" def set_reader(self, reader_name, parser, parser_name): """Set `self.reader` by name.""" reader_class = readers.get_reader_class(reader_name) self.reader = reader_class(parser, parser_name) + self.parser = self.reader.parser def set_writer(self, writer_name): """Set `self.writer` by name.""" writer_class = writers.get_writer_class(writer_name) self.writer = writer_class() - def set_options(self, **defaults): + def set_options(self, option_spec=None, **defaults): """ Set default option values (keyword arguments). @@ -73,12 +83,12 @@ class Publisher: from `self.publish()`. """ option_parser = OptionParser( - components=(self.reader, self.parser, self.writer), + components=(option_spec, self.reader, self.parser, self.writer), defaults=defaults) self.options = option_parser.get_default_values() def process_command_line(self, argv=None, usage=None, description=None, - option_spec=None): + option_spec=None): """ Pass an empty list to `argv` to avoid reading `sys.argv` (the default). @@ -90,29 +100,36 @@ class Publisher: usage=usage, description=description) if argv is None: argv = sys.argv[1:] - self.options, self.source, self.destination \ - = option_parser.parse_args(argv) + self.options, source, destination = option_parser.parse_args(argv) + self.source = self.source_class(self.options, source=source) + self.destination = self.destination_class(self.options, + destination=destination) def publish(self, argv=None, usage=None, description=None, option_spec=None): """ - Process command line options and arguments, run `self.reader` - and then `self.writer`. + Process command line options and arguments (if `self.options` not + already set), run `self.reader` and then `self.writer`. Return + `self.writer`'s output. """ if self.options is None: self.process_command_line(argv, usage, description, option_spec) document = self.reader.read(self.source, self.parser, self.options) - self.writer.write(document, self.destination) + output = self.writer.write(document, self.destination) if self.options.dump_internal_document_attributes: from pprint import pformat print >>sys.stderr, pformat(document.__dict__) + return output def publish(reader=None, reader_name='standalone', parser=None, parser_name='restructuredtext', writer=None, writer_name='pseudoxml', argv=None, usage=None, description=None, option_spec=None): - """A convenience function; set up & run a `Publisher`.""" + """ + A convenience function for file I/O front-ends; set up & run a + `Publisher`. + """ pub = Publisher(reader, parser, writer) if reader is None: pub.set_reader(reader_name, parser, parser_name) -- cgit v1.2.1 From be8b2373908f3c5f02b983b0f65016a0911ad921 Mon Sep 17 00:00:00 2001 From: goodger Date: Thu, 4 Jul 2002 01:36:18 +0000 Subject: updated git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@248 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index c0f9f4375..27a0e60b6 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -101,9 +101,9 @@ class Publisher: if argv is None: argv = sys.argv[1:] self.options, source, destination = option_parser.parse_args(argv) - self.source = self.source_class(self.options, source=source) - self.destination = self.destination_class(self.options, - destination=destination) + self.source = self.source_class(self.options, source_path=source) + self.destination = self.destination_class( + self.options, destination_path=destination) def publish(self, argv=None, usage=None, description=None, option_spec=None): -- cgit v1.2.1 From 65e978f5040e8eceddd3b46797177212cc940729 Mon Sep 17 00:00:00 2001 From: goodger Date: Fri, 19 Jul 2002 02:27:04 +0000 Subject: Updated ``Publisher.set_options()``; now returns option values object. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@321 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 27a0e60b6..24355d094 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -76,16 +76,17 @@ class Publisher: def set_options(self, option_spec=None, **defaults): """ - Set default option values (keyword arguments). + Set and return default option values (keyword arguments). Set components first (`self.set_reader` & `self.set_writer`). Explicitly setting options disables command line option processing from `self.publish()`. """ option_parser = OptionParser( - components=(option_spec, self.reader, self.parser, self.writer), - defaults=defaults) + components=(option_spec, self.reader, self.parser, self.writer)) + option_parser.set_defaults(**defaults) self.options = option_parser.get_default_values() + return self.options def process_command_line(self, argv=None, usage=None, description=None, option_spec=None): -- cgit v1.2.1 From c64f5a97435ccac6a66c79622e5836e4ed8a9b53 Mon Sep 17 00:00:00 2001 From: goodger Date: Sat, 20 Jul 2002 03:15:22 +0000 Subject: - Added support for configuration files (/etc/docutils.conf, ./docutils.conf, ~/.docutils). - Added ``Publisher.setup_option_parser()``. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@335 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 24355d094..5f2af0be3 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -17,9 +17,10 @@ custom component objects first, and pass *them* to __docformat__ = 'reStructuredText' import sys +import os.path from docutils import Component from docutils import readers, parsers, writers, io -from docutils.frontend import OptionParser +from docutils.frontend import OptionParser, ConfigParser class Publisher: @@ -28,6 +29,12 @@ class Publisher: A facade encapsulating the high-level logic of a Docutils system. """ + config_files = ('/etc/docutils.conf', # system-wide + './docutils.conf', # project-specific + os.path.expanduser('~/.docutils')) # user-specific + """Docutils configuration files, using ConfigParser syntax (section + 'options'). Later files override earlier ones.""" + def __init__(self, reader=None, parser=None, writer=None, source=None, source_class=io.FileIO, destination=None, destination_class=io.FileIO, @@ -74,7 +81,21 @@ class Publisher: writer_class = writers.get_writer_class(writer_name) self.writer = writer_class() - def set_options(self, option_spec=None, **defaults): + def setup_option_parser(self, usage=None, description=None, + option_spec=None, **defaults): + config = ConfigParser() + config.read(self.config_files) + if config.has_section('options'): + for option in config.options('options'): + defaults[option] = config.get('options', option) + option_parser = OptionParser( + components=(option_spec, self.reader, self.parser, self.writer), + usage=usage, description=description) + option_parser.set_defaults(**defaults) + return option_parser + + def set_options(self, usage=None, description=None, + option_spec=None, **defaults): """ Set and return default option values (keyword arguments). @@ -82,23 +103,21 @@ class Publisher: Explicitly setting options disables command line option processing from `self.publish()`. """ - option_parser = OptionParser( - components=(option_spec, self.reader, self.parser, self.writer)) - option_parser.set_defaults(**defaults) + option_parser = self.setup_option_parser(usage, description, + option_spec, **defaults) self.options = option_parser.get_default_values() return self.options def process_command_line(self, argv=None, usage=None, description=None, - option_spec=None): + option_spec=None, **defaults): """ Pass an empty list to `argv` to avoid reading `sys.argv` (the default). Set components first (`self.set_reader` & `self.set_writer`). """ - option_parser = OptionParser( - components=(option_spec, self.reader, self.parser, self.writer), - usage=usage, description=description) + option_parser = self.setup_option_parser(usage, description, + option_spec, **defaults) if argv is None: argv = sys.argv[1:] self.options, source, destination = option_parser.parse_args(argv) -- cgit v1.2.1 From 15f41ca47d032c33376f939059faf9eb82ac3f51 Mon Sep 17 00:00:00 2001 From: goodger Date: Thu, 25 Jul 2002 01:46:28 +0000 Subject: Added default usage message and description. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@366 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 5f2af0be3..eb6f35d20 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -136,18 +136,23 @@ class Publisher: self.process_command_line(argv, usage, description, option_spec) document = self.reader.read(self.source, self.parser, self.options) output = self.writer.write(document, self.destination) - if self.options.dump_internal_document_attributes: + if self.options.dump_internals: from pprint import pformat print >>sys.stderr, pformat(document.__dict__) return output +default_usage = '%prog [options] [ []]' +default_description = ('Reads from (default is stdin) and writes to ' + ' (default is stdout).') + def publish(reader=None, reader_name='standalone', parser=None, parser_name='restructuredtext', writer=None, writer_name='pseudoxml', - argv=None, usage=None, description=None, option_spec=None): + argv=None, usage=default_usage, description=default_description, + option_spec=None): """ - A convenience function for file I/O front-ends; set up & run a + A convenience function for file I/O front ends; set up & run a `Publisher`. """ pub = Publisher(reader, parser, writer) -- cgit v1.2.1 From 52307a7656fe38d3f35126a9bd90170e4fd10912 Mon Sep 17 00:00:00 2001 From: goodger Date: Sun, 28 Jul 2002 17:20:00 +0000 Subject: Source & destination paths now returned inside option values object. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@397 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index eb6f35d20..80344a793 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -120,10 +120,11 @@ class Publisher: option_spec, **defaults) if argv is None: argv = sys.argv[1:] - self.options, source, destination = option_parser.parse_args(argv) - self.source = self.source_class(self.options, source_path=source) + self.options = option_parser.parse_args(argv) + self.source = self.source_class(self.options, + source_path=self.options.source) self.destination = self.destination_class( - self.options, destination_path=destination) + self.options, destination_path=self.options.destination) def publish(self, argv=None, usage=None, description=None, option_spec=None): -- cgit v1.2.1 From acd9230bd930f072e3857c81da4a8e138957b379 Mon Sep 17 00:00:00 2001 From: goodger Date: Wed, 31 Jul 2002 01:33:11 +0000 Subject: Updated config file & command-line processing. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@419 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 80344a793..9e836c6be 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -17,9 +17,8 @@ custom component objects first, and pass *them* to __docformat__ = 'reStructuredText' import sys -import os.path from docutils import Component -from docutils import readers, parsers, writers, io +from docutils import frontend, io, readers, parsers, writers from docutils.frontend import OptionParser, ConfigParser @@ -29,12 +28,6 @@ class Publisher: A facade encapsulating the high-level logic of a Docutils system. """ - config_files = ('/etc/docutils.conf', # system-wide - './docutils.conf', # project-specific - os.path.expanduser('~/.docutils')) # user-specific - """Docutils configuration files, using ConfigParser syntax (section - 'options'). Later files override earlier ones.""" - def __init__(self, reader=None, parser=None, writer=None, source=None, source_class=io.FileIO, destination=None, destination_class=io.FileIO, @@ -84,10 +77,10 @@ class Publisher: def setup_option_parser(self, usage=None, description=None, option_spec=None, **defaults): config = ConfigParser() - config.read(self.config_files) - if config.has_section('options'): - for option in config.options('options'): - defaults[option] = config.get('options', option) + config.read_standard_files() + config_options = config.get_section('options') + frontend.make_paths_absolute(config_options) + defaults.update(config_options) option_parser = OptionParser( components=(option_spec, self.reader, self.parser, self.writer), usage=usage, description=description) @@ -121,10 +114,12 @@ class Publisher: if argv is None: argv = sys.argv[1:] self.options = option_parser.parse_args(argv) + + def set_io(self): self.source = self.source_class(self.options, - source_path=self.options.source) + source_path=self.options._source) self.destination = self.destination_class( - self.options, destination_path=self.options.destination) + self.options, destination_path=self.options._destination) def publish(self, argv=None, usage=None, description=None, option_spec=None): @@ -135,6 +130,7 @@ class Publisher: """ if self.options is None: self.process_command_line(argv, usage, description, option_spec) + self.set_io() document = self.reader.read(self.source, self.parser, self.options) output = self.writer.write(document, self.destination) if self.options.dump_internals: -- cgit v1.2.1 From d5f026e798598f57d02ab02ffa4b20efb818473f Mon Sep 17 00:00:00 2001 From: goodger Date: Fri, 9 Aug 2002 01:19:11 +0000 Subject: updated git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@492 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 9e836c6be..6dcc93d8a 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -29,8 +29,8 @@ class Publisher: """ def __init__(self, reader=None, parser=None, writer=None, - source=None, source_class=io.FileIO, - destination=None, destination_class=io.FileIO, + source=None, source_class=io.FileInput, + destination=None, destination_class=io.FileOutput, options=None): """ Initial setup. If any of `reader`, `parser`, or `writer` are not @@ -48,13 +48,13 @@ class Publisher: """A `writers.Writer` instance.""" self.source = source - """The source of input data, an `io.IO` instance.""" + """The source of input data, an `io.Input` instance.""" self.source_class = source_class """The class for dynamically created source objects.""" self.destination = destination - """The destination for docutils output, an `io.IO` instance.""" + """The destination for docutils output, an `io.Output` instance.""" self.destination_class = destination_class """The class for dynamically created destination objects.""" -- cgit v1.2.1 From dc60acc162c0031fabdcfb261ff4e2defda9f782 Mon Sep 17 00:00:00 2001 From: goodger Date: Fri, 23 Aug 2002 01:47:19 +0000 Subject: Made ``publish()`` a bit more convenient. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@573 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 6dcc93d8a..8c291ea3c 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -147,12 +147,12 @@ def publish(reader=None, reader_name='standalone', parser=None, parser_name='restructuredtext', writer=None, writer_name='pseudoxml', argv=None, usage=default_usage, description=default_description, - option_spec=None): + option_spec=None, options=None): """ A convenience function for file I/O front ends; set up & run a `Publisher`. """ - pub = Publisher(reader, parser, writer) + pub = Publisher(reader, parser, writer, options=options) if reader is None: pub.set_reader(reader_name, parser, parser_name) if writer is None: -- cgit v1.2.1 From 473739633ba059db86d233b2b383bc0108d65083 Mon Sep 17 00:00:00 2001 From: goodger Date: Sat, 24 Aug 2002 01:27:18 +0000 Subject: updated git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@592 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 8c291ea3c..9aa33467b 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -76,14 +76,15 @@ class Publisher: def setup_option_parser(self, usage=None, description=None, option_spec=None, **defaults): + option_parser = OptionParser( + components=(option_spec, self.reader, self.parser, self.writer), + usage=usage, description=description) config = ConfigParser() config.read_standard_files() config_options = config.get_section('options') - frontend.make_paths_absolute(config_options) + frontend.make_paths_absolute(config_options, + option_parser.relative_path_options) defaults.update(config_options) - option_parser = OptionParser( - components=(option_spec, self.reader, self.parser, self.writer), - usage=usage, description=description) option_parser.set_defaults(**defaults) return option_parser -- cgit v1.2.1 From 49373e9eff9b650bd989452f2067cf2b78a5cebe Mon Sep 17 00:00:00 2001 From: goodger Date: Sat, 7 Sep 2002 02:42:09 +0000 Subject: updated git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@653 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 9aa33467b..8354d8393 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -77,7 +77,7 @@ class Publisher: def setup_option_parser(self, usage=None, description=None, option_spec=None, **defaults): option_parser = OptionParser( - components=(option_spec, self.reader, self.parser, self.writer), + components=(option_spec, self.parser, self.reader, self.writer), usage=usage, description=description) config = ConfigParser() config.read_standard_files() -- cgit v1.2.1 From efdd39867964b92520a58c6796658895e412c441 Mon Sep 17 00:00:00 2001 From: goodger Date: Wed, 9 Oct 2002 00:51:53 +0000 Subject: changed docstring field lists into comments git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@778 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 8354d8393..6eee3829e 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -1,12 +1,10 @@ -#! /usr/bin/env python +# Authors: David Goodger +# Contact: goodger@users.sourceforge.net +# Revision: $Revision$ +# Date: $Date$ +# Copyright: This module has been placed in the public domain. """ -:Authors: David Goodger -:Contact: goodger@users.sourceforge.net -:Revision: $Revision$ -:Date: $Date$ -:Copyright: This module has been placed in the public domain. - Calling the `publish` convenience function (or instantiating a `Publisher` object) with component names will result in default behavior. For custom behavior (setting component options), create -- cgit v1.2.1 From 694c4be8294331e8a851661f34f0a1071ed80363 Mon Sep 17 00:00:00 2001 From: goodger Date: Thu, 17 Oct 2002 01:36:46 +0000 Subject: Generalized ``Publisher.set_io``. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@809 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 6eee3829e..c243df22d 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -115,10 +115,12 @@ class Publisher: self.options = option_parser.parse_args(argv) def set_io(self): - self.source = self.source_class(self.options, - source_path=self.options._source) - self.destination = self.destination_class( - self.options, destination_path=self.options._destination) + if self.source is None: + self.source = self.source_class(self.options, + source_path=self.options._source) + if self.destination is None: + self.destination = self.destination_class( + self.options, destination_path=self.options._destination) def publish(self, argv=None, usage=None, description=None, option_spec=None): @@ -129,7 +131,7 @@ class Publisher: """ if self.options is None: self.process_command_line(argv, usage, description, option_spec) - self.set_io() + self.set_io() document = self.reader.read(self.source, self.parser, self.options) output = self.writer.write(document, self.destination) if self.options.dump_internals: -- cgit v1.2.1 From afeedfb343c2904e9357997d2a50f8f3cabb2568 Mon Sep 17 00:00:00 2001 From: goodger Date: Fri, 18 Oct 2002 04:55:21 +0000 Subject: Refactored names (options -> settings; .transform() -> .apply(); etc.); updated. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@825 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 89 +++++++++++++++++++++++++++++++------------------------- 1 file changed, 49 insertions(+), 40 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index c243df22d..1e363fee5 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -29,7 +29,7 @@ class Publisher: def __init__(self, reader=None, parser=None, writer=None, source=None, source_class=io.FileInput, destination=None, destination_class=io.FileOutput, - options=None): + settings=None): """ Initial setup. If any of `reader`, `parser`, or `writer` are not specified, the corresponding ``set_...`` method should be called with @@ -57,9 +57,9 @@ class Publisher: self.destination_class = destination_class """The class for dynamically created destination objects.""" - self.options = options + self.settings = settings """An object containing Docutils settings as instance attributes. - Set by `self.process_command_line()` or `self.set_options()`.""" + Set by `self.process_command_line()` or `self.get_settings()`.""" def set_reader(self, reader_name, parser, parser_name): """Set `self.reader` by name.""" @@ -73,35 +73,36 @@ class Publisher: self.writer = writer_class() def setup_option_parser(self, usage=None, description=None, - option_spec=None, **defaults): + settings_spec=None, **defaults): option_parser = OptionParser( - components=(option_spec, self.parser, self.reader, self.writer), + components=(settings_spec, self.parser, self.reader, self.writer), usage=usage, description=description) config = ConfigParser() config.read_standard_files() - config_options = config.get_section('options') - frontend.make_paths_absolute(config_options, - option_parser.relative_path_options) - defaults.update(config_options) + config_settings = config.get_section('options') + frontend.make_paths_absolute(config_settings, + option_parser.relative_path_settings) + defaults.update(config_settings) option_parser.set_defaults(**defaults) return option_parser - def set_options(self, usage=None, description=None, - option_spec=None, **defaults): + def get_settings(self, usage=None, description=None, + settings_spec=None, **defaults): """ - Set and return default option values (keyword arguments). + Set and return default settings (overrides in `defaults` keyword + argument). Set components first (`self.set_reader` & `self.set_writer`). - Explicitly setting options disables command line option processing - from `self.publish()`. + Explicitly setting `self.settings` disables command line option + processing from `self.publish()`. """ option_parser = self.setup_option_parser(usage, description, - option_spec, **defaults) - self.options = option_parser.get_default_values() - return self.options + settings_spec, **defaults) + self.settings = option_parser.get_default_values() + return self.settings def process_command_line(self, argv=None, usage=None, description=None, - option_spec=None, **defaults): + settings_spec=None, **defaults): """ Pass an empty list to `argv` to avoid reading `sys.argv` (the default). @@ -109,32 +110,32 @@ class Publisher: Set components first (`self.set_reader` & `self.set_writer`). """ option_parser = self.setup_option_parser(usage, description, - option_spec, **defaults) + settings_spec, **defaults) if argv is None: argv = sys.argv[1:] - self.options = option_parser.parse_args(argv) + self.settings = option_parser.parse_args(argv) def set_io(self): if self.source is None: - self.source = self.source_class(self.options, - source_path=self.options._source) + self.source = self.source_class(self.settings, + source_path=self.settings._source) if self.destination is None: self.destination = self.destination_class( - self.options, destination_path=self.options._destination) + self.settings, destination_path=self.settings._destination) def publish(self, argv=None, usage=None, description=None, - option_spec=None): + settings_spec=None): """ - Process command line options and arguments (if `self.options` not + Process command line options and arguments (if `self.settings` not already set), run `self.reader` and then `self.writer`. Return `self.writer`'s output. """ - if self.options is None: - self.process_command_line(argv, usage, description, option_spec) + if self.settings is None: + self.process_command_line(argv, usage, description, settings_spec) self.set_io() - document = self.reader.read(self.source, self.parser, self.options) + document = self.reader.read(self.source, self.parser, self.settings) output = self.writer.write(document, self.destination) - if self.options.dump_internals: + if self.settings.dump_internals: from pprint import pformat print >>sys.stderr, pformat(document.__dict__) return output @@ -144,18 +145,26 @@ default_usage = '%prog [options] [ []]' default_description = ('Reads from (default is stdin) and writes to ' ' (default is stdout).') -def publish(reader=None, reader_name='standalone', - parser=None, parser_name='restructuredtext', - writer=None, writer_name='pseudoxml', - argv=None, usage=default_usage, description=default_description, - option_spec=None, options=None): - """ - A convenience function for file I/O front ends; set up & run a - `Publisher`. - """ - pub = Publisher(reader, parser, writer, options=options) +def publish_cmdline(reader=None, reader_name='standalone', + parser=None, parser_name='restructuredtext', + writer=None, writer_name='pseudoxml', + argv=None, usage=default_usage, + description=default_description, + settings_spec=None, settings=None): + """Set up & run a `Publisher`. For command-line front ends.""" + pub = Publisher(reader, parser, writer, settings=settings) if reader is None: pub.set_reader(reader_name, parser, parser_name) if writer is None: pub.set_writer(writer_name) - pub.publish(argv, usage, description, option_spec) + pub.publish(argv, usage, description, settings_spec) + +def publish_file(): + """ + Set up & run a `Publisher`. For programmatic use with file-like I/O. + """ + +def publish_string(): + """ + Set up & run a `Publisher`. For programmatic use with string I/O. + """ -- cgit v1.2.1 From a4736ddfb3d1836db095e199f39842d995657938 Mon Sep 17 00:00:00 2001 From: goodger Date: Fri, 18 Oct 2002 23:40:19 +0000 Subject: Rearranged the parameters of ``publish_cmdline()`` and improved its docstring. Added ``publish_file()`` and ``publish_string()``. Factored ``Publisher.set_source()`` and ``.set_destination()`` out of ``.set_io``. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@828 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 167 +++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 150 insertions(+), 17 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 1e363fee5..5d6927099 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -5,11 +5,11 @@ # Copyright: This module has been placed in the public domain. """ -Calling the `publish` convenience function (or instantiating a +Calling the ``publish_*`` convenience functions (or instantiating a `Publisher` object) with component names will result in default behavior. For custom behavior (setting component options), create custom component objects first, and pass *them* to -`publish`/`Publisher`. +``publish_*``/`Publisher`. """ __docformat__ = 'reStructuredText' @@ -115,23 +115,41 @@ class Publisher: argv = sys.argv[1:] self.settings = option_parser.parse_args(argv) - def set_io(self): + def set_io(self, source_path=None, destination_path=None): if self.source is None: - self.source = self.source_class(self.settings, - source_path=self.settings._source) + self.set_source(source_path=source_path) if self.destination is None: - self.destination = self.destination_class( - self.settings, destination_path=self.settings._destination) + self.set_destination(destination_path=destination_path) + + def set_source(self, source=None, source_path=None): + if source_path is None: + source_path = self.settings._source + else: + self.settings._source = source_path + self.source = self.source_class(self.settings, source=source, + source_path=source_path) + + def set_destination(self, destination=None, destination_path=None): + if destination_path is None: + destination_path = self.settings._destination + else: + self.settings._destination = destination_path + self.destination = self.destination_class( + self.settings, destination=destination, + destination_path=destination_path) def publish(self, argv=None, usage=None, description=None, - settings_spec=None): + settings_spec=None, settings_overrides=None): """ Process command line options and arguments (if `self.settings` not already set), run `self.reader` and then `self.writer`. Return `self.writer`'s output. """ if self.settings is None: - self.process_command_line(argv, usage, description, settings_spec) + self.process_command_line(argv, usage, description, settings_spec, + **(settings_overrides or {})) + elif settings_overrides: + self.settings._update(settings_overrides, 'loose') self.set_io() document = self.reader.read(self.source, self.parser, self.settings) output = self.writer.write(document, self.destination) @@ -148,23 +166,138 @@ default_description = ('Reads from (default is stdin) and writes to ' def publish_cmdline(reader=None, reader_name='standalone', parser=None, parser_name='restructuredtext', writer=None, writer_name='pseudoxml', - argv=None, usage=default_usage, - description=default_description, - settings_spec=None, settings=None): - """Set up & run a `Publisher`. For command-line front ends.""" + settings=None, settings_spec=None, + settings_overrides=None, argv=None, + usage=default_usage, description=default_description): + """ + Set up & run a `Publisher`. For command-line front ends. + + Parameters: + + - `reader`: A `docutils.readers.Reader` object. + - `reader_name`: Name or alias of the Reader class to be instantiated if + no `reader` supplied. + - `parser`: A `docutils.parsers.Parser` object. + - `parser_name`: Name or alias of the Parser class to be instantiated if + no `parser` supplied. + - `writer`: A `docutils.writers.Writer` object. + - `writer_name`: Name or alias of the Writer class to be instantiated if + no `writer` supplied. + - `settings`: Runtime settings object. + - `settings_spec`: Extra settings specification; a `docutils.SettingsSpec` + subclass. Used only if no `settings` specified. + - `settings_overrides`: A dictionary containing program-specific overrides + of component settings. + - `argv`: Command-line argument list to use instead of ``sys.argv[1:]``. + - `usage`: Usage string, output if there's a problem parsing the command + line. + - `description`: Program description, output for the "--help" option + (along with command-line option descriptions). + """ pub = Publisher(reader, parser, writer, settings=settings) if reader is None: pub.set_reader(reader_name, parser, parser_name) if writer is None: pub.set_writer(writer_name) - pub.publish(argv, usage, description, settings_spec) + pub.publish(argv, usage, description, settings_spec, settings_overrides) -def publish_file(): +def publish_file(source=None, source_path=None, + destination=None, destination_path=None, + reader=None, reader_name='standalone', + parser=None, parser_name='restructuredtext', + writer=None, writer_name='pseudoxml', + settings=None, settings_spec=None, settings_overrides=None): """ Set up & run a `Publisher`. For programmatic use with file-like I/O. + + Parameters: + + - `source`: A file-like object (must have "read" and "close" methods). + - `source_path`: Path to the input file. Opened if no `source` supplied. + If neither `source` nor `source_path` are supplied, `sys.stdin` is used. + - `destination`: A file-like object (must have "write" and "close" + methods). + - `destination_path`: Path to the input file. Opened if no `destination` + supplied. If neither `destination` nor `destination_path` are supplied, + `sys.stdout` is used. + - `reader`: A `docutils.readers.Reader` object. + - `reader_name`: Name or alias of the Reader class to be instantiated if + no `reader` supplied. + - `parser`: A `docutils.parsers.Parser` object. + - `parser_name`: Name or alias of the Parser class to be instantiated if + no `parser` supplied. + - `writer`: A `docutils.writers.Writer` object. + - `writer_name`: Name or alias of the Writer class to be instantiated if + no `writer` supplied. + - `settings`: Runtime settings object. + - `settings_spec`: Extra settings specification; a `docutils.SettingsSpec` + subclass. Used only if no `settings` specified. + - `settings_overrides`: A dictionary containing program-specific overrides + of component settings. """ + pub = Publisher(reader, parser, writer, settings=settings) + if reader is None: + pub.set_reader(reader_name, parser, parser_name) + if writer is None: + pub.set_writer(writer_name) + if settings is None: + settings = pub.get_settings(settings_spec=settings_spec) + if settings_overrides: + settings._update(settings_overrides, 'loose') + pub.set_source(source, source_path) + pub.set_destination(destination, destination_path) + pub.publish() -def publish_string(): +def publish_string(source, source_path=None, destination_path=None, + reader=None, reader_name='standalone', + parser=None, parser_name='restructuredtext', + writer=None, writer_name='pseudoxml', + settings=None, settings_spec=None, + settings_overrides=None): """ - Set up & run a `Publisher`. For programmatic use with string I/O. + Set up & run a `Publisher`, and return the string output. + For programmatic use with string I/O. + + For encoded string output, be sure to set the "output_encoding" setting to + the desired encoding. Set it to "unicode" for Unicode string output. + + Parameters: + + - `source`: An input string; required. This can be an encoded 8-big + string (in which case the "input_encoding" setting should be set) or a + Unicode string (in which case set the "input_encoding" setting to + "unicode"). + - `source_path`: Path to the file or object that produced `source`; + optional. Only used for diagnostic output. + - `destination_path`: Path to the file or object which will receive the + output; optional. Used for determining relative paths (stylesheets, + source links, etc.). + - `reader`: A `docutils.readers.Reader` object. + - `reader_name`: Name or alias of the Reader class to be instantiated if + no `reader` supplied. + - `parser`: A `docutils.parsers.Parser` object. + - `parser_name`: Name or alias of the Parser class to be instantiated if + no `parser` supplied. + - `writer`: A `docutils.writers.Writer` object. + - `writer_name`: Name or alias of the Writer class to be instantiated if + no `writer` supplied. + - `settings`: Runtime settings object. + - `settings_spec`: Extra settings specification; a `docutils.SettingsSpec` + subclass. Used only if no `settings` specified. + - `settings_overrides`: A dictionary containing program-specific overrides + of component settings. """ + pub = Publisher(reader, parser, writer, settings=settings, + source_class=io.StringInput, + destination_class=io.StringOutput) + if reader is None: + pub.set_reader(reader_name, parser, parser_name) + if writer is None: + pub.set_writer(writer_name) + if settings is None: + settings = pub.get_settings(settings_spec=settings_spec) + if settings_overrides: + settings._update(settings_overrides, 'loose') + pub.set_source(source, source_path) + pub.set_destination(destination_path=destination_path) + return pub.publish() -- cgit v1.2.1 From 04715867f21e8f700fe73c1409e7c69daa6d4cf2 Mon Sep 17 00:00:00 2001 From: goodger Date: Thu, 24 Oct 2002 00:37:00 +0000 Subject: Added ``Publisher.apply_transforms()`` method. Added support for "--dump-pseudo-xml", "--dump-settings", and "--dump-transforms" hidden options. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@844 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 5d6927099..14e794c7e 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -74,6 +74,7 @@ class Publisher: def setup_option_parser(self, usage=None, description=None, settings_spec=None, **defaults): + #@@@ Add self.source & self.destination to components in future? option_parser = OptionParser( components=(settings_spec, self.parser, self.reader, self.writer), usage=usage, description=description) @@ -138,6 +139,12 @@ class Publisher: self.settings, destination=destination, destination_path=destination_path) + def apply_transforms(self, document): + document.transformer.populate_from_components( + (self.source, self.reader, self.reader.parser, self.writer, + self.destination)) + document.transformer.apply_transforms() + def publish(self, argv=None, usage=None, description=None, settings_spec=None, settings_overrides=None): """ @@ -152,10 +159,24 @@ class Publisher: self.settings._update(settings_overrides, 'loose') self.set_io() document = self.reader.read(self.source, self.parser, self.settings) + self.apply_transforms(document) output = self.writer.write(document, self.destination) + if self.settings.dump_settings: + from pprint import pformat + print >>sys.stderr, '\n::: Docutils settings:' + print >>sys.stderr, pformat(self.settings.__dict__) if self.settings.dump_internals: from pprint import pformat + print >>sys.stderr, '\n::: Docutils internals:' print >>sys.stderr, pformat(document.__dict__) + if self.settings.dump_transforms: + from pprint import pformat + print >>sys.stderr, '\n::: Transforms applied:' + print >>sys.stderr, pformat(document.transformer.applied) + if self.settings.dump_pseudo_xml: + print >>sys.stderr, '\n::: Pseudo-XML:' + print >>sys.stderr, document.pformat().encode( + 'raw_unicode_escape') return output -- cgit v1.2.1 From 35a1beda765cd44715a9ea7c4064d71b1abbcf1e Mon Sep 17 00:00:00 2001 From: goodger Date: Thu, 24 Oct 2002 23:33:52 +0000 Subject: minor updates & fixes git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@859 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 14e794c7e..900498b9e 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -163,11 +163,11 @@ class Publisher: output = self.writer.write(document, self.destination) if self.settings.dump_settings: from pprint import pformat - print >>sys.stderr, '\n::: Docutils settings:' + print >>sys.stderr, '\n::: Runtime settings:' print >>sys.stderr, pformat(self.settings.__dict__) if self.settings.dump_internals: from pprint import pformat - print >>sys.stderr, '\n::: Docutils internals:' + print >>sys.stderr, '\n::: Document internals:' print >>sys.stderr, pformat(document.__dict__) if self.settings.dump_transforms: from pprint import pformat -- cgit v1.2.1 From cd4490bf85dd03ebe0e60f6cdd55d779cf111ffd Mon Sep 17 00:00:00 2001 From: goodger Date: Sat, 2 Nov 2002 23:23:49 +0000 Subject: Added ``Publisher.set_components()`` method; support for ``publish_*()`` conveninece functions. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@879 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 900498b9e..fa99df483 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -72,6 +72,16 @@ class Publisher: writer_class = writers.get_writer_class(writer_name) self.writer = writer_class() + def set_components(self, reader_name, parser_name, writer_name): + if self.reader is None: + self.set_reader(reader_name, self.parser, parser_name) + if self.parser is None: + if self.reader.parser is None: + self.reader.set_parser(parser_name) + self.parser = self.reader.parser + if self.writer is None: + self.set_writer(writer_name) + def setup_option_parser(self, usage=None, description=None, settings_spec=None, **defaults): #@@@ Add self.source & self.destination to components in future? @@ -216,10 +226,7 @@ def publish_cmdline(reader=None, reader_name='standalone', (along with command-line option descriptions). """ pub = Publisher(reader, parser, writer, settings=settings) - if reader is None: - pub.set_reader(reader_name, parser, parser_name) - if writer is None: - pub.set_writer(writer_name) + pub.set_components(reader_name, parser_name, writer_name) pub.publish(argv, usage, description, settings_spec, settings_overrides) def publish_file(source=None, source_path=None, @@ -257,10 +264,7 @@ def publish_file(source=None, source_path=None, of component settings. """ pub = Publisher(reader, parser, writer, settings=settings) - if reader is None: - pub.set_reader(reader_name, parser, parser_name) - if writer is None: - pub.set_writer(writer_name) + pub.set_components(reader_name, parser_name, writer_name) if settings is None: settings = pub.get_settings(settings_spec=settings_spec) if settings_overrides: @@ -280,14 +284,14 @@ def publish_string(source, source_path=None, destination_path=None, For programmatic use with string I/O. For encoded string output, be sure to set the "output_encoding" setting to - the desired encoding. Set it to "unicode" for Unicode string output. + the desired encoding. Set it to "unicode" for unencoded Unicode string + output. Parameters: - - `source`: An input string; required. This can be an encoded 8-big - string (in which case the "input_encoding" setting should be set) or a - Unicode string (in which case set the "input_encoding" setting to - "unicode"). + - `source`: An input string; required. This can be an encoded 8-bit + string (set the "input_encoding" setting to the correct encoding) or a + Unicode string (set the "input_encoding" setting to "unicode"). - `source_path`: Path to the file or object that produced `source`; optional. Only used for diagnostic output. - `destination_path`: Path to the file or object which will receive the @@ -311,10 +315,7 @@ def publish_string(source, source_path=None, destination_path=None, pub = Publisher(reader, parser, writer, settings=settings, source_class=io.StringInput, destination_class=io.StringOutput) - if reader is None: - pub.set_reader(reader_name, parser, parser_name) - if writer is None: - pub.set_writer(writer_name) + pub.set_components(reader_name, parser_name, writer_name) if settings is None: settings = pub.get_settings(settings_spec=settings_spec) if settings_overrides: -- cgit v1.2.1 From 673c882e62ff706d8706218bfe74c203c46c87f5 Mon Sep 17 00:00:00 2001 From: goodger Date: Thu, 28 Nov 2002 03:45:11 +0000 Subject: updated git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@983 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index fa99df483..f73ccf338 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -137,8 +137,9 @@ class Publisher: source_path = self.settings._source else: self.settings._source = source_path - self.source = self.source_class(self.settings, source=source, - source_path=source_path) + self.source = self.source_class( + source=source, source_path=source_path, + encoding=self.settings.input_encoding) def set_destination(self, destination=None, destination_path=None): if destination_path is None: @@ -146,8 +147,8 @@ class Publisher: else: self.settings._destination = destination_path self.destination = self.destination_class( - self.settings, destination=destination, - destination_path=destination_path) + destination=destination, destination_path=destination_path, + encoding=self.settings.output_encoding) def apply_transforms(self, document): document.transformer.populate_from_components( -- cgit v1.2.1 From 3171a1a73139979536cdb785dfd3795e7f40ac30 Mon Sep 17 00:00:00 2001 From: goodger Date: Sat, 29 Mar 2003 14:58:40 +0000 Subject: updated git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@1243 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index f73ccf338..20a4c7f19 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -286,7 +286,13 @@ def publish_string(source, source_path=None, destination_path=None, For encoded string output, be sure to set the "output_encoding" setting to the desired encoding. Set it to "unicode" for unencoded Unicode string - output. + output. Here's how:: + + publish_string(..., settings_overrides={'output_encoding': 'unicode'}) + + Similarly for Unicode string input (`source`):: + + publish_string(..., settings_overrides={'input_encoding': 'unicode'}) Parameters: -- cgit v1.2.1 From ee621bbedd53df1d330732f37c5df4cc1d9e0264 Mon Sep 17 00:00:00 2001 From: goodger Date: Tue, 3 Jun 2003 02:12:18 +0000 Subject: Moved config file processing to docutils/frontend.py. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@1368 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 20a4c7f19..381c65711 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -87,14 +87,8 @@ class Publisher: #@@@ Add self.source & self.destination to components in future? option_parser = OptionParser( components=(settings_spec, self.parser, self.reader, self.writer), + defaults=defaults, read_config_files=1, usage=usage, description=description) - config = ConfigParser() - config.read_standard_files() - config_settings = config.get_section('options') - frontend.make_paths_absolute(config_settings, - option_parser.relative_path_settings) - defaults.update(config_settings) - option_parser.set_defaults(**defaults) return option_parser def get_settings(self, usage=None, description=None, -- cgit v1.2.1 From bb2f2713e8cdf461d76b7354dc14525ae733545a Mon Sep 17 00:00:00 2001 From: goodger Date: Sun, 8 Jun 2003 20:58:58 +0000 Subject: updated git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@1401 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 381c65711..727d4e76a 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -142,7 +142,8 @@ class Publisher: self.settings._destination = destination_path self.destination = self.destination_class( destination=destination, destination_path=destination_path, - encoding=self.settings.output_encoding) + encoding=self.settings.output_encoding, + error_handler=self.settings.output_encoding_error_handler) def apply_transforms(self, document): document.transformer.populate_from_components( -- cgit v1.2.1 From 42355293ce630e6f8f8ab112286417dbfe796beb Mon Sep 17 00:00:00 2001 From: goodger Date: Mon, 16 Jun 2003 17:30:20 +0000 Subject: Added support for exit status ("exit_level" setting & ``enable_exit`` parameter for Publisher.publish() and convenience functions). git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@1477 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 727d4e76a..5fe7e93cd 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -152,7 +152,8 @@ class Publisher: document.transformer.apply_transforms() def publish(self, argv=None, usage=None, description=None, - settings_spec=None, settings_overrides=None): + settings_spec=None, settings_overrides=None, + enable_exit=None): """ Process command line options and arguments (if `self.settings` not already set), run `self.reader` and then `self.writer`. Return @@ -183,6 +184,9 @@ class Publisher: print >>sys.stderr, '\n::: Pseudo-XML:' print >>sys.stderr, document.pformat().encode( 'raw_unicode_escape') + if enable_exit and (document.reporter.max_level + >= self.settings.exit_level): + sys.exit(document.reporter.max_level + 10) return output @@ -194,7 +198,7 @@ def publish_cmdline(reader=None, reader_name='standalone', parser=None, parser_name='restructuredtext', writer=None, writer_name='pseudoxml', settings=None, settings_spec=None, - settings_overrides=None, argv=None, + settings_overrides=None, enable_exit=1, argv=None, usage=default_usage, description=default_description): """ Set up & run a `Publisher`. For command-line front ends. @@ -215,6 +219,7 @@ def publish_cmdline(reader=None, reader_name='standalone', subclass. Used only if no `settings` specified. - `settings_overrides`: A dictionary containing program-specific overrides of component settings. + - `enable_exit`: Boolean; enable exit status at end of processing? - `argv`: Command-line argument list to use instead of ``sys.argv[1:]``. - `usage`: Usage string, output if there's a problem parsing the command line. @@ -223,14 +228,16 @@ def publish_cmdline(reader=None, reader_name='standalone', """ pub = Publisher(reader, parser, writer, settings=settings) pub.set_components(reader_name, parser_name, writer_name) - pub.publish(argv, usage, description, settings_spec, settings_overrides) + pub.publish(argv, usage, description, settings_spec, settings_overrides, + enable_exit=enable_exit) def publish_file(source=None, source_path=None, destination=None, destination_path=None, reader=None, reader_name='standalone', parser=None, parser_name='restructuredtext', writer=None, writer_name='pseudoxml', - settings=None, settings_spec=None, settings_overrides=None): + settings=None, settings_spec=None, settings_overrides=None, + enable_exit=None): """ Set up & run a `Publisher`. For programmatic use with file-like I/O. @@ -258,6 +265,7 @@ def publish_file(source=None, source_path=None, subclass. Used only if no `settings` specified. - `settings_overrides`: A dictionary containing program-specific overrides of component settings. + - `enable_exit`: Boolean; enable exit status at end of processing? """ pub = Publisher(reader, parser, writer, settings=settings) pub.set_components(reader_name, parser_name, writer_name) @@ -267,14 +275,14 @@ def publish_file(source=None, source_path=None, settings._update(settings_overrides, 'loose') pub.set_source(source, source_path) pub.set_destination(destination, destination_path) - pub.publish() + pub.publish(enable_exit=enable_exit) def publish_string(source, source_path=None, destination_path=None, reader=None, reader_name='standalone', parser=None, parser_name='restructuredtext', writer=None, writer_name='pseudoxml', settings=None, settings_spec=None, - settings_overrides=None): + settings_overrides=None, enable_exit=None): """ Set up & run a `Publisher`, and return the string output. For programmatic use with string I/O. @@ -313,6 +321,7 @@ def publish_string(source, source_path=None, destination_path=None, subclass. Used only if no `settings` specified. - `settings_overrides`: A dictionary containing program-specific overrides of component settings. + - `enable_exit`: Boolean; enable exit status at end of processing? """ pub = Publisher(reader, parser, writer, settings=settings, source_class=io.StringInput, @@ -324,4 +333,4 @@ def publish_string(source, source_path=None, destination_path=None, settings._update(settings_overrides, 'loose') pub.set_source(source, source_path) pub.set_destination(destination_path=destination_path) - return pub.publish() + return pub.publish(enable_exit=enable_exit) -- cgit v1.2.1 From f971b5d387d6eda67c4b3e086dd40aea9e794f61 Mon Sep 17 00:00:00 2001 From: goodger Date: Sun, 29 Jun 2003 04:52:52 +0000 Subject: catch system messages to stop tracebacks from parsing errors git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@1523 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 5fe7e93cd..61bf534b8 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -16,7 +16,7 @@ __docformat__ = 'reStructuredText' import sys from docutils import Component -from docutils import frontend, io, readers, parsers, writers +from docutils import frontend, io, utils, readers, parsers, writers from docutils.frontend import OptionParser, ConfigParser @@ -165,9 +165,16 @@ class Publisher: elif settings_overrides: self.settings._update(settings_overrides, 'loose') self.set_io() - document = self.reader.read(self.source, self.parser, self.settings) - self.apply_transforms(document) - output = self.writer.write(document, self.destination) + try: + document = self.reader.read(self.source, self.parser, + self.settings) + self.apply_transforms(document) + output = self.writer.write(document, self.destination) + except utils.SystemMessage, error: + print >>sys.stderr, ('Exiting due to level-%s (%s) system message.' + % (error.level, + utils.Reporter.levels[error.level])) + sys.exit(1) if self.settings.dump_settings: from pprint import pformat print >>sys.stderr, '\n::: Runtime settings:' -- cgit v1.2.1 From ae3ee37139f01572fdb17a5e1b7ecdd0320cb757 Mon Sep 17 00:00:00 2001 From: goodger Date: Sun, 29 Jun 2003 23:34:45 +0000 Subject: remove unused import git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@1528 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 61bf534b8..0b8a91726 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -17,7 +17,7 @@ __docformat__ = 'reStructuredText' import sys from docutils import Component from docutils import frontend, io, utils, readers, parsers, writers -from docutils.frontend import OptionParser, ConfigParser +from docutils.frontend import OptionParser class Publisher: -- cgit v1.2.1 From 67d42a2ee52e043a2fc401db7e8ffc4082a7aebc Mon Sep 17 00:00:00 2001 From: goodger Date: Mon, 30 Jun 2003 15:50:45 +0000 Subject: Catch exceptions during processing, report and exit without tracebacks, except when "--traceback" used. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@1533 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 0b8a91726..6d78ac57f 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -15,7 +15,7 @@ custom component objects first, and pass *them* to __docformat__ = 'reStructuredText' import sys -from docutils import Component +from docutils import Component, __version__ from docutils import frontend, io, utils, readers, parsers, writers from docutils.frontend import OptionParser @@ -165,35 +165,52 @@ class Publisher: elif settings_overrides: self.settings._update(settings_overrides, 'loose') self.set_io() + exit = None + document = None try: document = self.reader.read(self.source, self.parser, self.settings) self.apply_transforms(document) output = self.writer.write(document, self.destination) except utils.SystemMessage, error: + if self.settings.traceback: + raise print >>sys.stderr, ('Exiting due to level-%s (%s) system message.' % (error.level, utils.Reporter.levels[error.level])) - sys.exit(1) + exit = 1 + except Exception, error: + if self.settings.traceback: + raise + print >>sys.stderr, error + print >>sys.stderr, ("""\ +Exiting due to error. Use "--traceback" to diagnose. +Please report errors to . +Include "--traceback" output, Docutils version (%s), +Python version (%s), your OS type & version, and the +command line used.""" % (__version__, sys.version.split()[0])) + exit = 1 if self.settings.dump_settings: from pprint import pformat print >>sys.stderr, '\n::: Runtime settings:' print >>sys.stderr, pformat(self.settings.__dict__) - if self.settings.dump_internals: + if self.settings.dump_internals and document: from pprint import pformat print >>sys.stderr, '\n::: Document internals:' print >>sys.stderr, pformat(document.__dict__) - if self.settings.dump_transforms: + if self.settings.dump_transforms and document: from pprint import pformat print >>sys.stderr, '\n::: Transforms applied:' print >>sys.stderr, pformat(document.transformer.applied) - if self.settings.dump_pseudo_xml: + if self.settings.dump_pseudo_xml and document: print >>sys.stderr, '\n::: Pseudo-XML:' print >>sys.stderr, document.pformat().encode( 'raw_unicode_escape') - if enable_exit and (document.reporter.max_level - >= self.settings.exit_level): + if enable_exit and document and (document.reporter.max_level + >= self.settings.exit_level): sys.exit(document.reporter.max_level + 10) + elif exit: + sys.exit(1) return output -- cgit v1.2.1 From c0230af3f9a5e317c5d3f47e3187d305b87a2df0 Mon Sep 17 00:00:00 2001 From: goodger Date: Wed, 27 Aug 2003 20:42:53 +0000 Subject: Reordered components for OptionParser; application comes last. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@1642 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 6d78ac57f..1049748e1 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -86,7 +86,7 @@ class Publisher: settings_spec=None, **defaults): #@@@ Add self.source & self.destination to components in future? option_parser = OptionParser( - components=(settings_spec, self.parser, self.reader, self.writer), + components=(self.parser, self.reader, self.writer, settings_spec), defaults=defaults, read_config_files=1, usage=usage, description=description) return option_parser @@ -94,8 +94,7 @@ class Publisher: def get_settings(self, usage=None, description=None, settings_spec=None, **defaults): """ - Set and return default settings (overrides in `defaults` keyword - argument). + Set and return default settings (overrides in `defaults` dict). Set components first (`self.set_reader` & `self.set_writer`). Explicitly setting `self.settings` disables command line option -- cgit v1.2.1 From 39b4d4330f7fc49f90959865ea9ad6eb30075f55 Mon Sep 17 00:00:00 2001 From: goodger Date: Mon, 1 Sep 2003 21:08:47 +0000 Subject: Added "config_section" parameter to several methods and functions, allowing front ends to easily specify their config file sections. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@1666 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 53 ++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 17 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 1049748e1..e2bb03074 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -15,7 +15,7 @@ custom component objects first, and pass *them* to __docformat__ = 'reStructuredText' import sys -from docutils import Component, __version__ +from docutils import __version__, Component, SettingsSpec from docutils import frontend, io, utils, readers, parsers, writers from docutils.frontend import OptionParser @@ -83,7 +83,14 @@ class Publisher: self.set_writer(writer_name) def setup_option_parser(self, usage=None, description=None, - settings_spec=None, **defaults): + settings_spec=None, config_section=None, + **defaults): + if config_section and not settings_spec: + settings_spec = SettingsSpec() + settings_spec.config_section = config_section + parts = config_section.split() + if len(parts) > 1 and parts[-1] == 'application': + settings_spec.config_section_dependencies = ['applications'] #@@@ Add self.source & self.destination to components in future? option_parser = OptionParser( components=(self.parser, self.reader, self.writer, settings_spec), @@ -92,7 +99,7 @@ class Publisher: return option_parser def get_settings(self, usage=None, description=None, - settings_spec=None, **defaults): + settings_spec=None, config_section=None, **defaults): """ Set and return default settings (overrides in `defaults` dict). @@ -100,21 +107,22 @@ class Publisher: Explicitly setting `self.settings` disables command line option processing from `self.publish()`. """ - option_parser = self.setup_option_parser(usage, description, - settings_spec, **defaults) + option_parser = self.setup_option_parser( + usage, description, settings_spec, config_section, **defaults) self.settings = option_parser.get_default_values() return self.settings def process_command_line(self, argv=None, usage=None, description=None, - settings_spec=None, **defaults): + settings_spec=None, config_section=None, + **defaults): """ Pass an empty list to `argv` to avoid reading `sys.argv` (the default). Set components first (`self.set_reader` & `self.set_writer`). """ - option_parser = self.setup_option_parser(usage, description, - settings_spec, **defaults) + option_parser = self.setup_option_parser( + usage, description, settings_spec, config_section,**defaults) if argv is None: argv = sys.argv[1:] self.settings = option_parser.parse_args(argv) @@ -152,15 +160,16 @@ class Publisher: def publish(self, argv=None, usage=None, description=None, settings_spec=None, settings_overrides=None, - enable_exit=None): + config_section=None, enable_exit=None): """ Process command line options and arguments (if `self.settings` not already set), run `self.reader` and then `self.writer`. Return `self.writer`'s output. """ if self.settings is None: - self.process_command_line(argv, usage, description, settings_spec, - **(settings_overrides or {})) + self.process_command_line( + argv, usage, description, settings_spec, config_section, + **(settings_overrides or {})) elif settings_overrides: self.settings._update(settings_overrides, 'loose') self.set_io() @@ -221,7 +230,8 @@ def publish_cmdline(reader=None, reader_name='standalone', parser=None, parser_name='restructuredtext', writer=None, writer_name='pseudoxml', settings=None, settings_spec=None, - settings_overrides=None, enable_exit=1, argv=None, + settings_overrides=None, config_section=None, + enable_exit=1, argv=None, usage=default_usage, description=default_description): """ Set up & run a `Publisher`. For command-line front ends. @@ -242,6 +252,8 @@ def publish_cmdline(reader=None, reader_name='standalone', subclass. Used only if no `settings` specified. - `settings_overrides`: A dictionary containing program-specific overrides of component settings. + - `config_section`: Name of configuration file section for application. + Used only if no `settings` or `settings_spec` specified. - `enable_exit`: Boolean; enable exit status at end of processing? - `argv`: Command-line argument list to use instead of ``sys.argv[1:]``. - `usage`: Usage string, output if there's a problem parsing the command @@ -252,7 +264,7 @@ def publish_cmdline(reader=None, reader_name='standalone', pub = Publisher(reader, parser, writer, settings=settings) pub.set_components(reader_name, parser_name, writer_name) pub.publish(argv, usage, description, settings_spec, settings_overrides, - enable_exit=enable_exit) + config_section=config_section, enable_exit=enable_exit) def publish_file(source=None, source_path=None, destination=None, destination_path=None, @@ -260,7 +272,7 @@ def publish_file(source=None, source_path=None, parser=None, parser_name='restructuredtext', writer=None, writer_name='pseudoxml', settings=None, settings_spec=None, settings_overrides=None, - enable_exit=None): + config_section=None, enable_exit=None): """ Set up & run a `Publisher`. For programmatic use with file-like I/O. @@ -288,12 +300,15 @@ def publish_file(source=None, source_path=None, subclass. Used only if no `settings` specified. - `settings_overrides`: A dictionary containing program-specific overrides of component settings. + - `config_section`: Name of configuration file section for application. + Used only if no `settings` or `settings_spec` specified. - `enable_exit`: Boolean; enable exit status at end of processing? """ pub = Publisher(reader, parser, writer, settings=settings) pub.set_components(reader_name, parser_name, writer_name) if settings is None: - settings = pub.get_settings(settings_spec=settings_spec) + settings = pub.get_settings(settings_spec=settings_spec, + config_section=config_section) if settings_overrides: settings._update(settings_overrides, 'loose') pub.set_source(source, source_path) @@ -305,7 +320,8 @@ def publish_string(source, source_path=None, destination_path=None, parser=None, parser_name='restructuredtext', writer=None, writer_name='pseudoxml', settings=None, settings_spec=None, - settings_overrides=None, enable_exit=None): + settings_overrides=None, config_section=None, + enable_exit=None): """ Set up & run a `Publisher`, and return the string output. For programmatic use with string I/O. @@ -344,6 +360,8 @@ def publish_string(source, source_path=None, destination_path=None, subclass. Used only if no `settings` specified. - `settings_overrides`: A dictionary containing program-specific overrides of component settings. + - `config_section`: Name of configuration file section for application. + Used only if no `settings` or `settings_spec` specified. - `enable_exit`: Boolean; enable exit status at end of processing? """ pub = Publisher(reader, parser, writer, settings=settings, @@ -351,7 +369,8 @@ def publish_string(source, source_path=None, destination_path=None, destination_class=io.StringOutput) pub.set_components(reader_name, parser_name, writer_name) if settings is None: - settings = pub.get_settings(settings_spec=settings_spec) + settings = pub.get_settings(settings_spec=settings_spec, + config_section=config_section) if settings_overrides: settings._update(settings_overrides, 'loose') pub.set_source(source, source_path) -- cgit v1.2.1 From 634a5d0622c6fbeb09face1622dc8b4d79878f00 Mon Sep 17 00:00:00 2001 From: reggie Date: Tue, 23 Mar 2004 22:29:44 +0000 Subject: Added funtionality to keep track of individual parts of a document and store them in a dictionary as the "parts" attribute of the writer. Added publish_parts convenience function to allow easy access to these parts. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@1879 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index e2bb03074..93defd38e 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -180,6 +180,7 @@ class Publisher: self.settings) self.apply_transforms(document) output = self.writer.write(document, self.destination) + self.writer.assemble_parts() except utils.SystemMessage, error: if self.settings.traceback: raise @@ -376,3 +377,66 @@ def publish_string(source, source_path=None, destination_path=None, pub.set_source(source, source_path) pub.set_destination(destination_path=destination_path) return pub.publish(enable_exit=enable_exit) + +def publish_parts(source, source_path=None, destination_path=None, + reader=None, reader_name='standalone', + parser=None, parser_name='restructuredtext', + writer=None, writer_name='pseudoxml', + settings=None, settings_spec=None, + settings_overrides=None, config_section=None, + enable_exit=None): + """ + Set up & run a `Publisher`, and return a dictionary of strings with the + names of parts as keys. For programmatic use with string I/O. + + For encoded string output, be sure to set the "output_encoding" setting to + the desired encoding. Set it to "unicode" for unencoded Unicode string + output. Here's how:: + + publish_string(..., settings_overrides={'output_encoding': 'unicode'}) + + Similarly for Unicode string input (`source`):: + + publish_string(..., settings_overrides={'input_encoding': 'unicode'}) + + Parameters: + + - `source`: An input string; required. This can be an encoded 8-bit + string (set the "input_encoding" setting to the correct encoding) or a + Unicode string (set the "input_encoding" setting to "unicode"). + - `source_path`: Path to the file or object that produced `source`; + optional. Only used for diagnostic output. + - `destination_path`: Path to the file or object which will receive the + output; optional. Used for determining relative paths (stylesheets, + source links, etc.). + - `reader`: A `docutils.readers.Reader` object. + - `reader_name`: Name or alias of the Reader class to be instantiated if + no `reader` supplied. + - `parser`: A `docutils.parsers.Parser` object. + - `parser_name`: Name or alias of the Parser class to be instantiated if + no `parser` supplied. + - `writer`: A `docutils.writers.Writer` object. + - `writer_name`: Name or alias of the Writer class to be instantiated if + no `writer` supplied. + - `settings`: Runtime settings object. + - `settings_spec`: Extra settings specification; a `docutils.SettingsSpec` + subclass. Used only if no `settings` specified. + - `settings_overrides`: A dictionary containing program-specific overrides + of component settings. + - `config_section`: Name of configuration file section for application. + Used only if no `settings` or `settings_spec` specified. + - `enable_exit`: Boolean; enable exit status at end of processing? + """ + pub = Publisher(reader, parser, writer, settings=settings, + source_class=io.StringInput, + destination_class=io.StringOutput) + pub.set_components(reader_name, parser_name, writer_name) + if settings is None: + settings = pub.get_settings(settings_spec=settings_spec, + config_section=config_section) + if settings_overrides: + settings._update(settings_overrides, 'loose') + pub.set_source(source, source_path) + pub.set_destination(destination_path=destination_path) + pub.publish(enable_exit=enable_exit) + return pub.writer.parts -- cgit v1.2.1 From 3a1f6be8832e3655f22cfd117fc321b2cde2c55a Mon Sep 17 00:00:00 2001 From: goodger Date: Sat, 3 Apr 2004 14:03:25 +0000 Subject: update publish_parts: docstring, and destination class (direct output is thrown away) git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@1911 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 93defd38e..96ed69bcc 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -386,16 +386,13 @@ def publish_parts(source, source_path=None, destination_path=None, settings_overrides=None, config_section=None, enable_exit=None): """ - Set up & run a `Publisher`, and return a dictionary of strings with the - names of parts as keys. For programmatic use with string I/O. + Set up & run a `Publisher`, and return a dictionary of document parts. + Dictionary keys are the names of parts, and values are Unicode strings; + encoding is up to the client. For programmatic use with string I/O. - For encoded string output, be sure to set the "output_encoding" setting to + For encoded string input, be sure to set the "input_encoding" setting to the desired encoding. Set it to "unicode" for unencoded Unicode string - output. Here's how:: - - publish_string(..., settings_overrides={'output_encoding': 'unicode'}) - - Similarly for Unicode string input (`source`):: + input. Here's how:: publish_string(..., settings_overrides={'input_encoding': 'unicode'}) @@ -429,7 +426,7 @@ def publish_parts(source, source_path=None, destination_path=None, """ pub = Publisher(reader, parser, writer, settings=settings, source_class=io.StringInput, - destination_class=io.StringOutput) + destination_class=io.NullOutput) pub.set_components(reader_name, parser_name, writer_name) if settings is None: settings = pub.get_settings(settings_spec=settings_spec, -- cgit v1.2.1 From 104d68ea45cc578d4aa06fcaedda795fbb093a1a Mon Sep 17 00:00:00 2001 From: goodger Date: Wed, 12 May 2004 23:58:49 +0000 Subject: Added special error handling & advice for UnicodeEncodeError. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@2090 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 62 +++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 52 insertions(+), 10 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 96ed69bcc..37f9100f2 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -184,20 +184,17 @@ class Publisher: except utils.SystemMessage, error: if self.settings.traceback: raise - print >>sys.stderr, ('Exiting due to level-%s (%s) system message.' - % (error.level, - utils.Reporter.levels[error.level])) + self.report_SystemMessage(error) + exit = 1 + except UnicodeEncodeError, error: + if self.settings.traceback: + raise + self.report_UnicodeEncodeError(error) exit = 1 except Exception, error: if self.settings.traceback: raise - print >>sys.stderr, error - print >>sys.stderr, ("""\ -Exiting due to error. Use "--traceback" to diagnose. -Please report errors to . -Include "--traceback" output, Docutils version (%s), -Python version (%s), your OS type & version, and the -command line used.""" % (__version__, sys.version.split()[0])) + self.report_Exception(error) exit = 1 if self.settings.dump_settings: from pprint import pformat @@ -222,6 +219,51 @@ command line used.""" % (__version__, sys.version.split()[0])) sys.exit(1) return output + def report_SystemMessage(self, error): + print >>sys.stderr, ('Exiting due to level-%s (%s) system message.' + % (error.level, + utils.Reporter.levels[error.level])) + + def report_UnicodeEncodeError(self, error): + print >>sys.stderr, '%s: %s' % (error.__class__.__name__, error) + print >>sys.stderr, (""" +The specified output encoding (%s) cannot +handle all of the output. +Try setting "--output-encoding-error-handler" to +""" % (self.settings.output_encoding)), + if sys.hexversion >= 0x02030000: # Python 2.3 + print >>sys.stderr, (""" +* "xmlcharrefreplace" (for HTML & XML output, Python 2.3+); + the output will contain "%s" and should be usable. +* "backslashreplace" (for other output formats, Python 2.3+); + look for "%s" in the output. +*""" % (error.object[error.start:error.end].encode('ascii', + 'xmlcharrefreplace'), + error.object[error.start:error.end].encode('ascii', + 'backslashreplace'))), + print >>sys.stderr, ("""\ +"replace" (Python 2.1 or 2.2); look for "?" in the output. + +"--output-encoding-error-handler" is currently set to +"%s". + +Exiting due to error. Use "--traceback" to diagnose. +If the advice above doesn't eliminate the error, +please report it to . +Include "--traceback" output, Docutils version (%s), +Python version (%s), your OS type & version, and the +command line used.""" % (self.settings.output_encoding_error_handler, + __version__, sys.version.split()[0])) + + def report_Exception(self, error): + print >>sys.stderr, '%s: %s' % (error.__class__.__name__, error) + print >>sys.stderr, ("""\ +Exiting due to error. Use "--traceback" to diagnose. +Please report errors to . +Include "--traceback" output, Docutils version (%s), +Python version (%s), your OS type & version, and the +command line used.""" % (__version__, sys.version.split()[0])) + default_usage = '%prog [options] [ []]' default_description = ('Reads from (default is stdin) and writes to ' -- cgit v1.2.1 From 1a4e77baaf1faa8c191325baf19d53d9a15eb73e Mon Sep 17 00:00:00 2001 From: goodger Date: Mon, 17 May 2004 18:42:49 +0000 Subject: change to UnicodeError, which exists in Python 2.1 & 2.2 (UnicodeEncodeError is new in 2.3) git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@2118 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 37f9100f2..0c7eba275 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -186,10 +186,10 @@ class Publisher: raise self.report_SystemMessage(error) exit = 1 - except UnicodeEncodeError, error: + except UnicodeError, error: if self.settings.traceback: raise - self.report_UnicodeEncodeError(error) + self.report_UnicodeError(error) exit = 1 except Exception, error: if self.settings.traceback: @@ -224,7 +224,7 @@ class Publisher: % (error.level, utils.Reporter.levels[error.level])) - def report_UnicodeEncodeError(self, error): + def report_UnicodeError(self, error): print >>sys.stderr, '%s: %s' % (error.__class__.__name__, error) print >>sys.stderr, (""" The specified output encoding (%s) cannot -- cgit v1.2.1 From 8dde89834330d67af2ba384f5b72ea8aa11b8f3e Mon Sep 17 00:00:00 2001 From: goodger Date: Tue, 1 Jun 2004 03:58:09 +0000 Subject: refactored: simplified Publisher.publish (exception handling & debug dumps) git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@2154 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 58 ++++++++++++++++++++++++++------------------------------ 1 file changed, 27 insertions(+), 31 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 0c7eba275..06a6fe2d3 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -15,6 +15,7 @@ custom component objects first, and pass *them* to __docformat__ = 'reStructuredText' import sys +import pprint from docutils import __version__, Component, SettingsSpec from docutils import frontend, io, utils, readers, parsers, writers from docutils.frontend import OptionParser @@ -181,43 +182,47 @@ class Publisher: self.apply_transforms(document) output = self.writer.write(document, self.destination) self.writer.assemble_parts() - except utils.SystemMessage, error: - if self.settings.traceback: - raise - self.report_SystemMessage(error) - exit = 1 - except UnicodeError, error: - if self.settings.traceback: - raise - self.report_UnicodeError(error) - exit = 1 except Exception, error: if self.settings.traceback: raise self.report_Exception(error) exit = 1 + self.debugging_dumps(document) + if enable_exit and document and (document.reporter.max_level + >= self.settings.exit_level): + sys.exit(document.reporter.max_level + 10) + elif exit: + sys.exit(1) + return output + + def debugging_dumps(self, document): if self.settings.dump_settings: - from pprint import pformat print >>sys.stderr, '\n::: Runtime settings:' - print >>sys.stderr, pformat(self.settings.__dict__) + print >>sys.stderr, pprint.pformat(self.settings.__dict__) if self.settings.dump_internals and document: - from pprint import pformat print >>sys.stderr, '\n::: Document internals:' - print >>sys.stderr, pformat(document.__dict__) + print >>sys.stderr, pprint.pformat(document.__dict__) if self.settings.dump_transforms and document: - from pprint import pformat print >>sys.stderr, '\n::: Transforms applied:' - print >>sys.stderr, pformat(document.transformer.applied) + print >>sys.stderr, pprint.pformat(document.transformer.applied) if self.settings.dump_pseudo_xml and document: print >>sys.stderr, '\n::: Pseudo-XML:' print >>sys.stderr, document.pformat().encode( 'raw_unicode_escape') - if enable_exit and document and (document.reporter.max_level - >= self.settings.exit_level): - sys.exit(document.reporter.max_level + 10) - elif exit: - sys.exit(1) - return output + + def report_Exception(self, error): + if isinstance(error, utils.SystemMessage): + self.report_SystemMessage(error) + elif isinstance(error, UnicodeError): + self.report_UnicodeError(error) + else: + print >>sys.stderr, '%s: %s' % (error.__class__.__name__, error) + print >>sys.stderr, ("""\ +Exiting due to error. Use "--traceback" to diagnose. +Please report errors to . +Include "--traceback" output, Docutils version (%s), +Python version (%s), your OS type & version, and the +command line used.""" % (__version__, sys.version.split()[0])) def report_SystemMessage(self, error): print >>sys.stderr, ('Exiting due to level-%s (%s) system message.' @@ -255,15 +260,6 @@ Python version (%s), your OS type & version, and the command line used.""" % (self.settings.output_encoding_error_handler, __version__, sys.version.split()[0])) - def report_Exception(self, error): - print >>sys.stderr, '%s: %s' % (error.__class__.__name__, error) - print >>sys.stderr, ("""\ -Exiting due to error. Use "--traceback" to diagnose. -Please report errors to . -Include "--traceback" output, Docutils version (%s), -Python version (%s), your OS type & version, and the -command line used.""" % (__version__, sys.version.split()[0])) - default_usage = '%prog [options] [ []]' default_description = ('Reads from (default is stdin) and writes to ' -- cgit v1.2.1 From 89274bc3056c7c02f626fe5a126b55f62e6a08be Mon Sep 17 00:00:00 2001 From: goodger Date: Thu, 3 Jun 2004 20:44:34 +0000 Subject: Renamed "enable_exit" parameter of convenience functions to "enable_exit_status" git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@2208 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 06a6fe2d3..9dd8f5760 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -161,7 +161,7 @@ class Publisher: def publish(self, argv=None, usage=None, description=None, settings_spec=None, settings_overrides=None, - config_section=None, enable_exit=None): + config_section=None, enable_exit_status=None): """ Process command line options and arguments (if `self.settings` not already set), run `self.reader` and then `self.writer`. Return @@ -188,8 +188,9 @@ class Publisher: self.report_Exception(error) exit = 1 self.debugging_dumps(document) - if enable_exit and document and (document.reporter.max_level - >= self.settings.exit_level): + if (enable_exit_status and document + and (document.reporter.max_level + >= self.settings.exit_status_level)): sys.exit(document.reporter.max_level + 10) elif exit: sys.exit(1) @@ -270,7 +271,7 @@ def publish_cmdline(reader=None, reader_name='standalone', writer=None, writer_name='pseudoxml', settings=None, settings_spec=None, settings_overrides=None, config_section=None, - enable_exit=1, argv=None, + enable_exit_status=1, argv=None, usage=default_usage, description=default_description): """ Set up & run a `Publisher`. For command-line front ends. @@ -293,7 +294,7 @@ def publish_cmdline(reader=None, reader_name='standalone', of component settings. - `config_section`: Name of configuration file section for application. Used only if no `settings` or `settings_spec` specified. - - `enable_exit`: Boolean; enable exit status at end of processing? + - `enable_exit_status`: Boolean; enable exit status at end of processing? - `argv`: Command-line argument list to use instead of ``sys.argv[1:]``. - `usage`: Usage string, output if there's a problem parsing the command line. @@ -303,7 +304,8 @@ def publish_cmdline(reader=None, reader_name='standalone', pub = Publisher(reader, parser, writer, settings=settings) pub.set_components(reader_name, parser_name, writer_name) pub.publish(argv, usage, description, settings_spec, settings_overrides, - config_section=config_section, enable_exit=enable_exit) + config_section=config_section, + enable_exit_status=enable_exit_status) def publish_file(source=None, source_path=None, destination=None, destination_path=None, @@ -311,7 +313,7 @@ def publish_file(source=None, source_path=None, parser=None, parser_name='restructuredtext', writer=None, writer_name='pseudoxml', settings=None, settings_spec=None, settings_overrides=None, - config_section=None, enable_exit=None): + config_section=None, enable_exit_status=None): """ Set up & run a `Publisher`. For programmatic use with file-like I/O. @@ -341,7 +343,7 @@ def publish_file(source=None, source_path=None, of component settings. - `config_section`: Name of configuration file section for application. Used only if no `settings` or `settings_spec` specified. - - `enable_exit`: Boolean; enable exit status at end of processing? + - `enable_exit_status`: Boolean; enable exit status at end of processing? """ pub = Publisher(reader, parser, writer, settings=settings) pub.set_components(reader_name, parser_name, writer_name) @@ -352,7 +354,7 @@ def publish_file(source=None, source_path=None, settings._update(settings_overrides, 'loose') pub.set_source(source, source_path) pub.set_destination(destination, destination_path) - pub.publish(enable_exit=enable_exit) + pub.publish(enable_exit_status=enable_exit_status) def publish_string(source, source_path=None, destination_path=None, reader=None, reader_name='standalone', @@ -360,7 +362,7 @@ def publish_string(source, source_path=None, destination_path=None, writer=None, writer_name='pseudoxml', settings=None, settings_spec=None, settings_overrides=None, config_section=None, - enable_exit=None): + enable_exit_status=None): """ Set up & run a `Publisher`, and return the string output. For programmatic use with string I/O. @@ -401,7 +403,7 @@ def publish_string(source, source_path=None, destination_path=None, of component settings. - `config_section`: Name of configuration file section for application. Used only if no `settings` or `settings_spec` specified. - - `enable_exit`: Boolean; enable exit status at end of processing? + - `enable_exit_status`: Boolean; enable exit status at end of processing? """ pub = Publisher(reader, parser, writer, settings=settings, source_class=io.StringInput, @@ -414,7 +416,7 @@ def publish_string(source, source_path=None, destination_path=None, settings._update(settings_overrides, 'loose') pub.set_source(source, source_path) pub.set_destination(destination_path=destination_path) - return pub.publish(enable_exit=enable_exit) + return pub.publish(enable_exit_status=enable_exit_status) def publish_parts(source, source_path=None, destination_path=None, reader=None, reader_name='standalone', @@ -422,7 +424,7 @@ def publish_parts(source, source_path=None, destination_path=None, writer=None, writer_name='pseudoxml', settings=None, settings_spec=None, settings_overrides=None, config_section=None, - enable_exit=None): + enable_exit_status=None): """ Set up & run a `Publisher`, and return a dictionary of document parts. Dictionary keys are the names of parts, and values are Unicode strings; @@ -460,7 +462,7 @@ def publish_parts(source, source_path=None, destination_path=None, of component settings. - `config_section`: Name of configuration file section for application. Used only if no `settings` or `settings_spec` specified. - - `enable_exit`: Boolean; enable exit status at end of processing? + - `enable_exit_status`: Boolean; enable exit status at end of processing? """ pub = Publisher(reader, parser, writer, settings=settings, source_class=io.StringInput, @@ -473,5 +475,5 @@ def publish_parts(source, source_path=None, destination_path=None, settings._update(settings_overrides, 'loose') pub.set_source(source, source_path) pub.set_destination(destination_path=destination_path) - pub.publish(enable_exit=enable_exit) + pub.publish(enable_exit_status=enable_exit_status) return pub.writer.parts -- cgit v1.2.1 From 1a1a2081d9aa20f9ce80cbb3c85fe82c786266be Mon Sep 17 00:00:00 2001 From: goodger Date: Thu, 3 Jun 2004 21:17:16 +0000 Subject: Enabled traceback (exception propagation) by default in programmatic convenience functions. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@2211 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 9dd8f5760..4b060b6d0 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -183,7 +183,7 @@ class Publisher: output = self.writer.write(document, self.destination) self.writer.assemble_parts() except Exception, error: - if self.settings.traceback: + if self.settings.traceback: # propagate exceptions? raise self.report_Exception(error) exit = 1 @@ -350,6 +350,8 @@ def publish_file(source=None, source_path=None, if settings is None: settings = pub.get_settings(settings_spec=settings_spec, config_section=config_section) + # Set to propagate exceptions by default when used programmatically: + settings.traceback = 1 if settings_overrides: settings._update(settings_overrides, 'loose') pub.set_source(source, source_path) @@ -412,6 +414,8 @@ def publish_string(source, source_path=None, destination_path=None, if settings is None: settings = pub.get_settings(settings_spec=settings_spec, config_section=config_section) + # Set to propagate exceptions by default when used programmatically: + settings.traceback = 1 if settings_overrides: settings._update(settings_overrides, 'loose') pub.set_source(source, source_path) @@ -471,6 +475,8 @@ def publish_parts(source, source_path=None, destination_path=None, if settings is None: settings = pub.get_settings(settings_spec=settings_spec, config_section=config_section) + # Set to propagate exceptions by default when used programmatically: + settings.traceback = 1 if settings_overrides: settings._update(settings_overrides, 'loose') pub.set_source(source, source_path) -- cgit v1.2.1 From 3023b038d9134115290f021ad0f65f1caee6e292 Mon Sep 17 00:00:00 2001 From: goodger Date: Mon, 21 Jun 2004 22:21:25 +0000 Subject: return encoded string results from publish_* convenience functions git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@2347 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 4b060b6d0..f3ceed38c 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -274,7 +274,8 @@ def publish_cmdline(reader=None, reader_name='standalone', enable_exit_status=1, argv=None, usage=default_usage, description=default_description): """ - Set up & run a `Publisher`. For command-line front ends. + Set up & run a `Publisher` (and return the encoded string output). + For command-line front ends. Parameters: @@ -303,9 +304,10 @@ def publish_cmdline(reader=None, reader_name='standalone', """ pub = Publisher(reader, parser, writer, settings=settings) pub.set_components(reader_name, parser_name, writer_name) - pub.publish(argv, usage, description, settings_spec, settings_overrides, - config_section=config_section, - enable_exit_status=enable_exit_status) + output = pub.publish( + argv, usage, description, settings_spec, settings_overrides, + config_section=config_section, enable_exit_status=enable_exit_status) + return output def publish_file(source=None, source_path=None, destination=None, destination_path=None, @@ -315,7 +317,8 @@ def publish_file(source=None, source_path=None, settings=None, settings_spec=None, settings_overrides=None, config_section=None, enable_exit_status=None): """ - Set up & run a `Publisher`. For programmatic use with file-like I/O. + Set up & run a `Publisher` (and return the encoded string output). + For programmatic use with file-like I/O. Parameters: @@ -356,7 +359,8 @@ def publish_file(source=None, source_path=None, settings._update(settings_overrides, 'loose') pub.set_source(source, source_path) pub.set_destination(destination, destination_path) - pub.publish(enable_exit_status=enable_exit_status) + output = pub.publish(enable_exit_status=enable_exit_status) + return output def publish_string(source, source_path=None, destination_path=None, reader=None, reader_name='standalone', @@ -366,7 +370,7 @@ def publish_string(source, source_path=None, destination_path=None, settings_overrides=None, config_section=None, enable_exit_status=None): """ - Set up & run a `Publisher`, and return the string output. + Set up & run a `Publisher`, and return the encoded string output. For programmatic use with string I/O. For encoded string output, be sure to set the "output_encoding" setting to -- cgit v1.2.1 From c08d3345ec4bf0ad19518fe87b7b91da61f72c64 Mon Sep 17 00:00:00 2001 From: goodger Date: Fri, 25 Jun 2004 22:11:23 +0000 Subject: Extracted common code from publish_file, publish_string, and publish_parts, into new publish_programmatically. In Publisher.publish, disabled ``settings_overrides`` when ``settings`` is supplied; redundant. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@2384 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 188 +++++++++++++++++++++---------------------------------- 1 file changed, 72 insertions(+), 116 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index f3ceed38c..da2333ceb 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -123,7 +123,7 @@ class Publisher: Set components first (`self.set_reader` & `self.set_writer`). """ option_parser = self.setup_option_parser( - usage, description, settings_spec, config_section,**defaults) + usage, description, settings_spec, config_section, **defaults) if argv is None: argv = sys.argv[1:] self.settings = option_parser.parse_args(argv) @@ -171,8 +171,6 @@ class Publisher: self.process_command_line( argv, usage, description, settings_spec, config_section, **(settings_overrides or {})) - elif settings_overrides: - self.settings._update(settings_overrides, 'loose') self.set_io() exit = None document = None @@ -189,7 +187,7 @@ class Publisher: exit = 1 self.debugging_dumps(document) if (enable_exit_status and document - and (document.reporter.max_level + and (document.reporter.max_level >= self.settings.exit_status_level)): sys.exit(document.reporter.max_level + 10) elif exit: @@ -277,25 +275,8 @@ def publish_cmdline(reader=None, reader_name='standalone', Set up & run a `Publisher` (and return the encoded string output). For command-line front ends. - Parameters: + Parameters: see `publish_programmatically` for the remainder. - - `reader`: A `docutils.readers.Reader` object. - - `reader_name`: Name or alias of the Reader class to be instantiated if - no `reader` supplied. - - `parser`: A `docutils.parsers.Parser` object. - - `parser_name`: Name or alias of the Parser class to be instantiated if - no `parser` supplied. - - `writer`: A `docutils.writers.Writer` object. - - `writer_name`: Name or alias of the Writer class to be instantiated if - no `writer` supplied. - - `settings`: Runtime settings object. - - `settings_spec`: Extra settings specification; a `docutils.SettingsSpec` - subclass. Used only if no `settings` specified. - - `settings_overrides`: A dictionary containing program-specific overrides - of component settings. - - `config_section`: Name of configuration file section for application. - Used only if no `settings` or `settings_spec` specified. - - `enable_exit_status`: Boolean; enable exit status at end of processing? - `argv`: Command-line argument list to use instead of ``sys.argv[1:]``. - `usage`: Usage string, output if there's a problem parsing the command line. @@ -320,49 +301,22 @@ def publish_file(source=None, source_path=None, Set up & run a `Publisher` (and return the encoded string output). For programmatic use with file-like I/O. - Parameters: - - - `source`: A file-like object (must have "read" and "close" methods). - - `source_path`: Path to the input file. Opened if no `source` supplied. - If neither `source` nor `source_path` are supplied, `sys.stdin` is used. - - `destination`: A file-like object (must have "write" and "close" - methods). - - `destination_path`: Path to the input file. Opened if no `destination` - supplied. If neither `destination` nor `destination_path` are supplied, - `sys.stdout` is used. - - `reader`: A `docutils.readers.Reader` object. - - `reader_name`: Name or alias of the Reader class to be instantiated if - no `reader` supplied. - - `parser`: A `docutils.parsers.Parser` object. - - `parser_name`: Name or alias of the Parser class to be instantiated if - no `parser` supplied. - - `writer`: A `docutils.writers.Writer` object. - - `writer_name`: Name or alias of the Writer class to be instantiated if - no `writer` supplied. - - `settings`: Runtime settings object. - - `settings_spec`: Extra settings specification; a `docutils.SettingsSpec` - subclass. Used only if no `settings` specified. - - `settings_overrides`: A dictionary containing program-specific overrides - of component settings. - - `config_section`: Name of configuration file section for application. - Used only if no `settings` or `settings_spec` specified. - - `enable_exit_status`: Boolean; enable exit status at end of processing? + Parameters: see `publish_programmatically`. """ - pub = Publisher(reader, parser, writer, settings=settings) - pub.set_components(reader_name, parser_name, writer_name) - if settings is None: - settings = pub.get_settings(settings_spec=settings_spec, - config_section=config_section) - # Set to propagate exceptions by default when used programmatically: - settings.traceback = 1 - if settings_overrides: - settings._update(settings_overrides, 'loose') - pub.set_source(source, source_path) - pub.set_destination(destination, destination_path) - output = pub.publish(enable_exit_status=enable_exit_status) + output, pub = publish_programmatically( + source=source, source_path=source_path, source_class=io.FileInput, + destination=destination, destination_path=destination_path, + destination_class=io.FileOutput, + reader=reader, reader_name=reader_name, + parser=parser, parser_name=parser_name, + writer=writer, writer_name=writer_name, + settings=settings, settings_spec=settings_spec, + settings_overrides=settings_overrides, + config_section=config_section, + enable_exit_status=enable_exit_status) return output -def publish_string(source, source_path=None, destination_path=None, +def publish_string(source, source_path=None, destination_path=None, reader=None, reader_name='standalone', parser=None, parser_name='restructuredtext', writer=None, writer_name='pseudoxml', @@ -375,7 +329,7 @@ def publish_string(source, source_path=None, destination_path=None, For encoded string output, be sure to set the "output_encoding" setting to the desired encoding. Set it to "unicode" for unencoded Unicode string - output. Here's how:: + output. Here's one way:: publish_string(..., settings_overrides={'output_encoding': 'unicode'}) @@ -383,50 +337,22 @@ def publish_string(source, source_path=None, destination_path=None, publish_string(..., settings_overrides={'input_encoding': 'unicode'}) - Parameters: - - - `source`: An input string; required. This can be an encoded 8-bit - string (set the "input_encoding" setting to the correct encoding) or a - Unicode string (set the "input_encoding" setting to "unicode"). - - `source_path`: Path to the file or object that produced `source`; - optional. Only used for diagnostic output. - - `destination_path`: Path to the file or object which will receive the - output; optional. Used for determining relative paths (stylesheets, - source links, etc.). - - `reader`: A `docutils.readers.Reader` object. - - `reader_name`: Name or alias of the Reader class to be instantiated if - no `reader` supplied. - - `parser`: A `docutils.parsers.Parser` object. - - `parser_name`: Name or alias of the Parser class to be instantiated if - no `parser` supplied. - - `writer`: A `docutils.writers.Writer` object. - - `writer_name`: Name or alias of the Writer class to be instantiated if - no `writer` supplied. - - `settings`: Runtime settings object. - - `settings_spec`: Extra settings specification; a `docutils.SettingsSpec` - subclass. Used only if no `settings` specified. - - `settings_overrides`: A dictionary containing program-specific overrides - of component settings. - - `config_section`: Name of configuration file section for application. - Used only if no `settings` or `settings_spec` specified. - - `enable_exit_status`: Boolean; enable exit status at end of processing? + Parameters: see `publish_programmatically`. """ - pub = Publisher(reader, parser, writer, settings=settings, - source_class=io.StringInput, - destination_class=io.StringOutput) - pub.set_components(reader_name, parser_name, writer_name) - if settings is None: - settings = pub.get_settings(settings_spec=settings_spec, - config_section=config_section) - # Set to propagate exceptions by default when used programmatically: - settings.traceback = 1 - if settings_overrides: - settings._update(settings_overrides, 'loose') - pub.set_source(source, source_path) - pub.set_destination(destination_path=destination_path) - return pub.publish(enable_exit_status=enable_exit_status) + output, pub = publish_programmatically( + source=source, source_path=source_path, source_class=io.StringInput, + destination=None, destination_path=destination_path, + destination_class=io.StringOutput, + reader=reader, reader_name=reader_name, + parser=parser, parser_name=parser_name, + writer=writer, writer_name=writer_name, + settings=settings, settings_spec=settings_spec, + settings_overrides=settings_overrides, + config_section=config_section, + enable_exit_status=enable_exit_status) + return output -def publish_parts(source, source_path=None, destination_path=None, +def publish_parts(source, source_path=None, destination_path=None, reader=None, reader_name='standalone', parser=None, parser_name='restructuredtext', writer=None, writer_name='pseudoxml', @@ -444,6 +370,33 @@ def publish_parts(source, source_path=None, destination_path=None, publish_string(..., settings_overrides={'input_encoding': 'unicode'}) + Parameters: see `publish_programmatically`. + """ + output, pub = publish_programmatically( + source=source, source_path=source_path, source_class=io.StringInput, + destination=None, destination_path=destination_path, + destination_class=io.StringOutput, + reader=reader, reader_name=reader_name, + parser=parser, parser_name=parser_name, + writer=writer, writer_name=writer_name, + settings=settings, settings_spec=settings_spec, + settings_overrides=settings_overrides, + config_section=config_section, + enable_exit_status=enable_exit_status) + return pub.writer.parts + +def publish_programmatically(source, source_path, source_class, + destination, destination_path, destination_class, + reader, reader_name, + parser, parser_name, + writer, writer_name, + settings, settings_spec, + settings_overrides, config_section, + enable_exit_status): + """ + Set up & run a `Publisher` for programmatic use. Return the encoded + string output and the Publisher object. + Parameters: - `source`: An input string; required. This can be an encoded 8-bit @@ -451,9 +404,12 @@ def publish_parts(source, source_path=None, destination_path=None, Unicode string (set the "input_encoding" setting to "unicode"). - `source_path`: Path to the file or object that produced `source`; optional. Only used for diagnostic output. + - `source_class`: The class for dynamically created source objects. - `destination_path`: Path to the file or object which will receive the output; optional. Used for determining relative paths (stylesheets, source links, etc.). + - `destination_class`: The class for dynamically created destination + objects. - `reader`: A `docutils.readers.Reader` object. - `reader_name`: Name or alias of the Reader class to be instantiated if no `reader` supplied. @@ -473,17 +429,17 @@ def publish_parts(source, source_path=None, destination_path=None, - `enable_exit_status`: Boolean; enable exit status at end of processing? """ pub = Publisher(reader, parser, writer, settings=settings, - source_class=io.StringInput, - destination_class=io.NullOutput) + source_class=source_class, + destination_class=destination_class) pub.set_components(reader_name, parser_name, writer_name) if settings is None: - settings = pub.get_settings(settings_spec=settings_spec, - config_section=config_section) - # Set to propagate exceptions by default when used programmatically: - settings.traceback = 1 - if settings_overrides: - settings._update(settings_overrides, 'loose') + pub.get_settings( + settings_spec=settings_spec, config_section=config_section, + # Propagate exceptions by default when used programmatically: + traceback=1, + # In case _disable_config is set: + **(settings_overrides or {})) pub.set_source(source, source_path) - pub.set_destination(destination_path=destination_path) - pub.publish(enable_exit_status=enable_exit_status) - return pub.writer.parts + pub.set_destination(destination, destination_path) + output = pub.publish(enable_exit_status=enable_exit_status) + return output, pub -- cgit v1.2.1 From 4e28d2a926aa3c43eebf821802f1424680a3d62a Mon Sep 17 00:00:00 2001 From: goodger Date: Sun, 27 Jun 2004 03:47:22 +0000 Subject: extracted ``publish_programmatically`` settings code to ``Publisher.process_programmatic_settings``; updated docstrings git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@2393 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 192 +++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 129 insertions(+), 63 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index da2333ceb..846041f32 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -1,5 +1,5 @@ # Authors: David Goodger -# Contact: goodger@users.sourceforge.net +# Contact: goodger@python.org # Revision: $Revision$ # Date: $Date$ # Copyright: This module has been placed in the public domain. @@ -9,15 +9,17 @@ Calling the ``publish_*`` convenience functions (or instantiating a `Publisher` object) with component names will result in default behavior. For custom behavior (setting component options), create custom component objects first, and pass *them* to -``publish_*``/`Publisher`. +``publish_*``/`Publisher`. See `The Docutils Publisher`_. + +.. _The Docutils Publisher: http://docutils.sf.net/docs/api/publisher.html """ __docformat__ = 'reStructuredText' import sys import pprint -from docutils import __version__, Component, SettingsSpec -from docutils import frontend, io, utils, readers, parsers, writers +from docutils import __version__, SettingsSpec +from docutils import frontend, io, utils, readers, writers from docutils.frontend import OptionParser @@ -38,22 +40,23 @@ class Publisher: """ self.reader = reader - """A `readers.Reader` instance.""" + """A `docutils.readers.Reader` instance.""" self.parser = parser - """A `parsers.Parser` instance.""" + """A `docutils.parsers.Parser` instance.""" self.writer = writer - """A `writers.Writer` instance.""" + """A `docutils.writers.Writer` instance.""" self.source = source - """The source of input data, an `io.Input` instance.""" + """The source of input data, a `docutils.io.Input` instance.""" self.source_class = source_class """The class for dynamically created source objects.""" self.destination = destination - """The destination for docutils output, an `io.Output` instance.""" + """The destination for docutils output, a `docutils.io.Output` + instance.""" self.destination_class = destination_class """The class for dynamically created destination objects.""" @@ -86,8 +89,9 @@ class Publisher: def setup_option_parser(self, usage=None, description=None, settings_spec=None, config_section=None, **defaults): - if config_section and not settings_spec: - settings_spec = SettingsSpec() + if config_section: + if not settings_spec: + settings_spec = SettingsSpec() settings_spec.config_section = config_section parts = config_section.split() if len(parts) > 1 and parts[-1] == 'application': @@ -113,6 +117,17 @@ class Publisher: self.settings = option_parser.get_default_values() return self.settings + def process_programmatic_settings(self, settings_spec, + settings_overrides, + config_section): + if self.settings is None: + defaults = (settings_overrides or {}).copy() + # Propagate exceptions by default when used programmatically: + defaults.setdefault('traceback', 1) + self.get_settings(settings_spec=settings_spec, + config_section=config_section, + **defaults) + def process_command_line(self, argv=None, usage=None, description=None, settings_spec=None, config_section=None, **defaults): @@ -272,8 +287,9 @@ def publish_cmdline(reader=None, reader_name='standalone', enable_exit_status=1, argv=None, usage=default_usage, description=default_description): """ - Set up & run a `Publisher` (and return the encoded string output). - For command-line front ends. + Set up & run a `Publisher` for command-line-based file I/O (input and + output file paths taken automatically from the command line). Return the + encoded string output also. Parameters: see `publish_programmatically` for the remainder. @@ -298,15 +314,15 @@ def publish_file(source=None, source_path=None, settings=None, settings_spec=None, settings_overrides=None, config_section=None, enable_exit_status=None): """ - Set up & run a `Publisher` (and return the encoded string output). - For programmatic use with file-like I/O. + Set up & run a `Publisher` for programmatic use with file-like I/O. + Return the encoded string output also. Parameters: see `publish_programmatically`. """ output, pub = publish_programmatically( - source=source, source_path=source_path, source_class=io.FileInput, - destination=destination, destination_path=destination_path, + source_class=io.FileInput, source=source, source_path=source_path, destination_class=io.FileOutput, + destination=destination, destination_path=destination_path, reader=reader, reader_name=reader_name, parser=parser, parser_name=parser_name, writer=writer, writer_name=writer_name, @@ -324,11 +340,11 @@ def publish_string(source, source_path=None, destination_path=None, settings_overrides=None, config_section=None, enable_exit_status=None): """ - Set up & run a `Publisher`, and return the encoded string output. - For programmatic use with string I/O. + Set up & run a `Publisher` for programmatic use with string I/O. Return + the encoded string or Unicode string output. - For encoded string output, be sure to set the "output_encoding" setting to - the desired encoding. Set it to "unicode" for unencoded Unicode string + For encoded string output, be sure to set the 'output_encoding' setting to + the desired encoding. Set it to 'unicode' for unencoded Unicode string output. Here's one way:: publish_string(..., settings_overrides={'output_encoding': 'unicode'}) @@ -340,9 +356,9 @@ def publish_string(source, source_path=None, destination_path=None, Parameters: see `publish_programmatically`. """ output, pub = publish_programmatically( - source=source, source_path=source_path, source_class=io.StringInput, - destination=None, destination_path=destination_path, + source_class=io.StringInput, source=source, source_path=source_path, destination_class=io.StringOutput, + destination=None, destination_path=destination_path, reader=reader, reader_name=reader_name, parser=parser, parser_name=parser_name, writer=writer, writer_name=writer_name, @@ -364,8 +380,8 @@ def publish_parts(source, source_path=None, destination_path=None, Dictionary keys are the names of parts, and values are Unicode strings; encoding is up to the client. For programmatic use with string I/O. - For encoded string input, be sure to set the "input_encoding" setting to - the desired encoding. Set it to "unicode" for unencoded Unicode string + For encoded string input, be sure to set the 'input_encoding' setting to + the desired encoding. Set it to 'unicode' for unencoded Unicode string input. Here's how:: publish_string(..., settings_overrides={'input_encoding': 'unicode'}) @@ -373,9 +389,9 @@ def publish_parts(source, source_path=None, destination_path=None, Parameters: see `publish_programmatically`. """ output, pub = publish_programmatically( - source=source, source_path=source_path, source_class=io.StringInput, - destination=None, destination_path=destination_path, + source_class=io.StringInput, source=source, source_path=source_path, destination_class=io.StringOutput, + destination=None, destination_path=destination_path, reader=reader, reader_name=reader_name, parser=parser, parser_name=parser_name, writer=writer, writer_name=writer_name, @@ -385,8 +401,8 @@ def publish_parts(source, source_path=None, destination_path=None, enable_exit_status=enable_exit_status) return pub.writer.parts -def publish_programmatically(source, source_path, source_class, - destination, destination_path, destination_class, +def publish_programmatically(source_class, source, source_path, + destination_class, destination, destination_path, reader, reader_name, parser, parser_name, writer, writer_name, @@ -394,51 +410,101 @@ def publish_programmatically(source, source_path, source_class, settings_overrides, config_section, enable_exit_status): """ - Set up & run a `Publisher` for programmatic use. Return the encoded - string output and the Publisher object. + Set up & run a `Publisher` for custom programmatic use. Return the + encoded string output and the Publisher object. + + Applications should not need to call this function directly. If it does + seem to be necessary to call this function directly, please write to the + docutils-develop@lists.sourceforge.net mailing list. Parameters: - - `source`: An input string; required. This can be an encoded 8-bit - string (set the "input_encoding" setting to the correct encoding) or a - Unicode string (set the "input_encoding" setting to "unicode"). - - `source_path`: Path to the file or object that produced `source`; - optional. Only used for diagnostic output. - - `source_class`: The class for dynamically created source objects. - - `destination_path`: Path to the file or object which will receive the - output; optional. Used for determining relative paths (stylesheets, - source links, etc.). - - `destination_class`: The class for dynamically created destination - objects. - - `reader`: A `docutils.readers.Reader` object. - - `reader_name`: Name or alias of the Reader class to be instantiated if + * `source_class` **required**: The class for dynamically created source + objects. Typically `io.FileInput` or `io.StringInput`. + + * `source`: Type depends on `source_class`: + + - `io.FileInput`: Either a file-like object (must have 'read' and + 'close' methods), or ``None`` (`source_path` is opened). If neither + `source` nor `source_path` are supplied, `sys.stdin` is used. + + - `io.StringInput` **required**: The input string, either an encoded + 8-bit string (set the 'input_encoding' setting to the correct + encoding) or a Unicode string (set the 'input_encoding' setting to + 'unicode'). + + * `source_path`: Type depends on `source_class`: + + - `io.FileInput`: Path to the input file, opened if no `source` + supplied. + + - `io.StringInput`: Optional. Path to the file or object that produced + `source`. Only used for diagnostic output. + + * `destination_class` **required**: The class for dynamically created + destination objects. Typically `io.FileOutput` or `io.StringOutput`. + + * `destination`: Type depends on `destination_class`: + + - `io.FileOutput`: Either a file-like object (must have 'write' and + 'close' methods), or ``None`` (`destination_path` is opened). If + neither `destination` nor `destination_path` are supplied, + `sys.stdout` is used. + + - `io.StringOutput`: Not used; pass ``None``. + + * `destination_path`: Type depends on `destination_class`: + + - `io.FileOutput`: Path to the output file. Opened if no `destination` + supplied. + + - `io.StringOutput`: Path to the file or object which will receive the + output; optional. Used for determining relative paths (stylesheets, + source links, etc.). + + * `reader`: A `docutils.readers.Reader` object. + + * `reader_name`: Name or alias of the Reader class to be instantiated if no `reader` supplied. - - `parser`: A `docutils.parsers.Parser` object. - - `parser_name`: Name or alias of the Parser class to be instantiated if + + * `parser`: A `docutils.parsers.Parser` object. + + * `parser_name`: Name or alias of the Parser class to be instantiated if no `parser` supplied. - - `writer`: A `docutils.writers.Writer` object. - - `writer_name`: Name or alias of the Writer class to be instantiated if + + * `writer`: A `docutils.writers.Writer` object. + + * `writer_name`: Name or alias of the Writer class to be instantiated if no `writer` supplied. - - `settings`: Runtime settings object. - - `settings_spec`: Extra settings specification; a `docutils.SettingsSpec` - subclass. Used only if no `settings` specified. - - `settings_overrides`: A dictionary containing program-specific overrides - of component settings. - - `config_section`: Name of configuration file section for application. - Used only if no `settings` or `settings_spec` specified. - - `enable_exit_status`: Boolean; enable exit status at end of processing? + + * `settings`: A runtime settings (`docutils.frontend.Values`) object, for + dotted-attribute access to runtime settings. It's the end result of the + `SettingsSpec`, config file, and option processing. If `settings` is + passed, it's assumed to be complete and no further setting/config/option + processing is done. + + * `settings_spec`: A `docutils.SettingsSpec` subclass or object. Provides + extra application-specific settings definitions independently of + components. In other words, the application becomes a component, and + its settings data is processed along with that of the other components. + Used only if no `settings` specified. + + * `settings_overrides`: A dictionary containing application-specific + settings defaults that override the defaults of other components. + Used only if no `settings` specified. + + * `config_section`: A string, the name of the configuration file section + for this application. Overrides the ``config_section`` attribute + defined by `settings_spec`. Used only if no `settings` specified. + + * `enable_exit_status`: Boolean; enable exit status at end of processing? """ pub = Publisher(reader, parser, writer, settings=settings, source_class=source_class, destination_class=destination_class) pub.set_components(reader_name, parser_name, writer_name) - if settings is None: - pub.get_settings( - settings_spec=settings_spec, config_section=config_section, - # Propagate exceptions by default when used programmatically: - traceback=1, - # In case _disable_config is set: - **(settings_overrides or {})) + pub.process_programmatic_settings( + settings_spec, settings_overrides, config_section) pub.set_source(source, source_path) pub.set_destination(destination, destination_path) output = pub.publish(enable_exit_status=enable_exit_status) -- cgit v1.2.1 From 253d5324a1754ddda972807b0e2ff6303c3dbef4 Mon Sep 17 00:00:00 2001 From: wiemann Date: Sat, 24 Jul 2004 18:35:09 +0000 Subject: xmlcharrefreplace backport git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@2448 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 846041f32..17b8ddd7f 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -245,23 +245,23 @@ command line used.""" % (__version__, sys.version.split()[0])) def report_UnicodeError(self, error): print >>sys.stderr, '%s: %s' % (error.__class__.__name__, error) - print >>sys.stderr, (""" + print >>sys.stderr, """ The specified output encoding (%s) cannot handle all of the output. Try setting "--output-encoding-error-handler" to -""" % (self.settings.output_encoding)), + +* "xmlcharrefreplace" (for HTML & XML output);""" if sys.hexversion >= 0x02030000: # Python 2.3 - print >>sys.stderr, (""" -* "xmlcharrefreplace" (for HTML & XML output, Python 2.3+); + print >>sys.stderr, """\ the output will contain "%s" and should be usable. * "backslashreplace" (for other output formats, Python 2.3+); - look for "%s" in the output. -*""" % (error.object[error.start:error.end].encode('ascii', - 'xmlcharrefreplace'), - error.object[error.start:error.end].encode('ascii', - 'backslashreplace'))), + look for "%s" in the output.""" % ( + error.object[error.start:error.end].encode('ascii', 'xmlcharrefreplace'), + error.object[error.start:error.end].encode('ascii', 'backslashreplace')) + else: + print >>sys.stderr, ' the output should be usable as-is.' print >>sys.stderr, ("""\ -"replace" (Python 2.1 or 2.2); look for "?" in the output. +* "replace"; look for "?" in the output. "--output-encoding-error-handler" is currently set to "%s". -- cgit v1.2.1 From 92cf1a41dfcfaecc618930fb85cf5fb5f775dc01 Mon Sep 17 00:00:00 2001 From: cben Date: Sun, 25 Jul 2004 02:14:23 +0000 Subject: Replace version number sniffing with specific features sniffing. Also fix 2 bugs in `report_UnicodeError`: * Print the current encoding (was a ``%s``). * Don't crash before Python 2.3 where `UnicodeError` objects had no ``object`` or ``encoding`` attributes. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@2450 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 17b8ddd7f..9794b95a6 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -245,21 +245,33 @@ command line used.""" % (__version__, sys.version.split()[0])) def report_UnicodeError(self, error): print >>sys.stderr, '%s: %s' % (error.__class__.__name__, error) - print >>sys.stderr, """ + + # A `UnicodeError` objected contained no useful args until Python 2.3 + try: + print >>sys.stderr, """ The specified output encoding (%s) cannot -handle all of the output. +handle all of the output.""" % error.encoding + except AttributeError: + print >>sys.stderr, """ +The specified output encoding cannot +handle all of the output.""" + + print """\ Try setting "--output-encoding-error-handler" to * "xmlcharrefreplace" (for HTML & XML output);""" - if sys.hexversion >= 0x02030000: # Python 2.3 + try: + data = error.object[error.start:error.end] print >>sys.stderr, """\ the output will contain "%s" and should be usable. * "backslashreplace" (for other output formats, Python 2.3+); look for "%s" in the output.""" % ( - error.object[error.start:error.end].encode('ascii', 'xmlcharrefreplace'), - error.object[error.start:error.end].encode('ascii', 'backslashreplace')) - else: - print >>sys.stderr, ' the output should be usable as-is.' + data.encode('ascii', 'xmlcharrefreplace'), + data.encode('ascii', 'backslashreplace')) + except AttributeError: + print >>sys.stderr, """\ + the output should be usable as-is.""" + print >>sys.stderr, ("""\ * "replace"; look for "?" in the output. -- cgit v1.2.1 From 00f1da6edfcac7e44bb561cff134ce408e9f5e2c Mon Sep 17 00:00:00 2001 From: wiemann Date: Sun, 25 Jul 2004 13:36:33 +0000 Subject: re-re-fixed encoding error message, cleanup git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@2455 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 69 ++++++++++++++++++++++++-------------------------------- 1 file changed, 30 insertions(+), 39 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 9794b95a6..84136d5b2 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -244,48 +244,39 @@ command line used.""" % (__version__, sys.version.split()[0])) utils.Reporter.levels[error.level])) def report_UnicodeError(self, error): - print >>sys.stderr, '%s: %s' % (error.__class__.__name__, error) - - # A `UnicodeError` objected contained no useful args until Python 2.3 - try: - print >>sys.stderr, """ -The specified output encoding (%s) cannot -handle all of the output.""" % error.encoding - except AttributeError: - print >>sys.stderr, """ -The specified output encoding cannot -handle all of the output.""" - - print """\ -Try setting "--output-encoding-error-handler" to - -* "xmlcharrefreplace" (for HTML & XML output);""" + sys.stderr.write( + '%s: %s\n' + '\n' + 'The specified output encoding (%s) cannot\n' + 'handle all of the output.\n' + 'Try setting "--output-encoding-error-handler" to\n' + '\n' + '* "xmlcharrefreplace" (for HTML & XML output);\n' + % (error.__class__.__name__, error, + self.settings.output_encoding)) try: data = error.object[error.start:error.end] - print >>sys.stderr, """\ - the output will contain "%s" and should be usable. -* "backslashreplace" (for other output formats, Python 2.3+); - look for "%s" in the output.""" % ( - data.encode('ascii', 'xmlcharrefreplace'), - data.encode('ascii', 'backslashreplace')) + sys.stderr.write( + ' the output will contain "%s" and should be usable.\n' + '* "backslashreplace" (for other output formats, Python 2.3+);\n' + ' look for "%s" in the output.\n' + % (data.encode('ascii', 'xmlcharrefreplace'), + data.encode('ascii', 'backslashreplace'))) except AttributeError: - print >>sys.stderr, """\ - the output should be usable as-is.""" - - print >>sys.stderr, ("""\ -* "replace"; look for "?" in the output. - -"--output-encoding-error-handler" is currently set to -"%s". - -Exiting due to error. Use "--traceback" to diagnose. -If the advice above doesn't eliminate the error, -please report it to . -Include "--traceback" output, Docutils version (%s), -Python version (%s), your OS type & version, and the -command line used.""" % (self.settings.output_encoding_error_handler, - __version__, sys.version.split()[0])) - + sys.stderr.write(' the output should be usable as-is.\n') + sys.stderr.write( + '* "replace"; look for "?" in the output.\n' + '\n' + '"--output-encoding-error-handler" is currently set to "%s".\n' + '\n' + 'Exiting due to error. Use "--traceback" to diagnose.\n' + 'If the advice above doesn\'t eliminate the error,\n' + 'please report it to .\n' + 'Include "--traceback" output, Docutils version (%s),\n' + 'Python version (%s), your OS type & version, and the\n' + 'command line used.\n' + % (self.settings.output_encoding_error_handler, + __version__, sys.version.split()[0])) default_usage = '%prog [options] [ []]' default_description = ('Reads from (default is stdin) and writes to ' -- cgit v1.2.1 From 999a66a7ae7196962673dc71b732a704d8275ed2 Mon Sep 17 00:00:00 2001 From: goodger Date: Sat, 26 Feb 2005 18:17:59 +0000 Subject: enable --dump-* options when --traceback specified; allows for easier debugging git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@2987 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 3 +++ 1 file changed, 3 insertions(+) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 84136d5b2..4cb9d7ec3 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -197,6 +197,7 @@ class Publisher: self.writer.assemble_parts() except Exception, error: if self.settings.traceback: # propagate exceptions? + self.debugging_dumps(document) raise self.report_Exception(error) exit = 1 @@ -210,6 +211,8 @@ class Publisher: return output def debugging_dumps(self, document): + if not document: + return if self.settings.dump_settings: print >>sys.stderr, '\n::: Runtime settings:' print >>sys.stderr, pprint.pformat(self.settings.__dict__) -- cgit v1.2.1 From b7b2f9f3ecf1b3877314db63d09242dfe71b41fc Mon Sep 17 00:00:00 2001 From: wiemann Date: Sat, 28 May 2005 00:30:02 +0000 Subject: replaced references to the mailing lists with references to the new "mailing-lists" document git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@3396 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 4cb9d7ec3..f31c3011a 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -421,7 +421,8 @@ def publish_programmatically(source_class, source, source_path, Applications should not need to call this function directly. If it does seem to be necessary to call this function directly, please write to the - docutils-develop@lists.sourceforge.net mailing list. + Docutils-develop mailing list + . Parameters: -- cgit v1.2.1 From 78ed5de20eb3c06ef02efd5710145321fc40596f Mon Sep 17 00:00:00 2001 From: goodger Date: Wed, 1 Jun 2005 14:44:50 +0000 Subject: docutils.__version_details__ renamed from .__version_suffix__ git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@3417 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index f31c3011a..302c39eb2 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -18,7 +18,7 @@ __docformat__ = 'reStructuredText' import sys import pprint -from docutils import __version__, SettingsSpec +from docutils import __version__, __version_details__, SettingsSpec from docutils import frontend, io, utils, readers, writers from docutils.frontend import OptionParser @@ -237,9 +237,10 @@ class Publisher: print >>sys.stderr, ("""\ Exiting due to error. Use "--traceback" to diagnose. Please report errors to . -Include "--traceback" output, Docutils version (%s), +Include "--traceback" output, Docutils version (%s [%s]), Python version (%s), your OS type & version, and the -command line used.""" % (__version__, sys.version.split()[0])) +command line used.""" % (__version__, __version_details__, + sys.version.split()[0])) def report_SystemMessage(self, error): print >>sys.stderr, ('Exiting due to level-%s (%s) system message.' -- cgit v1.2.1 From ac78efa666ab4f1d7f8dd5b46edb681a79e457e2 Mon Sep 17 00:00:00 2001 From: goodger Date: Mon, 20 Jun 2005 03:35:33 +0000 Subject: added Publisher.document object attribute, for direct access to the doctree git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@3520 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 45 ++++++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 21 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 302c39eb2..fa796a3db 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -39,6 +39,9 @@ class Publisher: a component name (`set_reader` sets the parser as well). """ + self.document = None + """The document tree (`docutils.nodes` objects).""" + self.reader = reader """A `docutils.readers.Reader` instance.""" @@ -168,11 +171,11 @@ class Publisher: encoding=self.settings.output_encoding, error_handler=self.settings.output_encoding_error_handler) - def apply_transforms(self, document): - document.transformer.populate_from_components( + def apply_transforms(self): + self.document.transformer.populate_from_components( (self.source, self.reader, self.reader.parser, self.writer, self.destination)) - document.transformer.apply_transforms() + self.document.transformer.apply_transforms() def publish(self, argv=None, usage=None, description=None, settings_spec=None, settings_overrides=None, @@ -188,43 +191,43 @@ class Publisher: **(settings_overrides or {})) self.set_io() exit = None - document = None try: - document = self.reader.read(self.source, self.parser, - self.settings) - self.apply_transforms(document) - output = self.writer.write(document, self.destination) + self.document = self.reader.read(self.source, self.parser, + self.settings) + self.apply_transforms() + output = self.writer.write(self.document, self.destination) self.writer.assemble_parts() except Exception, error: if self.settings.traceback: # propagate exceptions? - self.debugging_dumps(document) + self.debugging_dumps() raise self.report_Exception(error) exit = 1 - self.debugging_dumps(document) - if (enable_exit_status and document - and (document.reporter.max_level + self.debugging_dumps() + if (enable_exit_status and self.document + and (self.document.reporter.max_level >= self.settings.exit_status_level)): - sys.exit(document.reporter.max_level + 10) + sys.exit(self.document.reporter.max_level + 10) elif exit: sys.exit(1) return output - def debugging_dumps(self, document): - if not document: + def debugging_dumps(self): + if not self.document: return if self.settings.dump_settings: print >>sys.stderr, '\n::: Runtime settings:' print >>sys.stderr, pprint.pformat(self.settings.__dict__) - if self.settings.dump_internals and document: + if self.settings.dump_internals: print >>sys.stderr, '\n::: Document internals:' - print >>sys.stderr, pprint.pformat(document.__dict__) - if self.settings.dump_transforms and document: + print >>sys.stderr, pprint.pformat(self.document.__dict__) + if self.settings.dump_transforms: print >>sys.stderr, '\n::: Transforms applied:' - print >>sys.stderr, pprint.pformat(document.transformer.applied) - if self.settings.dump_pseudo_xml and document: + print >>sys.stderr, pprint.pformat( + self.document.transformer.applied) + if self.settings.dump_pseudo_xml: print >>sys.stderr, '\n::: Pseudo-XML:' - print >>sys.stderr, document.pformat().encode( + print >>sys.stderr, self.document.pformat().encode( 'raw_unicode_escape') def report_Exception(self, error): -- cgit v1.2.1 From 835cd6732507e8e4c76b0ef437bcc74177a9d174 Mon Sep 17 00:00:00 2001 From: wiemann Date: Sun, 26 Jun 2005 22:20:27 +0000 Subject: merge trunk/docutils@3525 branches/blais_interrupt_render/docutils@HEAD trunk/docutils git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@3581 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 132 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 131 insertions(+), 1 deletion(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index fa796a3db..7caa6cca7 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -19,7 +19,7 @@ __docformat__ = 'reStructuredText' import sys import pprint from docutils import __version__, __version_details__, SettingsSpec -from docutils import frontend, io, utils, readers, writers +from docutils import frontend, io, utils, readers, writers, parsers from docutils.frontend import OptionParser @@ -411,6 +411,135 @@ def publish_parts(source, source_path=None, destination_path=None, enable_exit_status=enable_exit_status) return pub.writer.parts +def publish_doctree(source, source_path=None, + source_class=io.StringInput, + reader=None, reader_name='standalone', + parser=None, parser_name='restructuredtext', + settings=None, settings_spec=None, + settings_overrides=None, config_section=None, + enable_exit_status=None): + """ + Set up & run a `Publisher` for programmatic use with string I/O. Return + a pair of the encoded string or Unicode string output, and the parts. + + For encoded string output, be sure to set the 'output_encoding' setting to + the desired encoding. Set it to 'unicode' for unencoded Unicode string + output. Here's one way:: + + publish_string(..., settings_overrides={'output_encoding': 'unicode'}) + + Similarly for Unicode string input (`source`):: + + publish_string(..., settings_overrides={'input_encoding': 'unicode'}) + + Parameters: see `publish_programmatically`. + """ + output, pub = publish_programmatically( + source_class=source_class, source=source, source_path=source_path, + destination_class=io.NullOutput, + destination=None, destination_path=None, + reader=reader, reader_name=reader_name, + parser=parser, parser_name=parser_name, + writer=None, writer_name='null', + settings=settings, settings_spec=settings_spec, + settings_overrides=settings_overrides, + config_section=config_section, + enable_exit_status=enable_exit_status) + + # Note: clean up the document tree object by removing some things that are + # not needed anymore and that would not pickled well (this is the primary + # intended use of this publish method). + pub.document.transformer = None + pub.document.reporter = None + + return pub.document, pub.writer.parts + + +class DummyParser(parsers.Parser): + """ + Dummy parser that does nothing. + """ + supported = ('dummy',) + + settings_spec = ('Dummy Parser Options', None, ()) + + config_section = 'dummy parser' + config_section_dependencies = ('parsers',) + + def parse(self, inputstring, document): + "No-op" + +class DummyReader(readers.Reader): + """ + Dummy reader that is initialized with an existing document tree. + Its 'reading' consists in preparing and fixing up the document tree for the + writing phase. + """ + supported = ('dummy',) + + document = None + + settings_spec = ('Dummy Reader', None, (),) + + config_section = 'dummy reader' + config_section_dependencies = ('readers',) + + default_transforms = () + + def __init__( self, doctree ): + readers.Reader.__init__(self, parser=DummyParser()) + self._doctree = doctree + + def read(self, source, parser, settings): + # Fixup the document tree with a transformer and a reporter if it does + # not have them yet. + if self._doctree.transformer is None: + import docutils.transforms + self._doctree.transformer = docutils.transforms.Transformer( + self._doctree) + + if self._doctree.reporter is None: + self._doctree.reporter = utils.new_reporter( + source.source_path, settings) + + self._doctree.settings = settings + + return self._doctree + + +def publish_from_doctree(doctree, source_path=None, destination_path=None, + writer=None, writer_name='pseudoxml', + settings=None, settings_spec=None, + settings_overrides=None, config_section=None, + enable_exit_status=None): + """ + + Set up & run a `Publisher` to render from an existing document tree data + structure, for programmatic use with string I/O. Return a pair of encoded + string output and document parts. + + For encoded string output, be sure to set the 'output_encoding' setting to + the desired encoding. Set it to 'unicode' for unencoded Unicode string + output. Here's one way:: + + publish_string(..., settings_overrides={'output_encoding': 'unicode'}) + + Parameters: see `publish_programmatically`. + """ + output, pub = publish_programmatically( + source_class=io.NullInput, source='', source_path=source_path, + destination_class=io.StringOutput, + destination=None, destination_path=destination_path, + reader=DummyReader(doctree), reader_name=None, + parser=None, parser_name=None, + writer=writer, writer_name=writer_name, + settings=settings, settings_spec=settings_spec, + settings_overrides=settings_overrides, + config_section=config_section, + enable_exit_status=enable_exit_status) + + return output, pub.writer.parts + def publish_programmatically(source_class, source, source_path, destination_class, destination, destination_path, reader, reader_name, @@ -520,3 +649,4 @@ def publish_programmatically(source_class, source, source_path, pub.set_destination(destination, destination_path) output = pub.publish(enable_exit_status=enable_exit_status) return output, pub + -- cgit v1.2.1 From 0c69925dc24c30bea76b320410a49f2f04460fef Mon Sep 17 00:00:00 2001 From: wiemann Date: Sun, 26 Jun 2005 22:37:08 +0000 Subject: polished git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@3582 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 42 ++++++++++++++---------------------------- 1 file changed, 14 insertions(+), 28 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 7caa6cca7..30476c575 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -21,6 +21,7 @@ import pprint from docutils import __version__, __version_details__, SettingsSpec from docutils import frontend, io, utils, readers, writers, parsers from docutils.frontend import OptionParser +from docutils.transforms import Transformer class Publisher: @@ -456,55 +457,40 @@ def publish_doctree(source, source_path=None, class DummyParser(parsers.Parser): - """ - Dummy parser that does nothing. - """ - supported = ('dummy',) - settings_spec = ('Dummy Parser Options', None, ()) + """Dummy parser that does nothing. Used by `DummyReader`.""" - config_section = 'dummy parser' - config_section_dependencies = ('parsers',) + supported = ('dummy',) def parse(self, inputstring, document): - "No-op" + pass class DummyReader(readers.Reader): + """ Dummy reader that is initialized with an existing document tree. Its 'reading' consists in preparing and fixing up the document tree for the writing phase. """ + supported = ('dummy',) document = None - settings_spec = ('Dummy Reader', None, (),) - - config_section = 'dummy reader' - config_section_dependencies = ('readers',) - - default_transforms = () - - def __init__( self, doctree ): + def __init__(self, doctree): readers.Reader.__init__(self, parser=DummyParser()) - self._doctree = doctree + self.doctree = doctree def read(self, source, parser, settings): # Fixup the document tree with a transformer and a reporter if it does # not have them yet. - if self._doctree.transformer is None: - import docutils.transforms - self._doctree.transformer = docutils.transforms.Transformer( - self._doctree) - - if self._doctree.reporter is None: - self._doctree.reporter = utils.new_reporter( + if self.doctree.transformer is None: + self.doctree.transformer = Transformer(self.doctree) + if self.doctree.reporter is None: + self.doctree.reporter = utils.new_reporter( source.source_path, settings) - - self._doctree.settings = settings - - return self._doctree + self.doctree.settings = settings + return self.doctree def publish_from_doctree(doctree, source_path=None, destination_path=None, -- cgit v1.2.1 From d9322bf1a32f81f8a42d73a5cdedf8467aa40e23 Mon Sep 17 00:00:00 2001 From: wiemann Date: Sun, 26 Jun 2005 22:41:29 +0000 Subject: removed unused "document" class variable of standalone and dummy reader git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@3583 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 2 -- 1 file changed, 2 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 30476c575..30731b167 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -475,8 +475,6 @@ class DummyReader(readers.Reader): supported = ('dummy',) - document = None - def __init__(self, doctree): readers.Reader.__init__(self, parser=DummyParser()) self.doctree = doctree -- cgit v1.2.1 From 35e38fe655db71e85b67c962d854eaab5f764608 Mon Sep 17 00:00:00 2001 From: wiemann Date: Sun, 26 Jun 2005 22:54:57 +0000 Subject: removed duplicate publish_doctree method; renamed publish_from_doctree to publish_doctree git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@3585 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 64 +++++++++----------------------------------------------- 1 file changed, 10 insertions(+), 54 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 30731b167..d702c916f 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -412,49 +412,6 @@ def publish_parts(source, source_path=None, destination_path=None, enable_exit_status=enable_exit_status) return pub.writer.parts -def publish_doctree(source, source_path=None, - source_class=io.StringInput, - reader=None, reader_name='standalone', - parser=None, parser_name='restructuredtext', - settings=None, settings_spec=None, - settings_overrides=None, config_section=None, - enable_exit_status=None): - """ - Set up & run a `Publisher` for programmatic use with string I/O. Return - a pair of the encoded string or Unicode string output, and the parts. - - For encoded string output, be sure to set the 'output_encoding' setting to - the desired encoding. Set it to 'unicode' for unencoded Unicode string - output. Here's one way:: - - publish_string(..., settings_overrides={'output_encoding': 'unicode'}) - - Similarly for Unicode string input (`source`):: - - publish_string(..., settings_overrides={'input_encoding': 'unicode'}) - - Parameters: see `publish_programmatically`. - """ - output, pub = publish_programmatically( - source_class=source_class, source=source, source_path=source_path, - destination_class=io.NullOutput, - destination=None, destination_path=None, - reader=reader, reader_name=reader_name, - parser=parser, parser_name=parser_name, - writer=None, writer_name='null', - settings=settings, settings_spec=settings_spec, - settings_overrides=settings_overrides, - config_section=config_section, - enable_exit_status=enable_exit_status) - - # Note: clean up the document tree object by removing some things that are - # not needed anymore and that would not pickled well (this is the primary - # intended use of this publish method). - pub.document.transformer = None - pub.document.reporter = None - - return pub.document, pub.writer.parts - class DummyParser(parsers.Parser): @@ -469,8 +426,10 @@ class DummyReader(readers.Reader): """ Dummy reader that is initialized with an existing document tree. - Its 'reading' consists in preparing and fixing up the document tree for the - writing phase. + Its 'reading' consists in preparing and fixing up the document + tree for the writing phase. + + Used by `publish_doctree`. """ supported = ('dummy',) @@ -491,13 +450,12 @@ class DummyReader(readers.Reader): return self.doctree -def publish_from_doctree(doctree, source_path=None, destination_path=None, - writer=None, writer_name='pseudoxml', - settings=None, settings_spec=None, - settings_overrides=None, config_section=None, - enable_exit_status=None): +def publish_doctree(doctree, source_path=None, destination_path=None, + writer=None, writer_name='pseudoxml', + settings=None, settings_spec=None, + settings_overrides=None, config_section=None, + enable_exit_status=None): """ - Set up & run a `Publisher` to render from an existing document tree data structure, for programmatic use with string I/O. Return a pair of encoded string output and document parts. @@ -506,7 +464,7 @@ def publish_from_doctree(doctree, source_path=None, destination_path=None, the desired encoding. Set it to 'unicode' for unencoded Unicode string output. Here's one way:: - publish_string(..., settings_overrides={'output_encoding': 'unicode'}) + publish_doctree(..., settings_overrides={'output_encoding': 'unicode'}) Parameters: see `publish_programmatically`. """ @@ -521,7 +479,6 @@ def publish_from_doctree(doctree, source_path=None, destination_path=None, settings_overrides=settings_overrides, config_section=config_section, enable_exit_status=enable_exit_status) - return output, pub.writer.parts def publish_programmatically(source_class, source, source_path, @@ -633,4 +590,3 @@ def publish_programmatically(source_class, source, source_path, pub.set_destination(destination, destination_path) output = pub.publish(enable_exit_status=enable_exit_status) return output, pub - -- cgit v1.2.1 From 86b1889e15d3cdbc3115746534623627e0c9edc2 Mon Sep 17 00:00:00 2001 From: wiemann Date: Sun, 26 Jun 2005 23:01:45 +0000 Subject: oh, publish_doctree was not a duplicate... 8-) git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@3586 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 55 +++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 49 insertions(+), 6 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index d702c916f..aa511296f 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -412,6 +412,49 @@ def publish_parts(source, source_path=None, destination_path=None, enable_exit_status=enable_exit_status) return pub.writer.parts +def publish_doctree(source, source_path=None, + source_class=io.StringInput, + reader=None, reader_name='standalone', + parser=None, parser_name='restructuredtext', + settings=None, settings_spec=None, + settings_overrides=None, config_section=None, + enable_exit_status=None): + """ + Set up & run a `Publisher` for programmatic use with string I/O. Return + a pair of the encoded string or Unicode string output, and the parts. + + For encoded string output, be sure to set the 'output_encoding' setting to + the desired encoding. Set it to 'unicode' for unencoded Unicode string + output. Here's one way:: + + publish_string(..., settings_overrides={'output_encoding': 'unicode'}) + + Similarly for Unicode string input (`source`):: + + publish_string(..., settings_overrides={'input_encoding': 'unicode'}) + + Parameters: see `publish_programmatically`. + """ + output, pub = publish_programmatically( + source_class=source_class, source=source, source_path=source_path, + destination_class=io.NullOutput, + destination=None, destination_path=None, + reader=reader, reader_name=reader_name, + parser=parser, parser_name=parser_name, + writer=None, writer_name='null', + settings=settings, settings_spec=settings_spec, + settings_overrides=settings_overrides, + config_section=config_section, + enable_exit_status=enable_exit_status) + + # Note: clean up the document tree object by removing some things that are + # not needed anymore and that would not pickled well (this is the primary + # intended use of this publish method). + pub.document.transformer = None + pub.document.reporter = None + + return pub.document, pub.writer.parts + class DummyParser(parsers.Parser): @@ -429,7 +472,7 @@ class DummyReader(readers.Reader): Its 'reading' consists in preparing and fixing up the document tree for the writing phase. - Used by `publish_doctree`. + Used by `publish_from_doctree`. """ supported = ('dummy',) @@ -450,11 +493,11 @@ class DummyReader(readers.Reader): return self.doctree -def publish_doctree(doctree, source_path=None, destination_path=None, - writer=None, writer_name='pseudoxml', - settings=None, settings_spec=None, - settings_overrides=None, config_section=None, - enable_exit_status=None): +def publish_from_doctree(doctree, source_path=None, destination_path=None, + writer=None, writer_name='pseudoxml', + settings=None, settings_spec=None, + settings_overrides=None, config_section=None, + enable_exit_status=None): """ Set up & run a `Publisher` to render from an existing document tree data structure, for programmatic use with string I/O. Return a pair of encoded -- cgit v1.2.1 From 38e233c4e2d2e1736af4b4938d622e358204e564 Mon Sep 17 00:00:00 2001 From: wiemann Date: Sun, 26 Jun 2005 23:09:59 +0000 Subject: publish_doctree now returns only the doctree, not an empty (useless) "parts" dictionary; polished git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@3587 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 94 +++++++++++++++++++++++++++----------------------------- 1 file changed, 45 insertions(+), 49 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index aa511296f..b198b3022 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -420,18 +420,14 @@ def publish_doctree(source, source_path=None, settings_overrides=None, config_section=None, enable_exit_status=None): """ - Set up & run a `Publisher` for programmatic use with string I/O. Return - a pair of the encoded string or Unicode string output, and the parts. + Set up & run a `Publisher` for programmatic use with string I/O. + Return the document tree. - For encoded string output, be sure to set the 'output_encoding' setting to + For encoded string input, be sure to set the 'input_encoding' setting to the desired encoding. Set it to 'unicode' for unencoded Unicode string - output. Here's one way:: + input. Here's one way:: - publish_string(..., settings_overrides={'output_encoding': 'unicode'}) - - Similarly for Unicode string input (`source`):: - - publish_string(..., settings_overrides={'input_encoding': 'unicode'}) + publish_doctree(..., settings_overrides={'input_encoding': 'unicode'}) Parameters: see `publish_programmatically`. """ @@ -453,45 +449,7 @@ def publish_doctree(source, source_path=None, pub.document.transformer = None pub.document.reporter = None - return pub.document, pub.writer.parts - - -class DummyParser(parsers.Parser): - - """Dummy parser that does nothing. Used by `DummyReader`.""" - - supported = ('dummy',) - - def parse(self, inputstring, document): - pass - -class DummyReader(readers.Reader): - - """ - Dummy reader that is initialized with an existing document tree. - Its 'reading' consists in preparing and fixing up the document - tree for the writing phase. - - Used by `publish_from_doctree`. - """ - - supported = ('dummy',) - - def __init__(self, doctree): - readers.Reader.__init__(self, parser=DummyParser()) - self.doctree = doctree - - def read(self, source, parser, settings): - # Fixup the document tree with a transformer and a reporter if it does - # not have them yet. - if self.doctree.transformer is None: - self.doctree.transformer = Transformer(self.doctree) - if self.doctree.reporter is None: - self.doctree.reporter = utils.new_reporter( - source.source_path, settings) - self.doctree.settings = settings - return self.doctree - + return pub.document def publish_from_doctree(doctree, source_path=None, destination_path=None, writer=None, writer_name='pseudoxml', @@ -507,7 +465,8 @@ def publish_from_doctree(doctree, source_path=None, destination_path=None, the desired encoding. Set it to 'unicode' for unencoded Unicode string output. Here's one way:: - publish_doctree(..., settings_overrides={'output_encoding': 'unicode'}) + publish_from_doctree( + ..., settings_overrides={'output_encoding': 'unicode'}) Parameters: see `publish_programmatically`. """ @@ -633,3 +592,40 @@ def publish_programmatically(source_class, source, source_path, pub.set_destination(destination, destination_path) output = pub.publish(enable_exit_status=enable_exit_status) return output, pub + + +class DummyParser(parsers.Parser): + + """Dummy parser that does nothing. Used by `DummyReader`.""" + + supported = ('dummy',) + + def parse(self, inputstring, document): + pass + +class DummyReader(readers.Reader): + + """ + Dummy reader that is initialized with an existing document tree. + Its 'reading' consists in preparing and fixing up the document + tree for the writing phase. + + Used by `publish_from_doctree`. + """ + + supported = ('dummy',) + + def __init__(self, doctree): + readers.Reader.__init__(self, parser=DummyParser()) + self.doctree = doctree + + def read(self, source, parser, settings): + # Fixup the document tree with a transformer and a reporter if it does + # not have them yet. + if self.doctree.transformer is None: + self.doctree.transformer = Transformer(self.doctree) + if self.doctree.reporter is None: + self.doctree.reporter = utils.new_reporter( + source.source_path, settings) + self.doctree.settings = settings + return self.doctree -- cgit v1.2.1 From 70461a0d6a58a0e74f4b880a495b1a0765717e19 Mon Sep 17 00:00:00 2001 From: wiemann Date: Mon, 27 Jun 2005 00:42:40 +0000 Subject: make sure the Writer transforms get applied when calling publish_from_doctree; keep correct source_path; set correct doctree.settings object; I implemented all this blindly, without proper testing git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@3590 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index b198b3022..de82370cc 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -443,15 +443,17 @@ def publish_doctree(source, source_path=None, config_section=config_section, enable_exit_status=enable_exit_status) + # The transformer is not needed anymore. + del pub.document.transformer + # Note: clean up the document tree object by removing some things that are # not needed anymore and that would not pickled well (this is the primary # intended use of this publish method). - pub.document.transformer = None pub.document.reporter = None return pub.document -def publish_from_doctree(doctree, source_path=None, destination_path=None, +def publish_from_doctree(doctree, destination_path=None, writer=None, writer_name='pseudoxml', settings=None, settings_spec=None, settings_overrides=None, config_section=None, @@ -470,17 +472,23 @@ def publish_from_doctree(doctree, source_path=None, destination_path=None, Parameters: see `publish_programmatically`. """ - output, pub = publish_programmatically( - source_class=io.NullInput, source='', source_path=source_path, - destination_class=io.StringOutput, - destination=None, destination_path=destination_path, - reader=DummyReader(doctree), reader_name=None, - parser=None, parser_name=None, - writer=writer, writer_name=writer_name, - settings=settings, settings_spec=settings_spec, - settings_overrides=settings_overrides, - config_section=config_section, - enable_exit_status=enable_exit_status) + # Create fresh Transformer object. + doctree.transformer = Transformer(doctree) + # Create Publisher. + pub = Publisher(DummyReader(doctree), None, writer, settings=settings, + source_class=io.NullInput, + destination_class=io.StringOutput) + # Set Writer. There's no Reader and Parser because the document + # is already parsed. + pub.set_components(None, None, writer_name) + # Set settings. + pub.process_programmatic_settings( + settings_spec, settings_overrides, config_section) + # Override document settings with new settings. + doctree.settings = pub.settings + # Set destination path and run. + pub.set_destination(None, destination_path) + output = pub.publish(enable_exit_status=enable_exit_status) return output, pub.writer.parts def publish_programmatically(source_class, source, source_path, @@ -620,10 +628,7 @@ class DummyReader(readers.Reader): self.doctree = doctree def read(self, source, parser, settings): - # Fixup the document tree with a transformer and a reporter if it does - # not have them yet. - if self.doctree.transformer is None: - self.doctree.transformer = Transformer(self.doctree) + # Fixup the document tree with a reporter if it does not have it yet. if self.doctree.reporter is None: self.doctree.reporter = utils.new_reporter( source.source_path, settings) -- cgit v1.2.1 From 4460491ab1b043e0d112a63a859b9053337a10a0 Mon Sep 17 00:00:00 2001 From: wiemann Date: Mon, 27 Jun 2005 00:59:00 +0000 Subject: do not set reporter=None -- pickling is not the only use case; added documentation about doctree.settings object git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@3591 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index de82370cc..5e511a98e 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -442,15 +442,9 @@ def publish_doctree(source, source_path=None, settings_overrides=settings_overrides, config_section=config_section, enable_exit_status=enable_exit_status) - - # The transformer is not needed anymore. + # The transformer is not needed anymore. (A new transformer will + # be created in `publish_from_doctree`.) del pub.document.transformer - - # Note: clean up the document tree object by removing some things that are - # not needed anymore and that would not pickled well (this is the primary - # intended use of this publish method). - pub.document.reporter = None - return pub.document def publish_from_doctree(doctree, destination_path=None, @@ -463,6 +457,9 @@ def publish_from_doctree(doctree, destination_path=None, structure, for programmatic use with string I/O. Return a pair of encoded string output and document parts. + Note that doctree.settings is overridden; if you want to use the settings + of the original `doctree` document, pass settings=doctree.settings. + For encoded string output, be sure to set the 'output_encoding' setting to the desired encoding. Set it to 'unicode' for unencoded Unicode string output. Here's one way:: @@ -472,7 +469,7 @@ def publish_from_doctree(doctree, destination_path=None, Parameters: see `publish_programmatically`. """ - # Create fresh Transformer object. + # Create fresh Transformer object, to be populated from Writer component. doctree.transformer = Transformer(doctree) # Create Publisher. pub = Publisher(DummyReader(doctree), None, writer, settings=settings, @@ -484,8 +481,6 @@ def publish_from_doctree(doctree, destination_path=None, # Set settings. pub.process_programmatic_settings( settings_spec, settings_overrides, config_section) - # Override document settings with new settings. - doctree.settings = pub.settings # Set destination path and run. pub.set_destination(None, destination_path) output = pub.publish(enable_exit_status=enable_exit_status) @@ -615,9 +610,6 @@ class DummyReader(readers.Reader): """ Dummy reader that is initialized with an existing document tree. - Its 'reading' consists in preparing and fixing up the document - tree for the writing phase. - Used by `publish_from_doctree`. """ @@ -628,9 +620,6 @@ class DummyReader(readers.Reader): self.doctree = doctree def read(self, source, parser, settings): - # Fixup the document tree with a reporter if it does not have it yet. - if self.doctree.reporter is None: - self.doctree.reporter = utils.new_reporter( - source.source_path, settings) + # Override document settings with new settings. self.doctree.settings = settings return self.doctree -- cgit v1.2.1 From 225cfed60f15481726d5a3d68beb3383e4a52a7a Mon Sep 17 00:00:00 2001 From: wiemann Date: Mon, 27 Jun 2005 01:13:51 +0000 Subject: fixed some docstrings git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@3592 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 5e511a98e..10fa13b06 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -395,7 +395,7 @@ def publish_parts(source, source_path=None, destination_path=None, the desired encoding. Set it to 'unicode' for unencoded Unicode string input. Here's how:: - publish_string(..., settings_overrides={'input_encoding': 'unicode'}) + publish_parts(..., settings_overrides={'input_encoding': 'unicode'}) Parameters: see `publish_programmatically`. """ @@ -427,7 +427,7 @@ def publish_doctree(source, source_path=None, the desired encoding. Set it to 'unicode' for unencoded Unicode string input. Here's one way:: - publish_doctree(..., settings_overrides={'input_encoding': 'unicode'}) + publish_doctree(..., settings_overrides={'input_encoding': 'unicode'}) Parameters: see `publish_programmatically`. """ -- cgit v1.2.1 From 98f4ec4fb3daf08766943a909740833cc1ab82f2 Mon Sep 17 00:00:00 2001 From: wiemann Date: Mon, 27 Jun 2005 01:20:00 +0000 Subject: in DummyReader.read: restore doctree.reporter if necessary; otherwise things get a bit more complicated for pickling applications because new_reporter requires a fully set-up settings object, which is most conveniently created by the publish_* functions git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@3593 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 10fa13b06..bf1ad460d 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -620,6 +620,10 @@ class DummyReader(readers.Reader): self.doctree = doctree def read(self, source, parser, settings): + # Useful for pickling, where the reporter is destroyed. + if self.doctree.reporter is None: + self.doctree.reporter = utils.new_reporter( + source.source_path, settings) # Override document settings with new settings. self.doctree.settings = settings return self.doctree -- cgit v1.2.1 From ddba601498ad2c7e472daa384357b384e6ac2f45 Mon Sep 17 00:00:00 2001 From: wiemann Date: Mon, 27 Jun 2005 02:04:39 +0000 Subject: moved Dummy reader and parser to readers/dummy.py and parsers/dummy.py, resp.; removed "restructuredtext" default for parser_name when initializing a reader git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@3594 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 46 ++++++++-------------------------------------- 1 file changed, 8 insertions(+), 38 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index bf1ad460d..5ffe6c911 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -19,7 +19,7 @@ __docformat__ = 'reStructuredText' import sys import pprint from docutils import __version__, __version_details__, SettingsSpec -from docutils import frontend, io, utils, readers, writers, parsers +from docutils import frontend, io, utils, readers, writers from docutils.frontend import OptionParser from docutils.transforms import Transformer @@ -471,13 +471,15 @@ def publish_from_doctree(doctree, destination_path=None, """ # Create fresh Transformer object, to be populated from Writer component. doctree.transformer = Transformer(doctree) + # Create reader with existing doctree. + from docutils.readers import dummy + reader = dummy.Reader(doctree) # Create Publisher. - pub = Publisher(DummyReader(doctree), None, writer, settings=settings, - source_class=io.NullInput, + pub = Publisher(reader, None, writer, + settings=settings, source_class=io.NullInput, destination_class=io.StringOutput) - # Set Writer. There's no Reader and Parser because the document - # is already parsed. - pub.set_components(None, None, writer_name) + # Set parser and writer name. + pub.set_components(None, 'dummy', writer_name) # Set settings. pub.process_programmatic_settings( settings_spec, settings_overrides, config_section) @@ -595,35 +597,3 @@ def publish_programmatically(source_class, source, source_path, pub.set_destination(destination, destination_path) output = pub.publish(enable_exit_status=enable_exit_status) return output, pub - - -class DummyParser(parsers.Parser): - - """Dummy parser that does nothing. Used by `DummyReader`.""" - - supported = ('dummy',) - - def parse(self, inputstring, document): - pass - -class DummyReader(readers.Reader): - - """ - Dummy reader that is initialized with an existing document tree. - Used by `publish_from_doctree`. - """ - - supported = ('dummy',) - - def __init__(self, doctree): - readers.Reader.__init__(self, parser=DummyParser()) - self.doctree = doctree - - def read(self, source, parser, settings): - # Useful for pickling, where the reporter is destroyed. - if self.doctree.reporter is None: - self.doctree.reporter = utils.new_reporter( - source.source_path, settings) - # Override document settings with new settings. - self.doctree.settings = settings - return self.doctree -- cgit v1.2.1 From 1dbe0e3b6756d6401fd7f4e10b7b5a1f873215e2 Mon Sep 17 00:00:00 2001 From: wiemann Date: Mon, 27 Jun 2005 14:45:14 +0000 Subject: beautified --dump-transforms output git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@3605 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 5ffe6c911..82d7fb918 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -225,7 +225,7 @@ class Publisher: if self.settings.dump_transforms: print >>sys.stderr, '\n::: Transforms applied:' print >>sys.stderr, pprint.pformat( - self.document.transformer.applied) + [a[1:] for a in self.document.transformer.applied]) if self.settings.dump_pseudo_xml: print >>sys.stderr, '\n::: Pseudo-XML:' print >>sys.stderr, self.document.pformat().encode( -- cgit v1.2.1 From 8e3d3b9a20ccb700b2269885de7daa65d45c797f Mon Sep 17 00:00:00 2001 From: wiemann Date: Mon, 27 Jun 2005 20:29:10 +0000 Subject: added assert to get a helpful error message when passing things like "writer='html'" to publish_* git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@3606 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 82d7fb918..a3f1c9ac1 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -18,6 +18,7 @@ __docformat__ = 'reStructuredText' import sys import pprint +from types import StringType from docutils import __version__, __version_details__, SettingsSpec from docutils import frontend, io, utils, readers, writers from docutils.frontend import OptionParser @@ -52,6 +53,11 @@ class Publisher: self.writer = writer """A `docutils.writers.Writer` instance.""" + for component in 'reader', 'parser', 'writer': + assert not isinstance(getattr(self, component), StringType), \ + ('passed string as "%s" parameter; use "%s_name" instead' + % (getattr(self, component), component, component)) + self.source = source """The source of input data, a `docutils.io.Input` instance.""" -- cgit v1.2.1 From 97f17ab24ee18309def82364e244897ade13fc2d Mon Sep 17 00:00:00 2001 From: goodger Date: Mon, 27 Jun 2005 21:00:22 +0000 Subject: Pythonicized kwargs; further beautified --dump-transforms git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@3608 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index a3f1c9ac1..d0e518c1d 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -230,8 +230,13 @@ class Publisher: print >>sys.stderr, pprint.pformat(self.document.__dict__) if self.settings.dump_transforms: print >>sys.stderr, '\n::: Transforms applied:' + print >>sys.stderr, (' (priority, transform class, ' + 'pending node details, keyword args)') print >>sys.stderr, pprint.pformat( - [a[1:] for a in self.document.transformer.applied]) + [(priority, '%s.%s' % (xclass.__module__, xclass.__name__), + pending and pending.details, kwargs) + for priority, xclass, pending, kwargs + in self.document.transformer.applied]) if self.settings.dump_pseudo_xml: print >>sys.stderr, '\n::: Pseudo-XML:' print >>sys.stderr, self.document.pformat().encode( -- cgit v1.2.1 From a8601c75767ed7185b1ca7ccec1b16e4e71c8c53 Mon Sep 17 00:00:00 2001 From: wiemann Date: Mon, 27 Jun 2005 22:25:11 +0000 Subject: do not double apply default transforms in publish_from_doctree; this does not look very clean... and there are still no tests which cover that behavior git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@3614 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 2 ++ 1 file changed, 2 insertions(+) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index d0e518c1d..9602f4a7b 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -482,6 +482,8 @@ def publish_from_doctree(doctree, destination_path=None, """ # Create fresh Transformer object, to be populated from Writer component. doctree.transformer = Transformer(doctree) + # Don't double apply default transforms. + doctree.transformer.default_transforms = () # Create reader with existing doctree. from docutils.readers import dummy reader = dummy.Reader(doctree) -- cgit v1.2.1 From 57d2f142c45280b6c47448797b560c8ec013db1b Mon Sep 17 00:00:00 2001 From: goodger Date: Wed, 29 Jun 2005 19:46:05 +0000 Subject: updated to support changes in revisions 3626-; revised ``publish_from_doctree`` API for consistency git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@3632 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 9602f4a7b..b7caf9cc5 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -23,6 +23,7 @@ from docutils import __version__, __version_details__, SettingsSpec from docutils import frontend, io, utils, readers, writers from docutils.frontend import OptionParser from docutils.transforms import Transformer +import docutils.readers.doctree class Publisher: @@ -453,12 +454,12 @@ def publish_doctree(source, source_path=None, settings_overrides=settings_overrides, config_section=config_section, enable_exit_status=enable_exit_status) - # The transformer is not needed anymore. (A new transformer will - # be created in `publish_from_doctree`.) + # The transformer is not needed any more + # (a new transformer will be created in `publish_from_doctree`): del pub.document.transformer return pub.document -def publish_from_doctree(doctree, destination_path=None, +def publish_from_doctree(document, destination_path=None, writer=None, writer_name='pseudoxml', settings=None, settings_spec=None, settings_overrides=None, config_section=None, @@ -468,8 +469,8 @@ def publish_from_doctree(doctree, destination_path=None, structure, for programmatic use with string I/O. Return a pair of encoded string output and document parts. - Note that doctree.settings is overridden; if you want to use the settings - of the original `doctree` document, pass settings=doctree.settings. + Note that document.settings is overridden; if you want to use the settings + of the original `document` document, pass settings=document.settings. For encoded string output, be sure to set the 'output_encoding' setting to the desired encoding. Set it to 'unicode' for unencoded Unicode string @@ -481,22 +482,17 @@ def publish_from_doctree(doctree, destination_path=None, Parameters: see `publish_programmatically`. """ # Create fresh Transformer object, to be populated from Writer component. - doctree.transformer = Transformer(doctree) - # Don't double apply default transforms. - doctree.transformer.default_transforms = () - # Create reader with existing doctree. - from docutils.readers import dummy - reader = dummy.Reader(doctree) - # Create Publisher. + document.transformer = Transformer(document) + # Don't apply default transforms twice: + document.transformer.default_transforms = (document.transformer + .reprocess_transforms) + reader = docutils.readers.doctree.Reader(parser_name='null') pub = Publisher(reader, None, writer, - settings=settings, source_class=io.NullInput, - destination_class=io.StringOutput) - # Set parser and writer name. - pub.set_components(None, 'dummy', writer_name) - # Set settings. + source=io.DocTreeInput(document), + destination_class=io.StringOutput, settings=settings) + pub.set_writer(writer_name) pub.process_programmatic_settings( settings_spec, settings_overrides, config_section) - # Set destination path and run. pub.set_destination(None, destination_path) output = pub.publish(enable_exit_status=enable_exit_status) return output, pub.writer.parts -- cgit v1.2.1 From f5fe452d38fcd576e79684f83c9916ffaa62277b Mon Sep 17 00:00:00 2001 From: goodger Date: Wed, 29 Jun 2005 20:37:37 +0000 Subject: docstring update & parameter indent fix git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@3636 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index b7caf9cc5..33098fe97 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -470,7 +470,7 @@ def publish_from_doctree(document, destination_path=None, string output and document parts. Note that document.settings is overridden; if you want to use the settings - of the original `document` document, pass settings=document.settings. + of the original `document`, pass settings=document.settings. For encoded string output, be sure to set the 'output_encoding' setting to the desired encoding. Set it to 'unicode' for unencoded Unicode string @@ -479,7 +479,10 @@ def publish_from_doctree(document, destination_path=None, publish_from_doctree( ..., settings_overrides={'output_encoding': 'unicode'}) - Parameters: see `publish_programmatically`. + Parameters: `document` is a `docutils.nodes.document` object, an existing + document tree. + + Other parameters: see `publish_programmatically`. """ # Create fresh Transformer object, to be populated from Writer component. document.transformer = Transformer(document) @@ -498,13 +501,13 @@ def publish_from_doctree(document, destination_path=None, return output, pub.writer.parts def publish_programmatically(source_class, source, source_path, - destination_class, destination, destination_path, - reader, reader_name, - parser, parser_name, - writer, writer_name, - settings, settings_spec, - settings_overrides, config_section, - enable_exit_status): + destination_class, destination, destination_path, + reader, reader_name, + parser, parser_name, + writer, writer_name, + settings, settings_spec, + settings_overrides, config_section, + enable_exit_status): """ Set up & run a `Publisher` for custom programmatic use. Return the encoded string output and the Publisher object. -- cgit v1.2.1 From 67bba794ba0290728c6cb0a9d184a2ebc3a278de Mon Sep 17 00:00:00 2001 From: blais Date: Thu, 30 Jun 2005 17:19:48 +0000 Subject: Fixed bug with setting the writer in the publisher from doctree. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@3641 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 33098fe97..d6c3ec973 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -493,7 +493,8 @@ def publish_from_doctree(document, destination_path=None, pub = Publisher(reader, None, writer, source=io.DocTreeInput(document), destination_class=io.StringOutput, settings=settings) - pub.set_writer(writer_name) + if not writer and writer_name: + pub.set_writer(writer_name) pub.process_programmatic_settings( settings_spec, settings_overrides, config_section) pub.set_destination(None, destination_path) -- cgit v1.2.1 From df203f30c658d959e8cd8c38f495d5d32c0b1edb Mon Sep 17 00:00:00 2001 From: wiemann Date: Thu, 30 Jun 2005 17:52:00 +0000 Subject: deactivated _stylesheet_required for programmatic use; improved documentation git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@3642 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index d6c3ec973..15d67af51 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -135,6 +135,12 @@ class Publisher: defaults = (settings_overrides or {}).copy() # Propagate exceptions by default when used programmatically: defaults.setdefault('traceback', 1) + # Do not complain on missing stylesheet when used + # programmatically. A stylesheet is often not necessary + # because the application uses only snippets of the + # output, and requiring a stylesheet would break existing + # applications which use Docutils programmatically. + defaults.setdefault('_stylesheet_required', 0) self.get_settings(settings_spec=settings_spec, config_section=config_section, **defaults) -- cgit v1.2.1 From 7447045b6bd0c5099e874fa42664724b5e654384 Mon Sep 17 00:00:00 2001 From: goodger Date: Sun, 3 Jul 2005 01:15:56 +0000 Subject: better wrapping git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@3650 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 15d67af51..694696f18 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -493,8 +493,8 @@ def publish_from_doctree(document, destination_path=None, # Create fresh Transformer object, to be populated from Writer component. document.transformer = Transformer(document) # Don't apply default transforms twice: - document.transformer.default_transforms = (document.transformer - .reprocess_transforms) + document.transformer.default_transforms = ( + document.transformer.reprocess_transforms) reader = docutils.readers.doctree.Reader(parser_name='null') pub = Publisher(reader, None, writer, source=io.DocTreeInput(document), -- cgit v1.2.1 From c5aecf0e115ab835a4c79657245bcaed911ad41e Mon Sep 17 00:00:00 2001 From: wiemann Date: Thu, 7 Jul 2005 02:11:14 +0000 Subject: separated default (universal) transforms into two stages so that no transform is applied twice; do not delete document.transformer in publish_doctree (Martin, this may be important for the pickle writer -- you may need to delete document.transformer); regenerate reporter object unconditionally; do so in publish_from_doctree, not the doctree reader; added tests; I will post about all that on Docutils-develop in one or two days git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@3663 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 55 ++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 34 insertions(+), 21 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 694696f18..8375fbe89 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -193,11 +193,16 @@ class Publisher: def publish(self, argv=None, usage=None, description=None, settings_spec=None, settings_overrides=None, - config_section=None, enable_exit_status=None): + config_section=None, enable_exit_status=None, stage=None): """ Process command line options and arguments (if `self.settings` not already set), run `self.reader` and then `self.writer`. Return `self.writer`'s output. + + Pass ``stage=1`` to set transformer.default_transforms to the + stage-1 transforms; dito for ``stage=2`` and stage-2 + transforms, resp. See the documentation in the + `transforms.Transformer` class. """ if self.settings is None: self.process_command_line( @@ -208,6 +213,13 @@ class Publisher: try: self.document = self.reader.read(self.source, self.parser, self.settings) + assert stage in (1, 2, None) + if stage == 1: + self.document.transformer.default_transforms = ( + self.document.transformer.stage1_transforms) + elif stage == 2: + self.document.transformer.default_transforms = ( + self.document.transformer.stage2_transforms) self.apply_transforms() output = self.writer.write(self.document, self.destination) self.writer.assemble_parts() @@ -449,20 +461,16 @@ def publish_doctree(source, source_path=None, Parameters: see `publish_programmatically`. """ - output, pub = publish_programmatically( - source_class=source_class, source=source, source_path=source_path, - destination_class=io.NullOutput, - destination=None, destination_path=None, - reader=reader, reader_name=reader_name, - parser=parser, parser_name=parser_name, - writer=None, writer_name='null', - settings=settings, settings_spec=settings_spec, - settings_overrides=settings_overrides, - config_section=config_section, - enable_exit_status=enable_exit_status) - # The transformer is not needed any more - # (a new transformer will be created in `publish_from_doctree`): - del pub.document.transformer + pub = Publisher(reader=reader, parser=parser, writer=None, + settings=settings, + source_class=source_class, + destination_class=io.NullOutput) + pub.set_components(reader_name, parser_name, 'null') + pub.process_programmatic_settings( + settings_spec, settings_overrides, config_section) + pub.set_source(source, source_path) + pub.set_destination(None, None) + output = pub.publish(enable_exit_status=enable_exit_status, stage=1) return pub.document def publish_from_doctree(document, destination_path=None, @@ -478,6 +486,9 @@ def publish_from_doctree(document, destination_path=None, Note that document.settings is overridden; if you want to use the settings of the original `document`, pass settings=document.settings. + Also, new document.transformer and document.reporter objects are + generated. + For encoded string output, be sure to set the 'output_encoding' setting to the desired encoding. Set it to 'unicode' for unencoded Unicode string output. Here's one way:: @@ -490,11 +501,6 @@ def publish_from_doctree(document, destination_path=None, Other parameters: see `publish_programmatically`. """ - # Create fresh Transformer object, to be populated from Writer component. - document.transformer = Transformer(document) - # Don't apply default transforms twice: - document.transformer.default_transforms = ( - document.transformer.reprocess_transforms) reader = docutils.readers.doctree.Reader(parser_name='null') pub = Publisher(reader, None, writer, source=io.DocTreeInput(document), @@ -503,8 +509,15 @@ def publish_from_doctree(document, destination_path=None, pub.set_writer(writer_name) pub.process_programmatic_settings( settings_spec, settings_overrides, config_section) + # Create fresh Transformer object, to be populated from Writer component. + document.transformer = Transformer(document) + # Create fresh Reporter object because it is dependent on (new) settings. + document.reporter = utils.new_reporter(document.get('source', ''), + pub.settings) + # Replace existing settings object with new one. + document.settings = pub.settings pub.set_destination(None, destination_path) - output = pub.publish(enable_exit_status=enable_exit_status) + output = pub.publish(enable_exit_status=enable_exit_status, stage=2) return output, pub.writer.parts def publish_programmatically(source_class, source, source_path, -- cgit v1.2.1 From dbe5d1f505a545190a1ed1adfc7d97772c710e38 Mon Sep 17 00:00:00 2001 From: wiemann Date: Fri, 8 Jul 2005 00:29:32 +0000 Subject: do not return parts dictionary in publish_from_doctree; you could use publish_parts for that, I think, and returning a tuple unnecessarily complicates the interface git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@3671 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 8375fbe89..df35656ad 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -517,8 +517,7 @@ def publish_from_doctree(document, destination_path=None, # Replace existing settings object with new one. document.settings = pub.settings pub.set_destination(None, destination_path) - output = pub.publish(enable_exit_status=enable_exit_status, stage=2) - return output, pub.writer.parts + return pub.publish(enable_exit_status=enable_exit_status, stage=2) def publish_programmatically(source_class, source, source_path, destination_class, destination, destination_path, -- cgit v1.2.1 From 019c9db1733295e6c56937d6a77005dba70b4fb9 Mon Sep 17 00:00:00 2001 From: wiemann Date: Fri, 8 Jul 2005 01:32:57 +0000 Subject: moved document refurbishing logic to doctree reader; added source_class argument to publish_parts; improved docstring git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@3674 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index df35656ad..b666006f3 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -409,7 +409,8 @@ def publish_string(source, source_path=None, destination_path=None, enable_exit_status=enable_exit_status) return output -def publish_parts(source, source_path=None, destination_path=None, +def publish_parts(source, source_path=None, source_class=io.StringInput, + destination_path=None, reader=None, reader_name='standalone', parser=None, parser_name='restructuredtext', writer=None, writer_name='pseudoxml', @@ -430,7 +431,7 @@ def publish_parts(source, source_path=None, destination_path=None, Parameters: see `publish_programmatically`. """ output, pub = publish_programmatically( - source_class=io.StringInput, source=source, source_path=source_path, + source=source, source_path=source_path, source_class=source_class, destination_class=io.StringOutput, destination=None, destination_path=destination_path, reader=reader, reader_name=reader_name, @@ -509,13 +510,6 @@ def publish_from_doctree(document, destination_path=None, pub.set_writer(writer_name) pub.process_programmatic_settings( settings_spec, settings_overrides, config_section) - # Create fresh Transformer object, to be populated from Writer component. - document.transformer = Transformer(document) - # Create fresh Reporter object because it is dependent on (new) settings. - document.reporter = utils.new_reporter(document.get('source', ''), - pub.settings) - # Replace existing settings object with new one. - document.settings = pub.settings pub.set_destination(None, destination_path) return pub.publish(enable_exit_status=enable_exit_status, stage=2) @@ -543,14 +537,15 @@ def publish_programmatically(source_class, source, source_path, * `source`: Type depends on `source_class`: - - `io.FileInput`: Either a file-like object (must have 'read' and - 'close' methods), or ``None`` (`source_path` is opened). If neither - `source` nor `source_path` are supplied, `sys.stdin` is used. + - If `source_class` is `io.FileInput`: Either a file-like object + (must have 'read' and 'close' methods), or ``None`` + (`source_path` is opened). If neither `source` nor + `source_path` are supplied, `sys.stdin` is used. - - `io.StringInput` **required**: The input string, either an encoded - 8-bit string (set the 'input_encoding' setting to the correct - encoding) or a Unicode string (set the 'input_encoding' setting to - 'unicode'). + - If `source_class` is `io.StringInput` **required**: The input + string, either an encoded 8-bit string (set the + 'input_encoding' setting to the correct encoding) or a Unicode + string (set the 'input_encoding' setting to 'unicode'). * `source_path`: Type depends on `source_class`: -- cgit v1.2.1 From 5c90ddc006b36818e727b4a42196a708b2a78a25 Mon Sep 17 00:00:00 2001 From: wiemann Date: Wed, 24 Aug 2005 21:33:21 +0000 Subject: made _stylesheet_required setting default to 0, activating it in the rst2html.py front-end tool git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@3830 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 6 ------ 1 file changed, 6 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index b666006f3..6c7c6c599 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -135,12 +135,6 @@ class Publisher: defaults = (settings_overrides or {}).copy() # Propagate exceptions by default when used programmatically: defaults.setdefault('traceback', 1) - # Do not complain on missing stylesheet when used - # programmatically. A stylesheet is often not necessary - # because the application uses only snippets of the - # output, and requiring a stylesheet would break existing - # applications which use Docutils programmatically. - defaults.setdefault('_stylesheet_required', 0) self.get_settings(settings_spec=settings_spec, config_section=config_section, **defaults) -- cgit v1.2.1 From 5b111006c653af57889892100b7adc5ffcca8e6f Mon Sep 17 00:00:00 2001 From: wiemann Date: Tue, 20 Sep 2005 20:04:53 +0000 Subject: Merged "transforms" branch into trunk. - Replaced ``default_transforms`` attribute of TransformSpec with ``get_transforms()`` method. - Added universal.Decorations and universal.ExposeInternals transforms as default transforms for all readers. - Added universal.Messages and universal.FilterMessages transforms as default transforms for all writers. - Added ``ReReader`` base class for readers that reread an existing document tree. - Added ``UnfilteredWriter`` base class for writers that pass the document tree on unchanged. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@3892 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 6c7c6c599..043c8ebfc 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -187,16 +187,11 @@ class Publisher: def publish(self, argv=None, usage=None, description=None, settings_spec=None, settings_overrides=None, - config_section=None, enable_exit_status=None, stage=None): + config_section=None, enable_exit_status=None): """ Process command line options and arguments (if `self.settings` not already set), run `self.reader` and then `self.writer`. Return `self.writer`'s output. - - Pass ``stage=1`` to set transformer.default_transforms to the - stage-1 transforms; dito for ``stage=2`` and stage-2 - transforms, resp. See the documentation in the - `transforms.Transformer` class. """ if self.settings is None: self.process_command_line( @@ -207,13 +202,6 @@ class Publisher: try: self.document = self.reader.read(self.source, self.parser, self.settings) - assert stage in (1, 2, None) - if stage == 1: - self.document.transformer.default_transforms = ( - self.document.transformer.stage1_transforms) - elif stage == 2: - self.document.transformer.default_transforms = ( - self.document.transformer.stage2_transforms) self.apply_transforms() output = self.writer.write(self.document, self.destination) self.writer.assemble_parts() @@ -465,7 +453,7 @@ def publish_doctree(source, source_path=None, settings_spec, settings_overrides, config_section) pub.set_source(source, source_path) pub.set_destination(None, None) - output = pub.publish(enable_exit_status=enable_exit_status, stage=1) + output = pub.publish(enable_exit_status=enable_exit_status) return pub.document def publish_from_doctree(document, destination_path=None, @@ -505,7 +493,7 @@ def publish_from_doctree(document, destination_path=None, pub.process_programmatic_settings( settings_spec, settings_overrides, config_section) pub.set_destination(None, destination_path) - return pub.publish(enable_exit_status=enable_exit_status, stage=2) + return pub.publish(enable_exit_status=enable_exit_status) def publish_programmatically(source_class, source, source_path, destination_class, destination, destination_path, -- cgit v1.2.1 From f4f7c04762a2ca3c6ef26cfa0be404c3da1ba956 Mon Sep 17 00:00:00 2001 From: goodger Date: Tue, 6 Dec 2005 01:07:16 +0000 Subject: in ``Publisher.publish()``, expanded the generic top-level exception catching git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@4148 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index 043c8ebfc..a9acfafb5 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -193,20 +193,21 @@ class Publisher: already set), run `self.reader` and then `self.writer`. Return `self.writer`'s output. """ - if self.settings is None: - self.process_command_line( - argv, usage, description, settings_spec, config_section, - **(settings_overrides or {})) - self.set_io() exit = None try: + if self.settings is None: + self.process_command_line( + argv, usage, description, settings_spec, config_section, + **(settings_overrides or {})) + self.set_io() self.document = self.reader.read(self.source, self.parser, self.settings) self.apply_transforms() output = self.writer.write(self.document, self.destination) self.writer.assemble_parts() except Exception, error: - if self.settings.traceback: # propagate exceptions? + if self.settings and self.settings.traceback: + # propagate exceptions? self.debugging_dumps() raise self.report_Exception(error) -- cgit v1.2.1 From 261175e07bc7221ba941ebb592781b5a8fbd7e6d Mon Sep 17 00:00:00 2001 From: goodger Date: Wed, 7 Dec 2005 14:30:56 +0000 Subject: don't catch SystemExit exceptions git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@4150 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index a9acfafb5..a0da54033 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -194,6 +194,7 @@ class Publisher: `self.writer`'s output. """ exit = None + exit_status = 0 try: if self.settings is None: self.process_command_line( @@ -205,6 +206,8 @@ class Publisher: self.apply_transforms() output = self.writer.write(self.document, self.destination) self.writer.assemble_parts() + except SystemExit: + exit = 1 except Exception, error: if self.settings and self.settings.traceback: # propagate exceptions? @@ -212,13 +215,14 @@ class Publisher: raise self.report_Exception(error) exit = 1 + exit_status = 1 self.debugging_dumps() if (enable_exit_status and self.document and (self.document.reporter.max_level >= self.settings.exit_status_level)): sys.exit(self.document.reporter.max_level + 10) elif exit: - sys.exit(1) + sys.exit(exit_status) return output def debugging_dumps(self): -- cgit v1.2.1 From 4d561e3fce3f36665abe998aaba18eb1dc8af1f1 Mon Sep 17 00:00:00 2001 From: wiemann Date: Tue, 13 Dec 2005 19:05:44 +0000 Subject: always propagate exceptions when in doubt (when self.settings couldn't be initialized) git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@4199 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index a0da54033..dd03f5786 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -209,8 +209,8 @@ class Publisher: except SystemExit: exit = 1 except Exception, error: - if self.settings and self.settings.traceback: - # propagate exceptions? + if not self.settings or self.settings.traceback: + # Propagate exceptions. self.debugging_dumps() raise self.report_Exception(error) -- cgit v1.2.1 From 3c936ee8411af341f1bbae4c042d3b85a37145ab Mon Sep 17 00:00:00 2001 From: goodger Date: Wed, 14 Dec 2005 02:12:00 +0000 Subject: corrected r4199 git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@4208 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/core.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'docutils/core.py') diff --git a/docutils/core.py b/docutils/core.py index dd03f5786..598ec8e68 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -194,7 +194,6 @@ class Publisher: `self.writer`'s output. """ exit = None - exit_status = 0 try: if self.settings is None: self.process_command_line( @@ -206,12 +205,14 @@ class Publisher: self.apply_transforms() output = self.writer.write(self.document, self.destination) self.writer.assemble_parts() - except SystemExit: + except SystemExit, error: exit = 1 + exit_status = error.code except Exception, error: - if not self.settings or self.settings.traceback: - # Propagate exceptions. - self.debugging_dumps() + if not self.settings: # exception too early to report nicely + raise + if self.settings.traceback: # Propagate exceptions? + self.debugging_dumps() raise self.report_Exception(error) exit = 1 -- cgit v1.2.1