
    Zh{1                        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Zddl	Z	ddl
mZmZmZmZmZmZ ddlZddlmZmZ ej,                  d   dk(  rddlmZ nddlmZ d Z G d d	e      Z G d
 de      Z G d de      Z G d de      Z G d de      Z  G d de      Z! G d de      Z" G d de"      Z# G d de"      Z$d Z%d Z&y)za
A module for fetching common image libraries and installing them into
your iconify installation
    N)IOAnyListMappingOptionalTuple)QtCoreQtXml   )urlretrievec                     g } t        j                  t        j                  t                 D ]G  \  }}t        |t              st        |t              s(|t        t        fvs7| j                  |       I | D ]  }|j                           y)z)
    Run all the available fetchers.
    N)inspect
getmemberssysmodules__name__
isinstancetype
issubclassFetcherEmojiFetcherappendfetch)fetchers_memberfetchers       \/mounts/lovelace/software/anaconda3/envs/py312/lib/python3.12/site-packages/iconify/fetch.pyr   r      sw    
 H''H(=> $	6fd#67+w55OOF#	$      c                   ^    e Zd ZdZdZdZdZedd       Zed        Z	ed        Z
ed        Zy)r   aO  
    The base class for all Fetchers which downloads the identified zip file,
    extracts potentially multiple sub paths from that file and installs them
    into the first available path found on the ICONIFY_PATH variable.

    It's main entry point is `fetch()` and should be fully functional when
    called with no arguments.
    Nc                 Z   t        dj                  | j                               |sTt        j                  j
                  }|st        d      t        j                  j                  |d   | j                        }|s| j                  }| j                  |      }| j                  ||       y)a  
        Downloads and installs a zip file into the iconify path.

        Parameters
        ----------
        urlOrFile : Optional[str]
            The location of the file to unpack.  If not provided, the location
            defined with the URL class var will be used.
        installLocation
            The location to install the content to. When not provided, the
            first directory on the iconify path will be used.
        zFetching {}...z]Please set the ICONIFY_PATH environment variable or provide the 'installLocation' argument...r   N)printformatr   icopath
_ICON_PATHEnvironmentErrorosjoin	NAMESPACEURLdownloadFileinstallZipFile)cls	urlOrFileinstallLocationiconPath	localFiles        r   r   zFetcher.fetch>   s     	%%cll34xx**H&@ 
 !ggll8A;FOI$$Y/	9o6r   c                 j   t         j                  j                  |      r|S t         j                  j                  |      }t         j                  j	                  t        j                         d| j                  |      }t         j                  j                  |      s{t        dj                  |             t         j                  j                  |      }t         j                  j                  |      st        j                  |       t        ||       |S t        dj                  |             |S )aS  
        Downloads the provided file into a temporary location and returns that
        location.

        If a file already exists at the temporary location, it will be used
        rather than downloading the file again.

        Parameters
        ----------
        urlOrFile : str

        Returns
        -------
        str
        iconifyTempDownloadzDownloading file: {}zFound existing download: {})r(   r%   isfilebasenamer)   tempfile
gettempdirr*   r"   r#   dirnameisdirmakedirsr   )r.   r/   urlFiledownloadDestdestDirs        r   r,   zFetcher.downloadFile^   s    $ 77>>)$''""9-ww||!!MM	
 ww~~l+(//	:;ggool3G77==)G$	<0  /66|DEr   c                 j   t         j                  j                  t        j                         d      }t         j                  j                  |      rt        j                  j                  |       t        dj                  |             t        |       t        j                  |      5 }|j                  |       | j                  |       | j                  D ]B  }t         j                  j                  ||      }t        j                  j!                  ||       D 	 ddd       y# 1 sw Y   yxY w)z
        Extract the provided zip file, and install the sub paths identified by
        the ZIP_FILE_PATHS class var into the provided installLocation.

        Parameters
        ----------
        localFile : str
        installLocation : str
        iconifyTempExtractionzExtracting to: {}N)r(   r%   r)   r7   r8   r:   	distutilsdir_utilremove_treer"   r#   zipfileZipFile
extractallupdateDataHookZIP_FILE_PATHS	copy_tree)r.   r2   r0   tmpdirzipDatazipFilePathsources          r   r-   zFetcher.installZipFile   s     h1135LM77== **62!((9:i__Y' 	F7v&/"11 Ffk:"",,V_EF	F 	F 	Fs   +A4D))D2c                      y)z
        An optional hook that an be used to update the downloaded
        data prior to being installed on the host system.
        N )r.   r0   s     r   rG   zFetcher.updateDataHook   s    r   )NN)r   
__module____qualname____doc__r*   r+   rH   classmethodr   r,   r-   rG   rO   r   r   r   r   *   sm     I C N7 7> % %N F F4  r   r   c                       e Zd ZdZdZdZy)FontAwesomezfont-awesomezhhttps://github.com/FortAwesome/Font-Awesome/releases/download/5.12.1/fontawesome-free-5.12.1-desktop.zip)z$fontawesome-free-5.12.1-desktop/svgsNr   rP   rQ   r*   r+   rH   rO   r   r   rU   rU      s    I@C?Nr   rU   c                       e Zd ZdZdZdZy)MaterialDesignzmaterial-designzDhttps://github.com/Templarian/MaterialDesign-SVG/archive/v4.9.95.zip)zMaterialDesign-SVG-4.9.95/svgNrV   rO   r   r   rX   rX      s    !I C8Nr   rX   c                       e Zd ZdZdZdZy)ElusiveelusivezBhttps://github.com/reduxframework/elusive-icons/archive/master.zip)z"elusive-icons-master/dev/icons-svgNrV   rO   r   r   rZ   rZ      s    IC=Nr   rZ   c                       e Zd ZdZdZdZy)Dashdashz9https://github.com/WordPress/dashicons/archive/master.zip)zdashicons-master/sources/svgNrV   rO   r   r   r]   r]      s    I
EC7Nr   r]   c                       e Zd ZdZdZdZy)Featherfeatherz;https://github.com/feathericons/feather/archive/v4.26.0.zip)zfeather-4.26.0/iconsNrV   rO   r   r   r`   r`      s    I
GC/Nr   r`   c                   (    e Zd ZdZdZdZed        Zy)r   z
    A Fetcher subclass specifically for emojis as they require additional
    processing when downloading and installing.
    	BaseEmojiz4https://unicode.org/Public/emoji/13.0/emoji-test.txtc                    ddi}| j                  | j                        }t        |      5 }|j                         }ddd       D ]  }t	        j
                  dt        |            }|s%|j                         \  }}|j                         j                  d      }	|j                         j                  d      }
t        |	      dk(  r|j                         g}
t        |
      D ]U  \  }}|	|   j                         j                         }||v r,t        |      ||j                         j                         <   W  |S # 1 sw Y   xY w)zq
        Create a map of emoji codes to name.

        Returns
        -------
        Mapping[str, str]
        200dandNz^(.*);.*E[0-9\.]+(.*)$ :   )r,   EMOJI_MAP_URL	_openFile	readlinesrematchstrgroupsstripsplitlen	enumeratelower
_cleanName)r.   emojiMapemojiMapUrlOrFileinfileemojiDataLineslinern   codenamecodesnamesis               r   getEmojiMapzEmojiFetcher.getEmojiMap   s;    E?,,S->->?() 	0V#--/N	0 # 	BDHH)D	E JD$JJL&&s+EJJL&&s+E5zQ$U+ B4Qx~~'--/8#1;D1A++-.	B!	B, 3	0 	0s   EEN)r   rP   rQ   rR   r*   rj   rS   r   rO   r   r   r   r      s%    
 IJM& &r   r   c                   H    e Zd ZdZdZdZed        Zed        Zed        Z	y)GoogleEmojiszgoogle-emojiszKhttps://github.com/googlefonts/noto-emoji/archive/v2019-11-19-unicode12.zip)z#noto-emoji-2019-11-19-unicode12/svgc                 j    | j                         }| j                  ||       | j                  |       y)z
        Re-implemented to rename the emoji files and update the files so
        that they're more compatible with Qt's tiny svg spec.

        Parameters
        ----------
        installLocation : str
        N)r   _renameEmojiFiles_removeUnsupportedNodesr.   r0   rw   s      r   rG   zGoogleEmojis.updateDataHook  s.     ??$ox8##O4r   c                    t        j                   t        j                  j                  |d            D ]  }t        j                  j	                  |      }t        j                  j                  |      \  }}g }|j                  dd      j                  d      D ]'  }|j                  |      }|s|j                  |       ) |rt        |D 	cg c]  }	|	dk(  	 c}	      rdj                  |      j                  dd      }|j                  ||      }
t        j                  j                  |
      rt        j                  ||
        yc c}	w )	z
        Replace the unicode codes in the source files with their display names.

        Parameters
        ----------
        installLocation : str
        emojiMap : Mapping[str, str]
        *.svgemoji_u r   rf   -rh   N)globr(   r%   r)   r6   splitextreplacerr   getr   allr5   renamer.   r0   rw   svgr6   extnewPartspartaliasadestinationFiles              r   r   zGoogleEmojis._renameEmojiFiles  s    99RWW\\/7CD 	0Cww'',HGG,,X6MHcH ((B7==cB + T*OOE*+
 s#A1AJ#ABHHX&..sB7E!kk(E:O77>>/2		#/#	0 $Bs   E
c                    t        j                   t        j                  j                  |d            D ]  }t	        j
                  d      }t        j                  |      }|j                  t        j                  j                         |j                  |       |j                          |j                  d      }t        |j                               D ]2  }|j!                  d      }|j#                         j%                  |       4 |j                  d      }t        |j                               D ]2  }|j!                  d      }|j#                         j%                  |       4 t        j&                         }	t        j(                  |	      }
|j+                  |
d       t        j                  |      }|j                  t        j                  j,                         |j/                  |	       |j                           y)z
        Removes unsupported nodes from svg files found under the provided
        installLocation and re-writes them in place.

        Parameters
        ----------
        installLocation : str
        r   initDatadefsr   symbolN)r   r(   r%   r)   r
   QDomDocumentr	   QFileopen	QIODeviceReadOnly
setContentcloseelementsByTagNamerangecountitem
parentNoderemoveChild
QByteArrayQTextStreamsave	WriteOnlywrite)r.   r0   r   domsvgFiledefNodesr   nodesymbolNodes	byteArray
textStreams              r   r   z$GoogleEmojis._removeUnsupportedNodes:  s    99RWW\\/7CD 	C$$Z0Cll3'GLL))223NN7#MMO,,V4H8>>+, 4}}Q'!--d34 //9K;,,./ 4"''*!--d34 ))+I++I6JHHZ#ll3'GLL))334MM)$MMO3	r   N)
r   rP   rQ   r*   r+   rH   rS   rG   r   r   rO   r   r   r   r     sM    I&C>N5 5 0 0: # #r   r   c                   8    e Zd ZdZdZdZed        Zed        Zy)EmojioneLegacyzemojione-legacyz?https://github.com/joypixels/emojione-legacy/archive/master.zip)zemojione-legacy-master/svgc                 H    | j                         }| j                  ||       y )N)r   r   r   s      r   rG   zEmojioneLegacy.updateDataHookg  s      ??$ox8r   c                    t        j                   t        j                  j                  |d            D ]  }t        j                  j	                  |      }t        j                  j                  |      \  }}g }|j                  d      D ]5  }|j                  |j                               }|s%|j                  |       7 |rt        |D 	cg c]  }	|	dk(  	 c}	      rdj                  |      j                  dd      }|j                  ||      }
t        j                  j                  |
      rt        j                  ||
        yc c}	w )z
        Rename emojione files by replacing the emoji code with the nice name.

        Parameters
        ----------
        installLocation : str
        emojiMap : Mapping[str, str]
        r   r   rf   rh   r   N)r   r(   r%   r)   r6   r   rr   r   ru   r   r   r   r5   r   r   s              r   r   z EmojioneLegacy._renameEmojiFilesm  s    99RWW\\/7CD 	0Cww'',HGG,,X6MHcH s+ + TZZ\2OOE*+
 s#A1AJ#ABHHX&..sB7E!kk(E:O77>>/2		#/#	0 $Bs   E
N)	r   rP   rQ   r*   r+   rH   rS   rG   r   rO   r   r   r   r   a  s7    !I
KC5N9 9
 0 0r   r   c                     | j                         } | j                  dd      j                  dd      } d | D        }dj                  |      } | j                         S )z
    Clean the emoji names that have been parsed from the source file.

    Parameters
    ----------
    name : str

    Returns
    -------
    str
    rg   r   rh   r   c              3   P   K   | ]  }d t        |      cxk  rdk  sn n|   yw)r      N)ord).0cs     r   	<genexpr>z_cleanName.<locals>.<genexpr>  s      4a1s1v#3#34s   &
&)rq   r   r)   ru   )r}   strippeds     r   rv   rv     sP     ::<D<<S!))#r2D444H778D::<r   c                 b    t         j                  d   dk(  rt        | dd      S t        | d      S )z
    Return an open file object from both python 2 and 3.

    Parameters
    ----------
    filePath : str

    Returns
    -------
    IO[Any]
    r      rzutf-8)encoding)r   version_infor   )filePaths    r   rk   rk     s3     aHcG44Hc""r   )'rR   distutils.dir_utilrA   r   r   r(   rm   r   r7   rD   typingr   r   r   r   r   r   iconifyr$   
iconify.qtr	   r
   r   urllibr   urllib.requestr   objectr   rU   rX   rZ   r]   r`   r   r   r   rv   rk   rO   r   r   <module>r      s   
    	 	 
   : :  $A!"*"}f }F@' @9W 9>g >87 80g 007 0fX< Xv(0\ (0V(#r   