
    -iE                        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
mZ d dlmZ d dlmZ d dlZddlT ddlT dd	lmZmZmZmZmZ d
ZdZ	 d dlmZ n# e$ r  G d d          Zde_        Y nw xY wedz   Z  G d d          Z! e j"        d          de#dej$        fd            Z%dde#de&dej$        fdZ'dS )    N)literal_eval)choices)ascii_lowercasedigits)Union)quote   )*)_FindPil	_pilcheck_stats_increaseXTIMEOUT)DAvatarz	avatar.py)Imagec                   (    e Zd Z G d d          ZdS )Imc                       e Zd Zd ZdS )Im.Imagec                     d S N )self_s     M/var/www/html/speakWrite/venv/lib/python3.11/site-packages/dicebear/avatar.pyshowzIm.Image.show1   s        N)__name__
__module____qualname__r   r   r   r   r   r   0   s#            r   r   N)r   r   r    r   r   r   r   r   r   /   s<        	 	 	 	 	 	 	 	 	 	r   r   Fz/{}/svg?seed={}&c                      e Zd ZU dZeZeed<   eZe	ed<   	 	 d@ddddde
eef         ded	ed
ededdfdZedefd            Zedefd            Zedefd            Zedefd            ZexZZdedefdZedefd            Zedefd            Zedefd            Zedefd            Zedefd            Zedefd            Zedefd            Zedefd            Zej         fdedefdZ!ej"        fdede#j$        fdZ%d Z&d  Z'd! Z(d" Z)d# Z*d$efd%Z+e,d&eddfd'            Z-dAd(Z.e,defd)            Z/ddddd*deded+ed,edef
d-Z0ddd.d+ed,edefd/Z1e1Z2e3ed0<   dd1ej         d2d2d3d4e
e4j5        ef         d5ed6ed7ed8edefd9Z6e7de8j9        fd:            Z:ej         dd;ded<eddfd=Z;e;Z<e3ed><   e7dAd?            Z=dS )Br   z.
    Base class for the avatar generator.
    default_optionsall_optionsNT)optionscustomsave_to_cachestyleseedr$   r%   r&   returnc                   |t                                           }|t          vrt          dd| d          |1d                    t          t          t          z   d                    }|t          j	        }|i }|| _
        || _        || _        || _        |  || _        |                                  t!          t"          | j        j        d           dS )	a  
        Create a new avatar object.

        :param style: class `dicebear.models.DStyle` :: the style of avatar you want to create; check the whole list at https://github.com/jvherck/dicebear#styles
        :type style: dicebear.models.DStyle
        :param seed: class `str` :: the seed for the avatar; the avatar will be edited according to the seed
        :type seed: str
        :param options: class `DOptions` :: the options for the avatar; check the whole list at https://github.com/jvherck/dicebear#base-options
        :type options: dicebear.models.DOptions
        :param custom: `class: dict` :: customisations for the specified avatar style; see all specific options at https://github.com/jvherck/dicebear#specific-style-options
        :type custom: dict
        :param save_to_cache: `class: bool` :: whether to cache the avatar for quicker future access or manipulation, as well as reducing the amount of API calls
        :type custom: bool
        NzInvalid Style"zE" is not a valid style! Use `DStyle.list` to see all available styles    )kz.__init__())DStylerandomstylesErrorjoinr   r   r   DOptionsempty_DAvatar__style_DAvatar__seed_DAvatar__options_DAvatar__specific_DAvatar__cache_DAvatar__updater   __filename__	__class__r   )r   r'   r(   r$   r%   r&   s         r   __init__zDAvatar.__init__A   s    . =MMOOE`E```   <777?V#;rBBBCCD?nG>F$#* &*dn&=}MMMMMr   c                 P    t          t          | j        j        d           | j        S )z2
        :return: the style of the avatar
        z.style)r   r<   r=   r   r6   r   s    r   r'   zDAvatar.stylen   s#    
 	dn&=xHHH|r   c                 P    t          t          | j        j        d           | j        S )z1
        :return: the seed of the avatar
        z.seed)r   r<   r=   r   r7   r@   s    r   r(   zDAvatar.seedv   s#    
 	dn&=wGGG{r   c                 
   t          t          | j        j        d           i }| j        D ]@}| j        |         t
          |         k    r"|                    || j        |         i           At                              |          S )z4
        :return: the options of the avatar
        z.options)	r   r<   r=   r   r8   r"   updater4   	from_dict)r   _option_listkeys      r   r$   zDAvatar.options~   s    
 	dn&=zJJJ> 	@ 	@C~c"oc&:::##S$.*=$>???!!,///r   c                 P    t          t          | j        j        d           | j        S )z;
        :return: the customisations of the avatar
        z.customisations)r   r<   r=   r   r9   r@   s    r   customisationszDAvatar.customisations   s$    
 	dn&=?PQQQr   fmtc                     t          t          | j        j        d|            | j                            dd| d          S )Nz.url_/svg?/?)r   r<   r=   r   _DAvatar__url_svgreplace)r   rI   s     r   _get_url_for_formatzDAvatar._get_url_for_format   s?    dn&=}s}}MMM~%%gz3zzz:::r   c                 P    t          t          | j        j        d           | j        S )z,
        :return: url to svg avatar
        z.url_svg)r   r<   r=   r   rN   r@   s    r   url_svgzDAvatar.url_svg   s#    
 	dn&=zJJJ~r   c                 ,    |                      d          S )z-
        :return: url to webp avatar
        webprP   r@   s    r   url_webpzDAvatar.url_webp       
 ''///r   c                 ,    |                      d          S )z-
        :return: url to avif avatar
        avifrU   r@   s    r   url_avifzDAvatar.url_avif   rW   r   c                 ,    |                      d          S )z,
        :return: url to png avatar
        pngrU   r@   s    r   url_pngzDAvatar.url_png       
 ''...r   c                 ,    |                      d          S )z,
        :return: url to jpg avatar
        jpgrU   r@   s    r   url_jpgzDAvatar.url_jpg   r^   r   c                 ,    |                      d          S )z-
        :return: url to jpeg avatar
        jpegrU   r@   s    r   url_jpegzDAvatar.url_jpeg   rW   r   c                 ,    |                      d          S )z5
        :return: url to json data of avatar
        jsonrU   r@   s    r   url_jsonzDAvatar.url_json   rW   r   c                 f    t          t          d| j         dz                                             S )zP
        :return: dict object containing the schema of the avatar style
        rL   z/schema.json)_get_requestr   r'   rf   r@   s    r   schemazDAvatar.schema   s1    
 A <DJ < < <<==BBDDDr   formatc                 L   t          t          | j        j        d           	 t	          | j                            dd| d                    }|                     |j                   |j        S # t          j
        $ r%}t          ddt          |          d          d}~ww xY w)	z
        :param format: class `dicebear.models.DFormat` :: which format to use
        :return: returns the avatar's request's full text/file in str format
        z.text()rK   rL   rM     Network Error
statusCodeerrormessageN)r   r<   r=   r   ri   rN   rO   _DAvatar__check_link_errortextrRequestException	HTTPErrorstrr   rk   reqes       r   rt   zDAvatar.text   s    
 	dn&=yIII	^t~55g}6}}}MMNNC##CH---8O! 	^ 	^ 	^3UXYZU[U[\\]]]	^s   AA/ /B#> BB#c                 p   t          t          | j        j        d           	 t	          | j                            dd| d                    }|                     |j                   t          j
        |j                  S # t          j        $ r%}t          ddt          |          d          d}~ww xY w)	z
        :param format: class `dicebear.models.DFormat` :: which format to use
        :return: returns the bytes of the avatar in `io.BytesIO` format
        z.bytes()rK   rL   rM   rm   rn   ro   N)r   r<   r=   r   ri   rN   rO   rs   rt   ioBytesIOcontentru   rv   rw   rx   ry   s       r   byteszDAvatar.bytes   s    
 	dn&=zJJJ	^t~55g}6}}}MMNNC##CH---:ck***! 	^ 	^ 	^3UXYZU[U[\\]]]	^s   AB B5 B00B5c           	      H    d| j          d| j         d| j         d| j         d	S )NzDAvatar(style=DStyle.z, seed="z", *, options=z	, custom=))r'   r(   r$   customizationsr@   s    r   __repr__zDAvatar.__repr__   sX     Btz  B  B49  B  BTXT`  B  Bkok~  B  B  B  	Br   c                     | j         S r   )rN   r@   s    r   __str__zDAvatar.__str__   s
    ~r   c                 b    t          |t                    r| j        |j        k    S | j        |k    S r   
isinstancer   rN   r   others     r   __eq__zDAvatar.__eq__   0    eW%% 	5>U_44~&&r   c                 b    t          |t                    r| j        |j        k    S | j        |k    S r   r   r   s     r   __ne__zDAvatar.__ne__   r   r   c                 8    | j         | j        | j        | j        dS )N)r'   r(   r$   r%   )r'   r(   r$   rH   r@   s    r   __dict__zDAvatar.__dict__   s&    ZI|)	
 
 	
r   rF   c                 n    || j                                         v p|| j                                        v S )z
        Returns `True` if the specified key is found in either DAvatar.options.keys() or DAvatar.customisations.keys(), else returns `False`
        )r$   keysrH   )r   rF   s     r   __contains__zDAvatar.__contains__  s6     dl'')))NSD4G4L4L4N4N-NNr   rt   c                     	 t          |           }n# t          t          f$ r d}Y nw xY wt          |t                    rd|v rt          |          dS dS )z5
        :param text: requests.Response.text
        r,   rp   N)r   
ValueErrorSyntaxErrorr   dictrw   )rt   statuss     r   __check_link_errorzDAvatar.__check_link_error  sy    
	!$''FFK( 	 	 	FFF	fd## 	$(>(>F###	$ 	$(>(>s    ((c                    g g }}dt           dt          fd}| j        D ]K}|t          v r@| j        |         t          |         k    r$|                     ||| j                             L| j        D ]&}|                     ||| j                             't           d                    t          j
        ||                     }|                    t          t          | j                            t          | j                            }	 t          || j                  }|                     |j                   |j        | _        d S # t*          j        $ r%}t/          ddt          |          d          d }~ww xY w)	Npplistc                     t          |            dt          ||                                        dd                              dd           S )N=FalsefalseTruetrue)r   rx   rO   )r   r   s     r   paramzDAvatar.__update.<locals>.param  sH    AhhbbU1X!6!6w!H!H!P!PQWY_!`!`bbbr   &)to_cacherm   rn   ro   )rx   r   r8   r#   r"   appendr9   Yr3   	itertoolschainrk   r   r6   r7   ri   r:   rs   rt   urlrN   ru   rv   rw   )r   _options_specoptionsr   item_linkrz   r{   s           r   __updatezDAvatar.__update  s   !#R,	cS 	c 	c 	c 	c 	c N 	= 	=D{""t~d';t?T'T'TdDN ; ;<<<O 	> 	>DdDO < <====IchhyxFFGGIIU3t|#4#455uT[7I7IJJ	^ut|<<<C##CH--- WDNNN! 	^ 	^ 	^3UXYZU[U[\\]]]	^s   <E F E>>Fc                 
   t           j                            |           \  }}d}t           j                            |           r?|dz  }|dz   t	          |          z   dz   |z   } t           j                            |           ?| S )Nr   r	   (r   )ospathsplitextexistsrx   )r   filename	extensioncounters       r   
__uniquifyzDAvatar.__uniquify,  s     g..t44)gnnT"" 	CqLGc>CLL036BD gnnT"" 	C r   )r'   r(   extra_optionsblank_optionsr   r   c                    t          t          | j        j        d           |r|| _        |r|| _        |r| j                            |           n	|r|| _        |                                  | j	        S )a-  
        Edit an already existing avatar.

        :param style: class `DStyle` :: edit the avatar's style (style of drawing)
        :type style: dicebear.models.DStyle
        :param seed: class `str` :: edit the avatar's seed (string to determine its looks)
        :type seed: str
        :param extra_options: class `DOptions` :: edit the avatar's options (old options stay, these get added) -- cannot be used at the same time with `blank_options` !
        :type extra_options: dicebear.models.DOptions
        :param blank_options: class `DOptions` :: reset old options and set these options as new ones (new options) -- cannot be used at the same with `extra_options` !
        :type blank_options: dicebear.models.DOptions
        :return: class `str` :: returns the link to the avatar url (svg)
        z.edit())
r   r<   r=   r   r6   r7   r8   rC   r;   rN   )r   r'   r(   r   r   s        r   editzDAvatar.edit5  s~    * 	dn&=yIII 	! DL 	DK 	+N!!-0000 	+*DN~r   )r   r   c                    t          t          | j        j        d           |r| j                            |           n	|r|| _        |                                  | j        S )ak  
        Customise the specific options for an already existing avatar.

        :param extra_options: class `DOptions` :: edit the avatar's specific options (old options stay, these get added) -- cannot be used at the same time with `blank_options` !
        :type extra_options: dicebear.models.DOptions
        :param blank_options: class `DOptions` :: reset old specific options and set these options as new ones (new options) -- cannot be used at the same with `extra_options` !
        :type blank_options: dicebear.models.DOptions
        :return: class `str` :: returns the link to the avatar url (svg)
        z.customise())r   r<   r=   r   r9   rC   r;   rN   )r   r   r   s      r   	customisezDAvatar.customiseV  s`     	dn&=~NNN 	,O""=1111 	,+DO~r   	customizedicebear_avatarF)location	file_namefile_format	overwriteopen_after_saver   r   r   r   r   c                   t          t          | j        j        d           |t          j        vrd| d}t          |          |%t          j        t          j
                              }t          j                            || d|           }|du r|                     |          n|}	 |t          j        t          j        fv rgt!          |dd	          5 }|                    |                     |                     ddd           n# 1 swxY w Y   |                                 nvt!          |d
          5 }|                    |                     |                                                     ddd           n# 1 swxY w Y   |                                 nF# t,          $ r t/          |          t0          $ r!}	t3          t5          |	                    d}	~	ww xY w|r|                     d           |S )a  
        Save the avatar to your device.

        :param location: class `pathlib.Path` :: the folder to save the file in. (default is `None` which saves it in the current directory `os.getcwd()`)
        :type location: pathlib.Path
        :param file_name: class `str` :: the name of the file to save. (default is "dicebear_avatar")
        :type file_name: str
        :param file_format: class `DFormat` :: the format of the file. (default is "svg")
        :type file_format: dicebear.models.DFormat
        :param overwrite: class `bool` ::  whether to overwrite an already existing file if it has the same file name and extension
        :type overwrite: bool
        :param open_after_save: class `bool` ::  whether to open the file after saving it to your device
        :type open_after_save: bool
        :return: class `str` :: the path of the saved image if saved successfully
        z.save()r+   " is not a supported format!N.FwzUTF-8)encodingwb)use_pil)r   r<   r=   r   DFormatall_formats
ImageErrorpathlibPathr   getcwdr   r3   _DAvatar__uniquifysvgrf   openwritert   closer   readr   ImageValueErrorOSErrorImageOSErrorrx   view)
r   r   r   r   r   r   s	_locationfr{   s
             r   savezDAvatar.savej  s_   0 	dn&=yIIIg111=K===AQ--|BIKK00HGLLi+G+G++G+GHH	2;u2D2DDOOI...)		'w{GL999)S7;;; 4qGGDIIk223334 4 4 4 4 4 4 4 4 4 4 4 4 4 4				)T** <aGGDJJ{3388::;;;< < < < < < < < < < < < < < <			 	- 	- 	-!),,, 	' 	' 	's1vv&&&	' 	%IIeI$$$s`   ,,F$ )DF$ DF$ D(F$ =;F8F$ FF$ FF$ $"G'G""G'c                     t          t          | j        j        d           t	          j        |                     t          j                            	                                }t	          j
        dd|          }|S )z
        Convert a :py:class:`DAvatar` to a :py:class:`PIL.Image.Image` object.

        :return: :py:class:`PIL.Image.Image`
        :raise `dicebear.errors.PILError`:
        z	.pillow()RGBA)   r   )r   r<   r=   r   r   r   r   r   r\   tobytes	frombytes)r   raw_imgimgs      r   pillowzDAvatar.pillow  s\     	dn&={KKK'$**W[1122::<<VZAA
r   )rk   r   r   c                   t          t          | j        j        d           |t          j        vrt          d| d          |r$t          j        du r| 	                                 d	S |r-t          j        du rt          t          d          d           d	S dt          |                                           }dd	l}|                    t!          | |                     d	S )
a  
        Open and view the avatar on your device.

        :param format: class `DFormat` :: what format to use when opening the DAvatar image
        :type format: dicebear.models.DFormat
        :param use_pil: class `bool` :: whether to use Pillow module to open the avatar image
        :type use_pil: bool
        :return: :py:class:`NoneType`
        :raise `dicebear.errors.PILError`:
        z.view()r+   r   TFz)Module `Pillow` is not found or installedurl_r   N)r   r<   r=   r   r   r   r   r   found_DAvatar__view_pil	log_errorModuleNotFoundErrorrx   lower
webbrowserr   getattr)r   rk   r   	attr_namer   s        r   r   zDAvatar.view  s     	dn&=yIII,,,EEEEFFF 	6x~--OO 	6500)*UVVX\]]]]]4s6{{002244IOOGD)4455555r   r   c                 T    |                                                      d           d S )NzDicebear Avatar)r   r   r@   s    r   
__view_pilzDAvatar.__view_pil  s%    ,-----r   )NN)r)   N)>r   r   r    __doc__r"   r   __annotations__r$   r#   listr   rx   r/   r4   boolr>   propertyr'   r(   rH   r   customsrP   rR   rV   rZ   r]   ra   rd   rg   rj   r   r   rt   r\   r}   r~   r   r   r   r   r   r   r   staticmethodrs   r;   r   r   r   r   callabler   r   r   r   r   r   r   r   r   r   r   r   r   r   r   9   s          ,OT+++K %)+N
 !"+N +N +NS&[!+N +N
 +N +N +N 
+N +N +N +NZ v    X c    X 	0 	0 	0 	0 X	0     X  .-NW;s ;s ; ; ; ;     X 0# 0 0 0 X0 0# 0 0 0 X0 / / / / X/ / / / / X/ 0# 0 0 0 X0 0# 0 0 0 X0 E E E E XE &-[ ^ ^7 ^S ^ ^ ^ ^ '.k ^ ^G ^bj ^ ^ ^ ^B B B  ' ' '
' ' '

 
 
O O O O O 	$ 	$ 	$ 	$ 	$ \	$^ ^ ^ ^* C    \ "&"&    	
     
   B 26T   $ d VY    $ $Ix###
 .2*&{ %/ / / c)*/ 	/
 / / / 
/ / / /b 
 
 
 
 Y
 )0T 6 6 6g 6d 6d 6 6 6 64 D(. . . Y. . .r   r      )maxsizer   r)   c                 4    t          j        | fdt          i|S )Ntimeout)ru   getr   )r   kwargss     r   _get_request_cachedr    s    500g0000r   Tr   c                 D    |rt          | fi |S t          j        | fi |S r   )r  ru   r  )r   r   r  s      r   ri   ri     s8     2"311&1115r   )T)(	functoolsr}   r   r   r   astr   r0   r   stringr   r   typingr   urllib.parser   requestsru   errorsmodelsr   r   r   r   r   __all__r<   PILr   r   	Exceptionr   r   r   	lru_cacherx   Responser  r   ri   r   r   r   <module>r     s  .     				     				              * * * * * * * *                         D D D D D D D D D D D D D D
	          
 HNNN M. M. M. M. M. M. M. M.` R   1S 1qz 1 1 1 ! 1   c  T  qz            s   A A76A7