U
    md                     @   s
  d dl Z d dlZd dlmZ d dlmZmZmZm	Z	 d dl
mZmZ d dlmZ d dlmZ d dlmZmZ d dlmZmZmZmZmZmZmZmZmZ G dd	 d	eZG d
d dejZ G dd deZ!ddddZ"dddeee#  e$ee dddZ%dd Z&dS )    N)ArgumentParser	Namespace_SubParsersActionArgumentError)	lru_cachepartial)Path)which)runCompletedProcess)	Optional	Generator	FrozenSetSequenceListTupleDictAnyMappingc                       s0   e Zd ZdZeeeef d fddZ  ZS )_DelegatingSubparsersActionzFLike a normal subcommand action, but uses a delegator for more choices)_command_runargsc                   s.   t  j|| || _t|| f| | _| _d S N)super__init__command_CommandDelegator_name_parser_mapchoices)selfr   r   argskwargs	__class__ C/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/scanpy/cli.pyr      s     z$_DelegatingSubparsersAction.__init__)	__name__
__module____qualname____doc__strr   r   r   __classcell__r$   r$   r"   r%   r      s   r   c                   @   s   e Zd ZdZeedddZeedddZeedd	d
dZ	eddddZ
eeddf dddZedddZedddZeeef dddZee ee dddZdS )r   ub       Provide the ability to delegate,
    but don’t calculate the whole list until necessary
    )r   actionc                 K   s   || _ || _i | _|| _d S r   r   r,   
parser_maprunargs)r   r   r,   r/   r$   r$   r%   r   '   s    z_CommandDelegator.__init__)kreturnc              
   C   sd   z| j | W S  tk
r^   t| j d| r>t| | Y S t| jd| dt|  Y nX d S )N-u   No command “u   ”. Choose from )r.   KeyErrorr	   r   _DelegatingParserr   r,   setr   r0   r$   r$   r%   __getitem__-   s     z_CommandDelegator.__getitem__N)r0   vr1   c                 C   s   || j |< d S r   r.   )r   r0   r8   r$   r$   r%   __setitem__8   s    z_CommandDelegator.__setitem__c                 C   s   | j |= d S r   r9   r6   r$   r$   r%   __delitem__;   s    z_CommandDelegator.__delitem__r1   c                 c   s   | j E d H  | jE d H  d S r   )r.   commandsr   r$   r$   r%   __iter__@   s    z_CommandDelegator.__iter__c                 C   s   t | jt | j S r   )lenr.   r=   r>   r$   r$   r%   __len__D   s    z_CommandDelegator.__len__c                 C   s
   t | jS r   )hashr   r>   r$   r$   r%   __hash__G   s    z_CommandDelegator.__hash__)otherc                    s,   t  tr"t fdddD S j kS )Nc                 3   s"   | ]}t |t  |kV  qd S r   )getattr).0attrrD   r   r$   r%   	<genexpr>L   s   z+_CommandDelegator.__eq__.<locals>.<genexpr>r-   )
isinstancer   allr.   )r   rD   r$   rH   r%   __eq__J   s
    
z_CommandDelegator.__eq__c                    s$   t  fddtjd tjD S )Nc                 3   sP   | ]H}t | j d D ],}t|tjr|jt jd d V  qqdS )z-*   N)r   globr   osaccessX_OKnamer@   )rF   bin_dirbinaryr>   r$   r%   rI   U   s
    z-_CommandDelegator.commands.<locals>.<genexpr>PATH)	frozensetrO   environsplitpathsepr>   r$   r>   r%   r=   R   s    z_CommandDelegator.commands)r&   r'   r(   r)   r*   r   r   r   r7   r:   r;   r   r?   intrA   rC   r   rL   propertyr   r   r=   r$   r$   r$   r%   r   !   s   r   c                       sT   e Zd ZdZeed fddZd	eee  ee	 e
e	ee f dddZ  ZS )
r4   z1Just sets parse_args().func to run the subcommand)cdsubcmdc                    s,   t  j|j d| dd || _|| _d S )Nr2   F)add_help)r   r   r   r\   r]   )r   r\   r]   r"   r$   r%   r   `   s    z_DelegatingParser.__init__N)r    	namespacer1   c                 C   s<   |d k	r|d kst dttt| jf|f| jjdg fS )Nz&Only use DelegatingParser as subparserfunc)AssertionErrorr   r   r
   progr\   r/   )r   r    r_   r$   r$   r%   parse_known_argse   s    z"_DelegatingParser.parse_known_args)NN)r&   r'   r(   r)   r   r*   r   r   r   r   r   r   rd   r+   r$   r$   r"   r%   r4   ]   s     
r4   r<   c                  C   s   ddl m}  t|  d S )NrM   settings) rf   printre   r$   r$   r%   _cmd_settingsp   s    ri   Tcheck)argvrk   r1   c                K   sX   t dd}|j|jd |jtd|d|id}|d}|jtd || }| S )z    Run a builtin scanpy command or a scanpy-* subcommand.

    Uses :func:`subcommand.run` for the latter:
    `~run(['scanpy', *argv], **runargs)`
    zSThere are a few packages providing commands. Try e.g. `pip install scanpy-scripts`!)descriptionr`   Zscanpyrk   )r,   r   r   rf   )	r   set_defaults
print_helpadd_subparsersr   
add_parserri   
parse_argsra   )rl   rk   r/   parserZ
subparsersZparser_settingsr    r$   r$   r%   mainv   s    	


rt   c                  C   s"   t dd} | dk	rt| j dS )zj    This serves as CLI entry point and will not show a Python traceback
    if a called command fails
    Frj   N)rt   sysexit
returncode)cmdr$   r$   r%   console_main   s    
ry   )N)'rO   ru   collections.abcabcZcabcargparser   r   r   r   	functoolsr   r   pathlibr   shutilr	   
subprocessr
   r   typingr   r   r   r   r   r   r   r   r   r   MutableMappingr   r4   ri   r*   boolrt   ry   r$   r$   r$   r%   <module>   s*   ,< 
 