a
    Z%b                      @   s   d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ d	d
lmZ G dd deZG dd deZG dd deZeedd dZdZg dfddZdS )    N)CalledProcessError)
raise_from)Data)data)which)shlex_quote)TempDir   )LatexBuildErrorc                   @   s$   e Zd ZdZg fddZdd ZdS )LatexBuilderzBase class for Latex builders.c                 C   s   t dS )a  Generates a PDF from LaTeX a source.

        If there are errors generating a ``LatexError`` is raised.

        :param source: The LaTeX source.
        :param texinputs: Include paths for TeX. An empty string causes the
                          default path to be added (see the tex manpage).
        :returns: A :class:`~data.Data` instance containing the generated PDF.
        NNotImplementedError)selfsource	texinputs r   [/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/site-packages/latex/build.py	build_pdf   s    
zLatexBuilder.build_pdfc                 C   s   t dS )zChecks if builder is available.

        Builders that depend on external programs like ``latexmk`` can check
        if these are found on the path or make sure other prerequisites are
        met.

        :return: A boolean indicating availability.Nr   r   r   r   r   is_available   s    zLatexBuilder.is_availableN)__name__
__module____qualname____doc__r   r   r   r   r   r   r      s   r   c                   @   s6   e Zd ZdZdddZedg fdd	Zd
d ZdS )LatexMkBuilderao  A latexmk based builder for LaTeX files.

    Uses the `latexmk
    <http://users.phys.psu.edu/~collins/software/latexmk-jcc/>`_ script to
    build latex files, which is part of some popular LaTeX distributions like
    `texlive <https://www.tug.org/texlive/>`_.

    The build process consists of copying the source file to a temporary
    directory and running latexmk on it, which will take care of reruns.

    :param latexmk: The path to the ``latexmk`` binary (will be looked up on
                    ``$PATH``).
    :param pdflatex: The path to the ``pdflatex`` binary (will be looked up on
                    ``$PATH``).
    :param xelatex: The path to the ``xelatex`` binary (will be looked up on
                    ``$PATH``).
    :param variant: The LaTeX variant to use. Valid choices are
                    `pdflatex` and `xelatex`. Defaults to `pdflatex`.
    latexmkpdflatexxelatexc                 C   s   || _ || _|| _|| _d S N)r   r   r   variant)r   r   r   r   r   r   r   r   __init__>   s    zLatexMkBuilder.__init__r   c                 C   s  t  ~}|jd|dL}|  tj|jd }|d }t| jddddd	d
g}| j	dkr~| j
ddd||jg}n*| j	dkr| j
d|jg}ntd| j	tj }	tj|tj |	d< z2tj|||	ttjdttjdttjdd W n8 ty2 }
 ztt|d |
 W Y d }
~
n
d }
~
0 0 tt|d d dW  d    W  d    S 1 sn0    Y  W d    n1 s0    Y  d S )N.latexsuffixdirr   .pdf-interaction=batchmode-halt-on-error-no-shell-escape-file-line-errorz%Oz%Sr   z-pdfz-pdflatex={} r   z-xelatexzInvalid LaTeX variant: {}	TEXINPUTSrw)cwdenvstdinstdoutstderr.logrbencoding)r   
temp_savedcloseospathsplitextnamer   r   r   r   formatjoin
ValueErrorenvironcopypathsep
subprocess
check_callopendevnullr   r   r
   Iread)r   r   r   tmpdirtmpbase_fn	output_fnZ	latex_cmdargsnewenver   r   r   r   E   sT    







(zLatexMkBuilder.build_pdfc                 C   sB   t | jsdS | jdkr&tt | jS | jdkr>tt | jS d S )NFr   r   )r   r   r   boolr   r   r   r   r   r   r   u   s    


zLatexMkBuilder.is_availableN)r   r   r   r   r   r   r   r   r    r   r   r   r   r   r   r   r   )   s     
/r   c                   @   s6   e Zd ZdZdddZedg fddZd	d
 ZdS )PdfLatexBuildera  A simple pdflatex based buidler for LaTeX files.

    Builds LaTeX files by copying them to a temporary directly and running
    ``pdflatex`` until the associated ``.aux`` file stops changing.

    .. note:: This may miss changes if ``biblatex`` or other additional tools
              are used. Usually, the :class:`~latex.build.LatexMkBuilder` will
              give more reliable results.

    :param pdflatex: The path to the ``pdflatex`` binary (will looked up on
                    ``$PATH``).
    :param max_runs: An integer providing an upper limit on the amount of times
                     ``pdflatex`` can be rerun before an exception is thrown.
    r      c                 C   s   || _ d| _d S )NrS   )r   max_runs)r   r   rT   r   r   r   r       s    zPdfLatexBuilder.__init__r   c                 C   s  t  n}|jd|d<}|  tj|jd }|d }|d }| jdddd	|jg}tj	 }	tj
|tj
 |	d
< d }
| j}|rz(tj|||	ttjdttjdd W n6 ty } ztt|d | W Y d }~n
d }~0 0 t|d }||
krq$|}
|d8 }qtd| jtt|d d dW  d    W  d    S 1 s^0    Y  W d    n1 s~0    Y  d S )Nr!   r"   r   r%   z.auxr&   r'   r(   r)   r+   r,   r-   )r.   r/   r0   r1   r3   r4   r	   z?Maximum number of runs ({}) without a stable .aux file reached.r5   )r   r7   r8   r9   r:   r;   r<   r   r@   rA   rB   r>   rT   rC   rD   rE   rF   r   r   r
   rH   RuntimeErrorr=   rG   )r   r   r   rI   rJ   rK   rL   Zaux_fnrM   rN   Zprev_auxZ	runs_leftrO   Zauxr   r   r   r      sH    




(

zPdfLatexBuilder.build_pdfc                 C   s   t t| jS r   )rP   r   r   r   r   r   r   r      s    zPdfLatexBuilder.is_availableN)r   rS   rQ   r   r   r   r   rR      s
   
.rR   c                   C   s
   t ddS )Nr   )r   )r   r   r   r   r   <lambda>       rV   )r   r   Z	xelatexmkc                 C   sb   |du rt }n|tvr tdn|f}|D ]*}t| }| }| sFq*|| |  S tddS )a  Builds a LaTeX source to PDF.

    Will automatically instantiate an available builder (or raise a
    :class:`exceptions.RuntimeError` if none are available) and build the
    supplied source with it.

    Parameters are passed on to the builder's
    :meth:`~latex.build.LatexBuilder.build_pdf` function.

    :param builder: Specify which builder should be used - ``latexmk``,
                    ``pdflatex`` or ``xelatexmk``.
    NzInvalid Builder specifiedzPNo available builder could be instantiated. Please make sure LaTeX is installed.)PREFERRED_BUILDERSBUILDERSrU   r   r   )r   r   ZbuilderZbuildersZbldZbld_clsr   r   r   r      s    
r   )r9   rC   r   Zfuture.utilsr   r   r   rG   Zdata.decoratorsZshutilwhichr   Z	six.movesr   tempdirr   excr
   objectr   r   rR   rY   rX   r   r   r   r   r   <module>   s$   VH