o
    i[                     @   s   d dl mZ d dlmZ d dlmZ d dlmZmZm	Z	m
Z
mZ d dlmZ d dlmZ er4d dlmZ e	dZG d	d
 d
ZdS )    )Sequence)contextmanager)chain)TYPE_CHECKINGAnyTypeVarcastoverload)inverse_groups_from_app)	Parameter)AppVc                
   @   s.  e Zd Zdd Zedded ee B deeef dB fddZ	e
d	efd
dZe
d	efddZe
d	ed fddZeded	efddZededed	efddZedededB ded	efddZeddededB ded	efddZddededB dedB d	edB fddZe
d	efddZdS )AppStackc                 C   s   |gg| _ i g| _d S N)stackoverrides_stack)selfapp r   X/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/cyclopts/app_stack.py__init__   s   
zAppStack.__init__Nappsr   	overridesc              	   c   s   | j |pi B }| j|pi  |s$zd V  W | j  d S | j  w t|d trFttt |}| jd d j	|dd\}}}t
|}n	tt
d t
|}~|sdzd V  W | j  d S | j  w g }dd |D }|D ]d}	|	jd u r|r|d jd ur|  |r|d jd us||	 |	jj|  |	jj|pi  |	}
|
j }
d urt|
|v rq| }||
 |
jj| |
jj|pi  |
j }
d usqoz@d V  W |D ]2}	|	jj  |	jj  |	}
|
j }
d urt|
|v rq|
jj  |
jj  |
j }
d usq| j  d S |D ]6}	|	jj  |	jj  |	}
|
j }
d urLt|
|v r8q'|
jj  |
jj  |
j }
d us/q| j  w )Nr   T)include_parent_metar   c                 S   s   h | ]}t |qS r   )id).0r   r   r   r   	<setcomp>6   s    z$AppStack.__call__.<locals>.<setcomp>)r   r   appendpop
isinstancestrr   r   r   parse_commandslist_meta_parent	app_stackcopy_metar   )r   r   r   str_apps_
apps_tupleresolved_appsso_farapp_idsr   meta_appmeta_subappsr   r   r   __call__   s   





	zAppStack.__call__returnc                 C   s>   i }t | jD ]}| D ]\}}|d ur||| qq|S r   )reversedr   items
setdefault)r   outoverrides_framekeyvaluer   r   r   r   ]   s   zAppStack.overridesc                 C   sL   g }t | jD ]}|jrq|dd |jjD  ||j qt	j
| S )zhdefault_parameter has special resolution since it needs to include the command groups in the derivation.c                 S   s   g | ]}|j qS r   )default_parameter)r   groupr   r   r   
<listcomp>m   s    z.AppStack.default_parameter.<locals>.<listcomp>)r   from_iterabler   r$   extendr%   command_groupsr   r9   r   combine)r   cparams	child_appr   r   r   r9   f   s   
zAppStack.default_parameterc                 C   s   | j st| j d S )Nr   )r   
ValueError)r   r   r   r   current_framer   s   
zAppStack.current_frame	attributec                 C      d S r   r   )r   rD   r   r   r   resolvey      zAppStack.resolveoverridec                 C   rE   r   r   )r   rD   rH   r   r   r   rF   |   rG   fallbackc                 C   rE   r   r   r   rD   rH   rI   r   r   r   rF      rG   c                C   rE   r   r   rJ   r   r   r   rF      rG   c           	      C   s   |dur|S t | jD ]}||v r|| }|dur|  S qt tt| jD ],}t||}|dur7|  S |}|j }durTt||}|durM|  S |j }dus@q(|S )z,Resolve an attribute from the App hierarchy.N)r2   r   r#   r   r<   r   getattrr$   )	r   rD   rH   rI   r6   r8   r   resultr.   r   r   r   rF      s(   

c                    s~   | j d  z| j d }W n ty   d }Y nw |d ur=zt fddt|D d W S  ty8   |j}Y nw |d usg S )Nr   c                 3   s     | ]}|d   u r|V  qdS )r   Nr   )r   xcommand_appr   r   	<genexpr>   s    z*AppStack.command_groups.<locals>.<genexpr>   )rC   
IndexErrornextr
   StopIterationr$   )r   current_appr   rO   r   r>      s   
 
zAppStack.command_groupsr   )NN)__name__
__module____qualname__r   r   r   r!   dictr   r0   propertyr   r   r9   r#   rC   r	   rF   r   r>   r   r   r   r   r      s*    .E "(r   N)collections.abcr   
contextlibr   	itertoolsr   typingr   r   r   r   r	   cyclopts.group_extractorsr
   cyclopts.parameterr   cyclopts.corer   r   r   r   r   r   r   <module>   s    