o
    ߤh                     @   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   O/var/www/html/ictaz-jobs/venv/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   
__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   modulesr:   exec_moduler   r   r   	traceback	print_excr   r   vars)r   filenameextmodule_namespecmsgloader_modr   r   r   get_config_from_filename]   s,   


z$Application.get_config_from_filenamec                 C   s   t t|S r   )rG   r=   import_module)r   rJ   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lenrQ   rO   itemsr
   settingssetlowerr   r   r   r   r   )r   locationrJ   r
   rH   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)rX   )r[   )r   rH   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rqu| j | | qu|   d S )Nr'   )r
   r%   
parse_argsr(   r'   r6   rT   rV   rW   get_cmd_args_from_envconfigr\   r   rG   )r   r%   r'   r
   rY   rZ   env_args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   NOTIFY_SOCKETz]Warning: you shouldn't specify `daemon = True` when launching by systemd with `Type = notify`T)r   r   ,)r
   print_configr   check_configr*   r   r   r   rE   rF   r   r   r+   r   daemonr5   environgetr   	daemonizeenable_stdio_inheritance
pythonpathsplitr7   abspathr8   superr.   )r   rL   pathsr7   rk   	__class__r   r   r.      s8   



zApplication.run)r0   r1   r2   r6   rO   rQ   r[   r\   r   r.   __classcell__r   r   rp   r   r4   O   s    	/r4   )importlib.utilr=   importlib.machineryr5   r   rE   gunicornr   gunicorn.arbiterr   gunicorn.configr   r   r   objectr   r4   r   r   r   r   <module>   s   >