o
    qoi`W                     @   s  d dl Z d dl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mZmZmZ d dlmZ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 d dl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( e)e*Z+dee, fddZ-dedeee, ee, f fddZ.de/deee, ee, f fddZ0de1fddZ2dee, dee, de,fddZ3dee, dee, dee, dee, fddZ4de1fddZ5dee, dee, dee, defd d!Z6d"ee, defd#d$Z7d%e,de1fd&d'Z8d(edefd)d*Z9	+dLd,e j:d-e j;de(dee, d.ee, d/e/ddfd0d1Z<d2e1d3e1d4ee' d5ed.ee, de(d6ee, ddfd7d8Z=de,fd9d:Z>de,fd;d<Z?de j;fd=d>Z@dMd,eee,  defd?d@ZAdAeee,  dee/ fdBdCZBdDe,defdEdFZCdNdHedIe1de,fdJdKZDdS )O    N)	dataclass)dirnamejoinnormpathrealpath)	FrameTypeTracebackType)AnyListOptionalSequenceTuple)OmegaConfBaseException)ConfigSearchPathImpl)ConfigSearchPathSearchPathQuery)get_valid_filenamevalidate_config_path)CompactHydraExceptionInstantiationExceptionSearchPathException)RunModeTaskFunctionreturnc                  C   s.   g d} | D ]}|t jv rt j|   S qd S )N)HYDRA_MAIN_MODULEFB_PAR_MAIN_MODULEFB_XAR_MAIN_MODULEosenviron)module_envs
module_env r"   I/home/ubuntu/.local/lib/python3.10/site-packages/hydra/_internal/utils.py_get_module_name_override   s   
r$   task_functionc                 C   s|   t | dr| j} t | ds| j}t }|d ur|}|dvr%d }|}||fS zt| }W n ty7   d }Y nw d }||fS )N__wrapped__)N__main__)hasattrr&   
__module__r$   inspectgetfile	TypeError)r%   mdloverridecalling_filecalling_moduler"   r"   r#   0detect_calling_file_or_module_from_task_function%   s$   

r1   stack_depthc                 C   s   t  }||  }t r$d|d jv r$|d jd d }t|d}|d fS |j}d }zt }|d u r@|d j|d  j}W ||fS W ||fS  tyi   z
|d j	d j}W n	 ty^   Y nw Y ||fS Y ||fS w )N_dhr   znotebook.ipynb   self)
r*   stackis_notebook	f_globalsr   filenamer$   r)   KeyErrorf_locals)r2   r6   framepynb_dirr/   r0   r"   r"   r#   .detect_calling_file_or_module_from_stack_frame@   s4   
r>   c                  C   sB   zt  jj} | dkrW dS | dkrW dS W dS  ty    Y dS w )NZMQInteractiveShellTTerminalInteractiveShellF)get_ipython	__class____name__	NameError)shellr"   r"   r#   r7   Y   s   
r7   r/   r0   c                 C   sh   | d urt j| }tt j|d }|S |d ur1|d}|dkr-||d d  }|S |}|S t )Nr   .   )r   pathbasenamer   splitextrfind
ValueError)r/   r0   target_file	task_namelast_dotr"   r"   r#   detect_task_namef   s   

rQ   config_pathc                 C   s(  |d urt j|r|S |dr|S | d ur/tt| }|d ur't||}nd S t|}|S |d ur|d}|dkrC|d| }nd}|d urw|	t jj
d}t|drw|tdd  }|d}|dkro|d| }nd}t|dsWd| }|d ur|dkr|d | }|S || }|S t )Nzpkg://rF   rG   r    /z../)r   rI   isabs
startswithr   r   r   r   rL   replacesepstrlenrM   )r/   r0   rR   abs_base_dirsearch_path_dirrP   r"   r"   r#   compute_search_path_dirv   sD   


r]   c                  C   sF   t  } t| dkr| d j}|drdS |drdS t duS )z8
    Attempts to detect if running under a debugger
    r4   z/pdb.pyTz
/pydevd.pyN)r*   r6   rZ   r9   endswithsysgettrace)framesr9   r"   r"   r#   is_under_debugger   s   


rc   c                 C   s   t | ||}t|S N)r]   create_config_search_path)r/   r0   rR   r\   r"   r"   r#   #create_automatic_config_search_path   s   rf   r\   c                 C   s   ddl m} ddlm} t }|dd | d ur|d|  | |}|D ]}| }t||s4J |	| q(|dd |S )	Nr   Plugins)SearchPathPluginhydrazpkg://hydra.confmainschemazstructured://)
hydra.core.pluginsrh    hydra.plugins.search_path_pluginri   r   appendinstancediscover
isinstancemanipulate_search_path)r\   rh   ri   search_pathsearch_path_pluginsspppluginr"   r"   r#   re      s   re   namec                 C   s   | t jv ot j|  dkS )N1r   )rx   r"   r"   r#   _is_env_set   s   rz   funcc                 C   s.  z|  W S  t y } ztdst r|zt|tr;tjt|t	j
  t|jtr:tjt|jt	j
  n|j}d}|dkr`|d u rIn|j}|j}|d }t|jdkr\n|dksD|dksh|d u rqt  td |}d}|d ur|j}t|}|d ur|jnd}|drn|j}|d }|d usytG dd	 d	}	|}
|	 }|}d}	 |
j|_|
j|_|
j|_||d krn|d }|	 |_|j}|
jd usJ |
j}
qtjd ||d tjd W n t y } ztjdt	j
 t| t	j
  |d }~ww td W Y d }~d S d }~ww )NHYDRA_FULL_ERROR
   r   rH   run_jobrS   z
omegaconf.c                   @   sJ   e Zd ZU dZeed< dZee ed< dZ	ee
 ed< dZee
 ed< dS )z)run_and_report.<locals>.FakeTracebackTypeNtb_nexttb_frametb_lasti	tb_lineno)rC   r)   __qualname__r   r	   __annotations__r   r   r   r   intr   r"   r"   r"   r#   FakeTracebackType  s
   
 r   T)valuetbzM
Set the environment variable HYDRA_FULL_ERROR=1 for a complete stack trace.
z6An error occurred during Hydra's exception formatting:)	Exceptionrz   rc   rr   r   r`   stderrwriterY   r   linesep	__cause__r   __traceback__r   r   r*   getframeinfofunction	traceback	print_excexit	getmodulerC   rV   r   r   r   print_exceptionrepr)r{   exr   
search_maxr<   end
num_framesr-   rx   r   iter_tbfinal_tbcuraddedex2r"   r"   r#   run_and_report   s   
	


	r      argsargs_parserconfig_namecaller_stack_depthc              	      s^  ddl m} ddlm  jd urjjd urj}t|\}}|d u r2|d u r2t|d \}}t||t	| t
|||dfdd}	t|	 t fdd	zЈjrhj|d
 td jd u}
jry|
syjsytdjrj|d
 td j|
 j j jd u }|dkrtd|dkrd_j}jsjrƈj|d}tjj|||d n>jrtfdd	 n9jrtfdd	 n4jrjjjd n/tjd td W |   d S W |   d S W |   d S W |   d S W |   d S |   w )Nr   )GlobalHydrarH   )Hydrar   c                     sX    j d ur*tj j } tj| std|  djdd|  tddd d S d S )NzAdditional config directory 'z' not foundzcommand-linezfile://rl   )provider)r   rI   anchor)
config_dirr   rI   abspathisdirr   prependr   )abs_config_dir)r   rt   r"   r#   add_conf_dirU  s   


z _run_hydra.<locals>.add_conf_dirc                      s    j dS )N)rO   config_search_path)create_main_hydra2r"   )r   rt   rO   r"   r#   <lambda>d  s    z_run_hydra.<locals>.<lambda>)r   r   r   zGThe --resolve flag can only be used in conjunction with --cfg or --helpzTOnly one of --run, --multirun, --cfg, --info and --shell_completion can be specifiedTr   	overridesrunmultirunmoderj   r   r%   r   c                      s   j  j j j jdS )N)r   r   cfg_typepackageresolve)show_cfgr   cfgr   r   r"   r   r   rj   r"   r#   r     s    c                      s   j  jdS )Nr   )shell_completionr   r"   r   r"   r#   r     s    zCommand not specified
)r   N) hydra.core.global_hydrar   rj   r   r   rR   r1   r>   rQ   r   rf   r   helpapp_helpr`   r   r   r   rM   
hydra_helpr   r   r   infor   get_mode_run_app	show_infor   r   rp   clear)r   r   r%   rR   r   r   r   r/   r0   r   has_show_cfgnum_commandsr   run_moder"   )r   r   r   rj   rt   rO   r#   
_run_hydra2  s   






		
!r   r   r   r   rj   r   c                    s   |d u r| rt j}dg n!t j}dg n|r1|t jkr1tjdtd t j}dg |t jkrCt fdd d S t fdd d S )Nzhydra.mode=RUNzhydra.mode=MULTIRUNzK
	Running Hydra app with --multirun, overriding with `hydra.mode=MULTIRUN`.)messagecategoryc                         j  dS N)r   r%   r   )r   r"   r   rj   r   r%   r"   r#   r     
    z_run_app.<locals>.<lambda>c                      r   r   )r   r"   r   r"   r#   r     r   )r   RUNextendMULTIRUNwarningswarnUserWarningr   r   r"   r   r#   r     s(   	
r   c                  C   s6   t jd drdt jd  S tjt jd } | S )Nr   z.pyzpython )r`   argvr_   r   rI   rJ   )
executabler"   r"   r#   _get_exec_command  s   r   c                  C   s   ddl m}  ddlm} |  |}g }|D ]2}t||s J dD ]!}|   d|  d}|	| |	|
|t  q"|	d qdd	d
 |D }|S )Nr   rg   )CompletionPlugin)install	uninstallz - :rS   
c                 S   s   g | ]}|rd | n|qS )z    r"   ).0xr"   r"   r#   
<listcomp>  s    z(_get_completion_help.<locals>.<listcomp>)rm   rh   hydra.plugins.completion_pluginr   rp   rq   
issubclassprovides
capitalizero   r   formatr   r   )rh   r   completion_pluginscompletion_info
plugin_clscmdheadcompletion_helpr"   r"   r#   _get_completion_help  s   
r   c                  C   sP  ddl m}  tjddd}|jdddd	d
 |jdddd
 |jdddd|  d |jdddd |jddg ddd |jdddd
 |jdddd  |jd!d"dd#d
 |jd$d%dd&d
 G d'd( d(}|jd)d*d| d
 |jd+d,d-d  |jd.d/d0d  |jd1d2d3d  |jd4d5d  g d6}|jd7d8d9d:d;|d<d=| d>d? |S )@Nr   )__version__Fr   )add_helpdescriptionz--helpz-h
store_truezApplication's help)actionr   z--hydra-helpzHydra's helpz	--versionversionzShow Hydra's version and exitzHydra )r   r   r   r   *zQAny key=value arguments to override config values (use dots for.nested=overrides))nargsr   z--cfgz-c)jobrj   allz.Show config instead of running [job|hydra|all])choicesr   z	--resolvezNUsed in conjunction with --cfg, resolve config interpolations before printing.z	--packagez-pzConfig package to show)r   z--runz-rz	Run a jobz
--multirunz-mz:Run multiple jobs with the configured launcher and sweeperc                   @   s   e Zd ZdefddZdS )z+get_args_parser.<locals>.LazyCompletionHelpr   c                 S   s   dt   S )Nz'Install or Uninstall shell completion:
)r   )r5   r"   r"   r#   __repr__   s   z4get_args_parser.<locals>.LazyCompletionHelp.__repr__N)rC   r)   r   rY   r   r"   r"   r"   r#   LazyCompletionHelp  s    r   z--shell-completionz-scz--config-pathz-cpzOverrides the config_path specified in hydra.main().
                    The config_path is absolute or relative to the Python file declaring @hydra.main()z--config-namez-cnz3Overrides the config_name specified in hydra.main()z--config-dirz-cdz7Adds an additional config dir to the config search pathz--experimental-rerunz)Rerun a job from a previous config pickle)r   configdefaultszdefaults-treeplugins
searchpathz--infoz-ir   ?storezPrint Hydra information [|])constr   r   r   r   )rS   r   argparseArgumentParseradd_argumentr   )r   parserr   info_choicesr"   r"   r#   get_args_parser  s   	r  c                 C   s   t  j| dS )Nr   )r  
parse_argsr  r"   r"   r#   get_argsV  s   r  matrixc                 C   s\   d}| D ]	}t |t|}qdg| }| D ]}t|D ]\}}t || t|||< qq|S )Nr   )maxrZ   	enumerate)r  num_colsrowwidthsidxcolr"   r"   r#   get_column_widthsZ  s   
r  rI   c                 C   s  | dkrt dddlm} ddlm} dd | dD }|D ]}t|s0td	|  d
d q t|dks9J |d }z||}W n tyb } zt d	|  dt	| d| d |d}~ww t
dt|D ]}|| }zt||}W qj ty }	 zud|d| }
t||rd|d|d  }z||}W W Y d}	~	qj ty } zt d	|  dt	| d| d|
 d |d}~w ty } zt d	|  dt	| |d}~ww t d	|  dt	|	 d| d|
 d |	d}	~	ww |S )z
    Locate an object by name or dotted path, importing as necessary.
    This is similar to the pydoc function `locate`, except that it checks for
    the module from the given path from back to front.
    rS   z
Empty pathr   )import_module)
ModuleTypec                 S   s   g | ]}|qS r"   r"   )r   partr"   r"   r#   r   q  s    z_locate.<locals>.<listcomp>rF   zError loading 'z': invalid dotstring.z$
Relative imports are not supported.z':
z
Are you sure that module 'z' is installed?NrH   z
Are you sure that 'z' is importable from module 'z'?z' is an attribute of ')ImportError	importlibr  typesr  splitrZ   rM   r   r   rangegetattrAttributeErrorr   rr   ModuleNotFoundError)rI   r  r  partsr  part0obj
exc_importmexc_attrparent_dotpathmodr"   r"   r#   _locatef  s   


r+  Tr   popc                 C   s>   d| vrt d|r| d}n| d }t|tst d|S )N_target_z-Input config does not have a `_target_` fieldz$_target_ field type must be a string)r   r,  rr   rY   )r   r,  	classnamer"   r"   r#   _get_cls_name  s   
r/  )r   rd   )T)Er  r*   logging.configloggingr   r`   r   r   dataclassesr   os.pathr   r   r   r   r  r   r   typingr	   r
   r   r   r   omegaconf.errorsr   'hydra._internal.config_search_path_implr   hydra.core.config_search_pathr   r   hydra.core.utilsr   r   hydra.errorsr   r   r   hydra.typesr   r   	getLoggerrC   logrY   r$   r1   r   r>   boolr7   rQ   r]   rc   rf   re   rz   r   	Namespacer  r   r   r   r   r  r  r  r+  r/  r"   r"   r"   r#   <module>   s   



2
	^
|
,	a6