ó
i#ñ]c           @   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 Z d  d l Z y e	 sk t
 ‚ Wn e k
 r… e Z	 n Xd e f d „  ƒ  YZ d S(   iÿÿÿÿNi   (   t   Source(   t   Configurationt   PDFKitc           B   s™   e  Z d  Z d e f d „  ƒ  YZ d d d d d e d „ Z d „  Z d d „ Z	 d d „ Z
 d d „ Z d „  Z d	 „  Z d
 „  Z d „  Z d „  Z RS(   s,  
    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()
    t   ImproperSourceErrorc           B   s    e  Z d  Z d „  Z d „  Z RS(   s!   Wrong source type for stylesheetsc         C   s   | |  _  d  S(   N(   t   msg(   t   selfR   (    (    sB   /data/av2000/b2b/venv/lib/python2.7/site-packages/pdfkit/pdfkit.pyt   __init__    s    c         C   s   |  j  S(   N(   R   (   R   (    (    sB   /data/av2000/b2b/venv/lib/python2.7/site-packages/pdfkit/pdfkit.pyt   __str__#   s    (   t   __name__t
   __module__t   __doc__R   R   (    (    (    sB   /data/av2000/b2b/venv/lib/python2.7/site-packages/pdfkit/pdfkit.pyR      s   	c	   	      C   s  t  | | ƒ |  _ | d  k r' t ƒ  n | |  _ y |  j j j d ƒ |  _ Wn  t k
 rn |  j j |  _ n Xt ƒ  |  _	 |  j j
 ƒ  r¦ |  j	 j |  j | ƒ ƒ n  | d  k	 rÅ |  j	 j | ƒ n  | d  k r× i  n | |  _ | |  _ | |  _ | |  _ g  |  _ d  S(   Ns   utf-8(   R    t   sourcet   NoneR   t   configurationt   wkhtmltopdft   decodet   AttributeErrort   dictt   optionst   isStringt   updatet   _find_options_in_metat   toct   covert   cover_firstt   csst   stylesheets(	   R   t   url_or_filet   type_R   R   R   R   R   R   (    (    sB   /data/av2000/b2b/venv/lib/python2.7/site-packages/pdfkit/pdfkit.pyR   &   s"    	 			c         c   sŠ   xƒ |  j  | ƒ D]r \ } } | Vt | t t f ƒ r} t | ƒ d k r\ | d r\ | d sh t d ƒ ‚ | d V| d Vq | Vq Wd S(   s   
        Generator of args parts based on options specification.

        Note: Empty parts will be filtered out at _command generator
        i   i    i   sF   Option value can only be either a string or a (tuple, list) of 2 itemsN(   t   _normalize_optionst
   isinstancet   listt   tuplet   lent   AssertionError(   R   t   optst   optkeyt   optval(    (    sB   /data/av2000/b2b/venv/lib/python2.7/site-packages/pdfkit/pdfkit.pyt   _genargs=   s    2	c         c   sO  |  j  r |  j |  j  ƒ n  |  j Vx( |  j |  j ƒ D] } | r7 | Vq7 q7 W|  j rq |  j rq d V|  j Vn  |  j r­ d Vx+ |  j |  j ƒ D] } | r’ | Vq’ q’ Wn  |  j rÐ |  j rÐ d V|  j Vn  |  j j	 ƒ  sî |  j j
 ƒ  rö d VnB t |  j j t ƒ r|  j j ƒ  Vn x |  j j D] } | Vq)W| rF| Vn d Vd S(   s0   
        Generator of all command parts
        R   R   t   -N(   R   t   _prepend_cssR   R&   R   R   R   R   R   R   t	   isFileObjR   t
   basestringt   to_s(   R   t   patht   argpartt   s(    (    sB   /data/av2000/b2b/venv/lib/python2.7/site-packages/pdfkit/pdfkit.pyt   _commandM   s4    			c         C   s   t  |  j | ƒ ƒ S(   N(   R   R/   (   R   R,   (    (    sB   /data/av2000/b2b/venv/lib/python2.7/site-packages/pdfkit/pdfkit.pyt   commandz   s    c         C   s)  |  j  | ƒ } t j | d t j d t j d t j ƒ} |  j j ƒ  s` |  j j ƒ  r{ |  j r{ |  j j ƒ  j	 d ƒ } n3 |  j j
 ƒ  r¨ |  j j j ƒ  j	 d ƒ } n d  } | j d | ƒ \ } } | pÏ | } y | j d ƒ } Wn t k
 rþ d } n X| j } d | k r't d | ƒ ‚ n  d	 | k rFt d
 | ƒ ‚ n  | d k rmt d j | | ƒ ƒ ‚ n  d | k rŒt j j | ƒ n  | s–| Sy] t j | d d ƒB } | j d ƒ }	 |	 d k rèt d d j | ƒ ƒ ‚ n  t SWd  QXWn/ t k
 r$}
 t d d j | ƒ |
 ƒ ‚ n Xd  S(   Nt   stdint   stdoutt   stderrs   utf-8t   inputt    s   cannot connect to X servers¼   %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-servert   Errors   wkhtmltopdf reported an error:
i    s5   wkhtmltopdf exited with non-zero code {0}. error:
{1}s   --quiett   encodingi   sB   Command failed: %s
Check whhtmltopdf output without 'quiet' optiont    sF   Command failed: %s
Check whhtmltopdf output without 'quiet' option
%s (   R0   t
   subprocesst   Popent   PIPER   R   t   isFileR   R+   t   encodeR)   t   readR   t   communicateR   t   UnicodeDecodeErrort
   returncodet   IOErrort   formatt   sysR2   t   writet   codecst   opent   joint   True(   R   R,   t   argst   resultR4   R2   R3   t	   exit_codet   ft   textt   e(    (    sB   /data/av2000/b2b/venv/lib/python2.7/site-packages/pdfkit/pdfkit.pyt   to_pdf}   sH    '
	c         c   s©   x¢ t  | j ƒ  ƒ D]Ž \ } } d | k rA d |  j | ƒ } n |  j | ƒ } t | t  t f ƒ r„ x9 | D] } | | f Vql Wq | | r™ t | ƒ n | f Vq Wd S(   sh   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
        s   --s   --%sN(   R   t   itemst   _normalize_argR   R    t   str(   R   R   t   keyt   valuet   normalized_keyR%   (    (    sB   /data/av2000/b2b/venv/lib/python2.7/site-packages/pdfkit/pdfkit.pyR   ·   s    c         C   s
   | j  ƒ  S(   N(   t   lower(   R   t   arg(    (    sB   /data/av2000/b2b/venv/lib/python2.7/site-packages/pdfkit/pdfkit.pyRR   Ð   s    c         C   s   d | S(   Ns   <style>%s</style>(    (   R   t
   stylesheet(    (    sB   /data/av2000/b2b/venv/lib/python2.7/site-packages/pdfkit/pdfkit.pyt   _style_tag_forÓ   s    c         C   s–  |  j  j ƒ  s$ t |  j  j  t ƒ r6 |  j d ƒ ‚ n  t | t ƒ sQ | g } n  g  } x? | D]7 } t j | d d ƒ } | j | j ƒ  ƒ Wd  QXq^ Wd j	 | ƒ } |  j  j
 ƒ  rt j |  j  j ƒ  d d ƒ } | j ƒ  } Wd  QXt | j d |  j | ƒ d ƒ d ƒ |  _  nz |  j  j ƒ  r’d |  j  j ƒ  k rm|  j  j ƒ  j d |  j | ƒ d ƒ |  j  _  q’|  j | ƒ |  j  j ƒ  |  j  _  n  d  S(   Ns6   CSS files can be added only to a single file or stringR7   s   UTF-8s   
s   </head>t   string(   R   t   isUrlR   R   R   RF   RG   t   appendR>   RH   R<   R+   R    t   replaceRZ   R   (   R   R,   t   css_datat   pRM   t   inp(    (    sB   /data/av2000/b2b/venv/lib/python2.7/site-packages/pdfkit/pdfkit.pyR(   Ö   s(    $!"c         C   s³   t  | t j ƒ s$ | j j d k r3 | j ƒ  } n  i  } xs t j d | ƒ D]_ } t j d |  j	 j
 | ƒ rL t j d |  j	 j
 | ƒ d } t j d | ƒ d | | <qL qL W| S(   sÊ   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}
        t   StreamReaderWriters   <meta [^>]*>s   name=["']%ss   name=["']%s([^"']*)i    s   content=["']([^"']*)(   R   t   iot   IOBaset	   __class__R   R>   t   ret   findallt   searchR   t   meta_tag_prefix(   R   t   contentt   foundt   xt   name(    (    sB   /data/av2000/b2b/venv/lib/python2.7/site-packages/pdfkit/pdfkit.pyR   ò   s    	!N(   R   R	   R
   t	   ExceptionR   R   t   FalseR   R&   R/   R0   RP   R   RR   RZ   R(   R   (    (    (    sB   /data/av2000/b2b/venv/lib/python2.7/site-packages/pdfkit/pdfkit.pyR      s   			-:				(   Rf   R9   RD   R   R    R   R   Rc   RF   R*   R"   t	   NameErrorRS   t   objectR   (    (    (    sB   /data/av2000/b2b/venv/lib/python2.7/site-packages/pdfkit/pdfkit.pyt   <module>   s   
