B
    `#                 @   s   d dl Z d dlmZ d dlm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 d dlmZmZ ejdfd	d
ZefddZG dd dZedkree pd  dS )    N)interact)contextmanager)	bootstrap)get_config_loader
parse_vars)aslist)DottedNameResolvermake_contextmanagerFc             C   s   t | |}| S )N)PShellCommandrun)argvquietcommand r   ^/home/kop/projects/devel/pgwui/test_venv/lib/python3.7/site-packages/pyramid/scripts/pshell.pymain   s    
r   c             C   s6   d}dt jt j|f }|d| d 7 }||| d d S )Nz!Type "help" for more information.zPython %s on %s
%sz


)local)sysversionplatform)envhelpr   ZcprtZbannerr   r   r   python_shell_runner   s    r   c               @   s  e Zd ZdZeeZeeZeZej	e
eejdZejddddddd	 ejd
ddddd ejdddd ejddddd ejddddd eZi Zi Zg ZdZejdZedZd/dd Zd!d" Zd#d$ Zd0d%d&Zed'd( Z d)d* Z!d+d, Z"d-d. Z#dS )1r
   a      Open an interactive shell with a Pyramid app loaded.  This command
    accepts one positional argument named "config_uri" which specifies the
    PasteDeploy config file to use for the interactive shell. The format is
    "inifile#name". If the name is left off, the Pyramid default application
    will be assumed.  Example: "pshell myapp.ini#main".

    If you do not point the loader directly at the section of the ini file
    containing your Pyramid application, the command will attempt to
    find the app for you. If you are loading a pipeline that contains more
    than one Pyramid application within it, the loader will use the
    last one.
    )descriptionformatter_classz-pz--python-shellstorepython_shell z_Select the shell to use. A list of possible shells is available using the --list-shells option.)actiondestdefaultr   z-lz--list-shellslist
store_truezList all available shells.)r    r   r   z--setupsetupzA callable that will be passed the environment before it is made available to the shell. This option will override the 'setup' key in the [pshell] ini section.)r    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PYTHONSTARTUPFc             C   s    || _ | j|dd  | _d S )N   )r   parser
parse_argsargs)selfr   r   r   r   r   __init__f   s    zPShellCommand.__init__c             C   s   | d|}i | _i | _d | _x^| D ]R\}}|dkr@|| _q(|dkr^dd t|D | _q(| j|| j|< || j|< q(W d S )NZpshellr$   Zdefault_shellc             S   s   g | ]}|  qS r   )lower).0xr   r   r   
<listcomp>s   s    z4PShellCommand.pshell_file_config.<locals>.<listcomp>)	Zget_settingsloaded_objectsobject_helpr$   itemsr   preferred_shellsresolvermaybe_resolve)r.   loaderdefaultssettingskvr   r   r   pshell_file_configj   s    z PShellCommand.pshell_file_configc             C   s   | j st| d S )N)r   print)r.   msgr   r   r   outx   s    zPShellCommand.outc          
   C   s   | j jr|  S | j js&| d dS | j j}t| j j}| |}|| | 	|| | j
||d| _| jd| _zl|d kry|  }W n0 tk
r } z| t| dS d }~X Y nX |   || j| j W d Q R X W d |   X d S )NzRequires a config file argument   )optionscloserr*   )r-   r"   show_shellsr%   rB   r   r(   r   setup_loggingr?   r   r   poprE   
make_shell
ValueErrorstr	setup_envr   )r.   shellr%   r(   r:   er   r   r   r   |   s,    



zPShellCommand.runc       
   
   c   s  | j }t|}d|d< d|d< d|d< d|d< d	|d
< || j x| jD ]}||krJ||= qJW | jjrr| jj| _| jr| j| j| _| }t	| j}||< xP|
 D ]D\}}||ks||| krt|ddr|jdd||< q|||< qW ~d}|r8|d7 }x*t| D ]}|d||| f 7 }qW | jrx|d7 }x.t| j D ]}|d|| j| f 7 }qXW | jrtj| jrt| jd}	t|	 d| W d Q R X d|kr|d= | | _d V  W d Q R X d S )NzThe WSGI application.Zappz"Root of the default resource tree.rootzActive Pyramid registry.registryzActive request object.requestz+Default root factory used to create `root`.Zroot_factory__doc__Fr    r   zEnvironment:z
  %-12s %sz

Custom Variables:rbzutf-8__builtins__)r   dictupdater4   r-   r$   r8   r9   copyr	   r6   getattrrR   replacesortedkeysr5   	pystartupospathisfileopenexecreaddecodestripr   )
r.   r   Zenv_helpr=   Zorig_envZsetup_managerr>   r   varfpr   r   r   rL      sP    




zPShellCommand.setup_envc             C   sH   |   }t| dd d}| d x|D ]}| d|f  q,W dS )Nc             S   s   |   S )N)r0   )r2   r   r   r   <lambda>       z+PShellCommand.show_shells.<locals>.<lambda>)keyzAvailable shells:z  %sr   )find_all_shellsr[   r\   rB   )r.   shellsZsorted_namesnamer   r   r   rF      s    

zPShellCommand.show_shellsc             C   s8   | j }i }x(|dD ]}|j}| }|||< qW |S )Nzpyramid.pshell_runner)pkg_resourcesiter_entry_pointsrm   load)r.   rn   rl   eprm   Zshell_factoryr   r   r   rk      s    zPShellCommand.find_all_shellsc                s   |   }d }| jj }|sx| js8dd | D t  fdd}t| |d}t|dkr|d d }n*|	|}|d k	r|}|d krt
d| |d kr| j}|S )	Nc             S   s   g | ]}|d kr|qS )pythonr   )r1   r=   r   r   r   r3      s    z,PShellCommand.make_shell.<locals>.<listcomp>c                s0   y | d    S  tk
r*   dS X d S )Nr   r*   )indexr0   rJ   )r2   )
max_weightr7   r   r   order   s    z'PShellCommand.make_shell.<locals>.order)rj   r   r*   z!could not find a shell named "%s")rk   r-   r   r0   r7   r\   lenr[   r6   getrJ   default_runner)r.   rl   rM   Z
user_shellru   Zsorted_shellsrunnerr   )rt   r7   r   rI      s*    

zPShellCommand.make_shell)F)N)$__name__
__module____qualname__r   staticmethodr   r   rn   argparseArgumentParsertextwrapdedentRawDescriptionHelpFormatterr+   add_argumentr   rx   r4   r5   r7   r$   r^   environrw   r]   r   r8   r/   r?   rB   r   r   rL   rF   rk   rI   r   r   r   r   r
      sb   


 ?	
r
   __main__)r~   coder   
contextlibr   r^   rn   r   r   Zpyramid.pasterr   Zpyramid.scripts.commonr   r   Zpyramid.settingsr   Zpyramid.utilr   r	   r   r   r   r
   rz   exitr   r   r   r   <module>   s"     