a
    ¥Q•h�B  ã                   @   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 ræd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�r´e,ƒ  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   ú?/var/www/sistema_ama/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r²tj	|dd	�d }| d
¡rš|d| d
¡ 7 }d|fg}|  
|||¡S |  || j ¡}d| dg ¡v oÞt |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   )ÚAllowz	GET, HEADÚ	PATH_INFOr   éÿÿÿÿú/F)Zinclude_queryÚQUERY_STRINGú?ÚLocationÚgzipZHTTP_ACCEPT_ENCODINGz.gzÚDatezLast-ModifiedZETagZHTTP_IF_MODIFIED_SINCEZHTTP_IF_NONE_MATCHÚ*zContent-Type)zContent-EncodingrJ   )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_typeÚ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/   rf   r   r   r   rP   Ä   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/   rg   r   r   r   rQ   È   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_typerq   r   r   r   rW   Ð   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ÚstrrY   rZ   ©r/   rg   r5   Úmtimer   r   r   rX   Ô   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   rx   r!   ry   )Úgetattrrp   rO   Úendswithr=   r_   )	r/   r7   r   ri   ÚDEFAULTZCONFIG_ITEMSÚconfigÚkeyÚvaluer   r   r   ra   Ù   s    ÿþzCling._add_headersc                 C   s
   t |dƒS )ú#Return the appropriate file object.r(   )r)   rq   r   r   r   r^   å   s    zCling._file_likec                 C   s   |  dt¡}||| jƒS )ú1Return an iterator over the body of the response.úwsgi.file_wrapper)rO   Úiter_and_closeÚ
block_size)r/   rg   r5   rk   Úway_to_sendr   r   r   rb   é   s    zCling._bodyN)r   r   r   r9   r„   rU   r,   rR   r]   rT   rN   r0   r8   rP   rQ   rW   rX   ra   r^   rb   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)rk   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/   rg   Ú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 )rm   N)r<   r   rV   r�   Únew_path)r/   rf   rg   r“   r   r   r   rP   .  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”   rr   rs   Úold_pathr’   r   r   r   rW   :  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   rt   ru   rY   rZ   )r/   rg   r5   r“   rw   r   r   r   rX   C  s
    

zShock._conditionsc                 C   s.   |   |¡}|dur | || j¡S t|dƒS dS )r€   Nr(   )r”   rk   r   r)   r’   r   r   r   r^   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  rM   zShock._body.<locals>.<listcomp>r‚   )r”   ÚbodyrO   rƒ   r„   )r/   rg   r5   rk   r“   r…   r   rš   r   rb   T  s    
ÿzShock._bodyN)r   r   r   r9   r�   r”   rP   rW   rX   r^   rb   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   rV   r•   rq   r   r   r   rV   m  s    zBaseMagic.existsc                 C   s
   || j  S )z#Add the self.extension to the path.)Ú	extensionrq   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   rq   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)r{   rž   rq   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).)Úintr[   ru   rY   rZ   rv   r   r   r   r—   ‚  s    zBaseMagic.conditionsc                 C   s
   t ||ƒS )zReturn a file object for path.)r+   )r/   rg   r   r   r   r   rk   ‡  s    zBaseMagic.file_likec                 C   s
   |  ¡ gS )r�   )r†   )r/   r5   rk   r   r   r   rœ   ‹  s    zBaseMagic.bodyN)r   r   r   r9   rž   rV   r•   r–   r‘   r—   rk   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 )zÎPass 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   rk   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   rg   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generateÚrender)r/   r5   rg   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 }q�t|ƒdkrp|\}}q�t d	¡ nt|ƒdkr�d
\}}|s˜d}|s d}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ÚKeyboardInterruptre   Ú
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/pubÚ	localhostr¸   zCiao, baby!)
Zwsgiref.validaterÈ   r¡   rª   r°   r;   r   r¼   r½   re   )rÈ   r�   rÆ   r   r   r   Útestõ  s    ÿrË   Ú__main__).r9   rr   Úemail.utilsÚutilsrY   r[   r#   r   Úosr   r   Zwsgirefr   Zwsgiref.headersr   Zwsgiref.simple_serverr   Úoptparser   Z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$
