B
    't\O                 @   s   d Z ddlmZ ddlmZ ddlmZmZmZm	Z	m
Z
 ddlmZmZ ddlmZmZ ddlZddlZd	d
dddgZG dd	 d	eeeZG dd
 d
eZG dd deZG dd deZG dd deZdS )a]  
Implementations for the history of a `Buffer`.

NOTE: Notice that there is no `DynamicHistory`. This doesn't work well, because
      the `Buffer` needs to be able to attach an event handler to the event
      when a history entry is loaded. This loading can be done asynchronously
      and making the history swappable would probably break this.
    )unicode_literals   )Event)AsyncGeneratorItemFromensure_futureconsume_async_generatorgenerator_to_async_generator)ABCMetaabstractmethod)with_metaclass	text_typeNHistoryThreadedHistoryDummyHistoryFileHistoryInMemoryHistoryc               @   s`   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	e
dd Zdd Ze
dd ZdS )r   zg
    Base ``History`` class.

    This also includes abstract methods for loading/storing history.
    c             C   s   d| _ g | _t| | _d S )NF)_loading_loaded_stringsr   _item_loaded)self r   5lib/python3.7/site-packages/prompt_toolkit/history.py__init__#   s    zHistory.__init__c             #   s,    fdd}t t  dd |dV  dS )z
        Consume the asynchronous generator: `load_history_strings_async`.

        This is only called once, because once the history is loaded, we don't
        have to load it again.
        c                s    j d|   j  dS )z9 Got one string from the asynchronous history generator. r   N)r   insertr   Zfire)string)r   r   r   
add_string0   s    z*History._start_loading.<locals>.add_stringc               S   s   dS )NFr   r   r   r   r   <lambda>7   s    z(History._start_loading.<locals>.<lambda>)ZcancelZitem_callbackN)r   r   load_history_strings_async)r   r   r   )r   r   _start_loading)   s
    zHistory._start_loadingc             C   s   | j sd| _ t|   dS )z Start loading the history. TN)r   r   r   )r   r   r   r   start_loading?   s    zHistory.start_loadingc             C   s   | j S )z5 Event which is triggered when a new item is loaded. )r   )r   r   r   r   get_item_loaded_eventE   s    zHistory.get_item_loaded_eventc             C   s   | j S )zJ
        Get the strings from the history that are loaded so far.
        )r   )r   r   r   r   get_stringsI   s    zHistory.get_stringsc             C   s   | j | | | dS )z Add string to the history. N)r   appendstore_string)r   r   r   r   r   append_stringO   s    zHistory.append_stringc             c   s   dS )z
        This should be a generator that yields `str` instances.

        It should yield the most recent items first, because they are the most
        important. (The history can already be used, even when it's only
        partially loaded.)
        Nr   )r   r   r   r   load_history_stringsX   s    	zHistory.load_history_stringsc             c   s.   x(|   D ]}t|tstt|V  q
W dS )a  
        Asynchronous generator for history strings. (Probably, you won't have
        to override this.)

        This should return an iterable that can yield both `str`
        and `Future` objects. The `str` objects have to be
        wrapped in a `AsyncGeneratorItem` object.

        If we drop Python 2 support in the future, this could become a true
        asynchronous generator.
        N)r&   
isinstancer   AssertionErrorr   )r   itemr   r   r   r   d   s    z"History.load_history_strings_asyncc             C   s   dS )z9
        Store the string in persistent storage.
        Nr   )r   r   r   r   r   r$   t   s    zHistory.store_stringN)__name__
__module____qualname____doc__r   r   r    r!   r"   r%   r   r&   r   r$   r   r   r   r   r      s   	c                   sB   e Zd ZdZd fdd	Zdd Zdd Zd	d
 Zdd Z  Z	S )r   a  
    Wrapper that runs the `load_history_strings` generator in a thread.

    Use this to increase the start-up time of prompt_toolkit applications.
    History entries are available as soon as they are loaded. We don't have to
    wait for everything to be loaded.
    Nc                s0   t |tstd|f || _tt|   d S )NzGot %r)r'   r   r(   historysuperr   r   )r   r.   )	__class__r   r   r      s    zThreadedHistory.__init__c             C   s   t | jjS )zp
        Asynchronous generator of completions.
        This yields both Future and Completion objects.
        )r	   r.   r&   )r   r   r   r   r      s    z*ThreadedHistory.load_history_strings_asyncc             C   s
   | j  S )N)r.   r&   )r   r   r   r   r&      s    z$ThreadedHistory.load_history_stringsc             C   s   | j | d S )N)r.   r$   )r   r   r   r   r   r$      s    zThreadedHistory.store_stringc             C   s   d| j f S )NzThreadedHistory(%r))r.   )r   r   r   r   __repr__   s    zThreadedHistory.__repr__)N)
r*   r+   r,   r-   r   r   r&   r$   r1   __classcell__r   r   )r0   r   r   {   s   
c               @   s    e Zd ZdZdd Zdd ZdS )r   zM
    :class:`.History` class that keeps a list of all strings in memory.
    c             C   s   g S )Nr   )r   r   r   r   r&      s    z$InMemoryHistory.load_history_stringsc             C   s   d S )Nr   )r   r   r   r   r   r$      s    zInMemoryHistory.store_stringN)r*   r+   r,   r-   r&   r$   r   r   r   r   r      s   c               @   s(   e Zd ZdZdd Zdd Zdd ZdS )	r   zB
    :class:`.History` object that doesn't remember anything.
    c             C   s   g S )Nr   )r   r   r   r   r&      s    z!DummyHistory.load_history_stringsc             C   s   d S )Nr   )r   r   r   r   r   r$      s    zDummyHistory.store_stringc             C   s   d S )Nr   )r   r   r   r   r   r%      s    zDummyHistory.append_stringN)r*   r+   r,   r-   r&   r$   r%   r   r   r   r   r      s   c                   s0   e Zd ZdZ fddZdd Zdd Z  ZS )r   zD
    :class:`.History` class that stores all strings in a file.
    c                s   || _ tt|   d S )N)filenamer/   r   r   )r   r3   )r0   r   r   r      s    zFileHistory.__init__c          	      s   g g   fdd}t j| jrt| jdL}x>|D ]6}|d}|drd |dd   q8|  g  q8W |  W d Q R X tS )Nc                 s$    r d  d d } |  d S )N )joinr#   )r   )linesstringsr   r   add   s    z-FileHistory.load_history_strings.<locals>.addrbzutf-8+r   )	ospathexistsr3   opendecode
startswithr#   reversed)r   r9   fliner   )r7   r8   r   r&      s    


z FileHistory.load_history_stringsc          	      sZ   t | jdD  fdd}|dtj   x|dD ]}|d|  q8W W d Q R X d S )NZabc                s     | d d S )Nzutf-8)writeencode)t)rC   r   r   rE      s    z'FileHistory.store_string.<locals>.writez
# %s

z+%s
)r?   r3   datetimeZnowsplit)r   r   rE   rD   r   )rC   r   r$      s
    zFileHistory.store_string)r*   r+   r,   r-   r   r&   r$   r2   r   r   )r0   r   r      s   )r-   Z
__future__r   Zutilsr   Z	eventloopr   r   r   r   r	   abcr
   r   Zsixr   r   rI   r<   __all__objectr   r   r   r   r   r   r   r   r   <module>   s"   ^!