o
    ir                     @   sX  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m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mZmZ dd	lmZmZ dd
lmZmZmZ ddlmZmZ m!Z! ddl"m"Z"m#Z# ddl$m%Z%m&Z& ddl'm(Z(m)Z) edddZ*e+ej,Z-ej.dk re	Z/ndd Z/e0dZ1e0dZ2G dd de3Z4G dd dZ5de6fddZ7d\ddZ8eG d d! d!Z9ed"d# Z:d$d% Z;eG d&d' d'Z<eG d(d) d)Z=eG d*d+ d+e=Z>eG d,d- d-e>Z?eG d.d/ d/e=Z@eG d0d1 d1e@ZAeG d2d3 d3e@ZBeG d4d5 d5e@ZCeG d6d7 d7ZDd8d9 ZEd:d; ZFd<d= ZGd>d? ZHd@dA ZIdBdC ZJdDdE ZKe>fdFdGZLe"dHeMfdIdJZNe"dKeejOejPf fdLdJZNe"dKejQfdMdJZNe"dKejRfdNdJZNe"dKejSfdOdJZNG dPdQ dQZTeG dRdS dSZUeG dTdU dUeUZVeG dVdW dWeUZWeG dXdY dYeUZXeG dZd[ d[eUZYdS )]z6
Applied https://github.com/breuleux/jurigged/pull/58
    N)abstractmethod)_splitlines_no_ff)Counter)contextmanagersuppress)
ContextVar)	dataclassfieldreplace)CodeType
ModuleType)ListOptionalUnion)ConformExceptioncode_registryconform)ovldrecurse   )	Variables	variables)EventSourceshift_linenocurrent_infodefault)      c                 C   s0   t | }t|dkr|d dkr|d d S |S )Nr    )_splitlineslen)slines r%   U/home/ubuntu/.local/lib/python3.10/site-packages/spaces/_vendor/jurigged/codetools.py
splitlines"   s   r'   z^ *[\n;]z[\n;] *$c                   @   s   e Zd ZdS )StaleExceptionN)__name__
__module____qualname__r%   r%   r%   r&   r(   -   s    r(   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
	attrproxyc                 C   s
   || _ d S Ncls)selfr/   r%   r%   r&   __init__2   s   
zattrproxy.__init__c                 C   s&   zt | j|W S  ty   t|w r-   )getattrr/   AttributeErrorKeyError)r0   itemr%   r%   r&   __getitem__5   s
   zattrproxy.__getitem__c                 C      t | j||S r-   )setattrr/   )r0   r5   valuer%   r%   r&   __setitem__;      zattrproxy.__setitem__c                 C   r7   r-   )r2   r/   )r0   r5   dfltr%   r%   r&   get>   r;   zattrproxy.getN)r)   r*   r+   r1   r6   r:   r=   r%   r%   r%   r&   r,   1   s
    r,   returnc              	   C   sP   d}|   t@ D ]}tt || | jO }W d   n1 s w   Y  q|S )zuse future feature names to get any that are set in the global namespace

    `|` these together to get the flags for the compile function

    see the source code for the `__future__` module for more info
    r   N)keys_future_feature_namesr   	Exceptioncompiler_flag)glbflagskeyr%   r%   r&   _get_future_compiler_flagsB   s   
rF   c                 C   s   |t |O }t| |||dS )zcompile the node with the future flags from the global namespace

    for now that means respect `from __future__ import annotations` if it exists
    )modefilenamerD   )rF   compile)noderG   rH   rC   rD   r%   r%   r&   _compile_with_flagsP   s   rK   c                   @   sF   e Zd ZU eed< eed< eed< eed< dZeed< eZ	dd Z
dS )	InforH   module_namesourcer$   Nvarinfoc           	      C   s   |j d }|j}|jd }|j}| j}||kr#||  ||  S ||  |d   }||  d |  }||d | }|d| || d	|S )Nr   r   r    )
lineno
col_offset
end_linenoend_col_offsetr$   encodedecodeinsertappendjoin)	r0   extrP   rQ   rR   rS   r$   firstlastr%   r%   r&   get_segmentc   s   



zInfo.get_segment)r)   r*   r+   str__annotations__listrO   r   
dc_replacer
   r\   r%   r%   r%   r&   rL   Y   s   
 rL   c               	   k   s>    t di | }t|}zd V  W t| d S t| w )Nr%   )rL   r   setreset)fieldsinfotokenr%   r%   r&   use_infov   s   
rf   c                   C   s   t  S r-   )r   r=   r%   r%   r%   r&   get_info   s   rg   c                   @   sx   e Zd ZU ded< ded< eed< dZeed< dZeed   ed< e	d	d
 Z
e	dd Zdd Zdd ZdddZdS )Correspondence
DefinitionoriginalnewcorrespondsFchangedNchild_correspondencesc                 C   s   t | |ddd dS )NF)rj   rk   rl   rm   rn   rh   rj   rk   r%   r%   r&   invalid   s   zCorrespondence.invalidc                 K   s   t d| |dd|S )NT)rj   rk   rl   r%   ro   )rj   rk   kwargsr%   r%   r&   valid   s   zCorrespondence.validc                 C   s   t | jdt | j fS )Nr   )intrl   rm   r0   r%   r%   r&   fitness   s   zCorrespondence.fitnessc                 c   s*    | V  | j pg D ]	}| E d H  q	d S r-   )rn   walkr0   childr%   r%   r&   rw      s
   zCorrespondence.walkc                 C   s   g g g g f\}}}}|   D ]6}|d u s||jp|jrD|jd u r(||j q|jd u r4||j q|jr>||j q||j q||||fS r-   )rw   rj   rk   rW   rm   )r0   filtersamechanges	additions	deletionscorrr%   r%   r&   summary   s   

zCorrespondence.summaryr-   )r)   r*   r+   r^   boolrm   rn   r   r   staticmethodrq   rs   rv   rw   r   r%   r%   r%   r&   rh      s   
 
	
rh   c                   @   s  e Zd ZU ejed< dZeed< dZeed< dZ	e
d  ed< dZeed< dd	 Zd
d Zd.ddZdd Zd.ddZdd Zdd Zdd Zedd Zedd Zedd Zed/d d!Zed"d# Zed$d% Zed&d' Zed(d) Zd*d+ Zd,d- Z dS )0ri   rJ   NnamerH   parentr   
groundlinec                 C   s"   d | _ | jd u rt j| _d S d S r-   )_coderH   rg   ru   r%   r%   r&   __post_init__   s   
zDefinition.__post_init__c                 C   s"   || _ | jddD ]}d |_q	d S Nr   skip)r   	hierarchyr   )r0   r   pr%   r%   r&   
set_parent   s   zDefinition.set_parentr   c                 c   s8    |dkr| V  | j d ur| j |d E d H  d S d S )Nr   r   )r   r   )r0   r   r%   r%   r&   r      s   
zDefinition.hierarchyc                 C   s$   t |  }ddd t|D S )N.c                 s   s    | ]}|j pd V  qdS )<line>N)r   ).0xr%   r%   r&   	<genexpr>   s    z%Definition.dotpath.<locals>.<genexpr>)r_   r   rX   reversed)r0   chainr%   r%   r&   dotpath   s   zDefinition.dotpathc                 C   s*   t | j|d}tdd tt|D S )Nr   c                 s   s,    | ]\}}|d kr|j n|jpdV  qdS )r   r   N)rH   r   )r   ir   r%   r%   r&   r      s
    
z&Definition.codepath.<locals>.<genexpr>)r_   r   tuple	enumerater   )r0   r   r   r%   r%   r&   codepath   s   
zDefinition.codepathc                 C   s   | j o| j  S r-   )r   get_globalsru   r%   r%   r&   r         zDefinition.get_globalsc                 C      d S r-   r%   ru   r%   r%   r&   
get_object   s   zDefinition.get_objectc                 c   s    | V  d S r-   r%   ru   r%   r%   r&   rw      s   
zDefinition.walkc                 C   s   | j d u r
|  | _ | j S r-   )r   reconstructru   r%   r%   r&   
codestring   s   

zDefinition.codestringc                 C   s   dS NFr%   ru   r%   r%   r&   is_whitespace      zDefinition.is_whitespacec                 C   r   r-   r%   ru   r%   r%   r&   r      r   zDefinition.reconstructr   c                 C   r   r-   r%   )r0   rP   rQ   r%   r%   r&   stash   r   zDefinition.stashc                 C   r   r-   r%   r0   textr%   r%   r&   prepend_text  r   zDefinition.prepend_textc                 C   r   r-   r%   r   r%   r%   r&   append_text  r   zDefinition.append_textc                 C   r   r-   r%   r0   otherr%   r%   r&   
correspond  r   zDefinition.correspondc                 C   r   r-   r%   r0   r   order
controllerr%   r%   r&   apply_correspondence  r   zDefinition.apply_correspondencec                 C   sl   | j d ur4tj| j gg d}t|d| j|d}|jdd}t||| tj|jdd| j	ddd	 d S d S )
Nbodytype_ignoresexecrG   rH   rC   <adjust>co_namer    r   r   )path)
rJ   astModulerK   rH   r
   r   codereg
assimilater   )r0   rC   lclrJ   coder%   r%   r&   evaluate  s   


zDefinition.evaluatec                 C   s    | j }|j }t|pt|S r-   )r   
sep_at_endsearchsep_at_start)r0   r   abr%   r%   r&   well_separated+  s   zDefinition.well_separatedr   r   r   )!r)   r*   r+   r   ASTr^   r   r]   rH   r   r   r   rt   r   r   r   r   r   r   r   rw   propertyr   r   r   r   r   r   r   r   r   r   r   r%   r%   r%   r&   ri      s@   
 
	








ri   c                   @   sX   e Zd ZU dZeed< dd ZdddZd	d
 Zdd Z	e
dd Zdd Zdd ZdS )LineDefinitionr    r   c                 C      | j S r-   r   ru   r%   r%   r&   r   9     zLineDefinition.reconstructr   r   c                 C   sX   | j d}t|d }t|||t| d t|dkr || n|| j| jd| _| jS )N
r   r   rP   rQ   rR   rS   rH   content)r   splitr"   ExtentrH   r   stashed)r0   rP   rQ   r$   r[   r%   r%   r&   r   <  s   zLineDefinition.stashc                 C   s   || j  | _ d S r-   r   r   r%   r%   r&   r   I  r   zLineDefinition.prepend_textc                 C   s   | j | | _ d S r-   r   r   r%   r%   r&   r   L  r   zLineDefinition.append_textc                 C   s   t dd | jdD  S )Nc                 s   s    | ]}t |V  qd S r-   substantial)r   liner%   r%   r&   r   Q  s    z/LineDefinition.is_whitespace.<locals>.<genexpr>r   )anyr   r   ru   r%   r%   r&   r   O  s   zLineDefinition.is_whitespacec                 C   s   | j |j kS r-   r   r   r%   r%   r&   	equiv_srcW     zLineDefinition.equiv_srcc                 C   s6   t |t | us| |st| |S tj| |ddS )NFrm   )typer   rh   rq   rs   r   r%   r%   r&   r   Z  s   zLineDefinition.correspondNr   )r)   r*   r+   r   r]   r^   r   r   r   r   r   r   r   r   r%   r%   r%   r&   r   1  s   
 

r   c                   @      e Zd Zdd ZdS )HeaderDefinitionc                 C   s   | j  |j  kS r-   )r   stripr   r%   r%   r&   r   g  s   zHeaderDefinition.equiv_srcN)r)   r*   r+   r   r%   r%   r%   r&   r   a  s    r   c                       s   e Zd ZU dZeed< eedZe	e
 ed<  fddZ fddZd	d
 Zdd Zdd Zd/ddZdd Zdd ZddddZdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Z fd)d*Zed+d, Zed-d. Z  ZS )0GroupDefinitionNr   r   childrenc                    s6   t    d| _| jg }| _|D ]}| | qd S r   )superr   ignore_namesr   rW   r0   r   ry   	__class__r%   r&   r   p  s   
zGroupDefinition.__post_init__c                    sX   t  | | jd ur*t }| jddD ]}|jr||jjO }q| jj|@ | j_d S d S r   )r   r   r   ra   r   assignedfreeclosure)r0   r   closabler   r   r%   r&   r   {  s   
zGroupDefinition.set_parentc                 C      d dd | jD S )Nr    c                 S   s   g | ]
}t |tr|jqS r%   )
isinstancer   r   r   ry   r%   r%   r&   
<listcomp>  s    z*GroupDefinition.header.<locals>.<listcomp>rX   r   ru   r%   r%   r&   header  s
   zGroupDefinition.headerc                 c   s&    | V  | j D ]	}| E d H  qd S r-   )r   rw   rx   r%   r%   r&   rw     s
   
zGroupDefinition.walkc                 C   r   )Nr    c                 S   s   g | ]}|j qS r%   )r   r   r%   r%   r&   r     s    z/GroupDefinition.reconstruct.<locals>.<listcomp>r   ru   r%   r%   r&   r     s   zGroupDefinition.reconstructr   r   c                 C   sV   t ||||| j| jd| _| j}| jD ]
}||j|j}q|j| j_|j| j_| jS )Nr   )r   rH   r   r   r   r   rR   rS   )r0   rP   rQ   currry   r%   r%   r&   r     s   


zGroupDefinition.stashc                 C   4   | j r| j d | d S | td || jd d S )Nr   rJ   r   rH   )r   r   prependr   rH   r   r%   r%   r&   r     s
   zGroupDefinition.prepend_textc                 C   r   )Nr   r   )r   r   rW   r   rH   r   r%   r%   r&   r     s
   zGroupDefinition.append_textFensure_separationc                G   sd   |D ]-}|r$| j r$| j d |s$td d| jd}| j | ||  | j | ||  qd S )Nr   r   r   )r   r   r   rH   rW   r   )r0   r   r   ry   wsr%   r%   r&   rW     s   
zGroupDefinition.appendc                 G   s&   || j dd< |D ]}||  q	d S )Nr   )r   r   r   r%   r%   r&   r     s   zGroupDefinition.prependc                    s  t |t | us| js| j|jkrt| |S | j|jkr%tj| |ddS g }t| j}|jD ]* fdd|D }|sFtjd dd nt	|dd d |
 j |  q/|D ]}tj|d dd |  q\td	d
 |D  }|rtj| |d|dS t| |S )NFr   c                    s    g | ]}|   jr qS r%   )r   rl   )r   
this_childr   other_childr%   r&   r     s    z.GroupDefinition.correspond.<locals>.<listcomp>Tc                 S   s   |   S r-   )rv   )r   r%   r%   r&   <lambda>  s    z,GroupDefinition.correspond.<locals>.<lambda>)rE   c                 s   s.    | ]}t |jtpt |jto|jV  qd S r-   )r   rj   r   rk   rm   )r   r   r%   r%   r&   r     s    

z-GroupDefinition.correspond.<locals>.<genexpr>)rj   rk   rm   rn   )r   r   r   rh   rq   r   rs   r_   r   maxremoverj   rW   r   )r0   r   	childcorrr   
candidatesry   	mergeabler%   r   r&   r     s@   


	zGroupDefinition.correspondc              
   C   s@  |j }|j}z|d u r'|d|r$| j|dd | | |d| W d S W d S |d u rI|d|r?t| d  |d| W d S | j|dd W d S |jr|| j|dd z|j|||d W W d S  ty{   | j	
  | jtd |||d Y W d S w | j|dd W d S  ty } z|d||d	 W Y d }~d S d }~ww )
Nzpre-addTr   post-add
pre-deletepost-deleter   r   error)exc)rj   rk   rW   evaluate_childr   r   rm   r   r   r   pop_process_child_correspondencerh   rs   rA   )r0   ccorrr   r   origrk   r  r%   r%   r&   r    sF   




z-GroupDefinition._process_child_correspondencec           	         sZ    fdd}| }g  _ |D ]	} ||| q| }t|t| D ]} | q#d S )Nc                     s:   t  }  jD ]}t|dd  }d ur| |  d7  < q| S )Nr   r   )r   r   r2   )cry   r   ru   r%   r&   
namecounts,  s   
z3GroupDefinition._apply_corrlist.<locals>.namecounts)r   r  ra   delete_property)	r0   corrsr   r   r  counts1r   counts2dltr%   ru   r&   _apply_corrlist+  s   zGroupDefinition._apply_corrlistc           	      C   s   t d g i}d }|jD ]D}|jd urF|jjd ur=|d u r(|t d  }|t d = ng }|j}||t |< |t | | q|g|t |j< q|t | | qg }| jD ]}||t |g 7 }qU||t d g 7 }| j|d|d d S )Nrj   r  )idrn   rj   rJ   rW   r   r=   r  )	r0   r   r   groupsr   r  initccorrsry   r%   r%   r&    _apply_correspondence_orig_order=  s.   



z0GroupDefinition._apply_correspondence_orig_orderc                 C   s   | j |jd|d d S )Nrk   r  )r  rn   )r0   r   r   r%   r%   r&   _apply_correspondence_new_orderZ  s   
z/GroupDefinition._apply_correspondence_new_orderc                 C   sj   |j sJ |js
d S |d|r3|dv sJ |dkr!| j||d n|dkr,| j||d |d| d S d S )N
pre-updaterp   rj   )r   rk   post-update)rl   rm   r  r  r   r%   r%   r&   r   _  s   

z$GroupDefinition.apply_correspondencec                    sP   t  || |p
|| jd }t|dr&d|  ddd  |_d S d S )Nr+   r   r   )	r   r   r=   r   hasattrrX   r   r   r+   )r0   rC   r   objr   r%   r&   r   w  s
   
"zGroupDefinition.evaluatec                 C   r   r-   r%   rx   r%   r%   r&   r  }  r   zGroupDefinition.evaluate_childc                 C   r   r-   r%   r0   propr%   r%   r&   r    r   zGroupDefinition.delete_propertyr   ) r)   r*   r+   r   r   r^   r	   r_   r   r   ri   r   r   r   rw   r   r   r   r   rW   r   r   r  r  r  r  r   r   r   r  r  __classcell__r%   r%   r   r&   r   k  s0   
 			
			1'
r   c                       sV   e Zd ZU dZeed< dZeed<  fddZdd Zdd	 Z	d
d Z
dd Z  ZS )
ModuleCodeNmoduleglobalsc                    s   t    d| _d S )NT)r   r   r   ru   r   r%   r&   r     s   

zModuleCode.__post_init__c                 C   r   r-   r!  ru   r%   r%   r&   r     r   zModuleCode.get_globalsc                 C   r   r-   r"  ru   r%   r%   r&   r     r   zModuleCode.get_objectc                 C   s   | |  d S r-   )r   r   rx   r%   r%   r&   r    r   zModuleCode.evaluate_childc                 C   s   | j |= d S r-   r"  r  r%   r%   r&   r    r   zModuleCode.delete_property)r)   r*   r+   r   objectr^   r!  r   r   r   r  r  r  r%   r%   r   r&   r    s   
 r  c                   @   s$   e Zd Zdd Zdd Zdd ZdS )ClassDefinitionc                 C   s0   | j  }t|tr|| jd S t|| jd S r-   )r   r   r   dictr=   r   r2   )r0   r   r%   r%   r&   r     s   

zClassDefinition.get_objectc                 C   s(   |    }d ur||  t|S d S r-   )r   r   r   r,   )r0   ry   r  r%   r%   r&   r    s   zClassDefinition.evaluate_childc                 C   s"   |    }d urt|| d S d S r-   )r   delattr)r0   r  r  r%   r%   r&   r    s   zClassDefinition.delete_propertyN)r)   r*   r+   r   r  r  r%   r%   r%   r&   r$    s    r$  c                       sN   e Zd ZU dZeed< d fdd	Zdd	d
Zdd Zdd Z	dd Z
  ZS )FunctionDefinitionN_codeobjr   r   c                    sH   t | jts|  }|r||j  }r| jt||dd t ||S )NF	use_cache)	r   r   r'  r   co_firstlinenorecoder   r   r   )r0   rP   rQ   codeltar   r%   r&   r     s
   zFunctionDefinition.stashTFc                    s   i  fdd |   } || |s|= |  D ]#}t|tr>|  d  }r>| }||ur>t|||d ||_qd S )Nc                    s8   | |< | j D ]}t|tr |g ||jR  qd S r-   )	co_constsr   r   r   )r   r   r-  _fill_subcodessubcodesr%   r&   r1    s   

z1FunctionDefinition.recode.<locals>._fill_subcodesr)  )r   rw   r   r'  r=   r   r   r(  )r0   new_coderecode_currentr*  herer   subcoder-  r%   r0  r&   r,    s    

zFunctionDefinition.recodec                 C   s   |j r|jsJ |d|rO|  }| |jj|}|j}| j|dd | D ]}t	|j
tr:|jd ur:|j
j|j_q'g | _| j|jj  || _|d| d S d S )Nr  F)r4  r  )rl   rm   r   
reevaluaterk   rJ   __code__r,  rw   r   rj   r'  r(  r   rW   )r0   r   r   r   rC   new_objr3  r  r%   r%   r&   r     s"   


z'FunctionDefinition.apply_correspondencec                 C   s:   | j d u rg |  | jR }|tjv rtj| | _ | j S r-   )r(  r   r   r   codes)r0   pthr%   r%   r&   r     s
   

zFunctionDefinition.get_objectc                    s   j }d}i }t  j j jg  j j j j j	 j
d
 || jd }| jjrod}tt| jj}ttjdtjg  fdd|D d g g d g d ttj jt dgg d d	 }t| tj|gg d
}	ntj gg d
}	t|	d|j|d}
|
jdd}
t|
|| |r|d }|| }n|| j }||| j< ||	_ |	| _|  }|d u rtd| j dt || |j!| _"|S )NF)
r   argsr   decorator_listreturnstype_commentrP   rQ   rR   rS   Tz##create_closurec                    s   g | ]}t j| jd dqS )r   )argrP   rQ   )r   r@  rP   )r   r   new_noder%   r&   r   (  s    z1FunctionDefinition.reevaluate.<locals>.<listcomp>)posonlyargsr<  vararg
kwonlyargskw_defaultskwargdefaults)r  ctx)r   r<  r   r=  r>  r   r   r   r   r   z0Cannot find existing object for replacement of 'z'.)#extentr   r   r<  r   r>  r?  rP   rQ   rR   rS   r=   r   r   r   sortedr   copy_locationFunctionDef	argumentsReturnNameLoadfix_missing_locationsr   rK   rH   r
   r   rJ   r   rA   r   r8  r(  )r0   rB  rC   rY   r   r   previousnameswraprJ   r   creatorr9  old_objr%   rA  r&   r7    s   






zFunctionDefinition.reevaluater   )TF)r)   r*   r+   r(  r#  r^   r   r,  r   r   r7  r  r%   r%   r   r&   r'    s   
 
r'  c                   @   sN   e Zd ZU eed< eed< eed< eed< dZeed< dZeed< dd	 ZdS )
r   rP   rQ   rR   rS   NrH   r   c                 C   s   | j d u rt j | _ d S d S r-   )rH   rg   ru   r%   r%   r&   r   c  s   
zExtent.__post_init__)	r)   r*   r+   rt   r^   rH   r]   r   r   r%   r%   r%   r&   r   Z  s   
 r   c                 C      t | j| j| j| jdS NrP   rQ   rR   rS   )r   rP   rQ   rY   r%   r%   r&   _collapse_to_beginningh     r\  c                 C   rX  rY  )r   rR   rS   r[  r%   r%   r&   _collapse_to_endq  r]  r^  c                 C   s   t | jd| j| jdS )Nr   rZ  )r   rP   rR   rS   rJ   r%   r%   r&   extend_to_linez  s   r`  c                    s    fddt | D }t| dr| jD ]	}|t| qt| drJ||  tdd |D \}}tdd |D \}}t||||d| _	| j	S d | _	| j	S )	Nc                    s   g | ]
}t |  r qS r%   )fill_real_extent)r   nr[  r%   r&   r     s
    z$fill_real_extent.<locals>.<listcomp>r=  rP   c                 s       | ]	}|j |jfV  qd S r-   )rP   rQ   r   rY   r%   r%   r&   r         
z#fill_real_extent.<locals>.<genexpr>c                 s   rc  r-   )rR   rS   rd  r%   r%   r&   r     re  rZ  )
r   iter_child_nodesr  r=  rW   r`  minr   r   rJ  )rJ   extentsdecorP   rQ   rR   rS   r%   r[  r&   ra    s.   




ra  c                 C   s   t d|  S )Nz *(#.*)?\n?)re	fullmatch)r#   r%   r%   r&   r     r;   r   c                 C   s   t | }tdd t|D dd}|d |d  }||d d  }|r0|d ds0| g}ng }d|d|d|fS )Nc                 S   s   g | ]
\}}t |r|qS r%   r   )r   r   r   r%   r%   r&   r     s    z!analyze_split.<locals>.<listcomp>r   r   r   r   r    )r'   r   r   endswithr  rX   )r#   r$   substleftmiddlerightr%   r%   r&   analyze_split  s   rq  c                 C   s    t  t| j| j|j|jdS rY  )rg   r\   r   rR   rS   rP   rQ   )node1node2r%   r%   r&   r.    s   r.  c                 C   sl   t | \}}}d}|r|r|| n||7 }|r||7 }|r+|r'|| n||7 }|r4|d |dgS g S )Nr    rJ   r   )rq  r   r   )betweendefn1defn2r/   rn  ro  rp  rvalr%   r%   r&   
distribute  s   ry  nodesc                 C   s   | sg S dd | D }g }t |d d |dd  D ]\\}}\}}t||}|| |t||| q||d d  |S )Nc                 S   s   g | ]	}|j t|fqS r%   )rJ  r   )r   rJ   r%   r%   r&   r     s    z'collect_definitions.<locals>.<listcomp>r   r   )zipr.  rW   extendry  )rz  defnsresultsrr  rv  rs  rw  ru  r%   r%   r&   collect_definitions  s   *

r  rJ   c           	      C   s   t  }t| j}t| j| ||j| t  d}g }t	| j
}t|| }|t|d d td7 }t	| }t|| jd j
}|t|d |d 7 }|j|  t| }t| jd j
|}|jt||d d   |j|_|S )Nr   rJ   r   r   r.   r   r   )rg   r   r   r'  r   rO   r=   r   r
   r\  rJ  r.  ry  r   r   r^  rW   rP   r   )	rJ   rd   r}  fndefnpreludedeco0ru  fnstartfnendr%   r%   r&   r    s*   



c                 C   s   t  }t| j}t| j| ||j| t  d}g }t	| j
}t|| jd j
}|t|d |d td7 }|j|  t| }t| jd j
|}|jt||d d   |S )Nr  r   r.   r   )rg   r   r   r$  r   rO   r=   r   r
   r\  rJ  r.  ry  r   r   r^  rW   )rJ   rd   r}  clsdefnr  r  ru  r  r%   r%   r&   r    s"   


c                 C   s   t  }tddddd}tt|jt|jd t|jt|jd d}t| |jt| jd}| jrYt|| jd j	 }rE|j
t|d d   t| jd j	| }rY|jt|d d   |S )Nr   r   rZ  r   )rJ   r   r   )rg   r   r"   r$   r  rM   r   r   r.  rJ  r   ry  rW   )rJ   rd   
begin_nodeend_nodecgru  r%   r%   r&   r    s"   c                 C   s   t | t | dS )Nrt  )r   rg   r\   r_  r%   r%   r&   r  /  s   c                   @   sV   e Zd ZdddZedd Zdd Zdd	 Zd
d ZdddZ	dddZ
dd ZdS )CodeFileNc                 C   s   t  | _|| _|| _|d u r|  n|| _| jds"|  jd7  _t| j}i }t	|| t
| j|| jt| j|d t| t|| _W d    n1 sQw   Y  | j  d| _d S )Nr   )rH   rM   rN   r$   rO   F)r   activityrH   rM   read_sourcesavedrl  r   parser   rf   r'   ra  r  rootr   dirty)r0   rH   rM   rN   treerO   r%   r%   r&   r1   5  s*   

	
zCodeFile.__init__c                 C   s   | j jS r-   )r  r   ru   r%   r%   r&   r   M  s   zCodeFile.modulec                 C   sH   t |tr|| j_t|| j_d S t |tr d | j_|| j_d S td)Nz"associate expects a dict or module)r   r   r  r   varsr!  r%  	TypeError)r0   r  r%   r%   r&   	associateQ  s   

zCodeFile.associatec                 C   sH   t | j}| }W d    n1 sw   Y  |ds"|d7 }|S )Nr   )openrH   readrl  )r0   frN   r%   r%   r&   r  [  s   

zCodeFile.read_sourcec                 C   s   |   | jkS r-   )r  r  ru   r%   r%   r&   staleb  r;   zCodeFile.stalerj   Tc                    sT   |dkr sJ d fdd	}j |j }|jrd_j j|||d | S )Nrk   c                    s   | dkr o du p|j  v S | dkr%|jjs#jt|j d S d S | dkr;|j js9jt|j  d S d S | dkrKjt|j  d S | dkr\jt|j| d S dS )Nr   Tr   r  r  r  )	rj   rk   r   r  emitAddOperationDeleteOperationUpdateOperationExceptionOperation)opr  r  allow_deletionsr0   r%   r&   r   i  s"   z"CodeFile.merge.<locals>.controllerTr  r-   )r  r   rm   r  r   r   )r0   r   r   r  r   r   r%   r  r&   mergee  s   zCodeFile.mergec                 C   s   | j sd S |r|  rtd| j d| j }|ds"|d7 }t| jd}|| W d    n1 s8w   Y  | j	  || _
d| _ d S )NzCannot commit changes to z because the file was changed.r   wF)r  r  r(   rH   r  r   rl  r  writer   r  )r0   check_stale
new_sourcer  r%   r%   r&   commit  s   



zCodeFile.commitc                 C   sL   |   }|| jjks| jr$t| j|| jd}| j|dd | j  d S d S )N)rN   rM   rk   )r   )	r  r  r   r  r  rH   rM   r  r   )r0   r  cfr%   r%   r&   refresh  s   
zCodeFile.refreshr-   )rj   T)T)r)   r*   r+   r1   r   r   r  r  r  r  r  r  r%   r%   r%   r&   r  4  s    




r  c                   @   s   e Zd ZU eed< eed< dS )CodeFileOperationcodefiledefnN)r)   r*   r+   r  r^   ri   r%   r%   r%   r&   r    s   
 r  c                   @   r   )r  c                 C      d| j   d| j jj S )NzUpdate  @Lr  r   r   rP   ru   r%   r%   r&   __str__     zUpdateOperation.__str__Nr)   r*   r+   r  r%   r%   r%   r&   r        r  c                   @   r   )r  c                 C   sP   t | jtrd| jj  d| jjj d| jj S d| j  d| jjj S )NzRun r  : zAdd )r   r  r   r   r   r   rP   r   ru   r%   r%   r&   r    s   (zAddOperation.__str__Nr  r%   r%   r%   r&   r    r  r  c                   @   s"   e Zd ZU ee ed< dd ZdS )r  r  c                 C   s   t | jtrd| jj  d| jjj d| jj }nd| j  d| jjj }| jd u r0|S d	t
t| j| j| jj}| d| S )NzException (run) r  r  zException (add) r    r   )r   r  r   r   r   r   rP   r   r  rX   	tracebackformat_exceptionr   __traceback__)r0   msgtbr%   r%   r&   r    s   *
 zExceptionOperation.__str__N)r)   r*   r+   r   rA   r^   r  r%   r%   r%   r&   r    s   
 r  c                   @   r   )r  c                 C   r  )NzDelete r  r  ru   r%   r%   r&   r    r  zDeleteOperation.__str__Nr  r%   r%   r%   r&   r    r  r  r   )Z__doc__
__future__r   rj  sysr  abcr   r   r!   collectionsr   
contextlibr   r   contextvarsr   dataclassesr   r	   r
   r`   typesr   r   typingr   r   r   codefindr   r   r   r   r   r   r  r   r   utilsr   r   r   ra   all_feature_namesr@   version_infor'   rI   r   r   rA   r(   r,   rt   rF   rK   rL   rf   rg   rh   ri   r   r   r   r  r$  r'  r   r\  r^  r`  ra  r   rq  r.  ry  r_   r  rM  AsyncFunctionDefClassDefr   stmtr  r  r  r  r  r  r%   r%   r%   r&   <module>   s    




	
	4w/	    			g