o
    2ѹg,                     @   s   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m	Z	m
Z
mZmZ d dlmZmZmZmZ d dlmZ d dlmZ d dlmZ d d	lmZ G d
d deZG dd deZdS )    N)abstractmethod)ConfigCompositionException)	Container
DictConfigMissingMandatoryValue	OmegaConf
ListConfig)AnyListOptionalTuple)ConfigLoader)
ObjectType)Plugin)RunModec                
   @   sD  e Zd ZdeddfddZed#ddZed#dd	Zeede	fd
dZ
edee	 ddfddZeede	de	fddZede	deee	 ee	 f fddZede	dee	 fddZedede	dee	 fddZde	dee	 dee	 deee	 ef fddZdee	 de	dee	 fdd Zede	de	fd!d"ZdS )$CompletionPluginconfig_loaderreturnNc                 C   s
   || _ d S N)r   )selfr    r   c/Users/admin/.pyenv/versions/3.10.0/lib/python3.10/site-packages/hydra/plugins/completion_plugin.py__init__   s   
zCompletionPlugin.__init__c                 C      d S r   r   r   r   r   r   install!      zCompletionPlugin.installc                 C   r   r   r   r   r   r   r   	uninstall%   r   zCompletionPlugin.uninstallc                   C      dS )zT
        :return: the name of the shell this plugin provides completion for
        Nr   r   r   r   r   provides)   s   zCompletionPlugin.providesconfig_namec                 C   r   r   r   r   r    r   r   r   query1   r   zCompletionPlugin.querycommandc                 C   r   )z
        :param command: "install" or "uninstall"
        :return: command the user can run to install or uninstall this shell completion on the appropriate shell
        Nr   r#   r   r   r   help5   s   zCompletionPlugin.helpfilenamec                 C   s   |  d}|dkrO| d|d  }| |d d  } g d}tjdr9ttdtdD ]}|t| d	 q,| s=d
S |D ]}|  |rN|| f  S q?d
S )N=r      )./\z./z.\winaz:)NN)	rfindsysplatform
startswithrangeordappendchrlower)r&   lastZkey_eqprefixesdriveprefixr   r   r   _get_filename>   s   
zCompletionPlugin._get_filenamewordc                 C   s   t j| r| }t | }d}nt j| }t j|r"t |}ng }t j| }g }|D ]}||r?|t j|| q.|S )N )	ospathisdirlistdirdirnamebasenamer4   r7   join)r?   rE   filesZfile_prefixretfiler   r   r   complete_filesP   s   

zCompletionPlugin.complete_filesconfigc              	      s
  dt dt dtfdd}| d u rg S t| rg }ds"drmdd	 }z
tj| |d
d}W n ty=   d}Y nw |d ur]t|rNt|d}nt	|t
rYt| }|g}ng }|fdd|D  |S d}|d	krd|  |d d  }t|  }t||}| fdd|D  |S t	| tr| jddD ]P\}	}
t|	}|r|||	|
 qt| rt	| tsJ tt| D ](}z| | }
t|r||||
 W q ty   |||d Y qw |S J dt|  )Nin_keyin_valuer   c                 S   s   t |r
|  dS |  dS )Nr*   r'   )r   	is_config)rM   rN   r   r   r   str_repe   s   


z.CompletionPlugin._get_matches.<locals>.str_repr*   r'   r   r(   T)Zthrow_on_missingr@   c                       g | ]}  | qS r   r   .0match)r?   r   r   
<listcomp>       z1CompletionPlugin._get_matches.<locals>.<listcomp>r)   c                    s   g | ]	}  d | qS )r*   r   rR   )base_keyr   r   rU      s    F)resolvez&Object is not an instance of config : )r	   strr   rO   endswithselectr   r   _get_matches
isinstanceboolr9   extendr1   r   Zitems_exr4   r7   Zis_listr   r5   lentype)rL   r?   rP   matchesZ	exact_keyZ	conf_nodeZkey_matchesZlast_dotZpartial_keykeyvalueZstr_keyidxr   )rW   r?   r   r\   c   sd   







zCompletionPlugin._get_matcheswordsc                    s  | d}| d}|s|r|d |dd   }nd |d}|d}d}|d	kr6|d| }	tj}
ntj}
|d	kr@d}	n|d| }	| jj|	|
||d
}g }|
tjkru|D ]u}|	dkrf|	 d| n|}| |rr|| d}qY|
tjkr|D ]R}|	dkr|	 d| n|}| |r| jj|tj||d
}| jj|tj||d
}t|dkrt|dkr|s|d }nt|dkrt|dkr|d }|| q| fdd|D }||fS )N+~r   r)   r@   r'   r+   Fr(   )Z
group_nameresults_filterr    	overridesTc                    rQ   r   r   )rS   groupr=   r   r   rU      rV   z9CompletionPlugin._query_config_groups.<locals>.<listcomp>)	r4   r1   r   ZCONFIGZGROUPr   Zget_group_optionsr7   r`   )r   r?   r    rf   Zis_additionZis_deletionZlast_eq_indexZlast_slash_indexexact_matchZparent_groupri   Zall_matched_groupsmatched_groupsrT   namerH   dirsr   rl   r   _query_config_groups   sh   










z%CompletionPlugin._query_config_groupslinec                    s&  ddl m} t|dkp|d dk}|| }|j}|s#t|dkr&d}n
|d }|dd }t|\ }|d urS d usAJ t|}	 fdd|	D }	t|	S | j|||d	\}
}g }|s|j	rgt
jnt
j}g }z| jj|||d
}t||}W n	 ty   Y nw tt|
| }	t|	S )N   )get_argsr   r(    r@   c                    s   g | ]} | qS r   r   )rS   rJ   Zfname_prefixr   r   rU      s    z+CompletionPlugin._query.<locals>.<listcomp>)r    rf   )r    rj   run_mode)Z_internal.utilsrt   r`   splitrj   r   r>   rK   rq   Zmultirunr   ZMULTIRUNZRUNr   Zload_configurationr\   r   listsetsorted)r   r    rr   rt   Znew_wordZparsed_argsrf   r?   r&   resultrn   rm   Zconfig_matchesrw   rL   r   rv   r   _query   s>   

zCompletionPlugin._queryc                 C   sL   d}d}t || }|r|dS t || }|r|dS td|  d)a  
        Take the command line received from shell completion, and strip the app name from it
        which could be at the form of python script.py or some_app.
        it also corrects the key (COMP_INDEX) to reflect the same location in the striped command line.
        :param line: input line, may contain python file.py followed=by_args..
        :return: tuple(args line, key of cursor in args line)
        z'^\s*[\w\/]*python[3]?\s*[\w/\.]*\s*(.*)z^\s*[\w_\-=\./]+\s*(.*)r)   zError parsing line ''N)rerT   rk   RuntimeError)rr   Zpython_argsZapp_argsrT   r   r   r   strip_python_or_app_name  s   	

z)CompletionPlugin.strip_python_or_app_namer   N)__name__
__module____qualname__r   r   r   r   r   staticmethodrY   r   r   r"   r%   r   r>   r
   rK   r   r\   r^   rq   r}   r   r   r   r   r   r      s@    $<
<*r   c                   @   sb   e Zd ZdZdddZdddZedefdd	Zd
e	e ddfddZ
ededefddZdS )DefaultCompletionPluginzK
    A concrete instance of CompletionPlugin that is used for testing.
    r   Nc                 C      t r   NotImplementedErrorr   r   r   r   r   !     zDefaultCompletionPlugin.installc                 C   r   r   r   r   r   r   r   r   $  r   z!DefaultCompletionPlugin.uninstallc                   C   r   r   r   r   r   r   r   r   '  r   z DefaultCompletionPlugin.providesr    c                 C   r   r   r   r!   r   r   r   r"   +  r   zDefaultCompletionPlugin.queryr#   c                 C   r   r   r   r$   r   r   r   r%   .  r   zDefaultCompletionPlugin.helpr   )r   r   r   __doc__r   r   r   rY   r   r   r"   r%   r   r   r   r   r     s    

r   )rA   r   r2   abcr   Zhydra.errorsr   Z	omegaconfr   r   r   r   r   typingr	   r
   r   r   Zhydra.core.config_loaderr   Zhydra.core.object_typer   Zhydra.plugins.pluginr   Zhydra.typesr   r   r   r   r   r   r   <module>   s     