U
    =g/                     @  s   d dl mZ 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	 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 eeZeZd Zd	Zd
ZdZdd ZG dd dZdd Zdd Z dd Z!ddddZ"dddZ#dd Z$edk re  dS )     )annotationsN)__version__)
httpConfig)DEFAULT_CSS)pisaDocument)getFilea  

USAGE: pisa [options] SRC [DEST]

SRC
  Name of a HTML file or a file pattern using * placeholder.
  If you want to read from stdin use "-" as file name.
  You may also load an URL over HTTP. Take care of putting
  the <src> in quotes if it contains characters like "?".

DEST
  Name of the generated PDF file or "-" if you like
  to send the result to stdout. Take care that the
  destination file is not already opened by an other
  application like the Adobe Reader. If the destination is
  not writeable a similar name will be calculated automatically.

[options]
  --base, -b:
    Specify a base path if input come via STDIN
  --css, -c:
    Path to default CSS file
  --css-dump:
    Dumps the default CSS definitions to STDOUT
  --debug, -d:
    Show debugging information
  --encoding:
    the character encoding of SRC. If left empty (default) this
    information will be extracted from the HTML header data
  --help, -h:
    Show this help text
  --quiet, -q:
    Show no messages
  --start-viewer, -s:
    Start PDF default viewer on Windows and MacOSX
    (e.g. AcrobatReader)
  --version:
    Show version information
  --warn, -w:
    Show warnings
  --xml, --xhtml, -x:
    Force parsing in XML Mode
    (automatically used if file ends with ".xml")
  --html:
    Force parsing in HTML Mode (default)

[HTTP Connection options]

  --http_nosslcheck:
    No check ssl certificate.

See http.client.HTTPSConnection documentation for this parameters

  --http_key_file
  --http_cert_file
  --http_source_address
  --http_timeout
a6  
Copyright 2010 Dirk Holtwick, holtwick.it

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.z$%(levelname)s [%(name)s] %(message)szB%(levelname)s [%(name)s] %(pathname)s line %(lineno)d: %(message)sc                   C  s   t t d S N)printUSAGE r   r   2/tmp/pip-unpacked-wheel-d1lgvatt/xhtml2pdf/pisa.pyusager   s    r   c                   @  s<   e Zd ZdZddddddZdddd	ZdddZd
S )pisaLinkLoaderz
    Helper to load page from an URL and load corresponding
    files to temporary files. If getFileName is called it
    returns the temporary filename and takes care to delete
    it when pisaLinkLoader is unloaded.
    T)quietNone)returnc                C  s   || _ || _g | _d S r   )r   src	tfileList)selfr   r   r   r   r   __init__~   s    zpisaLinkLoader.__init__c                 C  s   | j D ]}t| qd S r   )r   osremove)r   pathr   r   r   __del__   s    
zpisaLinkLoader.__del__Nc                 C  sR   t |p| j|}t|}| }|j}| j| | jsNt	d| d|  |S )Nz
  Loading  to )
urlparseurljoinr   r   ZgetNamedFilenamer   appendr   r	   )r   r   relativeurlinstanceZfiletmpdownloadedr   r   r   r   getFileName   s    zpisaLinkLoader.getFileName)N)__name__
__module____qualname____doc__r   r   r"   r   r   r   r   r   v   s   r   c                  C  sX   dt jkrNtd dd l} dd l}|  }|t ||	 
d  nt  d S )Nz	--profilez*** PROFILING ENABLEDr   Z
cumulative)sysargvr	   cProfilepstatsZProfileZruncallexecuteZStatsZ
strip_dirsZ
sort_statsZprint_stats)r)   r*   Zprofr   r   r   command   s    

r,   c                   C  s  zRt  tjdd  dddddddd	d
dddddddddddddddddg\} }W n& t jk
rx   t  td Y nX d}d}d}d}d }d}d }d }	d }
d }d }tj}t}| D ]\}}|dkrt  t  q|d krt	t
 td q|d!krt	t td q|d"kr~t	t t	  t	d# t	d$ t	d%tj  t	d&tj  t	d' dd l}t	d(|j  td q|d)krd}q|d*krd}q|d+krt|tj}q|d,krtj}t}|rt|}q|d-kr|}q|d.kr|}q|d/kr|r|}
q|d0kr<t|d1d2}| }W 5 Q R X q|d3krTt	t  d S |d4krftj}q|d5krvd6}	q|d7krd8}	qt||rqq|stj||d9 t|d:krt  td t|dkr|\}}n|d }d }d;|kr t|n|g}|D ]}|	d kr(| d<}	d }|d=ks@|d k	rZtj }t!" }|r|}nl|#d>r|}t$|% }d?&t'(|dd@ )dAd=}n.t!j*+| }}t|dB}| }W 5 Q R X |d kr|}| dCs| dDrdE&|,dEd dF }|dE |  }t-dGD ]\}z*t|dH}|.  W 5 Q R X W  qW n t/k
rh   Y nX |dI|| f  }q$n|}d}|d=ks|rtjdJkrdd l0}|1tj2 t!j3 tj}d}njt!j*+|}z"t|dH}|.  W 5 Q R X W n, t/k
r*   t	dK|  td Y nX t|dH}d}|sVt	dL| dM| dN t4||||tj|||||	|
|dO |r|5  |r|.  |s
|r
|st	dP|  t6| q
d S )QN   Z	dhqstwcxbr   helpzstart-viewerstartzdebug=	copyrightversionwarnztempdir=zformat=zcss=zbase=zcss-dumpzxml-dumpxhtmlxmlhtmlz	encoding=systemprofileZhttp_nosslcheckZhttp_key_fileZhttp_cert_fileZhttp_source_addressZhttp_timeout   r   Zpdf>   --help-h>   	--versionz--copyright>   --systemzSYSTEM INFORMATIONz,--------------------------------------------zOS:                %szPython:            %szhtml5lib:          ?zReportlab:         %s>   --start-viewer--start-s>   -q--quiet>   -w--warn>   -d--debug>   --format-t>   -b--base>   
--encoding>   --css-czutf-8)encoding>   
--css-dump>   
--xml-dump>   --xml-x--xhtmlT>   --htmlFlevelformat>   r-   r8   *z.xml-)zhttp:zhttps:    /rbz.htmlz.htm.
   wbz-%d.%swin32z4File '%s' seems to be in use of another application.zConverting r   z...)
debugr   ZerrouttempdirrV   Zlink_callbackZdefault_cssr3   rM   
xml_outputzOpen viewer for file %s)7getoptr'   r(   GetoptErrorr   exitloggingERROR
LOG_FORMATr	   r   	COPYRIGHTplatformr1   	reportlabVersionminWARNINGDEBUGLOG_FORMAT_DEBUGintopenreadr   stdoutr   Zis_http_configbasicConfiglengloblowerendswithstdinr   getcwd
startswithr   ZgetFileContentjoinr   urlsplitreplacer   abspathsplitrangeclose	ExceptionmsvcrtZsetmodefilenoO_BINARYr   getvaluestartViewer) optsargserrorsZstartviewerr   rb   rc   Zfile_formatcssr3   rM   rd   base_dir	log_level
log_formatoarm   file_handlerZa_srcZa_destr   lcfsrcZwpathZ	dest_partdestifileZ
fdestcloser   Zfdestr   r   r   r+      sj   


















"

r+   c                 C  s:   | r6zt |  W n" tk
r4   t d|   Y nX dS )zHelper for opening a PDF file.z	open "%s"N)r   Z	startfiler   r6   filenamer   r   r   r     s
    r   F)rb   c                 C  sH   z&t j}t}| rt j}t j||d W n tk
rB   t   Y nX dS )zShortcut for enabling log dump.rT   N)rh   rp   rr   rq   rw   r   )rb   r   r   r   r   r   showLogging  s    r   c                 C  sb   dd l }|s>|r2dd l}||d dd }nd}t|||  }d| d d| S )Nr   ;z<You need to provide a mimetype or a filename for makeDataURIzdata:z;base64,rY   )base64	mimetypes
guess_typer   RuntimeErrorencodebytesr   )datamimetyper   r   r   msgZencoded_datar   r   r   makeDataURI  s    r   c              	   C  s*   t | d}| }W 5 Q R X t|| dS )Nr\   r   )rt   ru   r   )r   r   r   r   r   r   makeDataURIFromFile  s    r   __main__)NNN)%
__future__r   re   ry   rh   r   r'   urllib.parseparser   Z	xhtml2pdfr   Zxhtml2pdf.config.httpconfigr   Zxhtml2pdf.defaultr   Zxhtml2pdf.documentr   Zxhtml2pdf.filesr   	getLoggerr#   logZ	CreatePDFstripr
   rk   rj   rr   r   r   r,   r+   r   r   r   r   r   r   r   r   <module>   s:   
= p


