o ?]s@sdZddlmZdZddlZddlZddlmZmZm Z ddlm Z m Z m Z m Z mZddlmZddlmZdd lmZmZddlZGd d d eZd Zd ZddddddddddddeefddZ       d#ddZ       d$ddZde jddddddddddddfddZde jdddddddddf ddZ     d%ddZ!ddddddddddddeede j"fdd Z#d!d"Z$dS)&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`. See `The Docutils Publisher`_. .. _The Docutils Publisher: http://docutils.sf.net/docs/api/publisher.html )print_functionreStructuredTextN) __version____version_details__ SettingsSpec)frontendioutilsreaderswriters) OptionParser) Transformer) ErrorOutput ErrorStringc@seZdZdZddddejdejdfddZddZddZ d d Z  d&d d Z  d&d dZ ddZ  d'ddZd(ddZd(ddZd(ddZddZ   d)ddZddZd d!Zd"d#Zd$d%ZdS)* PublisherzK A facade encapsulating the high-level logic of a Docutils system. Nc Csd|_ ||_ ||_ ||_ dD]} tt|| tr(Jdt|| | | fq||_ ||_ ||_ ||_ ||_ t |_ dS)z 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). N)readerparserwriterzpassed string "%s" as "%s" parameter; pass an instance, or use the "%s_name" parameter instead (in docutils.core.publish_* convenience functions).)documentrrr isinstancegetattrstrsource source_class destinationdestination_classsettingsr_stderr) selfrrrrrrrr componentr //usr/lib/python3/dist-packages/docutils/core.py__init__!s2  zPublisher.__init__cCs$t|}||||_|jj|_dS)zSet `self.reader` by name.N)r get_reader_classrr)r reader_namer parser_name reader_classr r r! set_readerQs  zPublisher.set_readercCst|}||_dS)zSet `self.writer` by name.N)r get_writer_classr)r writer_name writer_classr r r! set_writerWs  zPublisher.set_writercCsb|jdur |||j||jdur#|jjdur|j||jj|_|jdur/||dSdSN)rr'r set_parserrr+)rr$r%r)r r r!set_components\s      zPublisher.set_componentscKs`|r|st}||_|}t|dkr|ddkrdg|_t|j|j|j|f|d||d}|S)N application applicationsT) componentsdefaultsread_config_filesusage description) rconfig_sectionsplitlenconfig_section_dependenciesr rrr)rr6r7 settings_specr8r4parts option_parserr r r!setup_option_parserfszPublisher.setup_option_parsercKs(|j||||fi|}||_|jS)a  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 processing from `self.publish()`. )r?get_default_valuesr)rr6r7r<r8r4r>r r r! get_settingsws  zPublisher.get_settingscCs@|jdur|pi}|dd|jd||d|dSdS)N tracebackT)r<r8r )rcopy setdefaultrA)rr<settings_overridesr8r4r r r!process_programmatic_settingss    z'Publisher.process_programmatic_settingsc sd|j||||fi|}|dur*tjdd}tjdkr*tjp dfdd|D}|||_dS)z Pass an empty list to `argv` to avoid reading `sys.argv` (the default). Set components first (`self.set_reader` & `self.set_writer`). Nr/)rasciicsg|]}|qSr )decode).0a argv_encodingr r! sz2Publisher.process_command_line..)r?sysargv version_inforlocale_encoding parse_argsr)rrPr6r7r<r8r4r>r rLr!process_command_lines   zPublisher.process_command_linecCs4|jdur |j|d|jdur|j|ddSdS)N) source_path)destination_path)r set_sourcerset_destination)rrUrVr r r!set_ios   zPublisher.set_iocCsb|dur |jj}n||j_z|j|||jjd|_WdSty0|j|||jjd|_YdSw)N)rrUencoding)r_sourcerinput_encodingr TypeError)rrrUr r r!rWs  zPublisher.set_sourcecCs:|dur |jj}n||j_|j|||jj|jjd|_dS)N)rrVrZ error_handler)r _destinationroutput_encodingoutput_encoding_error_handlerr)rrrVr r r!rXs  zPublisher.set_destinationcCs4|jj|j|j|jj|j|jf|jjdSr,) r transformerpopulate_from_componentsrrrrrapply_transformsrr r r!rds zPublisher.apply_transformsFc Cs@d}z8|jdur|j|||||fi|pi||j|j|j|j|_||j |j|j } |j Wn>t yQ} z d}| j} WYd} ~ n,d} ~ wtyx} z|js\|jjre||| d}d} WYd} ~ nd} ~ ww||r|jr|jjj|jjkrt|jjjd| S|rt| | S)z Process command line options and arguments (if `self.settings` not already set), run `self.reader` and then `self.writer`. Return `self.writer`'s output. Nr/T )rrTrYrreadrrrrdrwriterassemble_parts SystemExitcode ExceptionrBdebugging_dumpsreport_Exceptionreporter max_levelexit_status_levelrOexit) rrPr6r7r<rEr8enable_exit_statusrroutputerror exit_statusr r r!publishsP     zPublisher.publishcCs|jsdS|jjrtd|jdtt|jj|jd|jjr3td|jdtt|jj|jd|jj rWtd|jdtd|jdttdd|jj j D|jd|jj rqtd|jdt|j d |jddSdS) Nz ::: Runtime settings:filez ::: Document internals:z ::: Transforms applied:z@ (priority, transform class, pending node details, keyword args)cSs2g|]\}}}}|d|j|jf|o|j|fqS)z%s.%s) __module____name__details)rJpriorityxclasspendingkwargsr r r!rNs   z-Publisher.debugging_dumps..z ::: Pseudo-XML:raw_unicode_escape)rr dump_settingsprintrpprintpformat__dict__dump_internalsdump_transformsrbapplieddump_pseudo_xmlencoderer r r!rms8  zPublisher.debugging_dumpscCst|tjr ||dSt|tr||dSt|tjr+|j dt |dSt|tj r=|j dt |dSt dt ||jdt dt tjrSdtjpTdtjdf|jddS) Nz-Unable to open source file for reading: %s z2Unable to open destination file for writing: %s z%srxzExiting due to error. Use "--traceback" to diagnose. Please report errors to . Include "--traceback" output, Docutils version (%s%s), Python version (%s), your OS type & version, and the command line used.z [%s]r)rr SystemMessagereport_SystemMessageUnicodeEncodeErrorreport_UnicodeErrorr InputErrorrrhr OutputErrorrrdocutilsrrOversionr9rrur r r!rn s4      zPublisher.report_ExceptioncCs&td|jtjj|jf|jddS)Nz,Exiting due to level-%s (%s) system message.rx)rlevelr Reporterlevelsrrr r r!r"s  zPublisher.report_SystemMessagec CsZ|j|j|j}|jdt||jj|dd|dd|jj t t j dfdS)Na%s The specified output encoding (%s) cannot handle all of the output. Try setting "--output-encoding-error-handler" to * "xmlcharrefreplace" (for HTML & XML output); the output will contain "%s" and should be usable. * "backslashreplace" (for other output formats); look for "%s" in the output. * "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. rHxmlcharrefreplacebackslashreplacer)objectstartendrrhrrr`rrarrOrr9)rrudatar r r!r's  zPublisher.report_UnicodeError)NNNN)NNNNN)NN)NNNNNNF)r{rz __qualname____doc__r FileInput FileOutputr"r'r+r.r?rArFrTrYrWrXrdrwrmrnrrr r r r!rs@ 0         ) rz*%prog [options] [ []]zReads from (default is stdin) and writes to (default is stdout). See for the full reference. standalonerestructuredtext pseudoxmlTc Cs:t||||d}|||||j| | | ||| | d}|S)a% 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. - `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). )rr8rsrr.rw)rr$rr%rr)rr<rEr8rsrPr6r7pubrtr r r!publish_cmdlineKs rFcCs|tdidtjd|d|dtjd|d|d|d|d |d |d |d | d | d| d| d| d|\}}|S)z Set up & run a `Publisher` for programmatic use with file-like I/O. Return the encoded string output also. Parameters: see `publish_programmatically`. rrrUrrrVrr$rr%rr)rr<rEr8rsNr )publish_programmaticallyrrr)rrUrrVrr$rr%rr)rr<rEr8rsrtrr r r! publish_filefsH    rcCs|tdidtjd|d|dtjddd|d|d |d |d |d |d |d| d| d| d| d| \}}|S)aB 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 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`. rrrUrrNrVrr$rr%rr)rr<rEr8rsr )rr StringInput StringOutput)rrUrVrr$rr%rr)rr<rEr8rsrtrr r r!publish_stringsH   rcCs~tdid|d|d|dtjddd|d|d |d |d |d |d | d| d| d| d| d|\}}|jjS)a 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 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_parts(..., settings_overrides={'input_encoding': 'unicode'}) Parameters: see `publish_programmatically`. rrUrrrNrVrr$rr%rr)rr<rEr8rsr )rrrrr=)rrUrrVrr$rr%rr)rr<rEr8rsrtrr r r! publish_partssH   rc Cs\t||d||tjd} | ||d| || | | ||| dd| j| d} | jS)a Set up & run a `Publisher` for programmatic use with string I/O. Return the document tree. 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 one way:: publish_doctree(..., settings_overrides={'input_encoding': 'unicode'}) Parameters: see `publish_programmatically`. N)rrrrrrnullrs) rr NullOutputr.rFrWrXrwr)rrUrrr$rr%rr<rEr8rsrrtr r r!publish_doctrees   rc Csdtjjjdd} t| d|t|tj|d} |s|r| || |||| d|| j |dS)aJ Set up & run a `Publisher` to render from an existing document tree data structure, for programmatic use with string I/O. Return the encoded string output. 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:: publish_from_doctree( ..., settings_overrides={'output_encoding': 'unicode'}) Parameters: `document` is a `docutils.nodes.document` object, an existing document tree. Other parameters: see `publish_programmatically`. r)r%N)rrrr) rr doctreeReaderrr DocTreeInputrr+rFrXrw) rrVrr)rr<rEr8rsrrr r r!publish_from_doctrees   rc Cs<t|||||d}|||||j| | | ||| | d}|S)a 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. This is just like publish_cmdline, except that it uses io.BinaryFileOutput instead of io.FileOutput. Parameters: see `publish_programmatically` for the remainder. - `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). )rrrr)rr$rr%rr)rr<rEr8rsrPr6r7rrrrtr r r!publish_cmdline_to_binary s  rcCs\t||| | ||d}||| | || |||||||||j|d}||fS)a7 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 mailing list . Parameters: * `source_class` **required**: The class for dynamically created source objects. Typically `io.FileInput` or `io.StringInput`. * `source`: Type depends on `source_class`: - 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. - 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`: - `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 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`: 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? )rrrr)rr.rFrWrXrw)rrrUrrrVrr$rr%rr)rr<rEr8rsrrtr r r!r-s d   r)NNNNNrNrNrNNNNF) NNNrNrNrNNNNF)NNrNNNNF)%r __future__r __docformat__rOrrrrrrrr r r docutils.frontendr docutils.transformsr docutils.utils.error_reportingrrdocutils.readers.doctreerr default_usagedefault_descriptionrrrrrrrBinaryFileOutputrrr r r r!s   ,   $ "  ' !