B
    `                 @   s   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
 ejdfddZG dd	 d	Zed
kr~ee pzd  dS )    N)unquote)Request)get_config_loader
parse_varsFc             C   s   t | |}| S )N)PRequestCommandrun)argvquietcommand r   `/home/kop/projects/devel/pgwui/test_venv/lib/python3.7/site-packages/pyramid/scripts/prequest.pymain   s    
r   c               @   s   e 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d e	j
dddddddddddg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/d0d1d+ eeZejZd9d3d4Zd5d6 Zd7d8 Zd)S ):r   a      Submit a HTTP request to a web application.

    This command makes an artificial request to a web application that uses a
    PasteDeploy (.ini) configuration file for the server and application.

    Use "prequest config.ini /path" to request "/path".

    Use "prequest --method=POST config.ini /path < data" to do a POST with
    the given request body.

    Use "prequest --method=PUT config.ini /path < data" to do a
    PUT with the given request body.

    Use "prequest --method=PATCH config.ini /path < data" to do a
    PATCH with the given request body.

    Use "prequest --method=OPTIONS config.ini /path" to do an
    OPTIONS request.

    Use "prequest --method=PROPFIND config.ini /path" to do a
    PROPFIND request.

    If the path is relative (doesn't begin with "/") it is interpreted as
    relative to "/".  The path passed to this script should be URL-quoted.
    The path can be succeeded with a query string (e.g. '/path?a=1&=b2').

    The variable "environ['paste.command_request']" will be set to "True" in
    the request's WSGI environment, so your application can distinguish these
    calls from normal requests.
    )descriptionformatter_classz-nz
--app-nameapp_nameNAMEz@Load the named application from the config file (default 'main'))destmetavarhelpz--headerheadersz
NAME:VALUEappendzAHeader to add to request (you can use this option multiple times))r   r   actionr   z-dz--display-headersdisplay_headers
store_truez3Display status and headers before the response body)r   r   r   z-mz--methodmethodGETHEADPOSTPUTPATCHDELETEZPROPFINDOPTIONSzFRequest method type (GET, POST, PUT, PATCH, DELETE, PROPFIND, OPTIONS))r   choicesr   z-lz--loginloginz&HTTP basic auth username:password pair)r   r   
config_uri?Nz"The URI to the configuration file.)nargsdefaultr   	path_infozThe path of the request.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.Fc             C   s    || _ | j|dd  | _d S )N   )r	   parser
parse_argsargs)selfr   r	   r   r   r   __init__}   s    zPRequestCommand.__init__c             C   s   | j st| d S )N)r	   print)r/   msgr   r   r   out   s    zPRequestCommand.outc             C   sH  | j jr| j js| d dS | j j}t| j j}| j j}| |}|| || j j	|}|
dspd| }y|dd\}}W n tk
r   d}Y nX t|}i }| j jrt| j jd}d|d |d	< | j jr,xH| j jD ]<}	d
|	kr| d|	  dS |	d
d\}
}| ||
< qW | j jp8d }|d|ddddddddtjdd|ddd}|dkr| j|d< d|d< xF| D ]:\}
}|
 dkrd}
nd|
 dd  }
|||
< qW tj||d!}||}| j j r"| |j! x&|j"D ]\}
}| d"|
|f  qW |j#r8| |j$ n| |j% d#S )$Nz'You must provide at least two arguments   /r%   r+    asciizBasic Authorization:z>Bad --header=%s option, value must be in the form 'name:value'r   	localhost80zHTTP/1.0z
text/plainz	127.0.0.1TFhttp)r+   r   ztext/plain;q=1.0, */*;q=0.1)REQUEST_METHODZSCRIPT_NAMEZ	PATH_INFOZSERVER_NAMEZSERVER_PORTZSERVER_PROTOCOLCONTENT_TYPEZREMOTE_ADDRzwsgi.run_oncezwsgi.multithreadzwsgi.multiprocesszwsgi.errorszwsgi.url_schemezwsgi.versionQUERY_STRINGZHTTP_ACCEPTzpaste.command_request)r   r   r   z
wsgi.inputz-1CONTENT_LENGTHzcontent-typer>   ZHTTP_-_)environz%s: %sr   )&r.   r$   r(   r3   r   r)   _get_config_loadersetup_loggingZget_wsgi_appr   
startswithsplit
ValueErrorr   r#   base64	b64encodeencodedecoder   stripr   uppersysstderrstdinitemslowerreplacer   ZblankZget_responser   statusZ
headerlistcharsetZubodybody)r/   r$   r)   pathloaderZappqsr   encitemnamevaluerequest_methodrC   requestresponser   r   r   r      s    










zPRequestCommand.run)F)__name__
__module____qualname__r   argparseArgumentParsertextwrapdedentRawDescriptionHelpFormatterr,   add_argumentstaticmethodr   rD   rO   rQ   r0   r3   r   r   r   r   r   r      sr   

r   __main__)re   rI   rO   rg   urllib.parser   Zpyramid.requestr   Zpyramid.scripts.commonr   r   r   r   r   rb   exitr   r   r   r   <module>   s    K