o $a'@s4dZddlZddlZddlmZddlmZmZmZm Z m Z ddl m Z ddl mZddlmZddlmZdd lmZmZmZmZmZdd lmZmZmZdd lmZee Z!d Z"e#d Z$GdddeZ%Gddde%Z&Gddde%Z'de(de(de(fddZ)Gddde%Z*dedee(effddZ+dS)z sphinx.transforms.post_transforms.images ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Docutils transforms used by Sphinx. :copyright: Copyright 2007-2021 by the Sphinx team, see AUTHORS. :license: BSD, see LICENSE for details. N)ceil)AnyDictListOptionalTuple)nodes)Sphinx)__)SphinxTransform)epoch_to_rfc1123loggingrequestsrfc1123_to_epochsha1)get_image_extensionguess_mimetypeparse_data_uri) ensuredir z [:;<>|*" ]c@sXeZdZdeddfddZdejdefddZdejddfd d Z e de fd d Z dS) BaseImageConverterkwargsreturnNcKs,|jtjD] }||r||qdSN)documenttraverserimagematchhandle)selfrnoder!J/usr/lib/python3/dist-packages/sphinx/transforms/post_transforms/images.pyapply s   zBaseImageConverter.applyr cCsdS)NTr!rr r!r!r"r%zBaseImageConverter.matchcCsdSrr!r$r!r!r"r(r%zBaseImageConverter.handlecCstj|jjdS)Nimages)ospathjoinapp doctreedirrr!r!r"imagedir+szBaseImageConverter.imagedir) __name__ __module__ __qualname__rr#rrboolrrpropertystrr-r!r!r!r"rs rc@8eZdZdZdejdefddZdejddfddZdS) ImageDownloaderdr rcCs,|jjjgkr dS|jjjrdSd|dvS)NF://uri)r*buildersupported_image_typessupported_remote_imagesr$r!r!r"r3s   zImageDownloader.matchNc Csz;tj|d}d|vr|dd}|dkst|tkr3tj|d\}}t| |}t t d|}|d ddtddtddi}t|tkr[t| }ttj|j|tj|j||}i}tj|rtt|j}t||d <tj|d|d } | jd krttd |d| jfWdS|d|jjj |<| jd krt!|d} | "| j#Wdn1swY| j$d} | rt%| }t&|||ft'|dd} | dkrtj|ddkrt(| }tj|j|||} t || |jjj )||d|jjj | <| }|d)d||d| <||d<|jjj*+|jj,|WdSt-y]}zttd|d|fWYd}~dSd}~ww)Nr8?r_r7/&zIf-Modified-Since)headersiz%Could not fetch remote image: %s [%d]wbz last-modified*)default candidatesz%Could not fetch remote image: %s [%s]).r'r(basenamesplitlenMAX_FILENAME_LENsplitextrencode hexdigestresubCRITICAL_PATH_CHAR_REreplace translateordrr)r-existsrstatst_mtimer rget status_codeloggerwarningr r*envoriginal_image_uriopenwritecontentrArutimerrpopr&add_filedocname Exception)rr rHfilenameextdirnamer(rA timestamprf last_modifiedmimetypenewpathexcr!r!r"r;sb              (zImageDownloader.handle r.r/r0default_priorityrrr1rrr!r!r!r"r50r5c@r4) DataURIExtractorr rcCs2|jjjgkr dS|jjjdurdS|ddS)NFTr8zdata:)r*r9r;supported_data_uri_images startswithr$r!r!r"rus zDataURIExtractor.matchNcCst|d}t|j}|dur!tjtd|ddd|ddSttj |j dt |j  }tj |j d||}|d|jjj|<t|d}||j Wdn1s\wY|dd||d|j<||d<|jjj|jj|dS) Nr8zUnknown image format: %s...r)locationembededrCrGr<)rrrmrZr[r rr'r(r)r-rdatarNr*r\r]r^r_rbr&rcrd)rr rrgdigestr(rkr!r!r"r}s$   zDataURIExtractor.handlerpr!r!r!r"rsrrrrsrfrmrcCs2tj|}ttd|}tj|dt|S)Nr>r)r'r(rHrOrPrQrLr)rfrmrHr!r!r"get_filename_fors r{cseZdZUdZdZdZeeed<gZ e e e e fed<de de ddffd d Zd ejdefd d Zd ejde e e ffddZdefddZd ejde e fddZd ejddfddZde de defddZZS)ImageConverteraA base class for image converters. An image converter is kind of Docutils transform module. It is used to convert image files which are not supported by a builder to the appropriate format for that builder. For example, :py:class:`LaTeX builder <.LaTeXBuilder>` supports PDF, PNG and JPEG as image formats. However it does not support SVG images. For such case, using image converters allows to embed these unsupported images into the document. One of the image converters; :ref:`sphinx.ext.imgconverter ` can convert a SVG image to PNG format using Imagemagick internally. There are three steps to make your custom image converter: 1. Make a subclass of ``ImageConverter`` class 2. Override ``conversion_rules``, ``is_available()`` and ``convert()`` 3. Register your image converter to Sphinx using :py:meth:`.Sphinx.add_post_transform` rBN availableconversion_rulesargsrrcstj|i|dSr)super__init__)rrr __class__r!r"rszImageConverter.__init__r cCsd|jjjsdSt||t|jjj@rdS|jdur"||j_|js'dS||}|r0dSdS)NFT) r*r9r:setguess_mimetypesr} is_availablerget_conversion_rule)rr ruler!r!r"rs    zImageConverter.matchcCsB||D]}|jjjD]}||f}||jvr|Sq qdSr)rr*r9r:r~)rr candidate supportedrr!r!r"rs  z"ImageConverter.get_conversion_rulecCt)z/Return the image converter is available or not.NotImplementedErrorr,r!r!r"rszImageConverter.is_availablecCs6d|dvrgSd|dvrt|dgS|dS)Nr<rGrDr8)rkeysr$r!r!r"rs   zImageConverter.guess_mimetypescCs||\}}||dvr|d|}n|dd}t||}t|jtj|j|}tj|jj|}| ||rdd|dvrH||dd<n||d|<||d<||j j |<|j j |j j|dSdS)NrGrDr8)rr{rr-r'r(r)r*srcdirconvertr\r]r&rcrd)rr _from_tosrcpathrfdestpath abs_srcpathr!r!r"rs         zImageConverter.handlerrcCr)zConvert an image file to the expected format. *_from* is a path of the source image file, and *_to* is a path of the destination file. r)rrrr!r!r"rszImageConverter.convert)r.r/r0__doc__rqr}rr1__annotations__r~rrr3rrrrrrrrrr __classcell__r!r!rr"r|s   r|r*cCs |t|tddddS)NbuiltinT)versionparallel_read_safeparallel_write_safe)add_post_transformr5rs)r*r!r!r"setup s  r),rr'rOmathrtypingrrrrrdocutilsrsphinx.applicationr sphinx.localer sphinx.transformsr sphinx.utilr r rrrsphinx.util.imagesrrrsphinx.util.osutilr getLoggerr.rZrKcompilerQrr5rsr3r{r|rr!r!r!r"s*         B!t