a
    dA'                     @   s  U d dl Z d dlZd dlmZ d dlmZ d dlZd dlZd dlZe	dZ
ejed< e
e  e
ej e Zd/ddZdd	 ZG d
d dZG dd deZzd dlZW n ey   dZY n0 d dlZd dlZedZdd ZdZed Zdd Z dd Z!dd Z"dd Z#G dd deZ$dd Z%zd dl&Z&W n eyT   dZ&Y n0 G dd  d Z'd!d" Z(G d#d$ d$e)Z*d%d& Z+d'd( Z,d)d* Z-d+d, Z.d-d. Z/dS )0    N)reduce)dequeZlarkloggerc                 C   s\   i }| D ]N}|d ur||n|}|d ur0||n|}||v rL||  | q|g||< q|S Nappend)seqkeyvalueditemkv r   S/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/lark/utils.pyclassify   s    r   c                    sx   t | trVd| v r*| d  }||  S d| v r> | d  S  fdd|  D S t | trt fdd| D S | S )N__type__@c                    s   i | ]\}}|t | qS r   _deserialize).0r	   r
   memo	namespacer   r   
<dictcomp>&       z _deserialize.<locals>.<dictcomp>c                    s   g | ]}t | qS r   r   )r   r
   r   r   r   
<listcomp>(   r   z _deserialize.<locals>.<listcomp>)
isinstancedictdeserializeitemslist)datar   r   class_r   r   r   r      s    

r   c                   @   s.   e Zd ZdZdd Zd	ddZedd ZdS )
	Serializeal  Safe-ish serialization interface that doesn't rely on Pickle

    Attributes:
        __serialize_fields__ (List[str]): Fields (aka attributes) to serialize.
        __serialize_namespace__ (list): List of classes that deserialization is allowed to instantiate.
                                        Should include all field types that aren't builtin types.
    c                 C   s   t |}| || fS r   )SerializeMemoizer	serialize)selftypes_to_memoizer   r   r   r   memo_serialize5   s    zSerialize.memo_serializeNc                    sd    r  rd jiS td} fdd|D }tj|d< tdr`|  |S )Nr   __serialize_fields__c                    s   i | ]}|t t| qS r   )
_serializegetattr)r   fr   r'   r   r   r   >   r   z'Serialize.serialize.<locals>.<dictcomp>r   r+   )in_typesmemoizedgetr,   type__name__hasattrr+   )r'   r   fieldsresr   r.   r   r&   9   s    

zSerialize.serializec                 C   s   t | dg }dd |D }t | d}d|v r8||d  S | | }|D ]R}zt||t|| || W qF ty } ztd| |W Y d }~qFd }~0 0 qFt|dr|  |S )NZ__serialize_namespace__c                 S   s   i | ]}|j |qS r   )r3   )r   cr   r   r   r   G   r   z)Serialize.deserialize.<locals>.<dictcomp>r*   r   zCannot find key for classr   )r,   __new__setattrr   KeyErrorr4   )clsr"   r   r   r5   instr-   er   r   r   r   D   s    

$
zSerialize.deserialize)N)r3   
__module____qualname____doc__r)   r&   classmethodr   r   r   r   r   r$   ,   s
   
r$   c                   @   s8   e Zd ZdZdZdd Zdd Zdd Zed	d
 Z	dS )r%   z<A version of serialize that memoizes objects to reduce space)r0   c                 C   s   t || _t | _d S r   )tupler(   
Enumeratorr0   )r'   r(   r   r   r   __init__`   s    
zSerializeMemoizer.__init__c                 C   s   t || jS r   )r   r(   )r'   r
   r   r   r   r/   d   s    zSerializeMemoizer.in_typesc                 C   s   t | j d S r   )r+   r0   reversedr'   r   r   r   r&   g   s    zSerializeMemoizer.serializec                 C   s   t |||S r   r   )r;   r"   r   r   r   r   r   r   j   s    zSerializeMemoizer.deserializeN)
r3   r>   r?   r@   r*   rD   r/   r&   rA   r   r   r   r   r   r%   [   s   r%   z\\p{[A-Za-z_]+}c                 C   s   t rttd| }ntt| r*td| | }zdd t| D W S  t	j
y   t sft| n<t |}|dd u rdtt	jf Y S dtt	jf Y S Y n0 d S )NAzD`regex` module must be installed in order to use Unicode categories.c                 S   s   g | ]}t |qS r   )intr   xr   r   r   r      r   z$get_regexp_width.<locals>.<listcomp>    r   )regexresubcateg_patternsearchImportError	sre_parseparsegetwidthsre_constantserror
ValueErrorcompilematchrH   	MAXREPEAT)exprZregexp_finalr7   r   r   r   get_regexp_widthx   s    


r]   )ZLuZLlLtZLmZLoZMnZMcZPc)ZNdZNlc                    s8   t | dkr"t fdd| D S | dkp6t|  v S )NrL   c                 3   s   | ]}t | V  qd S r   )_test_unicode_category)r   char
categoriesr   r   	<genexpr>   r   z)_test_unicode_category.<locals>.<genexpr>_)lenallunicodedatacategory)srb   r   ra   r   r_      s    r_   c                 C   s
   t | tS )z
    Checks if all characters in `s` are alphanumeric characters (Unicode standard, so diacritics, indian vowels, non-latin
    numbers, etc. all pass). Synonymous with a Python `ID_CONTINUE` identifier. See PEP 3131 for details.
    )r_   _ID_CONTINUEri   r   r   r   is_id_continue   s    rl   c                 C   s
   t | tS )z
    Checks if all characters in `s` are alphabetic characters (Unicode standard, so diacritics, indian vowels, non-latin
    numbers, etc. all pass). Synonymous with a Python `ID_START` identifier. See PEP 3131 for details.
    )r_   	_ID_STARTrk   r   r   r   is_id_start   s    rn   c                    s   t    fdd| D S )zGiven a list (l) will removing duplicates from the list,
       preserving the original order of the list. Assumes that
       the list entries are hashable.c                    s"   g | ]}| v s  |s|qS r   )addrI   Zdedupr   r   r      r   zdedup_list.<locals>.<listcomp>)set)lr   rp   r   
dedup_list   s    rs   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
rC   c                 C   s
   i | _ d S r   )enumsrF   r   r   r   rD      s    zEnumerator.__init__c                 C   s$   || j vrt| j | j |< | j | S r   )rt   re   )r'   r   r   r   r   r1      s    
zEnumerator.getc                 C   s
   t | jS r   )re   rt   rF   r   r   r   __len__   s    zEnumerator.__len__c                 C   s.   dd | j  D }t|t| j ks*J |S )Nc                 S   s   i | ]\}}||qS r   r   )r   r   r   r   r   r   r      r   z'Enumerator.reversed.<locals>.<dictcomp>)rt   r    re   )r'   rr   r   r   rE      s    zEnumerator.reversedN)r3   r>   r?   rD   r1   ru   rE   r   r   r   r   rC      s   rC   c                 C   sN   | s
g gS t dd | D s$J | dd | d D }tdd | dd	 |S )
a  
    Accepts a list of alternatives, and enumerates all their possible concatinations.

    Examples:
        >>> combine_alternatives([range(2), [4,5]])
        [[0, 4], [0, 5], [1, 4], [1, 5]]

        >>> combine_alternatives(["abc", "xy", '$'])
        [['a', 'x', '$'], ['a', 'y', '$'], ['b', 'x', '$'], ['b', 'y', '$'], ['c', 'x', '$'], ['c', 'y', '$']]

        >>> combine_alternatives([])
        [[]]
    c                 s   s   | ]
}|V  qd S r   r   )r   rr   r   r   r   rc      r   z'combine_alternatives.<locals>.<genexpr>c                 S   s   g | ]
}|gqS r   r   rI   r   r   r   r      r   z(combine_alternatives.<locals>.<listcomp>r   c                    s    fdd| D S )Nc                    s    g | ]} D ]}||g qqS r   r   )r   ijbr   r   r      r   z:combine_alternatives.<locals>.<lambda>.<locals>.<listcomp>r   )arz   r   ry   r   <lambda>   r   z&combine_alternatives.<locals>.<lambda>rL   N)rf   r   )listsinitr   r   r   combine_alternatives   s
    r   c                   @   s&   e Zd ZeejjZedddZdS )FSrv   c                 K   s:   t r$d|v r$t j| f|dd|S t| |fi |S d S )NwT)mode	overwrite)atomicwritesZatomic_writeopen)namer   kwargsr   r   r   r      s    zFS.openN)rv   )r3   r>   r?   staticmethodospathexistsr   r   r   r   r   r      s   r   c                 C   sP   z
|   W S  tyJ   z| d W Y dS  ttfyD   Y Y dS 0 Y n0 dS )z' str.isascii only exists in python3.7+ asciiTFN)isasciiAttributeErrorencodeUnicodeDecodeErrorUnicodeEncodeErrorrk   r   r   r   r      s    

r   c                   @   s   e Zd Zdd ZdS )fzsetc                 C   s   dd tt|  S )Nz{%s}z, )joinmapreprrF   r   r   r   __repr__   s    zfzset.__repr__N)r3   r>   r?   r   r   r   r   r   r      s   r   c                 C   s8   g }g }| D ]"}||r$| | q| | q||fS r   r   )r   predZ
true_elemsZfalse_elemselemr   r   r   classify_bool   s    r   c                 c   sV   t t| }t|}|rR| }|V  ||D ] }||vr.|| || q.qd S r   )r   r!   rq   popleftro   r   )initialexpandopen_qvisitednodeZ	next_noder   r   r   bfs
  s    
r   c                 c   s0   t t| }|r,| }|V  |||7 }qdS )zVbfs, but doesn't keep track of visited (aka seen), because there can be no repetitionsN)r   r!   r   )r   r   r   r   r   r   r   bfs_all_unique  s
    r   c                    sf   t | tr|  S t | tr0 fdd| D S t | trBt| S t | trb fdd|  D S | S )Nc                    s   g | ]}t | qS r   r+   )r   r   r   r   r   r   "  r   z_serialize.<locals>.<listcomp>c                    s   i | ]\}}|t | qS r   r   )r   r	   r   r   r   r   r   &  r   z_serialize.<locals>.<dictcomp>)r   r$   r&   r!   	frozensetr   r    )r
   r   r   r   r   r+     s    




r+   c                 C   s   | dksJ |dksJ | |kr*| dfgS t |ddD ]6}t| |\}}|| |kr6t||||fg   S q6ds~J d|  dS )a  
    Splits n up into smaller factors and summands <= max_factor.
    Returns a list of [(a, b), ...]
    so that the following code returns n:

    n = 1
    for a, b in values:
        n = n * a + b

    Currently, we also keep a + b <= max_factor, but that might change
    r      rL   FzFailed to factorize %sN)rangedivmodsmall_factors)nZ
max_factorr{   rv   rz   r   r   r   r   -  s    
r   )NN)0rg   r   	functoolsr   collectionsr   sysrN   logging	getLoggerr   Logger__annotations__
addHandlerStreamHandlersetLevelCRITICALobjectZNO_VALUEr   r   r$   r%   rM   rR   rS   rV   rY   rP   r]   rm   rj   r_   rl   rn   rs   rC   r   r   r   r   r   r   r   r   r   r+   r   r   r   r   r   <module>   sR   

/


	