o
    u]                     @   s(   d Z ddlZdd ZG dd deZdS )a  
Basic enumeration, providing ordered types for collections. These can be
constructed as simple type listings...

::

  >>> from stem.util import enum
  >>> insects = enum.Enum('ANT', 'WASP', 'LADYBUG', 'FIREFLY')
  >>> insects.ANT
  'Ant'
  >>> tuple(insects)
  ('Ant', 'Wasp', 'Ladybug', 'Firefly')

... or with overwritten string counterparts...

::

  >>> from stem.util import enum
  >>> pets = enum.Enum(('DOG', 'Skippy'), 'CAT', ('FISH', 'Nemo'))
  >>> pets.DOG
  'Skippy'
  >>> pets.CAT
  'Cat'

**Module Overview:**

::

  UppercaseEnum - Provides an enum instance with capitalized values

  Enum - Provides a basic, ordered  enumeration
    |- keys - string representation of our enum keys
    |- index_of - index of an enum value
    |- next - provides the enum after a given enum value
    |- previous - provides the enum before a given value
    |- __getitem__ - provides the value for an enum key
    +- __iter__ - iterator over our enum keys
    Nc                  G   s   t dd | D  S )a  
  Provides an :class:`~stem.util.enum.Enum` instance where the values are
  identical to the keys. Since the keys are uppercase by convention this means
  the values are too. For instance...

  ::

    >>> from stem.util import enum
    >>> runlevels = enum.UppercaseEnum('DEBUG', 'INFO', 'NOTICE', 'WARN', 'ERROR')
    >>> runlevels.DEBUG
    'DEBUG'

  :param list args: enum keys to initialize with

  :returns: :class:`~stem.util.enum.Enum` instance with the given keys
  c                 S   s   g | ]}||fqS  r   ).0vr   r   0/usr/lib/python3/dist-packages/stem/util/enum.py
<listcomp>@   s    z!UppercaseEnum.<locals>.<listcomp>)Enum)argsr   r   r   UppercaseEnum.   s   r	   c                   @   sH   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dS )r   z
  Basic enumeration.
  c                 G   s   ddl m} g g }}|D ]6}tj|r|||}}nt|tr-t|dkr-|\}}ntd| |	| |	| t
| || qt|| _t|| _d S )Nr   )_to_camel_case   zUnrecognized input: %s)Zstem.util.str_toolsr
   stemutilZ_is_str
isinstancetuplelen
ValueErrorappendsetattr_keys_values)selfr   r
   keysvaluesentrykeyvalr   r   r   __init__H   s   




zEnum.__init__c                 C   s
   t | jS )zu
    Provides an ordered listing of the enumeration keys in this set.

    :returns: **list** with our enum keys
    )listr   )r   r   r   r   r   ]   s   
z	Enum.keysc                 C   s   | j |S )z
    Provides the index of the given value in the collection.

    :param str value: entry to be looked up

    :returns: **int** index of the given entry

    :raises: **ValueError** if no such element exists
    )r   index)r   valuer   r   r   index_off   s   zEnum.index_ofc                 C   sF   || j vrtd|d| j f | j |d t| j  }| j | S )z
    Provides the next enumeration after the given value.

    :param str value: enumeration for which to get the next entry

    :returns: enum value following the given entry

    :raises: **ValueError** if no such element exists
    ,No such enumeration exists: %s (options: %s),    r   r   joinr   r   )r   r   Z
next_indexr   r   r   nexts      

z	Enum.nextc                 C   sF   || j vrtd|d| j f | j |d t| j  }| j | S )z
    Provides the previous enumeration before the given value.

    :param str value: enumeration for which to get the previous entry

    :returns: enum value proceeding the given entry

    :raises: **ValueError** if no such element exists
    r!   r"   r#   r$   )r   r   Z
prev_indexr   r   r   previous   r'   zEnum.previousc                 C   s4   |t | v rt| |S d|  }td||f )z
    Provides the values for the given key.

    :param str item: key to be looked up

    :returns: **str** with the value for the given key

    :raises: **ValueError** if the key doesn't exist
    r"   z9'%s' isn't among our enumeration keys, which includes: %s)varsgetattrr%   r   r   )r   itemr   r   r   r   __getitem__   s   
zEnum.__getitem__c                 c   s    | j D ]}|V  qdS )z?
    Provides an ordered listing of the enums in this set.
    N)r   )r   r   r   r   r   __iter__   s   
zEnum.__iter__N)__name__
__module____qualname____doc__r   r   r    r&   r(   r,   r-   r   r   r   r   r   C   s    	r   )r1   Z	stem.utilr   r	   objectr   r   r   r   r   <module>   s   '