U
    Åmœdï  ã                   @   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*   ,< ÿÿ
 þ