๓
าbc           @`  si  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 Wn; e k
 rี e j j d  e j j   e j e  n Xy d  d l m Z Wn! e k
 rd  d l m Z n Xy e Wn e k
 r/e Z n Xg  Z  e j! d	  Z" e j! d
 e j#  Z$ d   Z% e  j& d  d   Z' e  j& d  d   Z( e  j& d  d   Z) e  j& d  d   Z* e  j& d  d d e+ d  Z, e  j& d  d d e+ d  Z- e  j& d  d d  Z. e  j& d  d d  Z/ e  j& d  e0 e+ d  Z1 e  j& d   d e+ d!  Z3 e  j& d"  e+ d#  Z4 e  j& d$  d%   Z5 e  j& d&  d'   Z6 e  j& d(  d)   Z7 e  j& d*  d+   Z8 e  j& d,  d-   Z9 e  j& d.  d/   Z: e  j& d0  d1   Z; e  j& d2  d3   Z< e  j& d4  d5   Z= e  j& d6  d7   Z> e  j& d8  d S(9   i    (   t   absolute_importt   divisionN(   t   timegm(   t   debugt   warningt   errori   (   t	   EX_OSFILEu*  
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
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
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
(   t   quotes   \.[0-9]*(?:[Z\-\+]*?)s9   ^(<?[^>]+?>\s*|\s*)(<\w+) xmlns=['"](https?://[^'"]+)['"]c         C`  s(   t  j d |   }  t j j |  d t S(   Ns   .000t   fuzzy(   t   RE_S3_DATESTRINGt   subt   dateutilt   parsert   parset   True(   t   date(    (    sL   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/BaseUtils.pyt   dateS3toPython?   s    R   c         C`  s   t  t |   j    S(   N(   R   R   t   utctimetuple(   R   (    (    sL   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/BaseUtils.pyt   dateS3toUnixF   s    R   c         C`  s   t  j j |  d t S(   sV   
    Convert a string formated like '2020-06-27T15:56:34Z' into a python datetime
    R   (   R   R   R   R   (   R   (    (    sL   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/BaseUtils.pyt   dateRFC822toPythonL   s    R   c         C`  s   t  t |   j    S(   N(   R   R   R   (   R   (    (    sL   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/BaseUtils.pyt   dateRFC822toUnixT   s    R   c         C`  s%   t  j j |  d t } | j d  S(   NR   s   %Y-%m-%d %H:%M(   R   R   R   R   t   strftime(   t   s3timestampt   date_obj(    (    sL   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/BaseUtils.pyt   formatDateTimeY   s    R   s   UTF-8t   replacec         C`  so   t  |   t k r |  S| s3 t d |  | f  n  y t |  | |  SWn! t k
 rj t d |    n Xd S(   s<   
    Convert 'string' to Unicode or raise an exception.
    s   Unicodising %r using %ss    Conversion to unicode failed: %rN(   t   typet   unicodeR   t   UnicodeDecodeError(   t   stringt   encodingt   errorst   silent(    (    sL   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/BaseUtils.pyt   base_unicodiseb   s    R!   c         C`  so   t  |   t k r |  S| s3 t d |  | f  n  y |  j | |  SWn! t k
 rj t d |    n Xd S(   s   
    Convert unicode 'string' to <type str>, by default replacing
    all invalid characters with '?' or raise an exception.
    s   DeUnicodising %r using %ss"   Conversion from unicode failed: %rN(   R   R   R   t   encodet   UnicodeEncodeError(   R   R   R   R    (    (    sL   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/BaseUtils.pyt   base_deunicodiser   s    R$   c         C`  s   t  |  d | t  S(   sE   
    Convert S3 UTF-8 'string' to Unicode or raise an exception.
    s   UTF-8(   R!   R   (   R   R   (    (    sL   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/BaseUtils.pyt   decode_from_s3   s    R%   c         C`  s   t  |  d | t  S(   s   
    Convert Unicode to S3 UTF-8 'string', by default replacing
    all invalid characters with '?' or raise an exception.
    s   UTF-8(   R$   R   (   R   R   (    (    sL   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/BaseUtils.pyt   encode_to_s3   s    R&   c         C`  sX   | r d } n d } t  |   }  t |  d | }  | rH t |   }  n t  |   }  |  S(   s  
    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.
    t   ~s   ~/t   safe(   R&   R   R%   (   t   paramt   quote_backslashest   unicode_outputt
   safe_chars(    (    sL   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/BaseUtils.pyt   s3_quote   s    
	R-   c         C`  s`   t  |   }  | d k r |  St |  d d } t d |  | f  | rR t |  St  |  Sd  S(   Nt   verbatimR(   s   ~/s   String '%s' encoded to '%s'(   R&   R   R   R%   (   R   t   urlencoding_modeR+   t   encoded(    (    sL   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/BaseUtils.pyt   base_urlencode_stringฌ   s    
R1   c         C`  sด   d } d } x~ |  D]v } t  |  } | d k r\ | d t t  d  |  7} | d 7} q | d k r | d 7} | d 7} q | | 7} q W| rฐ | rฐ t d	 | | f  n  | S(
   s   
    replace_nonprintables(string)

    Replaces all non-printable characters 'ch' in 'string'
    where ord(ch) <= 26 with ^@, ^A, ... ^Z
    t    i    i   t   ^t   @i   i   s   ^?s+   %d non-printable characters replaced in: %s(   t   ordt   chrR   (   R   t   with_messaget
   new_stringt   modifiedt   ct   o(    (    sL   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/BaseUtils.pyt   base_replace_nonprintablesผ   s    
R<   c         C`  sธ   g  } xซ |  D]ฃ } i  } x~ | D]v } t  | j  } t |  rW t | g  | | <q  | j d | j  } | d  k	 r t  |  | | <q  d  | | <q  W| r | j |  q q W| S(   Ns   .//%s(   R%   t   tagt   lent
   parseNodest   findtextt   Nonet   append(   t   nodest   retvalt   nodet   retval_itemt   childt   namet
   found_text(    (    sL   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/BaseUtils.pyR?   ุ   s    R?   c         C`  s   t  j j j |   } | j   S(   N(   t   xmlt   domt   minidomt   parseStringt   toprettyxml(   t   xmlstrt	   xmlparser(    (    sL   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/BaseUtils.pyt   getPrettyFromXml๐   s    RQ   c         C`  sL   t  j |   } | r< | j d  } t  j d |  d  }  n d } |  | f S(   sy   
    removeNameSpace(xml) -- remove top-level AWS namespace
    Operate on raw byte(utf-8) xml string. (Not unicode)
    i   s   \1\2i   N(   t   RE_XML_NAMESPACEt   matcht   groupR
   RA   (   RJ   t   xmlns_matcht   xmlns(    (    sL   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/BaseUtils.pyt   stripNameSpace๗   s    RW   c         C`  sy   t  t |    \ }  } y- t j |   } | r@ | | j d <n  | SWn- t k
 rt } t d |  t |     n Xd  S(   NRV   s   Error parsing xml: %s(   RW   R&   t   ETt
   fromstringt   attribt	   ExceptionR   (   RJ   RV   t   treet   e(    (    sL   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/BaseUtils.pyt   getTreeFromXml  s    
R^   c         C`  s)   t  |   } | j d |  } t |  S(   Ns   .//%s(   R^   t   findallR?   (   RJ   RE   R\   RC   (    (    sL   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/BaseUtils.pyt   getListFromXml  s    R`   c         C`  sษ   i  } xผ |  D]ด } t  |  r. t |  } n$ | j d  k	 rL t | j  n d  } t | j  } | | k rฑ t | |  t k s | | g | | <n  | | j | pช d  q | pบ d | | <q W| S(   NR2   (	   R>   t   getDictFromTreet   textRA   R%   R=   R   t   listRB   (   R\   t   ret_dictRG   t   contentt	   child_tag(    (    sL   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/BaseUtils.pyRa     s    $Ra   c         C`  sk   t  |   } | j j |  r> | j d  k	 r: t | j  Sd  S| j |  } | d  k	 rc t |  Sd  Sd  S(   N(   R^   R=   t   endswithRb   RA   R%   R@   (   RJ   t   xpathR\   t   result(    (    sL   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/BaseUtils.pyt   getTextFromXml.  s
     Rj   c         C`  s,   t  |   } | j d  k	 r( t | j  Sd  S(   N(   R^   R=   RA   R%   (   RJ   R\   (    (    sL   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/BaseUtils.pyt   getRootTagName8  s    Rk   c         C`  s"   t  j |   } t |  | _ | S(   N(   RX   t   ElementR%   Rb   (   t   tag_nameRb   t   el(    (    sL   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/BaseUtils.pyt   xmlTextNode>  s    Ro   c         C`  s    t  |  |  } | j |  | S(   sฏ   
    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.
    (   Ro   RB   (   Rm   Rb   t   parentRn   (    (    sL   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/BaseUtils.pyt   appendXmlTextNodeE  s    Rq   (?   t
   __future__R    R   t   ret   syst   calendarR   t   loggingR   R   R   t   xml.dom.minidomRJ   t   xml.etree.ElementTreet   etreet   ElementTreeRX   t	   ExitCodesR   t   dateutil.parserR   t   ImportErrort   stderrt   writet   flusht   exitt   urllibR   t   urllib.parseR   t	   NameErrort   strt   __all__t   compileR	   t	   MULTILINERR   R   RB   R   R   R   R   t   FalseR!   R$   R%   R&   R   R-   RA   R1   R<   R?   RQ   RW   R^   R`   Ra   Rj   Rk   Ro   Rq   (    (    (    sL   /oak/stanford/groups/akundaje/marinovg/programs/s3cmd-master/S3/BaseUtils.pyt   <module>	   s   	

															
