a
    ((aV                     @   s   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 d dl	m
Z
 d dlmZmZmZmZmZ ddlmZ edZeed	d
dZejee ed dddZeee ddddZeddddZeee dddZeddddZdS )    N)import_module)set_start_method)Path)Any	GeneratorListOptionalSized   )run_processzwatchgod.cli)dotted_pathreturnc              
   C   s   z|  ddd\}}W n4 tyN } ztd| |W Y d}~n
d}~0 0 t|}zt||W S  ty } ztd|||W Y d}~n
d}~0 0 dS )z
    Stolen approximately from django. Import a dotted module path and return the attribute/class designated by the
    last name in the path. Raise ImportError if the import fails.
     .r
   z$"{}" doesn't look like a module pathNz,Module "{}" does not define a "{}" attribute)striprsplit
ValueErrorImportErrorformatr   getattrAttributeError)r   module_path
class_nameemodule r   S/Users/bob/PycharmProjects/fastapi/venv/lib/python3.9/site-packages/watchgod/cli.pyimport_string   s    &r   )NNN)tty_pathr   c                 c   sb   | rXz8t | }|t_d V  W d    n1 s00    Y  W q^ tyT   d V  Y q^0 nd V  d S N)opensysstdinOSError)r   ttyr   r   r   set_tty!   s    
(r%   )functionr   r   c                 C   s:   t | t| }|  W d    n1 s,0    Y  d S r   )r%   r   )r&   r   funcr   r   r   run_function0   s    
r(   )changesr   c                 C   s   t dt|  d S )Nz%d files changed, reloading)loggerinfolen)r)   r   r   r   callback6   s    r-   )r&   r   c                 C   sl   |  ddd }tjj| d }tj|}ttjD ],\}}|dv r8|gtj|d d    S q8|gS )z^
    Remove watchgod-related arguments from sys.argv and prepend with func's script path.
    r   Nz.py>   -a--argsr
   )splitospathjoinabspath	enumerater!   argv)r&   Zbases_baseiargr   r   r   sys_argv:   s    r;   )args_r   c               
   G   s  | pt jdd  }tjddd}|jddd |jdd	d
dd |jdd	tddd |jddtg dd |jddtjdd ||}t	j
t	jt	jd|j }t	 }|| |t	jddd t	d}|| || t jt  zt|j W nF tyF } z,td|t jd t d W Y d }~d S d }~0 0 t|j}|  s~td|t jd t d d S |! }zt"t j#$ }	W n. t%y   d}	Y n t&y   d }	Y n0 t'(d||j t)d  t*|jt _d!d" |j+D }
t,|t-|j|	ft.d#|
id$ d S )%Nr
   Zwatchgodz;Watch a directory and execute a python function on changes.)progdescriptionr&   z#Path to python function to execute.)helpr3   ?r   z8Filesystem path to watch, defaults to current directory.)nargsdefaultr?   z--verbosityz0, 1 (default) or 2)rA   typerB   r?   z--ignore-paths*z=Specify paths to files or directories to ignore their updatesr0   r/   z]Arguments for argparser inside executed function. Ex.: module.func path --args --inner arg -v)rA   r?   )r   r
      z[%(asctime)s] %(message)sz%H:%M:%S)fmtdatefmtzImportError: {})filezpath "{}" does not existz/dev/ttyz.watching "%s" and reloading "%s" on changes...spawnc                 S   s   h | ]}t t| qS r   )strr   resolve).0pr   r   r   	<setcomp>       zcli.<locals>.<setcomp>ignored_paths)argsr-   Zwatcher_kwargs)/r!   r7   argparseArgumentParseradd_argumentintrJ   Z	REMAINDER
parse_argsloggingWARNINGINFODEBUG	verbosityStreamHandlersetLevelsetFormatter	Formatter	getLogger
addHandlerr3   appendr2   getcwdr   r&   r   printr   stderrexitr   existsrK   ttynamer"   filenor#   r   r*   r+   r   r;   Zignore_pathsr   r(   r-   )r<   rQ   parserZarg_namespace	log_levelhdlrZ	wg_loggerr   r3   r   rP   r   r   r   cliG   st    









rm   )rR   
contextlibrW   r2   r!   	importlibr   multiprocessingr   pathlibr   typingr   r   r   r   r	   mainr   r`   r*   rJ   r   contextmanagerr%   r(   r-   r;   rm   r   r   r   r   <module>   s"   
