
    w:i]                         d Z ddlZddlmZmZ dZ G d de          Zd Zdd	Z	 ej
        d
          ZddZ ej
        d          Zd ZdS )z&Various utility classes and functions.    N)html_entitiesunichrzrestructuredtext enc                   j    e Zd ZdZ G d de          Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd ZdS )LRUCacheae  A 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                       e Zd Zd Zd ZdS )LRUCache._Itemc                 >    d x| _         | _        || _        || _        d S N)prvnxtkeyvalue)selfr   r   s      C/var/www/html/trac/venv/lib/python3.11/site-packages/genshi/util.py__init__zLRUCache._Item.__init__>   s"    "&&DHtxDHDJJJ    c                 *    t          | j                  S r
   )reprr   r   s    r   __repr__zLRUCache._Item.__repr__B   s    
###r   N)__name__
__module____qualname__r   r    r   r   _Itemr   =   s2        	 	 		$ 	$ 	$ 	$ 	$r   r   c                 V    t                      | _        || _        d | _        d | _        d S r
   )dict_dictcapacityheadtail)r   r   s     r   r   zLRUCache.__init__E   s&    VV
 				r   c                     || j         v S r
   )r   )r   r   s     r   __contains__zLRUCache.__contains__K   s    dj  r   c              #   D   K   | j         }|r|j        V  |j        }|d S d S r
   )r    r   r   )r   curs     r   __iter__zLRUCache.__iter__N   sE      i 	'MMM'C  	 	 	 	 	r   c                 *    t          | j                  S r
   )lenr   r   s    r   __len__zLRUCache.__len__T   s    4:r   c                 T    | j         |         }|                     |           |j        S r
   )r   _update_itemr   )r   r   items      r   __getitem__zLRUCache.__getitem__W   s)    z#$zr   c                    | j                             |          }|7|                     ||          }|| j         |<   |                     |           d S ||_        |                     |           |                                  d S r
   )r   getr   _insert_itemr   r+   _manage_size)r   r   r   r,   s       r   __setitem__zLRUCache.__setitem__\   s    z~~c""<::c5))D"DJsOd#####DJd###r   c                 *    t          | j                  S r
   )r   r   r   s    r   r   zLRUCache.__repr__g   s    DJr   c                     d |_         | j        |_        | j        || j        _         n|| _        || _        |                                  d S r
   )r   r    r   r!   r1   )r   r,   s     r   r0   zLRUCache._insert_itemj   sK    99  DIMMDI	r   c                    t          | j                  | j        k    rm| j        | j        j        = | j        | j        k    r| j        j        | _        d | j        _        nd x| _        | _        t          | j                  | j        k    kd S d S r
   )r(   r   r   r!   r   r    r   r   r   s    r   r1   zLRUCache._manage_sizet   s{    $*oo--
49=)yDI%% IM	 $	(,,	DI $*oo------r   c                     | j         |k    rd S |j        }|j        |_        |j        ||j        _        n|| _        d |_        | j         |_        |x| j         _        | _         d S r
   )r    r   r   r!   )r   r,   r   s      r   r+   zLRUCache._update_item}   sb    9Fh(8DHLLDI9$((				r   N)r   r   r   __doc__objectr   r   r#   r&   r)   r-   r2   r   r0   r1   r+   r   r   r   r   r      s        # #J$ $ $ $ $ $ $ $  ! ! !      
	  	  	        - - -) ) ) ) )r   r   c                     g }| D ]R}t          |t          t          t          t          f          r|t          |          z  }=|                    |           S|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	frozensetlistsettupleflattenappend)itemsretvalr,   s      r   r?   r?      s_     F    dYc59:: 	 gdmm#FFMM$Mr   Tc                 n    t          t          |                     } |s|                     dd          } | S )a  Return the text with all entities and tags removed.
    
    >>> plaintext('<b>1 &lt; 2</b>')
    '1 < 2'
    
    The `keeplinebreaks` parameter can be set to ``False`` to replace any line
    breaks by simple spaces:
    
    >>> plaintext('''<b>1
    ... &lt;
    ... 2</b>''', 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	striptagsreplace)textkeeplinebreakss     r   	plaintextrK      s7    $ 4))D '||D#&&Kr   z-&(?:#((?:\d+)|(?:[xX][0-9a-fA-F]+));?|(\w+);)Fc                 D    fd}t                               ||           S )u5  Return a copy of the given text with any character or numeric entities
    replaced by the equivalent UTF-8 characters.
    
    >>> stripentities('1 &lt; 2')
    '1 < 2'
    >>> stripentities('more &hellip;')
    'more …'
    >>> stripentities('&#8230;')
    '…'
    >>> stripentities('&#x2026;')
    '…'
    
    If the `keepxmlentities` parameter is provided and is a truth value, the
    core XML entities (&amp;, &apos;, &gt;, &lt; and &quot;) are left intact.
    
    >>> stripentities('1 &lt; 2 &hellip;', keepxmlentities=True)
    '1 &lt; 2 …'
    c                    |                      d          rb|                      d          }|                    d          rt          |dd          d          }nt          |d          }t          |          S |                      d          }r	|dv rd|z  S 	 t          t          j        |                   S # t          $ r rd|z  cY S |cY S w xY w)	N   x   
      )ampaposgtltquotz&%s;z&amp;%s;)group
startswithintr   r   name2codepointKeyError)matchrefkeepxmlentitiess     r   _replace_entityz&stripentities.<locals>._replace_entity   s    ;;q>> 	++a..C~~c"" ##abb'2&&#rll#;;++a..C $3*M#M#M|#m:3?@@@   " %++++JJJ	s   B9 9CCC)_STRIPENTITIES_REsub)rI   r_   r`   s    ` r   rF   rF      s4    &    &   $777r   z(<!--.*?-->|<[^>]*>)c                 8    t                               d|           S )a  Return a copy of the text with any XML/HTML tags removed.
    
    >>> striptags('<span>Foo</span> bar')
    'Foo bar'
    >>> striptags('<span class="bar">Foo</span>')
    'Foo'
    >>> striptags('Foo<br />')
    'Foo'
    
    HTML/XML comments are stripped, too:
    
    >>> striptags('<!-- <blub>hehe</blah> -->test')
    'test'
    
    :param text: the string to remove tags from
    :return: the text with tags removed
     )_STRIPTAGS_RErb   )rI   s    r   rG   rG      s    $ R&&&r   )T)F)r7   regenshi.compatr   r   __docformat__r   r   r?   rK   compilera   rF   re   rG   r   r   r   <module>rj      s    - , 				 / / / / / / / /%s) s) s) s) s)t s) s) s)l  *   0 BJOPP &8 &8 &8 &8R 
233' ' ' ' 'r   