
    EPcj!                     "   d Z ddl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m	Z	 ddl
mZ ddlmZ ddlmZmZmZ ddlmZ ej                            ej                            e          d          Zd	d
gdddddgdddddgddddddgddddddgddddg dddddg ddddd dd!gd"d#dd$dd%gd&d'dd(d)dgd*d+dd,d)dgd-d+dd.d)dgd/d+dd0d)gd1d2dd3d)gd4d5dd6d)d7gd8d9dd:Z G d; d<          Z G d= d>          Z G d? d@          ZdS )Az7
Base module for all import web search source modules.
    N)CacheControlCacheControlAdapter)	FileCache)ExpiresAfter)CommandNotFoundProgramRunErrorcall)defaultdictlicensesCC0nocopyrightz2https://creativecommons.org/publicdomain/zero/1.0/zcc0.svg)namemodulesurloverlayzCC-BY 3.0 Unportedbyz,https://creativecommons.org/licenses/by/3.0/z	cc-by.svgzCC-BY 4.0 Unportedz,https://creativecommons.org/licenses/by/4.0/zCC-BY SA 4.0saz/https://creativecommons.org/licenses/by-sa/4.0/zcc-by-sa.svgzCC-BY SA 3.0z/https://creativecommons.org/licenses/by-sa/3.0/zCC-BY NC SA 4.0)r   r   ncz2https://creativecommons.org/licenses/by-nc-sa/4.0/zcc-by-nc-sa.svgzCC-BY NC SA 3.0z2https://creativecommons.org/licenses/by-nc-sa/3.0/zCC-BY NC 3.0r   z/https://creativecommons.org/licenses/by-nc/3.0/zcc-by-nc.svgzCC-BY ND 3.0ndz/https://creativecommons.org/licenses/by-nd/3.0/zcc-by-nd.svgGPLv2retaincopyrightnoticez5https://www.gnu.org/licenses/old-licenses/gpl-2.0.txtzgpl.svgGPLv3z(https://www.gnu.org/licenses/gpl-3.0.txtAGPLv3z)https://www.gnu.org/licenses/agpl-3.0.txtMITzhttps://mit-license.org/zmit.svgzApache Licensez/https://www.apache.org/licenses/LICENSE-2.0.txtzasl.svgBSDnoendorsementz,https://opensource.org/licenses/BSD-3-Clausezbsd.svg)zcc-0z	cc-by-3.0z	cc-by-4.0zcc-by-sa-4.0zcc-by-sa-3.0zcc-by-nc-sa-4.0zcc-by-nc-sa-3.0zcc-by-nc-3.0zcc-by-nd-3.0zgpl-2zgpl-3zagpl-3mitaslbsdc                   :    e Zd ZdZdZ ed           Zd Zd ZdS )
RemotePagezLazy access to paging systemszsources/next_page.svgc                     dS )Nz	Next Page selfs    >/usr/share/inkscape/extensions/other/clipart/import_sources.py<lambda>zRemotePage.<lambda>   s    ;     c                 "    || _         || _        d S N)funcremote)r%   r,   r+   s      r&   __init__zRemotePage.__init__   s    	r(   c              #   p   K   |                                  D ]}| j                            |          V  d S r*   )r+   r,   result_to_clsr%   infos     r&   get_next_pagezRemotePage.get_next_page   sF      IIKK 	2 	2D+++D111111	2 	2r(   N)	__name__
__module____qualname____doc__iconpropertystringr-   r2   r#   r(   r&   r!   r!      sP        ''"DX..//F  2 2 2 2 2r(   r!   c                       e Zd ZdZ ed           Zd Zd Zed             Zed             Z	d Z
ed             Zed	             Zd
S )
RemoteFilezLazy access to remote filesc                 L    | j                             | j        d                   S )N	thumbnailr,   to_local_filer1   r$   s    r&   r'   zRemoteFile.<lambda>   s    !:!:49[;Q!R!R r(   c                 L    | j                             | j        d                   S )Nfiler>   r$   s    r&   r'   zRemoteFile.<lambda>   s    DK55di6GHH r(   c                 Z    dD ]}||vrt          d| d          || _        || _        d S )N)r   r=   licenserA   zField z# not provided in RemoteFile package)
ValueErrorr1   r,   )r%   r,   r1   fields       r&   r-   zRemoteFile.__init__   sO    = 	V 	VED   !T%!T!T!TUUU !	r(   c                     | j         d         S )Nr   r1   r$   s    r&   r9   zRemoteFile.string   s    y  r(   c                     | j         d         S )NrC   rG   r$   s    r&   rC   zRemoteFile.license   s    y##r(   c                     | j         d         S )Nr   )license_infor$   s    r&   get_overlayzRemoteFile.get_overlay   s     ++r(   c                 ~    t                               | j        d| j                            dd          g dd          S )NUnknowndescriptionurl zunknown.svg)r   r   r   r   )LICENSESgetrC   r1   r$   s    r&   rJ   zRemoteFile.license_info   sC    ||DL)-- 0"55#	+
 +
   	r(   c                     | j         d         S )NauthorrG   r$   s    r&   rS   zRemoteFile.author   s    y""r(   N)r3   r4   r5   r6   r8   r7   get_filer-   r9   rC   rK   rJ   rS   r#   r(   r&   r;   r;      s        %%8RRSSDHHH   ! ! X! $ $ X$, , ,   X # # X# # #r(   r;   c                   r    e Zd ZdZdZdZeZeZ	dZ
dZed             Zg fdZi Zd Zd Zd	 Zd
 Zd Zd ZdS )RemoteSourcezBA remote source of svg images which can be searched and downloadedNFTc                    t           j                            |          rt           j                            |          gt          j        z   t          j        ct          _        }	 t          j        t           j                            |                              dd          d                    n'# t          $ r t          j        d|            Y nw xY w|t          _        dS t           j                            |          rtt          j        |          D ]a}|                    d          sH|                    d          r3|                     t           j                            ||                     `dS dS )z,Load the file or directory of remote sources.   r   zFailed to load module: _z.pyN)ospathisfiledirnamesys	importlibimport_modulebasenamersplitImportErrorloggingerrorisdirlistdir
startswithendswithloadjoin)clsr   sys_pathchilds       r&   rk   zRemoteSource.load   sY    7>>$ 
	8"$'//$"7"7!838!CSXCHh@'(8(8(>(>(E(Ec1(M(Ma(PQQQQ @ @ @>>>?????@CHHHW]]4   	8D)) 8 8'',, 81F1F 8HHRW\\$66777	8 	88 8s   %AB1 1!CCc                      t          d          )z
        Search for the given query and yield basic informational blocks t hand to file_cls.

        Required fields per yielded object are: name, license, thumbnail and file.
        Optional fields are: id, summary, author, created, popularity
        z<You must implement a search function for this remote source!)NotImplementedError)r%   querytagss      r&   searchzRemoteSource.search   s     "J
 
 	
r(   c                 >    | t           k    r| | j        | j        <   d S d S r*   )rV   sourcesr3   )rm   s    r&   __init_subclass__zRemoteSource.__init_subclass__   s)    ,(+CK%%% r(   c           	          t          j                    | _        || _        | j                            dt	          t          |          t          d                               d S )Nzhttps://   )days)cache	heuristic)requestssession	cache_dirmountr   r   r   )r%   r   s     r&   r-   zRemoteSource.__init__   sk    '))"	**&A...  	
 	
 	
 	
 	
r(   c                 8    | j                                          d S r*   )r~   closer$   s    r&   __del__zRemoteSource.__del__   s    r(   c              #   h   K   |                      |          D ]}|                     |          V  dS )zSearch for extension packagesN)rt   r/   )r%   rr   r1   s      r&   file_searchzRemoteSource.file_search   sH      KK&& 	+ 	+D$$T******	+ 	+r(   c                 x    t          |          r|                     | |          S |                     | |          S r*   )callablepage_clsfile_clsr0   s     r&   r/   zRemoteSource.result_to_cls   s8    D>> 	-==t,,,}}T4(((r(   c                    t           j                            | j        |                    d          d                   }ddi}	 | j                            ||          }nJ# t          j        j	        $ r}Y d}~dS d}~wt          $ r}Y d}~dS d}~wt          j        j        $ r Y nw xY w|r\|j        dk    rQt          |d          5 }|                    |j                  r|cddd           S 	 ddd           n# 1 swxY w Y   dS )	z.Get a remote url and turn it into a local file/z
User-AgentInkscape)headersN   wb)r[   r\   rl   r   splitr~   rQ   r}   
exceptionsRequestExceptionConnectionErrorRequestsWarningstatus_codeopenwritecontent)r%   r   filepathr   r,   errfhls          r&   r?   zRemoteSource.to_local_file   s   7<<		#r0BCC,		\%%W &  FF "3 	 	 	44444 	 	 	44444"2 	 	 	D	  	$f(C//h%% $99V^,, $#$ $ $ $ $ $ $ $$$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ ts0   A! !B(;B(B('B(C>>DD)r3   r4   r5   r6   r   r7   r;   r   r!   r   
is_default
is_enabledclassmethodrk   rt   rv   rw   r-   r   r   r/   r?   r#   r(   r&   rV   rV      s        LL DDHHJJ8 8 [8 "$ 	
 	
 	
 	
 G, , ,	
 	
 	
  + + +
) ) )
    r(   rV   )r6   rer[   r_   re   r}   r`   cachecontrolr   r   cachecontrol.caches.file_cacher   cachecontrol.heuristicsr   inkex.commandr   r   r	   collectionsr
   r\   rl   r^   __file__LICENSE_ICONSrP   r!   r;   rV   r#   r(   r&   <module>r      s  $  
			 				 



       : : : : : : : : 4 4 4 4 4 4 0 0 0 0 0 0 @ @ @ @ @ @ @ @ @ @ # # # # # #RW__X66
CC !?C	  %6=	  %6=	  $<@!	  $<@!	  "%%%C$	  "%%%C$	  $<@!	  $<@!	  +T2F	  +T29	  +T2:	  +,)	  !+,@	  +_==	 k[ [|2 2 2 2 2 2 2 2## ## ## ## ## ## ## ##LW W W W W W W W W Wr(   