U
    ‰dî  ã                   @   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mZ d dlmZ G dd„ deƒZG dd	„ d	eƒZdS )
é    N)Úutil)ÚArbiter)ÚConfigÚget_default_config_file)Údebugc                   @   sZ   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ ZdS )ÚBaseApplicationzw
    An application interface for configuring and loading
    the various necessities for any given web framework.
    Nc                 C   s*   || _ d | _d | _|| _d | _|  ¡  d S ©N)ÚusageÚcfgÚcallableÚprogÚloggerÚdo_load_config)Úselfr	   r   © r   ú5/tmp/pip-unpacked-wheel-1tw_qy24/gunicorn/app/base.pyÚ__init__   s    zBaseApplication.__init__c              
   C   sf   z|   ¡  |  ¡  W nL tk
r` } z.tdt|ƒ tjd tj ¡  t d¡ W 5 d}~X Y nX dS )z)
        Loads the configuration
        z

Error: %s©Úfileé   N)	Úload_default_configÚload_configÚ	ExceptionÚprintÚstrÚsysÚstderrÚflushÚexit©r   Úer   r   r   r      s    
zBaseApplication.do_load_configc                 C   s   t | j| jd| _d S )N)r   )r   r	   r   r
   ©r   r   r   r   r   *   s    z#BaseApplication.load_default_configc                 C   s   t ‚d S r   ©ÚNotImplementedError)r   ÚparserÚoptsÚargsr   r   r   Úinit.   s    zBaseApplication.initc                 C   s   t ‚d S r   r"   r!   r   r   r   Úload1   s    zBaseApplication.loadc                 C   s   t ‚dS )zÆ
        This method is used to load the configuration from one or several input(s).
        Custom Command line, configuration file.
        You have to override this method in your class.
        Nr"   r!   r   r   r   r   4   s    zBaseApplication.load_configc                 C   s   |   ¡  | jjrt ¡  d S r   )r   r
   Úspewr   r!   r   r   r   Úreload<   s    zBaseApplication.reloadc                 C   s   | j d kr|  ¡ | _ | j S r   )r   r(   r!   r   r   r   ÚwsgiA   s    

zBaseApplication.wsgic              
   C   s^   zt | ƒ ¡  W nH tk
rX } z*td| tjd tj ¡  t d¡ W 5 d }~X Y nX d S )Nz
Error: %s
r   r   )r   ÚrunÚRuntimeErrorr   r   r   r   r   r   r   r   r   r,   F   s    
zBaseApplication.run)NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r'   r(   r   r*   r+   r,   r   r   r   r   r      s   
r   c                       sL   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Z‡ fdd„Z	‡  Z
S )ÚApplicationc                 C   s2   t  | jj¡ | jjtjkr.tj d| jj¡ d S )Nr   )ÚosÚchdirr
   r   ÚpathÚinsertr!   r   r   r   r4   T   s    zApplication.chdirc                 C   sä   t j |¡std| ƒ‚t j |¡d }zpd}|dkrFtj ||¡}n.d}t |¡ tj	 
||¡}tjj|||d}tj |¡}|tj|< |j |¡ W nB tk
rÚ   td| tjd t ¡  tj ¡  t d¡ Y nX t|ƒS )	Nz%r doesn't existr   Z
__config__)z.pyz.pycz9configuration file should have a valid Python extension.
)ÚloaderzFailed to read config file: %sr   )r3   r5   Úexistsr-   ÚsplitextÚ	importlibr   Úspec_from_file_locationÚwarnÚ	machineryÚSourceFileLoaderÚmodule_from_specr   Úmodulesr7   Úexec_moduler   r   r   Ú	tracebackÚ	print_excr   r   Úvars)r   ÚfilenameÚextÚmodule_nameÚspecÚmsgZloader_Úmodr   r   r   Úget_config_from_filename]   s(    


z$Application.get_config_from_filenamec                 C   s   t t |¡ƒS r   )rD   r:   Úimport_module)r   rG   r   r   r   Úget_config_from_module_namex   s    z'Application.get_config_from_module_namec              	   C   sÂ   |  d¡r&|tdƒd… }|  |¡}n*|  d¡rB|tdƒd… }n|}|  |¡}| ¡ D ]d\}}|| jjkrnqXz| j | ¡ |¡ W qX t	k
rº   t
d||f tjd tj ¡  ‚ Y qXX qX|S )zÃ
        Loads the configuration file: the file is a python file, otherwise raise an RuntimeError
        Exception or stop the process if the configuration file contains a syntax error.
        zpython:Nzfile:zInvalid value for %s: %s
r   )Ú
startswithÚlenrM   rK   Úitemsr
   ÚsettingsÚsetÚlowerr   r   r   r   r   )r   ÚlocationrG   r
   rE   ÚkÚvr   r   r   Ú(load_config_from_module_name_or_filename{   s"    




z4Application.load_config_from_module_name_or_filenamec                 C   s   | j |dS )N)rT   )rW   )r   rE   r   r   r   Úload_config_from_file˜   s    z!Application.load_config_from_filec                 C   s(  | j  ¡ }| ¡ }|  |||j¡}|  ¡  |rR| ¡ D ]\}}| j  | ¡ |¡ q6| | j  	¡ ¡}|j
rv|  |j
¡ n,|j
rŠ|  |j
¡ ntƒ }|d k	r¢|  |¡ t|ƒ ¡ D ].\}}|d krÀq®|dkrÊq®| j  | ¡ |¡ q®t|ƒ ¡ D ]0\}}|d krüqê|dkrqê| j  | ¡ |¡ qê|  ¡  d S )Nr&   )r
   r$   Ú
parse_argsr'   r&   r4   rP   rR   rS   Zget_cmd_args_from_envÚconfigrX   r   rD   )r   r$   r&   r
   rU   rV   Zenv_argsZdefault_configr   r   r   r   ›   s8    


zApplication.load_configc                    sô   | j jrt| j ƒ | j js"| j jr|z|  ¡  W nB tk
rp   d}t|tjd t 	¡  tj 
¡  t d¡ Y nX t d¡ | j jrŒt ¡  | j jr¢t | j j¡ | j jræ| j j d¡}|D ](}tj |¡}|tjkr¼tj d|¡ q¼tƒ  ¡  d S )Nz&
Error while loading the application:
r   r   r   ú,)r
   Zprint_configr   Zcheck_configr(   r   r   r   rB   rC   r   r   r)   r   Údaemonr   Z	daemonizeZenable_stdio_inheritanceÚ
pythonpathÚsplitr3   r5   Úabspathr6   Úsuperr,   )r   rI   Úpathsr5   r]   ©Ú	__class__r   r   r,   Ê   s.    



zApplication.run)r.   r/   r0   r4   rK   rM   rW   rX   r   r,   Ú__classcell__r   r   rb   r   r2   O   s   	/r2   )Úimportlib.utilr:   Zimportlib.machineryr3   r   rB   Zgunicornr   Zgunicorn.arbiterr   Zgunicorn.configr   r   r   Úobjectr   r2   r   r   r   r   Ú<module>   s   >