a
    1WB                     @   s  d Z ddlZddlmZ ddlZddlZddlZddlm	Z	m
Z
 ddlmZ ddlmZ ddlmZ ddlmZ zddlmZmZ W n   Y n0 zddlZW n   Y n0 zdd	lmZ W n   Y n0 ejd
k rddlZdd Zndd ZG dd deZdd Zdd Zdd Z G dd dZ!G dd de"Z#dd Z$dd Z%G dd de#Z&G d d! d!e"Z'G d"d# d#e'Z(G d$d% d%e(Z)G d&d' d'e(Z*d(d) Z+d*d+ Z,e-d,kre,  dS )-z4
Copyright (C) 2012 Roman Mohr <roman@fenkhuber.at>
    N)pathstat)util)Headers)make_server)OptionParser)resource_filenameRequirement)MarkupTemplate3c                 C   s   t | d S )Nr   )codecsunicode_escape_decodex r   K/var/www/viveiro_mudafortebrasil/venv/lib/python3.9/site-packages/static.pyu>   s    r   c                 C   s   | S Nr   r   r   r   r   r   A   s    c                   @   s   e Zd ZdS )
MagicErrorN)__name__
__module____qualname__r   r   r   r   r   E   s   r   c                 C   sB   t jd dkr| j|ddS ttdt| kr:| |} | S d S )Nr      strictencodingerrors )sysversion_infoencodetyper   stringr   r   r   r   _encodeI   s
    
r$   c                 C   s$   t jd dkr| j|ddS | S d S )Nr   r   r   r   )r   r   decoder"   r   r   r   _decodeR   s    r&   c                 C   s,   t jd dkrt| d|ddS t| dS d S )Nr   r   rr   r   rb)r   r   open)filenamer   r   r   r   _openY   s    r+   c                   @   s.   e Zd ZdZde fddZg fddZdS )	StatusAppz-Used by WSGI apps to return some HTTP status.Nc                 C   s&   || _ || _|d u r|| _n|| _d S r   )statusr   message)selfr-   r.   r   r   r   r   __init__d   s
    zStatusApp.__init__c                 C   sP   | j rt|dd || j| |d dkr<td| jgS t| j | jgS d S )NzContent-type
text/plainREQUEST_METHODHEADr   )r.   r   
add_headerr-   r$   r   )r/   environstart_responseheadersr   r   r   __call__l   s    zStatusApp.__call__)r   r   r   __doc__r   getdefaultencodingr0   r8   r   r   r   r   r,   `   s   r,   c                   @   s   e Zd ZdZdZdZedZeddZedZ	e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d ZdS )Clinga=  A stupidly simple way to serve static content via WSGI.

    Serve the file of the same path as PATH_INFO in self.datadir.

    Look up the Content-type in self.content_types by extension
    or use 'text/plain' if the extension is not found.

    Serve up the contents of the file or delegate to self.not_found.
    i   z
index.htmlz404 Not Foundz304 Not Modifiedr   z301 Moved Permanentlyz405 Method Not Allowedc                 K   s2   || _ t | _| D ]\}}t| || qdS )z8Just set the root and any other attribs passes via **kw.N)rootr   r:   r   itemssetattr)r/   r<   kwkvr   r   r   r0      s    
zCling.__init__c              
   C   sD  |d dvr dg}|  |||S |dd}| |}| |sL| ||S t|r|d dksl|| jkrtj	|dd	d }|d
r|d|d
 7 }d|fg}| 
|||S | || j }d|dg v ot|d }|r|d7 }| |}z| ||\}	}
dtt fd|
fd|	fg}|d}|r`t|t|
kr`| |||W S |d}|r|dks|	|v r| |||W S | |}|d|f |r|ddg | ||| |d| |d dkr| |||W S dgW S W n> ttfy> } z t| | ||W  Y d}~S d}~0 0 dS )z7Respond to a request when called in the usual WSGI way.r2   )GETr3   )ZAllowz	GET, HEADZ	PATH_INFOr   /F)Zinclude_queryZQUERY_STRING?ZLocationgzipZHTTP_ACCEPT_ENCODINGz.gzZDatezLast-ModifiedZETagZHTTP_IF_MODIFIED_SINCEZHTTP_IF_NONE_MATCH*zContent-Type)zContent-EncodingrF   )ZVaryzAccept-Encodingz200 OKrB       N)method_not_allowedget
_full_path_is_under_root	not_foundr   isdirr<   r   Zrequest_urimoved_permanently
index_fileexists_guess_type_conditionsrfc822
formatdatetime	parsedatenot_modified
_file_likeappendextend_add_headers_bodyIOErrorOSErrorprint)r/   r5   r6   r7   	path_info	full_pathlocationZ	prezippedcontent_typeZetagZlast_modifiedZif_modifiedZif_none	file_likeer   r   r   r8      sb    









zCling.__call__c                 C   s
   | j | S )(Return the full path from which to read.)r<   )r/   ra   r   r   r   rK      s    zCling._full_pathc                 C   s0   t |t j t | jt j r(dS dS dS )z'Guard against arbitrary file retrieval.TFN)r   abspathsep
startswithr<   r/   rb   r   r   r   rL      s
    zCling._is_under_rootc                 C   s   t |d pdS )z/Guess the mime type using the mimetypes module.r   r1   )	mimetypes
guess_typerk   r   r   r   rR      s    zCling._guess_typec                 C   s   t |j}t|t|fS )z9Return a tuple of etag, last_modified by mtime from stat.)r   st_mtimestrrT   rU   r/   rb   r5   mtimer   r   r   rS      s    
zCling._conditionsc           	      C   s~   d}g d}t | dg D ]`}||d|sP||d|ksP||d|r| D ]\}}||vrX|||f qXqd S )NZ__static_no_match__)prefixr!   extr7   rr   r!   rs   )getattrrj   rJ   endswithr=   rZ   )	r/   r7   r   rd   DEFAULTZCONFIG_ITEMSconfigkeyvaluer   r   r   r\      s    zCling._add_headersc                 C   s
   t |dS )#Return the appropriate file object.r(   )r)   rk   r   r   r   rY      s    zCling._file_likec                 C   s   | dt}||| jS )1Return an iterator over the body of the response.wsgi.file_wrapper)rJ   iter_and_close
block_size)r/   rb   r5   re   way_to_sendr   r   r   r]      s    zCling._bodyN)r   r   r   r9   r~   rP   r,   rM   rX   rO   rI   r0   r8   rK   rL   rR   rS   r\   rY   r]   r   r   r   r   r;   v   s    

3r;   c                 c   sB   z|  |}|r|V  ntW q  ty:   |   Y dS 0 q dS )z1Yield file contents by block then close the file.N)readStopIterationclose)re   r~   blockr   r   r   r}      s    
r}   c                 K   s    t | }tt||fi |S )as  Return a Cling that serves from the given package and dir_name.

    This uses pkg_resources.resource_filename which is not the
    recommended way, since it extracts the files.

    I think this works fine unless you have some _very_ serious
    requirements for static content, in which case you probably
    shouldn't be serving it through a WSGI app, IMHO. YMMV.
    )r	   parser;   r   )package_namedir_namer?   resourcer   r   r   
cling_wrap   s    

r   c                   @   sD   e Zd ZdZdZdd Zdd Zdd Zd	d
 Zdd Z	dd Z
dS )Shocka=  A stupidly simple way to serve up mixed content.

    Serves static content just like Cling (it's superclass)
    except that it process content with the first matching
    magic from self.magics if any apply.

    See Cling and classes with "Magic" in their names in this module.

    If you are using Shock with the StringMagic class for instance:

    shock = Shock('/data', magics=[StringMagic(food='cheese')])

    Let's say you have a file called /data/foo.txt.stp containing one line:

    "I love to eat $food!"

    When you do a GET on /foo.txt you will see this in your browser:

    "I love to eat cheese!"

    This is really nice if you have a color variable in your css files or
    something trivial like that. It seems silly to create or change a
    handful of objects for a couple of dynamic bits of text.
    r   c                 C   s"   | j D ]}||r|  S qdS )z6Return the first magic that matches this path or None.N)magicsmatchesr/   rb   magicr   r   r   _match_magic(  s    

zShock._match_magicc                 C   sJ   | j | }t|r|S | jD ]"}t||r||  S q|S dS )rg   N)r<   r   rQ   r   new_path)r/   ra   rb   r   r   r   r   rK   .  s    


zShock._full_pathc                 C   s@   |  |}|dur*t||d p(dS t|d p:dS dS )z<Guess the mime type magically or using the mimetypes module.Nr   r1   )r   rl   rm   old_pathr   r   r   r   rR   :  s    
zShock._guess_typec                 C   s>   |  |}|dur|||S t|j}t|t|fS dS )z>Return Etag and Last-Modified values defaults to now for both.N)r   
conditionsr   rn   ro   rT   rU   )r/   rb   r5   r   rq   r   r   r   rS   C  s
    

zShock._conditionsc                 C   s.   |  |}|dur ||| jS t|dS dS )rz   Nr(   )r   re   r   r)   r   r   r   r   rY   L  s    
zShock._file_likec                    sH     |}|dur, fdd|||D S |dt}|| jS dS )r{   Nc                    s   g | ]}t | jqS r   )r$   r   ).0sr/   r   r   
<listcomp>X  rH   zShock._body.<locals>.<listcomp>r|   )r   bodyrJ   r}   r~   )r/   rb   r5   re   r   r   r   r   r   r]   T  s    
zShock._bodyN)r   r   r   r9   r   r   rK   rR   rS   rY   r]   r   r   r   r   r     s   		r   c                   @   sL   e Zd ZdZ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 )	BaseMagicaD  Base class for magic file handling.

    Really a do nothing if you were to use this directly.

    In a strait forward case you would just override .extension and body().
    (See StringMagic in this module for a simple example of subclassing.)

    In a more complex case you may need to override many or all methods.
    r   c                 C   s   t | |r| |S dS )z+Check that self.new_path(full_path) exists.N)r   rQ   r   rk   r   r   r   rQ   m  s    zBaseMagic.existsc                 C   s
   || j  S )z#Add the self.extension to the path.)	extensionrk   r   r   r   r   r  s    zBaseMagic.new_pathc                 C   s*   |  |r|dt| j  S tddS )z4Remove self.extension from path or raise MagicError.NzPath does not match this magic.)r   lenr   r   rk   r   r   r   r   v  s    
zBaseMagic.old_pathc                 C   s   | | jr|S dS )z)Check that path ends with self.extension.N)ru   r   rk   r   r   r   r   }  s    zBaseMagic.matchesc                 C   s   t t }t|t|fS )z6Return Etag and Last-Modified values (based on mtime).)intrV   ro   rT   rU   rp   r   r   r   r     s    zBaseMagic.conditionsc                 C   s
   t ||S )zReturn a file object for path.)r+   )r/   rb   r   r   r   r   re     s    zBaseMagic.file_likec                 C   s
   |  gS )r{   )r   )r/   r5   re   r   r   r   r     s    zBaseMagic.bodyN)r   r   r   r9   r   rQ   r   r   r   r   re   r   r   r   r   r   r   _  s   
r   c                   @   s(   e Zd ZdZdZdZdd Zdd ZdS )	StringMagiczkMagic to replace variables in file contents using string.Template.

    Using this requires Python2.4.
    z.stpFc                 K   s
   || _ dS )z*Keyword arguments populate self.variables.N)	variables)r/   r   r   r   r   r0     s    zStringMagic.__init__c                 C   sH   |  }|| j t| }| jdu r8||gS ||gS dS )zPass environ and self.variables in to template.

        self.variables overrides environ so that suprises in environ don't
        cause unexpected output if you are passing a value in explicitly.
        TN)	copyupdater   r#   Templater   safesafe_substitute
substitute)r/   r5   re   r   templater   r   r   r     s    
zStringMagic.bodyN)r   r   r   r9   r   r   r0   r   r   r   r   r   r     s
   r   c                   @   s   e Zd ZdZdZdd ZdS )KidMagicztLike StringMagic only using the Kid templating language.

    Using this requires Kid: http://kid.lesscode.org/
    z.kidc                 C   s"   t jf ||d| j}| gS )4Pass environ and **self.variables into the template.)filer5   )kidr   r   	serialize)r/   r5   rb   r   r   r   r   r     s    zKidMagic.bodyNr   r   r   r9   r   r   r   r   r   r   r     s   r   c                   @   s   e Zd ZdZdZdd ZdS )GenshiMagicz`Like StringMagic only using the Genshi templating language.

    Using this requires Genshi
    z.genshic                 C   s:   t | }| j }||d< |jf i |jdddgS )r   r5   html)doctype)r
   r   r   r   ZgenerateZrender)r/   r5   rb   r   r   r   r   r   r     s    
zGenshiMagic.bodyNr   r   r   r   r   r     s   r   c                  C   s0  t ddd} |  \}}t|dv rt|dkr||d d}t|dkrZ|d }d }qt|dkrp|\}}qtd	 nt|dkrd
\}}|sd}|sd}zt|}W n   td	 Y n0 t|d }zt|||	  W n. t
y   td Y n   td Y n0 n| tj td d S )Nz%prog DIR [HOST][:][PORT]zstatic 0.3.6)usageversion)   r   r   r   :r   z Invalid host:port specification.)NNz0.0.0.0'  z
Cio, baby!zProblem initializing server.)r   
parse_argsr   splitr   exitr   r;   r   serve_foreverKeyboardInterruptr`   
print_helpstderr)parseroptionsargspartshostportappr   r   r   command  s@    
r   c                  C   sh   ddl m}  tddtddtddf}td}ztdd	| |  W n tyb   t	d
 Y n0 d S )Nr   )	validatorzString Test)titlezKid TestzGenshi Testztestdata/pubZ	localhostr   zCiao, baby!)
Zwsgiref.validater   r   r   r   r;   r   r   r   r`   )r   r   r   r   r   r   test  s    r   __main__).r9   rl   email.utilsutilsrT   rV   r#   r   osr   r   Zwsgirefr   Zwsgiref.headersr   Zwsgiref.simple_serverr   optparser   pkg_resourcesr   r	   r   Zgenshi.templater
   r   r   r   	Exceptionr   r$   r&   r+   r,   objectr;   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   sV   

	yT1$
