dbfdg 3 qDY\"@sdZddlZddlmZmZddlZddlZddlZddlm Z m Z ddl Z ddl mZeZddZejdejZd d Zejd Zdd dZddZGdddejZdddZGdddeZGdddeZe jZej Z ej!Z!ej"Z"ej#Z#ej$Z$ej%Z%dS)z babel.util ~~~~~~~~~~ Various utility classes and functions. :copyright: (c) 2013 by the Babel Team. :license: BSD, see LICENSE for more details. N) timedeltatzinfo)izipimap) localtimeccs4t}x(t|D]}||kr|V|j|qWdS)aYield 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)setiteradd)iterableseenitemr /usr/lib/python3.6/util.pydistincts rs([ \t\f]* \# .* coding[=:][ \t]*([-\w.]+)cCs|j}|jdz|j}|jtj}|r>|ttjd}tj|}|syddl }|j |j dWnt t tfk rYnX|j}tj|}|r|r|jdj d}|dkrt dj|dS|r|jdj dSdSWd|j|XdS)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) rNzlatin-1zutf-8zencoding problem: {0} with BOM)tellseekreadline startswithcodecsBOM_UTF8lenPYTHON_MAGIC_COMMENT_rematchparserZsuitedecode ImportError SyntaxErrorUnicodeEncodeErrorgroupformat)fpposZline1Zhas_bommrZline2Zmagic_comment_encodingr r rparse_encoding0s8     r$z'from\s+__future__\s+import\s+\(*(.+)\)*latin-1c Csddl}|j}|jdd}z|jj|}tjdd|}tjdd|}tjdd|}xTtj|D]F}d d |j d j d D}x&|D]}t ||d} | r|| j O}qWqdWWd|j|X|S) zRParse the compiler flags by :mod:`__future__` from the given Python code. rNzimport\s*\([\r\n]+zimport (z ,\s*[\r\n]+z, z \\\s*[\r\n]+ cSsg|]}|jjdqS)z())strip).0xr r r |sz&parse_future_flags..r,) __future__rrreadrresubPYTHON_FUTURE_IMPORT_refinditerrsplitgetattrZ compiler_flag) r!encodingr,r"flagsZbodyr#namesnameZfeaturer r rparse_future_flagsfs"    r8cCsddddddd}g}xHttjd|D]4\}}|d rH|j||q(|r(|jtj|q(Wtjd j|d |jtj d }|d k S)aExtended 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.r2appendescaperjoinreplaceossep)patternfilenameZsymbolsZbufidxpartrr r r pathmatchs rJc@seZdZejdZdS) TextWrapperz((\s+|(?<=[\w\!\"\'\&\.\,\?])-{2,}(?=\w))N)__name__ __module__ __qualname__r.compileZ wordsep_rer r r rrKsrKFr<cCst|||dd}|j|S)aSimple 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)widthinitial_indentsubsequent_indentZbreak_long_words)rKZwrap)textrQrRrSwrapperr r rwraptexts rVc@seZdZdZd!ddZddZddZd d ZeZd d Z d dZ ddZ ddZ ddZ efddZddZd"ddZddZddZdd ZdS)#odictzkOrdered dict implementation. :see: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/107747 NcCs$tj||p ittj||_dS)N)dict__init__listkeys_keys)selfdatar r rrYszodict.__init__cCstj|||jj|dS)N)rX __delitem__r\remove)r]keyr r rr_s zodict.__delitem__cCs*||k}tj||||r&|jj|dS)N)rX __setitem__r\r@)r]rar Znew_keyr r rrbszodict.__setitem__cCs t|jS)N)rr\)r]r r r__iter__szodict.__iter__cCstj|g|_dS)N)rXclearr\)r]r r rrds z odict.clearcCst}|j||S)N)rWupdate)r]dr r rcopys z odict.copycCst|j|jS)N)zipr\values)r]r r ritemssz odict.itemscCst|j|jS)N)rr\ itervalues)r]r r r iteritemsszodict.iteritemscCs|jddS)N)r\)r]r r rr[sz odict.keyscCsVytj||}|jj||Stk rP}z|tkr<|n|SWYdd}~XnXdS)N)rXpopr\r`KeyErrormissing)r]radefaultvalueer r rrms  z odict.popcCs|jj|tj|S)N)r\r`rXpopitem)r]rar r rrss z odict.popitemcCs(tj|||||jkr$|jj|dS)N)rX setdefaultr\r@)r]raZfailobjr r rrts zodict.setdefaultcCs"x|jD]\}}|||<q WdS)N)rj)r]rXravalr r rre sz odict.updatecCst|j|jS)N)mapgetr\)r]r r rrisz odict.valuescCst|j|jS)N)rrwr\)r]r r rrkszodict.itervalues)N)N)rLrMrN__doc__rYr_rbrcZiterkeysrdrgrjrlr[rormrsrtrerirkr r r rrWs"   rWc@sBeZdZdZdddZddZddZd d Zd d Zd dZ dS)FixedOffsetTimezonez&Fixed offset in minutes east from UTC.NcCs&t|d|_|dkrd|}||_dS)N)Zminutesz Etc/GMT%+d)r_offsetzone)r]offsetr7r r rrYs zFixedOffsetTimezone.__init__cCs|jS)N)r{)r]r r r__str__szFixedOffsetTimezone.__str__cCsd|j|jfS)Nz)r{rz)r]r r r__repr__!szFixedOffsetTimezone.__repr__cCs|jS)N)rz)r]dtr r r utcoffset$szFixedOffsetTimezone.utcoffsetcCs|jS)N)r{)r]rr r rtzname'szFixedOffsetTimezone.tznamecCstS)N)ZERO)r]rr r rdst*szFixedOffsetTimezone.dst)N) rLrMrNrxrYr}r~rrrr r r rrys ry)r%)rPr<r<)&rxrZdatetimerrrDr.textwrapZ babel._compatrrZpytzZ_pytzZbabelrobjectrorrOVERBOSErr$r0r8rJrKrVrXrWryZutcZUTCZLOCALTZZ get_localzoneZ STDOFFSETZ DSTOFFSETZDSTDIFFrr r r r s8  2 . I