B
    }<[a"                 @   s  d Z ddlZddlmZmZ ddlZddlZddlZddlm	Z	m
Z
 ddlZddlmZ e Zdd ZedejZd	d
 ZedZdddZdd ZG dd dejZdddZG dd deZG dd deZejZej Z ej!Z!ej"Z"ej#Z#ej$Z$ej%Z%dS )z
    babel.util
    ~~~~~~~~~~

    Various utility classes and functions.

    :copyright: (c) 2013-2018 by the Babel Team.
    :license: BSD, see LICENSE for more details.
    N)	timedeltatzinfo)izipimap)	localtimec             c   s4   t  }x(t| D ]}||kr|V  || qW dS )a  Yield all items in an iterable collection that are distinct.

    Unlike when using sets for a similar effect, the original ordering of the
    items in the collection is preserved by this function.

    >>> print(list(distinct([1, 2, 1, 3, 4, 4])))
    [1, 2, 3, 4]
    >>> print(list(distinct('foobar')))
    ['f', 'o', 'b', 'a', 'r']

    :param iterable: the iterable collection providing the data
    N)setiteradd)iterableseenitem r   ?lib/python3.7/site-packages/Babel-2.6.0-py3.7.egg/babel/util.pydistinct   s
    r   s(   [ \t\f]* \# .* coding[=:][ \t]*([-\w.]+)c          
   C   s   |   }| d z|  }|tj}|r>|ttjd }t|}|syddl	}|
|d W n tttfk
r   Y nX |  }t|}|r|r|dd}|dkrtd|dS |r|ddS dS W d| | X dS )a/  Deduce the encoding of a source file from magic comment.

    It does this in the same way as the `Python interpreter`__

    .. __: https://docs.python.org/3.4/reference/lexical_analysis.html#encoding-declarations

    The ``fp`` argument should be a seekable file object.

    (From Jeff Dairiki)
    r   Nzlatin-1   zutf-8zencoding problem: {0} with BOM)tellseekreadline
startswithcodecsBOM_UTF8lenPYTHON_MAGIC_COMMENT_rematchparserZsuitedecodeImportErrorSyntaxErrorUnicodeEncodeErrorgroupformat)fpposZline1Zhas_bommr   Zline2Zmagic_comment_encodingr   r   r   parse_encoding0   s8    


r$   z'from\s+__future__\s+import\s+\(*(.+)\)*latin-1c       
   	   C   s   ddl }|  }| d d}z|  |}tdd|}tdd|}tdd|}xTt|D ]F}d	d
 |	d
dD }x&|D ]}t||d}	|	r||	jO }qW qdW W d| | X |S )zRParse the compiler flags by :mod:`__future__` from the given Python
    code.
    r   Nzimport\s*\([\r\n]+zimport (z,\s*[\r\n]+z, z\\\s*[\r\n]+ c             S   s   g | ]}|   d qS )z())strip).0xr   r   r   
<listcomp>|   s    z&parse_future_flags.<locals>.<listcomp>r   ,)
__future__r   r   readr   resubPYTHON_FUTURE_IMPORT_refinditerr   splitgetattrZcompiler_flag)
r!   encodingr,   r"   flagsZbodyr#   namesnameZfeaturer   r   r   parse_future_flagsf   s"    

r8   c             C   s   ddddddd}g }xHt td| D ]4\}}|d	 rH|||  q(|r(|t| q(W td
|d |tj	d}|dk	S )a  Extended pathname pattern matching.

    This function is similar to what is provided by the ``fnmatch`` module in
    the Python standard library, but:

     * can match complete (relative or absolute) path names, and not just file
       names, and
     * also supports a convenience pattern ("**") to match files at any
       directory level.

    Examples:

    >>> pathmatch('**.py', 'bar.py')
    True
    >>> pathmatch('**.py', 'foo/bar/baz.py')
    True
    >>> pathmatch('**.py', 'templates/index.html')
    False

    >>> pathmatch('**/templates/*.html', 'templates/index.html')
    True
    >>> pathmatch('**/templates/*.html', 'templates/foo/bar.html')
    False

    :param pattern: the glob pattern
    :param filename: the path name of the file to match against
    z[^/]z[^/]/z[^/]+z[^/]+/z	(?:.+/)*?z(?:.+/)*?[^/]+)?z?/*z*/z**/z**z	([?*]+/?)    $/N)
	enumerater.   r2   appendescaper   joinreplaceossep)patternfilenameZsymbolsZbufidxpartr   r   r   r   	pathmatch   s     rJ   c               @   s   e Zd ZedZdS )TextWrapperz((\s+|(?<=[\w\!\"\'\&\.\,\?])-{2,}(?=\w))N)__name__
__module____qualname__r.   compileZ
wordsep_rer   r   r   r   rK      s   rK   F   r<   c             C   s   t |||dd}|| S )a  Simple wrapper around the ``textwrap.wrap`` function in the standard
    library. This version does not wrap lines on hyphens in words.

    :param text: the text to wrap
    :param width: the maximum line width
    :param initial_indent: string that will be prepended to the first line of
                           wrapped output
    :param subsequent_indent: string that will be prepended to all lines save
                              the first of wrapped output
    F)widthinitial_indentsubsequent_indentZbreak_long_words)rK   Zwrap)textrQ   rR   rS   wrapperr   r   r   wraptext   s    rV   c               @   s   e Zd ZdZd!ddZdd Zdd Zd	d
 ZeZdd Z	dd Z
dd Zdd Zdd ZefddZdd Zd"ddZdd Zdd Zdd  ZdS )#odictzkOrdered dict implementation.

    :see: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/107747
    Nc             C   s$   t | |pi  tt | | _d S )N)dict__init__listkeys_keys)selfdatar   r   r   rY      s    zodict.__init__c             C   s   t | | | j| d S )N)rX   __delitem__r\   remove)r]   keyr   r   r   r_      s    zodict.__delitem__c             C   s*   || k}t | || |r&| j| d S )N)rX   __setitem__r\   r@   )r]   ra   r   Znew_keyr   r   r   rb      s    zodict.__setitem__c             C   s
   t | jS )N)r   r\   )r]   r   r   r   __iter__   s    zodict.__iter__c             C   s   t |  g | _d S )N)rX   clearr\   )r]   r   r   r   rd      s    
zodict.clearc             C   s   t  }||  |S )N)rW   update)r]   dr   r   r   copy   s    
z
odict.copyc             C   s   t | j|  S )N)zipr\   values)r]   r   r   r   items   s    zodict.itemsc             C   s   t | j|  S )N)r   r\   
itervalues)r]   r   r   r   	iteritems   s    zodict.iteritemsc             C   s   | j d d  S )N)r\   )r]   r   r   r   r[      s    z
odict.keysc          
   C   sV   yt | |}| j| |S  tk
rP } z|tkr<|n|S W d d }~X Y nX d S )N)rX   popr\   r`   KeyErrormissing)r]   ra   defaultvalueer   r   r   rm      s    z	odict.popc             C   s   | j | t|S )N)r\   r`   rX   popitem)r]   ra   r   r   r   rs     s    zodict.popitemc             C   s(   t | || || jkr$| j| d S )N)rX   
setdefaultr\   r@   )r]   ra   Zfailobjr   r   r   rt     s    
zodict.setdefaultc             C   s"   x|  D ]\}}|| |< q
W d S )N)rj   )r]   rX   ra   valr   r   r   re   
  s    zodict.updatec             C   s   t | j| jS )N)mapgetr\   )r]   r   r   r   ri     s    zodict.valuesc             C   s   t | j| jS )N)r   rw   r\   )r]   r   r   r   rk     s    zodict.itervalues)N)N)rL   rM   rN   __doc__rY   r_   rb   rc   Ziterkeysrd   rg   rj   rl   r[   ro   rm   rs   rt   re   ri   rk   r   r   r   r   rW      s"   

rW   c               @   sB   e Zd ZdZdddZdd Zdd Zd	d
 Zdd Zdd Z	dS )FixedOffsetTimezonez&Fixed offset in minutes east from UTC.Nc             C   s&   t |d| _|d krd| }|| _d S )N)Zminutesz
Etc/GMT%+d)r   _offsetzone)r]   offsetr7   r   r   r   rY     s    zFixedOffsetTimezone.__init__c             C   s   | j S )N)r{   )r]   r   r   r   __str__  s    zFixedOffsetTimezone.__str__c             C   s   d| j | jf S )Nz<FixedOffset "%s" %s>)r{   rz   )r]   r   r   r   __repr__!  s    zFixedOffsetTimezone.__repr__c             C   s   | j S )N)rz   )r]   dtr   r   r   	utcoffset$  s    zFixedOffsetTimezone.utcoffsetc             C   s   | j S )N)r{   )r]   r   r   r   r   tzname'  s    zFixedOffsetTimezone.tznamec             C   s   t S )N)ZERO)r]   r   r   r   r   dst*  s    zFixedOffsetTimezone.dst)N)
rL   rM   rN   rx   rY   r}   r~   r   r   r   r   r   r   r   ry     s   
ry   )r%   )rP   r<   r<   )&rx   r   Zdatetimer   r   rD   r.   textwrapZbabel._compatr   r   ZpytzZ_pytzZbabelr   objectro   r   rO   VERBOSEr   r$   r0   r8   rJ   rK   rV   rX   rW   ry   ZutcZUTCZLOCALTZZget_localzoneZ	STDOFFSETZ	DSTOFFSETZDSTDIFFr   r   r   r   r   <module>
   s8   
2
 .
I