B
    .Kc                 @   s   d Z ddlZddlZddlZddlmZ ddlmZmZm	Z	 ddl
mZ ddlmZ ddlmZ ddlmZmZ dd	lmZmZ dd
lmZ yddlmZ ddlmZ W n, ek
r   ddlmZ ddlmZ Y nX G dd deZ dS )zSetuptools/distutils commands to assist the building of sphinx documentation.

:author: Sebastian Wiesner <basti.wiesner@gmx.net>
    N)StringIO)AnyDictOptional)Sphinx)handle_exception)RemovedInSphinx70Warning)color_terminalnocolor)docutils_namespacepatch_docutils)abspath)Command)	ExecError)DistutilsExecErrorc               @   s   e Zd ZdZdZddddddd	d
dddddddddgZdddddgZddddZedddZ	dddd Z
ddd!d"ZdS )#BuildDoca9  
    Distutils command to build Sphinx documentation.

    The Sphinx build can then be triggered from distutils, and some Sphinx
    options can be set in ``setup.py`` or ``setup.cfg`` instead of Sphinx's
    own configuration file.

    For instance, from `setup.py`::

       # this is only necessary when not using setuptools/distribute
       from sphinx.setup_command import BuildDoc
       cmdclass = {'build_sphinx': BuildDoc}

       name = 'My project'
       version = '1.2'
       release = '1.2.0'
       setup(
           name=name,
           author='Bernard Montgomery',
           version=release,
           cmdclass=cmdclass,
           # these are optional and override conf.py settings
           command_options={
               'build_sphinx': {
                   'project': ('setup.py', name),
                   'version': ('setup.py', version),
                   'release': ('setup.py', release)}},
       )

    Or add this section in ``setup.cfg``::

       [build_sphinx]
       project = 'My project'
       version = 1.2
       release = 1.2.0
    zBuild Sphinx documentation)z	fresh-envEzdiscard saved environment)z	all-filesazbuild all files)zsource-dir=szSource directory)z
build-dir=NzBuild directory)zconfig-dir=cz'Location of the configuration directory)zbuilder=bz]The builder (or builders) to use. Can be a comma- or space-separated list. Defaults to "html")zwarning-is-errorWzTurn warning into errors)zproject=NzThe documented project's name)zversion=NzThe short X.Y version)zrelease=Nz.The full version, including alpha/beta/rc tags)ztoday=NzCHow to format the current date, used as the replacement for |today|)z
link-indexiz!Link index.html to the master doc)	copyrightNzThe copyright string)pdbNzStart pdb on exception)	verbosityvz$increase verbosity (can be repeated))nitpickynz1nit-picky mode, warn about all missing references)z
keep-goingNz)With -W, keep going when getting warningsz	fresh-envz	all-fileszwarning-is-errorz
link-indexr   N)returnc             C   sx   d | _ | _d| _d | _d | _d| _d| _d| _d| _d| _	d| _
d | _d| _d| _| jjd | _d| _d| _d| _d S )NFZhtml    )	fresh_env	all_filesr   
source_dir	build_dirbuilderwarning_is_errorprojectversionreleasetoday
config_dir
link_indexr   Zdistributionverboser   	tracebackr   
keep_going)self r2   3lib/python3.7/site-packages/sphinx/setup_command.pyinitialize_optionsZ   s"    zBuildDoc.initialize_optionsc             C   sH   x@dD ]8}t j|sqx$t |D ]\}}}d|kr$|S q$W qW t jS )N)docZdocszconf.py)ospathisdirwalkcurdir)r1   ZguessrootZ	_dirnames	filenamesr2   r2   r3   _guess_source_dirn   s    
zBuildDoc._guess_source_dirc                s     d  jd kr.   _ d j   d  jd krJ j _ jd krt d}tj	
t|jd _tj	
 jd _ fdd jD  _d S )	Nr&   zUsing source directory %sr$   buildZsphinxZdoctreesc                s    g | ]}|t j j|fqS r2   )r6   r7   joinr%   ).0r&   )r1   r2   r3   
<listcomp>   s   z-BuildDoc.finalize_options.<locals>.<listcomp>)Zensure_string_listr$   r=   ZannounceZensure_dirnamer,   r%   Zget_finalized_commandr6   r7   r?   r   Z
build_basedoctree_dirr&   builder_target_dirs)r1   r>   r2   )r1   r3   finalize_optionsw   s    







zBuildDoc.finalize_optionsc       
      C   s  t jdtdd t st  | js*t }ntj}i }| j	rD| j	|d< | j
rT| j
|d< | jrd| j|d< | jrt| j|d< | jr| j|d< | jr| j|d	< x| jD ]\}}d }y| jp| j}t|j t X t| j| j|| j|||| j| j| j| jd
}|j| jd |jrtd|jj W d Q R X W d Q R X W nF t k
rz } z&t!|| |tj" | j#sjt$d|W d d }~X Y nX | j%sq|j&j'|jj( }|j)d}	t*+||	 qW d S )Nz$setup.py build_sphinx is deprecated.   )
stacklevelr(   r)   r*   r+   r   r   )ZfreshenvZwarningiserrorr   r0   )Z	force_allzcaused by %s builder.r!   index),warningswarnr   r	   r
   r.   r   sysstdoutr(   r)   r*   r+   r   r   rC   r,   r$   r   r   r   rB   r"   r'   r   r0   r>   r#   Z
statuscoder   r&   name	Exceptionr   stderrr   
SystemExitr-   ZconfigZroot_docZ
out_suffixZget_outfilenamer6   symlink)
r1   Zstatus_streamZconfoverridesr&   Zbuilder_target_dirZappZconfdirexcsrcdstr2   r2   r3   run   sV    







(zBuildDoc.run)__name__
__module____qualname____doc__descriptionZuser_optionsZboolean_optionsr4   strr=   rD   rT   r2   r2   r2   r3   r      s2   $	r   )!rX   r6   rJ   rH   ior   typingr   r   r   Zsphinx.applicationr   Zsphinx.cmd.buildr   Zsphinx.deprecationr   Zsphinx.util.consoler	   r
   Zsphinx.util.docutilsr   r   Zsphinx.util.osutilr   Z
setuptoolsr   Zsetuptools.errorsr   ImportErrorZdistutils.cmdZdistutils.errorsr   r   r2   r2   r2   r3   <module>   s$   