
    BPc                         d 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
 ddlZddlmZmZmZmZmZ ddlmZ  G d d	ej                  Zed
k    r e                                             dS dS )z
An extension which collects all images to the documents directory and
creates a zip archive containing all images and the document
    N)ListTuple)urlparse)url2pathname)TextElementTspanFlowRootFlowParaFlowSpan)inkex_gettextc                        e Zd ZdZ fdZd Zdedeeef         fdZ	de
j        fdZde
j        fd	Zd
 Zd Zde
j        fdZd Z xZS )CompressedMediazOutput a compressed filec                 V    t                                                       i | _        d S )N)super__init__	path_dict)self	__class__s    +/usr/share/inkscape/extensions/media_zip.pyr   zCompressedMedia.__init__:   s$        c                 z    |                     ddd           |                     dt          j        d           d S )Nz--image_dirzImage directoryimages)helpdefaultz--font_listzAdd font list)typer   )add_argumentinkexBoolean)r   parss     r   add_argumentszCompressedMedia.add_arguments>   sA    -.?RRR-em/RRRRRr   pathreturnc                    || j         v r't          j                            |          d         dfS d}t          j                            |          \  }}t          j                            |          \  }}||z   }	 |t          | j                                                   v r|dz  }| | | }n|| j         |<   |dfS E)zIProcesses an absolute path and returns
        (unique filename, exists).   Tr   F)r   osr!   splitsplitextlistvalues)r   r!   indexpthext_filenametrypaths           r   process_pathzCompressedMedia.process_pathB   s     4>!!7==&&q)4//7##D))SgmmC((8S.	&$t~44667777
%3u3c33'.t$~%	&r   zc                    | j         j        }| j                            d          D ]S}|                    d          }|dd         dk    r,t          |          }t          |j                  }|                     |pd          }t          j        
                    |          s|                    d|          }t          j        
                    |          s5t          j        t          d                              |                     |                     |          \  }	}
|
s4|                    |t          j                            ||	                     |                    d| d	|	            UdS )
zg
        Collects all images in the document
        and copy them to the temporary directory.
        z//svg:imagez
xlink:hrefN   data zsodipodi:absrefz+File not found "{}". Unable to embed image./)options	image_dirsvgxpathgetr   r   r!   absolute_hrefr%   isfiler   errormsgr-   formatr0   writejoinset)r   docnamer1   imgdirnodexlinkurlhref
image_pathzippathexistss              r   collect_imageszCompressedMedia.collect_imagesU   sq   
 'HNN=11 	B 	BDHH\**ERaRyF""uoo#CH--!//
;;
 w~~j11 I!%*;Z!H!HJw~~j11 BNGHHOO&   
 &*&7&7
&C&COGV! K
BGLL,I,IJJJHH\f+@+@w+@+@AAA1	B 	Br   c                    t           j                            | j        |          }t	          |d          5 }| j                            |           ddd           n# 1 swxY w Y   |                    ||dz              dS )zr
        Copy SVG document to the temporary directory
        and add it to the temporary compressed file
        wbN.svg)r%   r!   rA   tmp_diropendocumentr@   )r   docstrippedr1   dst_filestreams        r   collect_svgzCompressedMedia.collect_svgv   s    
 7<<k::(D!! 	(VM'''	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	(	+./////s   AA!$A!c                 ^    t          |t          t          t          t          t
          f          S )z_
        Returns true if the tag in question is an element that
        can hold text.
        )
isinstancer   r   r	   r
   r   )r   rE   s     r   is_textzCompressedMedia.is_text   s    
 $eXx RSSSr   c                 l   g }d}d|j         v r7t          t          j                            |j         d                             }|s|S d|v rHd|v r(|                    |d         dz   |d         z              n;|                    |d                    nd|v r|                    |d                    |S )zg
        Given a node, returns a list containing all the fonts that
        the node is using.
        r5   stylezfont-familyzfont-weight z-inkscape-font-specification)attribdictr   Style	parse_strappend)r   rE   fontsss       r   	get_fontszCompressedMedia.get_fonts   s    
 dk!!U[**4;w+?@@AAA 	LA!!Q}-3a6FFGGGGQ}-....+q00LL9:;;;r   c                 >   g }| j                                         }|                    t          | j        |                     g }|D ]3}|                     |          D ]}||vr|                    |           4t          |          }d}t          j	        
                    | j        |          }	t          |	d          5 }
t          |          dk    r#|
                    t          d                     nwt          |          dk    r,|
                    t          d          |d         z             n8|
                    t          d          d
                    |          z             d	d	d	           n# 1 swxY w Y   |                    |	|           d	S )
z
        Walks through nodes, building a list of all fonts found, then
        reports to the user with that list.
        Based on Craig Marshall's replace_font.py
        zfontlist.txtwr   z1Didn't find any fonts in this document/selection.r$   z!Found the following font only: %szFound the following fonts:
%s
N)r9   iterdescendantsextendfilterrY   rd   ra   sortedr%   r!   rA   rP   rQ   lenr@   r-   )r   r1   nodesitemsfonts_foundrE   ffindingsr.   rT   rU   s              r   
list_fontszCompressedMedia.list_fonts   s    *,((**VDL%00111 	* 	*D^^D)) * *K''&&q)))* +&&!7<<h77(C   		F8}}!!QRSSTTTTx==A%%LL#F!G!G(ST+!UVVVVLL:;;dii>Q>QQ  		 		 		 		 		 		 		 		 		 		 		 		 		 		 		 	
(#####s   B.E<<F F c                 :   | j                             d          }|| j        j        }t          j                            |                    dd                    }|                    dd          }|                    dd          }t          j	                    | _
        t          j        |d          5 }|                     ||           |                     ||           | j        j        r|                     |           d d d            d S # 1 swxY w Y   d S )Nzsodipodi:docnamez.zipr5   rO   z.svgzrf   )r9   r;   r7   
input_filer%   r!   basenamereplacetempfilemkdtemprP   zipfileZipFilerL   rV   	font_listrr   )r   rU   rC   rS   r1   s        r   savezCompressedMedia.save   sF   (,,122?l-G g&&wvr'B'BCC!))&"55!))'266  ')) _VS)) 	#Q+++[!,,,|% #"""		# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	#s   5ADDD)__name__
__module____qualname____doc__r   r    strr   boolr0   ry   rz   rL   rV   rY   rd   rr   r|   __classcell__)r   s   @r   r   r   7   s       ""    S S S& &sDy)9 & & & &&B B B B BB0'/ 0 0 0 0T T T  *$GO $ $ $ $<# # # # # # #r   r   __main__)r   r%   rw   ry   typingr   r   urllib.parser   urllib.requestr   r   r   r   r	   r
   r   inkex.localizationr   r-   OutputExtensionr   r}   run r   r   <module>r      s  J 
 
			           ! ! ! ! ! ! ' ' ' ' ' '  B B B B B B B B B B B B B B 1 1 1 1 1 1V# V# V# V# V#e+ V# V# V#r zO r   