o
    Š©Zhè  ã                   @   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   úH/var/www/html/lang_env/lib/python3.10/site-packages/gunicorn/app/base.pyÚ__init__   s   zBaseApplication.__init__c              
   C   sh   z|   ¡  |  ¡  W dS  ty3 } ztdt|ƒ tjd tj ¡  t d¡ W Y d}~dS d}~ww )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   ó   t ‚r   ©ÚNotImplementedError)r   ÚparserÚoptsÚargsr   r   r   Úinit.   ó   zBaseApplication.initc                 C   r"   r   r#   r!   r   r   r   Úload1   r)   zBaseApplication.loadc                 C   r"   )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.
        r#   r!   r   r   r   r   4   s   zBaseApplication.load_configc                 C   s    |   ¡  | jjrt ¡  d S d S r   )r   r
   Úspewr   r!   r   r   r   Úreload<   s   ÿzBaseApplication.reloadc                 C   s   | j d u r
|  ¡ | _ | j S r   )r   r*   r!   r   r   r   ÚwsgiA   s   

zBaseApplication.wsgic              
   C   s`   z	t | ƒ ¡  W d S  ty/ } ztd| tjd tj ¡  t d¡ W Y d }~d S d }~ww )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   s6   t  | jj¡ | jjtjvrtj d| jj¡ d S d S )Nr   )ÚosÚchdirr
   r   ÚpathÚinsertr!   r   r   r   r6   T   s   ÿzApplication.chdirc                 C   sæ   t j |¡std| ƒ‚t j |¡d }z;d}|dv r#tj ||¡}nd}t |¡ tj	 
||¡}tjj|||d}tj |¡}|tj|< |j |¡ W t|ƒS  tyr   td| tjd t ¡  tj ¡  t d¡ Y t|ƒS w )	Nz%r doesn't existr   Z
__config__)z.pyz.pycz9configuration file should have a valid Python extension.
)ÚloaderzFailed to read config file: %sr   )r5   r7   Úexistsr/   ÚsplitextÚ	importlibr   Úspec_from_file_locationÚwarnÚ	machineryÚSourceFileLoaderÚmodule_from_specr   Úmodulesr9   Ú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   )rF   r<   Úimport_module)r   rI   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¡r!|tdƒd… }n|}|  |¡}| ¡ D ].\}}|| jjvr7q,z| j | ¡ |¡ W q, t	yZ   t
d||f tjd tj ¡  ‚ w |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ÚlenrO   rM   Úitemsr
   ÚsettingsÚsetÚlowerr   r   r   r   r   )r   ÚlocationrI   r
   rG   Ú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)rV   )rY   )r   rG   r   r   r   Úload_config_from_file˜   s   z!Application.load_config_from_filec                 C   s&  | j  ¡ }| ¡ }|  |||j¡}|  ¡  |r)| ¡ D ]\}}| j  | ¡ |¡ q| | j  	¡ ¡}|j
r;|  |j
¡ n|j
rE|  |j
¡ ntƒ }|d urQ|  |¡ t|ƒ ¡ D ]\}}|d u r`qW|dkreqW| j  | ¡ |¡ qWt|ƒ ¡ D ]\}}|d u r~qu|dkrƒqu| j  | ¡ |¡ qu|  ¡  d S )Nr'   )r
   r%   Ú
parse_argsr(   r'   r6   rR   rT   rU   Zget_cmd_args_from_envÚconfigrZ   r   rF   )r   r%   r'   r
   rW   rX   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 n  ty7   d}t|tjd t 	¡  tj 
¡  t d¡ Y nw t d¡ | j jrEt ¡  | j jr`tj d¡rYd}t|tjdd t | j j¡ | j jr‚| j j d	¡}|D ]}tj |¡}|tjvrtj d|¡ qmtƒ  ¡  d S )
Nz&
Error while loading the application:
r   r   r   ZNOTIFY_SOCKETz]Warning: you shouldn't specify `daemon = True` when launching by systemd with `Type = notify`T)r   r   ú,)r
   Zprint_configr   Zcheck_configr*   r   r   r   rD   rE   r   r   r+   r   Údaemonr5   ÚenvironÚgetr   Z	daemonizeZenable_stdio_inheritanceÚ
pythonpathÚsplitr7   Úabspathr8   Úsuperr.   )r   rK   Úpathsr7   ra   ©Ú	__class__r   r   r.   Ê   s8   

û

€zApplication.run)r0   r1   r2   r6   rM   rO   rY   rZ   r   r.   Ú__classcell__r   r   rf   r   r4   O   s    	/r4   )Úimportlib.utilr<   Zimportlib.machineryr5   r   rD   Zgunicornr   Zgunicorn.arbiterr   Zgunicorn.configr   r   r   Úobjectr   r4   r   r   r   r   Ú<module>   s   >