o
    VÛë\ž  ã                   @   s   d Z ddl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ej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-2019 by the Babel Team.
    :license: BSD, see LICENSE for more details.
é    N)Ú	timedeltaÚtzinfo)ÚizipÚimap)Ú	localtimec                 c   s2   � t ƒ }t| ƒD ]}||vr|V  | |¡ q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   ú,/usr/lib/python3/dist-packages/babel/util.pyÚdistinct   s   €
€ýr   s(   [ \t\f]* \# .* coding[=:][ \t]*([-\w.]+)c              
   C   s  |   ¡ }|  d¡ zw|  ¡ }| tj¡}|r|ttjƒd… }t |¡}|sJzddl	}| 
| d¡¡ W n tttfy@   Y n
w |  ¡ }t |¡}|ri|ra| d¡ d¡}|dkratd |¡ƒ‚W |  |¡ dS |ry| d¡ d¡W |  |¡ S W |  |¡ dS |  |¡ w )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   Núlatin-1é   zutf-8zencoding problem: {0} with BOM)ÚtellÚseekÚreadlineÚ
startswithÚcodecsÚBOM_UTF8ÚlenÚPYTHON_MAGIC_COMMENT_reÚmatchÚparserÚsuiteÚdecodeÚImportErrorÚSyntaxErrorÚUnicodeEncodeErrorÚgroupÚformat)ÚfpÚposÚline1Úhas_bomÚmr   Úline2Úmagic_comment_encodingr   r   r   Úparse_encoding1   sB   

û
ÿÿûþr*   z'from\s+__future__\s+import\s+\(*(.+)\)*r   c           
   	   C   sÂ   ddl }|  ¡ }|  d¡ d}zK|  ¡  |¡}t dd|¡}t dd|¡}t dd|¡}t |¡D ]!}d	d
„ | 	d¡ 
d¡D ƒ}|D ]}t||dƒ}	|	rQ||	jO }qBq1W |  |¡ |S |  |¡ w )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ÚgetattrÚcompiler_flag)
r#   Úencodingr1   r$   ÚflagsÚbodyr'   ÚnamesÚnameÚfeaturer   r   r   Úparse_future_flagsg   s*   

€ýþ
ÿr@   c                 C   sÀ   dddddddœ}|   d¡rdg}| d	d
… } n|   d¡r'dg}| dd
… } ng }tt d| ¡ƒD ]\}}|d rA| || ¡ q1|rK| t |¡¡ q1t d |¡d | t	j
d¡¡}|d
u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('./foo/**.py', 'foo/bar/baz.py')
    True
    >>> pathmatch('./foo/**.py', 'bar/baz.py')
    False

    >>> pathmatch('^foo/**.py', 'foo/bar/baz.py')
    True
    >>> pathmatch('^foo/**.py', 'bar/baz.py')
    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**ú^r   Nz./é   z	([?*]+/?)Ú ú$ú/)r   Ú	enumerater3   r7   ÚappendÚescaper   ÚjoinÚreplaceÚosÚsep)ÚpatternÚfilenameÚsymbolsÚbufÚidxÚpartr   r   r   r   Ú	pathmatch‡   s,   'ú
	
€ rU   c                   @   s   e Zd Ze d¡ZdS )ÚTextWrapperz((\s+|(?<=[\w\!\"\'\&\.\,\?])-{2,}(?=\w))N)Ú__name__Ú
__module__Ú__qualname__r3   ÚcompileÚ
wordsep_rer   r   r   r   rV   È   s    ÿrV   éF   rE   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_indentÚbreak_long_words)rV   Úwrap)Útextr]   r^   r_   Úwrapperr   r   r   ÚwraptextÏ   s
   þ
rd   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 u rd| }|| _d S )N)Úminutesz
Etc/GMT%+d)r   Ú_offsetÚzone)ÚselfÚoffsetr>   r   r   r   Ú__init__ç   s   
zFixedOffsetTimezone.__init__c                 C   ó   | j S ©N©rh   ©ri   r   r   r   Ú__str__í   ó   zFixedOffsetTimezone.__str__c                 C   s   d| j | jf S )Nz<FixedOffset "%s" %s>)rh   rg   ro   r   r   r   Ú__repr__ð   s   zFixedOffsetTimezone.__repr__c                 C   rl   rm   )rg   ©ri   Údtr   r   r   Ú	utcoffsetó   rq   zFixedOffsetTimezone.utcoffsetc                 C   rl   rm   rn   rs   r   r   r   Útznameö   rq   zFixedOffsetTimezone.tznamec                 C   s   t S rm   )ÚZEROrs   r   r   r   Údstù   s   zFixedOffsetTimezone.dstrm   )
rW   rX   rY   Ú__doc__rk   rp   rr   ru   rv   rx   r   r   r   r   re   ä   s    
re   )r   )r\   rE   rE   )'ry   r   ÚcollectionsÚdatetimer   r   rM   r3   ÚtextwrapÚbabel._compatr   r   ÚpytzÚ_pytzÚbabelr   ÚobjectÚmissingr   rZ   ÚVERBOSEr   r*   r5   r@   rU   rV   rd   ÚOrderedDictÚodictre   ÚutcÚUTCÚLOCALTZÚget_localzoneÚ	STDOFFSETÚ	DSTOFFSETÚDSTDIFFrw   r   r   r   r   Ú<module>   s@   
ÿ2ÿ
 A

