B
    `.4              
   @   s   d dl Z d dlZd dlZd dlZ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mZ d dlmZ ejddfddZG dd	 d	Zd
d ZdddZedkree pd  dS )    N)AssetResolver)get_config_loader
parse_vars)aslistFc             C   s   t | ||d}| S )N)quietoriginal_ignore_files)PServeCommandrun)argvr   r   command r   ^/home/kop/projects/devel/pgwui/test_venv/lib/python3.7/site-packages/pyramid/scripts/pserve.pymain   s    r   c            	   @   sH  e Zd ZdZdZejeeej	dZ
e
jdddddd	 e
jd
ddddd	 e
jddddd	 e
jddddd e
jddddd e
jdddddd e
jd d!ed"d#d$ee d% d& e
jd'd(d)d*d"d+d, e
jd-d.d/d0d1 e
jd2d3d4d5d1 eeZd/Zed6ejZdDd8d9Zd:d; Zd<d= ZdEd>d?ZdFd@dAZdBdC Zd/S )Gr   z    This command serves a web application that uses a PasteDeploy
    configuration file for the server and application.

    You can also include variable assignments like 'http_port=8080'
    and then use %(http_port)s in your config files.
       )descriptionformatter_classz-nz
--app-nameapp_nameNAMEz)Load the named application (default main))destmetavarhelpz-sz--serverserverZSERVER_TYPEzUse the named server.z--server-nameserver_nameZSECTION_NAMEzIUse the named server as defined in the configuration file (default: main)z--reloadreload
store_truezUse auto-restart file monitor)r   actionr   z--reload-intervalreload_intervalzKSeconds between checking files (low number can cause significant CPU usage))r   defaultr   z-bz	--browserbrowserzOpen a web browser to the server url. The server url is determined from the 'open_url' setting in the 'pserve' section of the configuration file.z-vz	--verboseverbosecountzSet verbose level (default ))r   r   r   r   z-qz--quietstore_constr   zSuppress verbose output)r   constr   r   
config_uri?Nz"The URI to the configuration file.)nargsr   r   config_vars*r   zVariables required by the config file. For example, `http_port=%%(http_port)s` would expect `http_port=8080` to be passed here.z^[a-z][a-z]+:Fc             C   sP   | j |dd  | _|r"d| j_| jjr6||d| _t | _t | _|| _	d S )Nr   r   )r
   r   )
parser
parse_argsargsr   r   worker_kwargssetwatch_filesignore_filesr   )selfr
   r   r   r   r   r   __init__   s    zPServeCommand.__init__c             C   s   | j jdkrt|tjd d S )Nr   )file)r+   r   printsysstderr)r0   msgr   r   r   out   s    zPServeCommand.outc             C   s   t j|jjS )N)ospathabspathuri)r0   loaderr   r   r   get_config_path   s    zPServeCommand.get_config_pathc       
      C   s   | d|}| |}tj|}t|dddd}tt|dddd| _t	d d}xR|D ]J}d|kr||
| }ntj|stj||}| jtj| q`W |d	}	|	r|	| _d S )
NZpserver.    F)flattenr/   )package:open_url)get_settingsr=   r8   r9   dirnamer   getr-   r/   r   resolver:   isabsjoinr.   addrB   )
r0   r<   global_confsettingsconfig_pathherer.   resolverr2   rB   r   r   r   pserve_file_config   s     



z PServeCommand.pserve_file_configc             C   s0   |pd}| d| |}d|kr,djf |S d S )Nr   zserver:portzhttp://127.0.0.1:{port})rC   format)r0   r<   r   rJ   rK   r   r   r   guess_server_url   s    zPServeCommand.guess_server_urlc          
      s  | j js| d dS | j j}t| j j}| j j}| j j}| |}| j jrTt	 r^|
| | j||d | j j}| j jrd}|d kst| j j}n|}|}||kr| |}| j jrt	 s| j  s| |||  s| d n* fdd}	tj|	d}
|
d	 |
  | j jrvt	 sv| j jd
krB| d | j| jd< tjdt| j j| j j| j| jd dS | |}| j| | |}| j| t	 rt  }|t!| j | j"d k	r| j"| jkr| d |#||}|$||}| j jdkr6t%t&dr(dt&'  }nd}| | y|| W nb t(t)fk
r } z>| j jd
krj t*|rdt*| }nd}| d|  W d d }~X Y nX d S )NzYou must give a config file   )rJ   zegg:pyramidzWARNING: could not determine the server's url to open the browser. To fix this set the "open_url" setting in the [pserve] section of the configuration file.c                  s   t d t  d S )Nr   )timesleep
webbrowseropenr   )urlr   r   open_browser   s    
z'PServeCommand.run.<locals>.open_browser)targetTr   zRunning reloading file monitorr   zpyramid.scripts.pserve.main)r   r   r,   r/   r   z^A change to "ignore_files" was detected but it will not take effect until pserve is restarted.getpidzStarting server in PID %i.zStarting server. r>   zExiting%s (-v to see traceback))+r+   r$   r7   r   r'   r   _get_config_loaderr   hupperZ	is_activesetup_loggingrO   r   r   AssertionErrorr   r   rB   rR   	threadingThread	setDaemonstartr   r/   r,   Zstart_reloaderintr   r=   r.   rI   Zget_reloaderlistr   Zget_wsgi_serverZget_wsgi_apphasattrr8   r[   
SystemExitKeyboardInterruptstr)r0   r$   r'   Zapp_specr   r<   r   Zserver_specZserver_loaderrY   trL   Zserver_pathZreloaderr   appr6   er   )rX   r   r	      s    














zPServeCommand.run)FN)N)N)__name__
__module____qualname__r   Zdefault_verbosityargparseArgumentParsertextwrapdedentRawDescriptionHelpFormatterr)   add_argumentrj   staticmethodr   r]   rB   recompileIZ
_scheme_rer1   r7   r=   rO   rR   r	   r   r   r   r   r   !   s   




r   c             K   sV   ddl m} |dd}t|dd}|||| }td||f tjd |  d S )	Nr   )make_serverhostz0.0.0.0rP   i  z$Starting HTTP server on http://%s:%s)r2   )Zwsgiref.simple_serverr{   rE   re   r3   r4   r5   serve_forever)Zwsgi_apprJ   kwr{   r|   rP   r   r   r   r   wsgiref_server_runner%  s    r   	127.0.0.1c          	   C   s  d}|r|pd}d}|s6d|kr2| dd\}}nd}|t|f}i }x*dD ]"}t | }|dk	rLt|||< qLW yd	d
lm} W n  tk
r   d	dlm} Y nX ||| fd|i|}|dk	r"yd	dlm	} d	dl
m} W n. tk
r   d	dlm	} d	dlm} Y nX |  ||||_|r.||_yX|r<dp>d}|dkrdtd|||f tjd ntd|||f tjd |  W n" ttfk
r   |  Y nX |S )a   
    Entry point for CherryPy's WSGI server

    Serves the specified WSGI app via CherryPyWSGIServer.

    ``app``

        The WSGI 'application callable'; multiple WSGI applications
        may be passed as (script_name, callable) pairs.

    ``host``

        This is the ipaddress to bind to (or a hostname if your
        nameserver is properly configured).  This defaults to
        127.0.0.1, which is not a public interface.

    ``port``

        The port to run on, defaults to 8080 for HTTP, or 4443 for
        HTTPS. This can be a string or an integer value.

    ``ssl_pem``

        This an optional SSL certificate file (via OpenSSL) You can
        generate a self-signed test PEM certificate file as follows:

            $ openssl genrsa 1024 > host.key
            $ chmod 400 host.key
            $ openssl req -new -x509 -nodes -sha1 -days 365  \
                          -key host.key > host.cert
            $ cat host.cert host.key > host.pem
            $ chmod 400 host.pem

    ``protocol_version``

        The protocol used by the server, by default ``HTTP/1.1``.

    ``numthreads``

        The number of worker threads to create.

    ``server_name``

        The string to set for WSGI's SERVER_NAME environ entry.

    ``max``

        The maximum number of queued requests. (defaults to -1 = no
        limit).

    ``request_queue_size``

        The 'backlog' argument to socket.listen(); specifies the
        maximum number of queued connections.

    ``timeout``

        The timeout in seconds for accepted connections.
    Fi[  TrA   r   i  )
numthreadsmaxrequest_queue_sizetimeoutNr   )Server)CherryPyWSGIServerr   )get_ssl_adapter_class)BuiltinSSLAdapterhttpshttpz0.0.0.0z/serving on 0.0.0.0:%s view at %s://127.0.0.1:%s)r2   zserving on %s://%s:%s)splitre   localsZcheroot.wsgir   ImportErrorZcherrypy.wsgiserverr   Zcheroot.serverr   Zcheroot.ssl.builtinr   Zcherrypy.wsgiserver.ssl_builtinZssl_adapterprotocolr3   r4   r5   rd   ri   rh   stop)rl   rJ   r|   rP   Zssl_pemZprotocol_versionr   r   r   r   r   Zis_sslZ	bind_addrkwargsvar_namevarZ
WSGIServerr   r   r   r   r   r   r   cherrypy_server_runner2  sX    H





r   __main__)
Nr   NNNNNNNN)rq   r^   r8   rx   r4   rs   ra   rT   rV   Zpyramid.pathr   Zpyramid.scripts.commonr   r   Zpyramid.settingsr   r
   r   r   r   r   rn   exitr   r   r   r   <module>   s8              
w