
y Xr                 @   s   d  d l  m 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 d  d l m Z Gd d	   d	 e  Z Gd
 d   d e  Z d S)    )print_functionN)	execfile_)util)Arbiter)Configget_default_config_file)debugc               @   s   e  Z d  Z d Z d 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  |  _ |  j   d  S)N)usagecfgcallableprogloggerdo_load_config)selfr
   r    r   A/home/ubuntu/projects/ifolica/build/gunicorn/gunicorn/app/base.py__init__   s    					zBaseApplication.__init__c             C   s{   y |  j    |  j   Wn\ t k
 rv } z< t d t |  d t j t j j   t j d  WYd d } ~ Xn Xd 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 d |  j |  _ d  S)Nr   )r   r
   r   r   )r   r   r   r   r   *   s    z#BaseApplication.load_default_configc             C   s
   t   d  S)N)NotImplementedError)r   parseroptsargsr   r   r   init.   s    zBaseApplication.initc             C   s
   t   d  S)N)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.
        N)r    )r   r   r   r   r   4   s    zBaseApplication.load_configc             C   s'   |  j    |  j j r# t j   n  d  S)N)r   r   spewr   )r   r   r   r   reload<   s    
zBaseApplication.reloadc             C   s(   |  j  d  k r! |  j   |  _  n  |  j  S)N)r   r%   )r   r   r   r   wsgiA   s    zBaseApplication.wsgic             C   sq   y t  |   j   WnV t k
 rl } z6 t d | d t j t j j   t j d  WYd  d  } ~ Xn Xd  S)Nz
Error: %s
r   r   )r   runRuntimeErrorr   r   r   r   r   )r   r   r   r   r   r)   F   s    zBaseApplication.run)__name__
__module____qualname____doc__r   r   r   r$   r%   r   r'   r(   r)   r   r   r   r   r	      s   r	   c                   s^   e  Z d  Z d d   Z d d   Z d d   Z d d   Z d	 d
   Z   f d d   Z   S)Applicationc             C   s   t  j j |  s% t d |   n  i t d 6d d 6| d 6d  d 6d  d 6} y t | | |  WnM t k
 r t d | d	 t j	 t
 j   t j	 j   t j d
  Yn X| S)Nz%r doesn't exist__builtins__Z
__config__r+   __file__r.   __package__zFailed to read config file: %sr   r   )ospathexistsr*   r0   r   r   r   r   r   	traceback	print_excr   r   )r   filenamer   r   r   r   get_config_from_filenameP   s     

z$Application.get_config_from_filenamec             C   s   t  j |  j S)N)r   import_module__dict__)r   module_namer   r   r   get_config_from_module_namef   s    z'Application.get_config_from_module_namec             C   s  | j  d  r7 | t d  d  } |  j |  } n= | j  d  r_ | t d  d  } n | } |  j |  } x | j   D]y \ } } | |  j j k r q n  y |  j j | j   |  Wq t	 d | | f d t
 j t
 j j     Yq Xq 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lenr=   r9   itemsr   Zsettingssetlowerr   r   r   r   )r   locationr<   r   r8   kvr   r   r   (load_config_from_module_name_or_filenamei   s"    z4Application.load_config_from_module_name_or_filenamec             C   s   |  j  d |  S)NrC   )rF   )r   r8   r   r   r   load_config_from_file   s    z!Application.load_config_from_filec             C   s   |  j  j   } | j   } |  j | | | j  } | r~ | d  k	 r~ x6 | j   D]% \ } } |  j  j | j   |  qR Wn  | j r |  j	 | j  n% t
   } | d  k	 r |  j	 |  n  xZ | j j   D]I \ } } | d  k r q n  | d k r q n  |  j  j | j   |  q Wd  S)Nr#   )r   r!   
parse_argsr$   r#   r@   rA   rB   configrG   r   r;   )r   r!   r#   r   rD   rE   Zdefault_configr   r   r   r      s"     		zApplication.load_configc          
      sB  |  j  j rr y |  j   WnE d } t | d t j t j   t j j   t j	 d  Yn Xt j	 d  n  |  j  j
 r t j
   n  |  j  j r t j |  j  j  n  |  j  j r+|  j  j d  k	 r+|  j  j j d  } xH | D]= } t j j |  } | t j k r t j j d |  q q Wn  t t |   j   d  S)Nz&
Error while loading the application:
r   r   r   ,)r   Zcheck_configr%   r   r   r   r6   r7   r   r   r&   r   daemonr   Z	daemonizeZenable_stdio_inheritance
pythonpathsplitr3   r4   abspathinsertsuperr/   r)   )r   msgpathsr4   rL   )	__class__r   r   r)      s*    
zApplication.run)	r+   r,   r-   r9   r=   rF   rG   r   r)   r   r   )rS   r   r/   N   s   r/   )
__future__r   r3   r   r6   Zgunicorn._compatr   Zgunicornr   Zgunicorn.arbiterr   Zgunicorn.configr   r   r   objectr	   r/   r   r   r   r   <module>   s   =