
    kh&                     h    d dl Z d dlZd dlZd dlZd dlZd dlmZmZ ddlm	Z	 ddl
mZ  G d d      Zy)    N)
raise_fromstring_types   )Config)Sourcec                   ~    e Zd ZdZ G d de      ZddZd ZddZddZ	d	 Z
ed
        Zed        Zd Zd ZddZy)IMGKitzMain class for imgkitc                       e Zd ZdZd Zy)IMGKit.SourceErrorz!Wrong source type for stylesheetsc                 <    t         j                  |        || _        y)zSourceError messageN)	Exception__init__message)selfr   s     I/var/www/teggl/fontify/venv/lib/python3.12/site-packages/imgkit/imgkit.pyr   zIMGKit.SourceError.__init__   s     t$"DL    N)__name__
__module____qualname____doc__r    r   r   SourceErrorr      s
    /	#r   r   Nc           	         g d}|D ]  }t        | ||j                  |d             ! t        ||      | _        |s
t	               n|| _        | j
                  j                         | _        i | _        | j                  j                         r*| j                  j                  | j                  |             |r| j                  j                  |       |r#d|v r| j
                  j                         | _        | j                  r| j                  ni | _        | j                  | _        | j                   | _        | j"                  | _        g | _        y)zDeliver parameters into IMGkit)toccovercover_firstcssNxvfb)setattrgetr   sourcer   configget_wkhtmltoimagewkhtmltoimageoptionsisStringupdate_find_options_in_metaget_xvfbr   r   r   r   r   stylesheets)r   url_or_filesource_typer%   r"   kwargsparamsparams           r   r   zIMGKit.__init__   s    8 	:ED%E4!89	:[+6&,fh&![[::<;;!LL : :; GHLL(v(,,.DI#xx488RZZ
++88r   c              #      K   | j                  |      D ]Q  \  }}| t        |t        t        f      r.t	        |      dk(  r
|d   r|d   sJ d       |d    |d    N| S yw)z7Generator of args parts based on options specification.   r   r   zFOption value can only be either a string or a (tuple, list) of 2 itemsN)_normalize_options
isinstancelisttuplelen)r   r%   optkeyoptvals       r   _gegetate_argszIMGKit._gegetate_args6   sz      #55g> 
	NFFL&4-0K1$vay\[\@QiQi
	s   A'A)c              #     K   | j                  | j                        }|D cg c]  }| }}| j                  r| j                  | j                         d|v r#|j	                  d       | j
                   d | j                   |D ]	  }|s|  | j                  r| j                  rd | j                   | j                  r+d | j                  | j                        D ]	  }|s|  | j                  r| j                  sd | j                   | j                  j                         s| j                  j                         rd n`t        | j                  j                  t              r| j                  j                          n| j                  j                  D ]  }|  |r| yd yc c}w w)zGenerator of all command partsz--xvfbz-ar   r   -N)r9   r%   r   _prepend_cssremover   r$   r   r   r   r!   r&   	isFileObjr3   r   to_s)r   pathr%   xargpartss         r   _commandzIMGKit._commandE   s     %%dll3%&1&&88dhh'wNN8$))O J    	G	 ::$**M**88K..txx8 "!M" ::d..M** ;;!T[[%:%:%<I$++,,l;kk&&((++ AG
 JI_ 's    G	GA%GA G2CGc                 6    t        | j                  |            S )zGenerate command)r4   rD   )r   r@   s     r   commandzIMGKit.commandz   s     DMM$'((r   c              #   (  K   t        |j                               D ]r  \  }}d|v r| j                  |      }n dj                  | j                  |            }t	        |t         t
        f      r|D ]  }||f 
 `||rt        |      n|f t yw)ak  
        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: value}

        returns:
          iterator (option-key, option-value)
          - option names lower cased and prepended with
          '--' if necessary. Non-empty values cast to str
        z--z--{}N)r4   items_normalize_argformatr3   r5   str)r   r%   keyvaluenormalized_keyopt_vals         r   r2   zIMGKit._normalize_options   s      w}}/ 
	EJCs{!%!4!4S!9!'t/B/B3/G!H%$/$ 2G('112 %Ec%juDD
	Es   BBc                 "    | j                         S N)lower)args    r   rI   zIMGKit._normalize_arg   s    yy{r   c                 $    dj                  |       S )Nz<style>{}</style>)rJ   )
stylesheets    r   
_style_tagzIMGKit._style_tag   s    "))*55r   c                 V   | j                   j                         s$t        | j                   j                   t              r| j	                  d      t        |t              s|g}g }|D ]A  }t        j                  |d      5 }|j                  |j                                d d d        C dj                  |      }| j                   j                         r|t        j                  | j                   j                         d      5 }|j                         }d d d        t        j                  d| j                  |      dz         d      | _         y | j                   j                         rd| j                   j                         v rL| j                   j                         j                  d| j                  |      dz         | j                   _         y | j                  |      | j                   j                         z   | j                   _         y y # 1 sw Y   xY w# 1 sw Y   
xY w)Nz6CSS files can be added only to a single file or stringzUTF-8)encoding
z</head>string)r!   isUrlr3   r4   r   codecsopenappendreadjoinisFiler?   r   replacerV   r&   )r   r@   css_datacss_filefinps         r   r<   zIMGKit._prepend_css   s   ;;*T[[-?-?"F""H  $%6D 	*HX8 *A)* *	* 99X&;;T[[--/'B affh Itx'@9'LMxDK [[!!#DKK,,..%)[[%5%5%7%?%?tx89D&" &*__X%>AQAQAS%S" $* *
 s    HHH	H(c                    t        |t        j                        s|j                  j                  dk(  r|j                         }i }t        j                  d|      D ]  }t        j                  dj                  | j                  j                        |      s=t        j                  dj                  | j                  j                        |      d   }t        j                  d|      d   ||<    |S )zReads '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=["']{}zname=["']{}([^"']*)r   zcontent=["']([^"']*))r3   ioIOBase	__class__r   r_   refindallsearchrJ   r"   meta_tag_prefix)r   contentfoundconnames        r   r(   zIMGKit._find_options_in_meta   s     w		*  ))-AAllnG::ng6 	KCyy..t{{/J/JKSQzz+224;;3N3NOQT !jj)A3GJd	K r   c           
      ,   | j                  |      }t        j                  |t        j                  t        j                  t        j                        }| j                  j                         s&| j                  j                         r;| j                  r/d}|| j                  j                         z   j                  d      }nP| j                  j                         r4| j                  j                  j                         j                  d      }nd}|j                  |      \  }}|xs |}	 |j                  d      }|j                  }d|v rt!        dj#                  |            d	|v rt!        d
|z         |dk7  r$d}	d|v rd}	t!        dj#                  |||	            d|vrt$        j&                  j)                  |       |s|S 	 t+        j,                  |d      5 }
|
j                  d      }|dk(  r)t!        dj#                  dj/                  |                  	 ddd       y# t        $ r d}Y w xY w# 1 sw Y   yxY w# t0        $ r>}t3        t!        dj#                  dj/                  |      |            |       Y d}~yd}~ww xY w)zGenerate image to path)stdinstdoutstderrz<meta charset="UTF-8">zutf-8N)input zcannot connect to X serverz{}
You will need to run wkhtmltoimage within a "virtual" X server.
Go to the link below for more information
http://wkhtmltopdf.orgErrorz!wkhtmltoimage reported an error:
r   QXcbConnectionzzYou need to install xvfb(sudo apt-get install xvfb, yum install xorg-x11-server-Xvfb, etc), then add option: {"xvfb": ""}.z<wkhtmltoimage exited with non-zero code {0}. error:
{1}

{2}z--quietrb)mode   zDCommand failed: {}
Check whhtmltoimage output without 'quiet' option TzJCommand failed: {0}
Check whhtmltoimage output without 'quiet' option
{1} )rF   
subprocessPopenPIPEr!   r&   ra   r   r?   encoder>   r_   communicatedecodeUnicodeDecodeError
returncodeOSErrorrJ   sysrv   writer\   r]   r`   IOErrorr   )r   r@   argsresultcharset_metarZ   rv   rw   	exit_code
xvfb_errorre   textio_errors                r   to_imgzIMGKit.to_img   sk    ||D!!!//????	
 ;;!dkk&8&8&:txx3L"T[[%5%5%77??HF[[""$[['',,.55g>FF++&+9!6	]]7+F %%	'61) *0	  f>GHH>J6) Z
QXXvz  D JJV$M	T- vvay2:!))/)? 
  C " 	F	B   	++16#((4.(+K
  	sI   0H/ I %AI &I /H=<H= I	I 	I 	J4JJ)NNrQ   )r   r   r   r   r   r   r   r9   rD   rF   r2   staticmethodrI   rV   r<   r(   r   r   r   r   r	   r	      se    #i #43j)
E2   6 6T<4Nr   r	   )r\   ri   rl   r   r   sixr   r   r"   r   r!   r   r	   r   r   r   <module>r      s)     	 	  
 (  X Xr   