o
    ö Ži!+  ã                   @   sˆ   d dl Z 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	Z	d dl
Z
ze e W n ey9   eZeZY nw G dd„ deƒZdS )é    N)ÚOrderedDicté   )ÚSource)ÚConfigurationc                   @   sˆ   e Zd ZdZG dd„ deƒZ		ddd„Zdd	„ Zdd
d„Zddd„Z	e
dd„ ƒZddd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )ÚPDFKita,  
    Main class that does all generation routine.

    :param url_or_file: str - either a URL, a path to a file or a string containing HTML
                       to convert
    :param type_: str - either 'url', 'file' or 'string'
    :param options: dict (optional) with wkhtmltopdf options, with or w/o '--'
    :param toc: dict (optional) - toc-specific wkhtmltopdf options, with or w/o '--'
    :param cover: str (optional) - url/filename with a cover html page
    :param configuration: (optional) instance of pdfkit.configuration.Configuration()
    c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )zPDFKit.ImproperSourceErrorz!Wrong source type for stylesheetsc                 C   s
   || _ d S ©N©Úmsg)Úselfr	   © r   úWC:\Users\psant\Downloads\Anu Raj Backend\school\venv\Lib\site-packages\pdfkit/pdfkit.pyÚ__init__#   s   
z#PDFKit.ImproperSourceError.__init__c                 C   s   | j S r   r   )r
   r   r   r   Ú__str__&   s   z"PDFKit.ImproperSourceError.__str__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   ÚImproperSourceError    s    r   NFc
           
      C   sÈ   t ||ƒ| _|d u rtƒ n|| _z
| jj d¡| _W n ty(   | jj| _Y nw tƒ | _| j 	¡ r;| j 
|  |¡¡ | jj| _|d urJ| j 
|¡ |d u rPi n|| _|| _|| _|	| _|| _g | _d S )Núutf-8)r   Úsourcer   ÚconfigurationÚwkhtmltopdfÚdecodeÚAttributeErrorr   ÚoptionsÚisStringÚupdateÚ_find_options_in_metaÚenvironÚtocÚcoverÚcover_firstÚverboseÚcssZstylesheets)
r
   Zurl_or_fileÚtype_r   r   r    r#   r   r!   r"   r   r   r   r   )   s*   ÿÿ


zPDFKit.__init__c                 c   sn   � |   |¡D ].\}}|V  t|ttfƒr1t|ƒdkr"|d r"|d s&J dƒ‚|d V  |d V  q|V  qdS )z�
        Generator of args parts based on options specification.

        Note: Empty parts will be filtered out at _command generator
        é   r   r   zFOption value can only be either a string or a (tuple, list) of 2 itemsN)Ú_normalize_optionsÚ
isinstanceÚlistÚtupleÚlen)r
   ÚoptsZoptkeyÚoptvalr   r   r   Ú_genargsD   s   €$
øzPDFKit._genargsc                 c   s  � | j r
|  | j ¡ | jV  | js| j ddi¡ |  | j¡D ]}|r&|V  q| jr4| jr4dV  | jV  | j	rHdV  |  | j	¡D ]}|rG|V  q@| jrU| jsUdV  | jV  | j
 ¡ s_| j
 ¡ rcdV  nt| j
j
tƒrq| j
 ¡ V  n
| j
j
D ]}|V  qu|r‚|V  dS dV  dS )z0
        Generator of all command parts
        ú--quietÚ r    r   ú-N)r#   Ú_prepend_cssr   r"   r   r   r-   r    r!   r   r   r   Ú	isFileObjr'   Ú
basestringÚto_s)r
   ÚpathZargpartÚsr   r   r   Ú_commandT   s>   €€€

zPDFKit._commandc                 C   s   t |  |¡ƒS r   )r(   r7   )r
   r5   r   r   r   Úcommand„   s   zPDFKit.commandc                 C   sx   | dkrd S |  ¡ }t|ƒdkr|  ¡ d  ¡ dkrd S d|v r&td| ƒ‚d|v r0td| ƒ‚|p3d	}td
 | |¡ƒ‚)Nr   r   éþÿÿÿÚDonezcannot connect to X serverz¼%s
You will need to run wkhtmltopdf within a "virtual" X server.
Go to the link below for more information
https://github.com/JazzCore/python-pdfkit/wiki/Using-wkhtmltopdf-without-X-serverÚErrorzwkhtmltopdf reported an error:
zUnknown Errorz5wkhtmltopdf exited with non-zero code {0}. error:
{1})Ú
splitlinesr*   ÚstripÚIOErrorÚformat)Ú	exit_codeÚstderrZstderr_linesÚ	error_msgr   r   r   Úhandle_error‡   s    ýzPDFKit.handle_errorc              
   C   s¨  |   |¡}tjdkr*t ¡ }| jtjO  _tj|_tj	|tj
tj
tj
| j|d�}ntj	|tj
tj
tj
| jd�}| j ¡ sE| j ¡ rN| jrN| j ¡  d¡}n| j ¡ r]| jj ¡  d¡}nd }|j|d�\}}|pl|pld}|jddd�}|j}|  ||¡ d	|vr‡tj |¡ |s‹|S z/tj|dd
��}	|	 d¡}
|
dkr¦tdd |¡ ƒ‚	 W d   ƒ W dS 1 s³w   Y  W d S  ttfyÓ } ztdd |¡|f ƒ‚d }~ww )NÚwin32)ÚstdinÚstdoutrA   ÚenvÚstartupinfo)rE   rF   rA   rG   r   )Úinputó    Úreplace)Úerrorsr.   ©Úencodingé   r/   zBCommand failed: %s
Check whhtmltopdf output without 'quiet' optionú TzFCommand failed: %s
Check whhtmltopdf output without 'quiet' option
%s )r8   ÚsysÚplatformÚ
subprocessÚSTARTUPINFOÚdwFlagsÚSTARTF_USESHOWWINDOWÚSW_HIDEÚwShowWindowÚPopenÚPIPEr   r   r   ÚisFiler#   r4   Úencoder2   ÚreadÚcommunicater   Ú
returncoderC   rF   ÚwriteÚcodecsÚopenr>   ÚjoinÚOSError)r
   r5   ÚargsrH   ÚresultrI   rF   rA   r@   ÚfÚtextÚer   r   r   Úto_pdf    sb   

ú	û

þ(ùþ€ÿzPDFKit.to_pdfc                 c   sˆ   � t | ¡ ƒD ]:\}}d|vrd|  |¡ }n|  |¡}t|t tfƒr-|D ]}||fV  q%t|tƒr4dn|}||r=t|ƒn|fV  qdS )ah   Generator of 2-tuples (option-key, option-value).
        When options spec is a list, generate a 2-tuples per list item.

        :param options: dict {option name: value}

        returns:
          iterator (option-key, option-value)
          - option names lower cased and prepended with
          '--' if necessary. Non-empty values cast to str
        z--z--%sr/   N)r(   ÚitemsÚ_normalize_argr'   r)   ÚboolÚunicode)r
   r   ÚkeyÚvalueZnormalized_keyr,   Znormalized_valuer   r   r   r&   á   s   €
€õzPDFKit._normalize_optionsc                 C   s   |  ¡ S r   )Úlower)r
   Úargr   r   r   rl   ú   ó   zPDFKit._normalize_argc                 C   s   d| S )Nz<style>%s</style>r   )r
   Z
stylesheetr   r   r   Ú_style_tag_forý   rs   zPDFKit._style_tag_forc              	   C   sJ  | j  ¡ st| j j tƒr|  d¡‚t|tƒs|g}g }|D ] }tj|dd��}| | ¡ ¡ W d   ƒ q1 s8w   Y  qd 	|¡}| j  
¡ rwtj| j  ¡ dd��}| ¡ }W d   ƒ n1 saw   Y  t| d|  |¡d ¡dƒ| _ d S | j  ¡ r£d| j  ¡ v r•| j  ¡  d|  |¡d ¡| j _ d S |  |¡| j  ¡  | j _ d S d S )Nz6CSS files can be added only to a single file or stringzUTF-8rM   Ú
z</head>Ústring)r   ZisUrlr'   r(   r   ra   rb   Úappendr]   rc   r[   r4   r   rK   rt   r   )r
   r5   Zcss_dataÚprg   Úinpr   r   r   r1      s2   

ÿ


ÿ
þ

ÿûzPDFKit._prepend_cssc                 C   sz   t |tjƒs|jjdkr| ¡ }i }t d|¡D ]"}t d| j	j
 |¡r:t d| j	j
 |¡d }t d|¡d ||< q|S )zÊReads 'content' and extracts options encoded in HTML meta tags

        :param content: str or file-like object - contains HTML to parse

        returns:
          dict: {config option: value}
        ÚStreamReaderWriterz<meta [^>]*>zname=["']%szname=["']%s([^"']*)r   zcontent=["']([^"']*)N)r'   ÚioÚIOBaseÚ	__class__r   r]   ÚreÚfindallÚsearchr   Zmeta_tag_prefix)r
   ÚcontentÚfoundÚxÚnamer   r   r   r     s"   
ÿÿÿÿ€zPDFKit._find_options_in_meta)NNNNNFFr   )r   r   r   r   Ú	Exceptionr   r   r-   r7   r8   ÚstaticmethodrC   rj   r&   rl   rt   r1   r   r   r   r   r   r      s"    	
ÿ

0

Ar   )r~   rS   rQ   Úcollectionsr   r   r   r   r   r{   ra   r3   rn   Ú	NameErrorÚstrÚobjectr   r   r   r   r   Ú<module>   s    þ