o !bm@spdZddlZddlmZddlZdZGdddeZddZ dd d Z e d Z dddZ e dZddZdS)z&Various utility classes and functions.N) html_entitieszrestructuredtext enc@speZdZdZGdddeZddZddZdd Zd d Z d d Z ddZ ddZ ddZ ddZddZdS)LRUCacheaeA dictionary-like object that stores only a certain number of items, and discards its least recently used item when full. >>> cache = LRUCache(3) >>> cache['A'] = 0 >>> cache['B'] = 1 >>> cache['C'] = 2 >>> len(cache) 3 >>> cache['A'] 0 Adding new items to the cache does not increase its size. Instead, the least recently used item is dropped: >>> cache['D'] = 3 >>> len(cache) 3 >>> 'B' in cache False Iterating over the cache returns the keys, starting with the most recently used: >>> for key in cache: ... print(key) D A C This code is based on the LRUCache class from ``myghtyutils.util``, written by Mike Bayer and released under the MIT license. See: http://svn.myghty.org/myghtyutils/trunk/lib/myghtyutils/util.py c@seZdZddZddZdS)zLRUCache._ItemcCsd|_|_||_||_dSN)prvnxtkeyvalue)selfrrr -/usr/lib/python3/dist-packages/genshi/util.py__init__@s  zLRUCache._Item.__init__cC t|jSr)reprrr r r r __repr__D zLRUCache._Item.__repr__N)__name__ __module__ __qualname__r rr r r r _Item?s rcCst|_||_d|_d|_dSr)dict_dictcapacityheadtail)r rr r r r Gs zLRUCache.__init__cCs ||jvSr)r)r rr r r __contains__MrzLRUCache.__contains__ccs&|j}|r|jV|j}|sdSdSr)rrr)r curr r r __iter__Ps  zLRUCache.__iter__cCr r)lenrrr r r __len__VrzLRUCache.__len__cCs|j|}|||jSr)r _update_itemr)r ritemr r r __getitem__Ys  zLRUCache.__getitem__cCsT|j|}|dur|||}||j|<||dS||_|||dSr)rgetr _insert_itemrr _manage_size)r rrr!r r r __setitem__^s     zLRUCache.__setitem__cCr r)rrrr r r rirzLRUCache.__repr__cCs:d|_|j|_|jdur||j_n||_||_|dSr)rrrrr%)r r!r r r r$ls   zLRUCache._insert_itemcCs`t|j|jkr.|j|jj=|j|jkr|jj|_d|j_nd|_|_t|j|jksdSdSr)rrrrrrrrrr r r r%vs     zLRUCache._manage_sizecCsV|j|krdS|j}|j|_|jdur||j_n||_d|_|j|_||j_|_dSr)rrrr)r r!rr r r r s   zLRUCache._update_itemN)rrr__doc__objectrr rrrr"r&rr$r%r r r r r rs%  rcCs<g}|D]}t|ttttfr|t|7}q||q|S)zFlattens a potentially nested sequence into a flat list. :param items: the sequence to flatten >>> flatten((1, 2)) [1, 2] >>> flatten([1, (2, 3), 4]) [1, 2, 3, 4] >>> flatten([1, (2, [3, 4]), 5]) [1, 2, 3, 4, 5] ) isinstance frozensetlistsettupleflattenappend)itemsretvalr!r r r r.s   r.TcCs tt|}|s|dd}|S)aReturn the text with all entities and tags removed. >>> plaintext('1 < 2') '1 < 2' The `keeplinebreaks` parameter can be set to ``False`` to replace any line breaks by simple spaces: >>> plaintext('''1 ... < ... 2''', keeplinebreaks=False) '1 < 2' :param text: the text to convert to plain text :param keeplinebreaks: whether line breaks in the text should be kept intact :return: the text with tags and entities removed   ) stripentities striptagsreplace)textkeeplinebreaksr r r plaintexts  r9z-&(?:#((?:\d+)|(?:[xX][0-9a-fA-F]+));?|(\w+);)Fcsfdd}t||S)u5Return a copy of the given text with any character or numeric entities replaced by the equivalent UTF-8 characters. >>> stripentities('1 < 2') '1 < 2' >>> stripentities('more …') 'more …' >>> stripentities('…') '…' >>> stripentities('…') '…' If the `keepxmlentities` parameter is provided and is a truth value, the core XML entities (&, ', >, < and ") are left intact. >>> stripentities('1 < 2 …', keepxmlentities=True) '1 < 2 …' cs|dr#|d}|drt|ddd}nt|d}t|S|d}r2|dvr2d|Sz ttj|WStyNrJd|YS|YSw) Nx )ampaposgtltquotz&%s;z&%s;)group startswithintsixunichrentitiesname2codepointKeyError)matchrefkeepxmlentitiesr r _replace_entitys         z&stripentities.._replace_entity)_STRIPENTITIES_REsub)r7rOrPr rNr r4s  r4z(|<[^>]*>)cCs td|S)aReturn a copy of the text with any XML/HTML tags removed. >>> striptags('Foo bar') 'Foo bar' >>> striptags('Foo') 'Foo' >>> striptags('Foo
') 'Foo' HTML/XML comments are stripped, too: >>> striptags('test') 'test' :param text: the string to remove tags from :return: the text with tags removed ) _STRIPTAGS_RErR)r7r r r r5s r5)T)F)r're six.movesrrIrG __docformat__rrr.r9compilerQr4rTr5r r r r s  v    )