3
bl%                 @   s  d dl mZmZ d dlZd dlZd dlmZ d dlmZm	Z	m
Z
 d dlZd dljjZddlmZ yd dlZW n4 ek
r   ejjd ejj  eje Y nX yd dlmZ W n  ek
r   d dlmZ Y nX ye W n ek
r   eZY nX g Z ej!d	Z"ej!d
ej#Z$dd Z%e j&d dd Z'e j&d dd Z(e j&d dd Z)e j&d dd Z*e j&d d;ddZ+e j&d d<ddZ,e j&d d=ddZ-e j&d d>ddZ.e j&d d?d!d"Z/e j&d" d@d#d$Z0e j&d$ dAd%d&Z1e j&d& d'd( Z2e j&d( d)d* Z3e j&d* d+d, Z4e j&d, d-d. Z5e j&d. d/d0 Z6e j&d0 d1d2 Z7e j&d2 d3d4 Z8e j&d4 d5d6 Z9e j&d6 d7d8 Z:e j&d8 d9d: Z;e j&d: dS )B    )absolute_importdivisionN)timegm)debugwarningerror   )	EX_OSFILEa*  
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
ImportError trying to import dateutil.parser.
Please install the python dateutil module:
$ sudo apt-get install python-dateutil
  or
$ sudo yum install python-dateutil
  or
$ pip install python-dateutil
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
)quotez\.[0-9]*(?:[Z\-\+]*?)s9   ^(<?[^>]+?>\s*|\s*)(<\w+) xmlns=['"](https?://[^'"]+)['"]c             C   s   t jd| } tjj| ddS )Nz.000T)fuzzy)RE_S3_DATESTRINGsubdateutilparserparse)date r   L/oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/BaseUtils.pydateS3toPython?   s    r   c             C   s   t t| j S )N)r   r   utctimetuple)r   r   r   r   dateS3toUnixF   s    r   c             C   s   t jj| ddS )zV
    Convert a string formated like '2020-06-27T15:56:34Z' into a python datetime
    T)r   )r   r   r   )r   r   r   r   dateRFC822toPythonL   s    r   c             C   s   t t| j S )N)r   r   r   )r   r   r   r   dateRFC822toUnixT   s    r   c             C   s   t jj| dd}|jdS )NT)r   z%Y-%m-%d %H:%M)r   r   r   strftime)Zs3timestampZdate_objr   r   r   formatDateTimeY   s    r   UTF-8replaceFc             C   sV   t | tkr| S |s$td| |f  yt| ||S  tk
rP   td|  Y nX dS )z<
    Convert 'string' to Unicode or raise an exception.
    zUnicodising %r using %sz Conversion to unicode failed: %rN)typeunicoder   UnicodeDecodeError)stringencodingerrorssilentr   r   r   base_unicodiseb   s    r$   c             C   sV   t | tkr| S |s$td| |f  y| j||S  tk
rP   td|  Y nX dS )z
    Convert unicode 'string' to <type str>, by default replacing
    all invalid characters with '?' or raise an exception.
    zDeUnicodising %r using %sz"Conversion from unicode failed: %rN)r   r   r   encodeUnicodeEncodeError)r    r!   r"   r#   r   r   r   base_deunicodiser   s    r'   c             C   s   t | d|dS )zE
    Convert S3 UTF-8 'string' to Unicode or raise an exception.
    zUTF-8T)r$   )r    r"   r   r   r   decode_from_s3   s    r(   c             C   s   t | d|dS )z
    Convert Unicode to S3 UTF-8 'string', by default replacing
    all invalid characters with '?' or raise an exception.
    zUTF-8T)r'   )r    r"   r   r   r   encode_to_s3   s    r)   Tc             C   s<   |r
d}nd}t | } t| |d} |r0t| } nt | } | S )a  
    URI encode every byte. UriEncode() must enforce the following rules:
    - URI encode every byte except the unreserved characters: 'A'-'Z', 'a'-'z', '0'-'9', '-', '.', '_', and '~'.
    - The space character is a reserved character and must be encoded as "%20" (and not as "+").
    - Each URI encoded byte is formed by a '%' and the two-digit hexadecimal value of the byte.
    - Letters in the hexadecimal value must be uppercase, for example "%1A".
    - Encode the forward slash character, '/', everywhere except in the object key name.
    For example, if the object key name is photos/Jan/sample.jpg, the forward slash in the key name is not encoded.
    ~z~/)safe)r)   r
   r(   )paramZquote_backslashesunicode_outputZ
safe_charsr   r   r   s3_quote   s    

r.   c             C   sH   t | } |dkr| S t| dd}td| |f  |r<t|S t |S d S )Nverbatimz~/)r+   zString '%s' encoded to '%s')r)   r
   r   r(   )r    urlencoding_moder-   Zencodedr   r   r   base_urlencode_string   s    r1   c             C   s   d}d}x`| D ]X}t |}|dkrD|dtt d|  7 }|d7 }q|dkr^|d7 }|d7 }q||7 }qW |r|rtd	||f  |S )
z
    replace_nonprintables(string)

    Replaces all non-printable characters 'ch' in 'string'
    where ord(ch) <= 26 with ^@, ^A, ... ^Z
     r      ^@r      z^?z+%d non-printable characters replaced in: %s)ordchrr   )r    Zwith_messageZ
new_stringZmodifiedcor   r   r   base_replace_nonprintables   s    


r;   c             C   s   g }x|| D ]t}i }x\|D ]T}t |j}t|r>t|g||< q|jd|j }|d k	rdt |||< qd ||< qW |r
|j| q
W |S )Nz.//%s)r(   taglen
parseNodesfindtextappend)nodesretvalnodeZretval_itemchildnameZ
found_textr   r   r   r>      s    


r>   c             C   s   t jjj| }|j S )N)xmlZdomZminidomZparseStringZtoprettyxml)ZxmlstrZ	xmlparserr   r   r   getPrettyFromXml   s    rG   c             C   s4   t j| }|r(|jd}t jd| d} nd}| |fS )zy
    removeNameSpace(xml) -- remove top-level AWS namespace
    Operate on raw byte(utf-8) xml string. (Not unicode)
       z\1\2r   N)RE_XML_NAMESPACEmatchgroupr   )rF   Zxmlns_matchxmlnsr   r   r   stripNameSpace   s    

rM   c             C   sh   t t| \} }ytj| }|r*||jd< |S  tk
rb } ztd| t|   W Y d d }~X nX d S )NrL   zError parsing xml: %s)rM   r)   ETZ
fromstringZattrib	Exceptionr   )rF   rL   treeer   r   r   getTreeFromXml  s    


rR   c             C   s   t | }|jd| }t|S )Nz.//%s)rR   findallr>   )rF   rC   rP   rA   r   r   r   getListFromXml  s    rT   c             C   s   i }x| D ]~}t |r t|}n|jd k	r4t|jnd }t|j}||kr|t|| tksh|| g||< || j|pvd q
|pd||< q
W |S )Nr2   )r=   getDictFromTreetextr(   r<   r   listr@   )rP   Zret_dictrD   ZcontentZ	child_tagr   r   r   rU     s    


rU   c             C   sN   t | }|jj|r,|jd k	r(t|jS d S |j|}|d k	rFt|S d S d S )N)rR   r<   endswithrV   r(   r?   )rF   ZxpathrP   resultr   r   r   getTextFromXml.  s
    
rZ   c             C   s    t | }|jd k	rt|jS d S )N)rR   r<   r(   )rF   rP   r   r   r   getRootTagName8  s    r[   c             C   s   t j| }t||_|S )N)rN   ZElementr(   rV   )tag_namerV   elr   r   r   xmlTextNode>  s    

r^   c             C   s   t | |}|j| |S )z
    Creates a new <tag_name> Node and sets
    its content to 'text'. Then appends the
    created Node to 'parent' element if given.
    Returns the newly created Node.
    )r^   r@   )r\   rV   parentr]   r   r   r   appendXmlTextNodeE  s    

r`   )r   r   F)r   r   F)r   )r   )TF)NF)F)<
__future__r   r   resysZcalendarr   loggingr   r   r   Zxml.dom.minidomrF   xml.etree.ElementTreeZetreeZElementTreerN   	ExitCodesr	   Zdateutil.parserr   ImportErrorstderrwriteflushexitZurllibr
   Zurllib.parser   	NameErrorstr__all__compiler   	MULTILINErI   r   r@   r   r   r   r   r$   r'   r(   r)   r.   r1   r;   r>   rG   rM   rR   rT   rU   rZ   r[   r^   r`   r   r   r   r   <module>	   s   
































