# encoding: utf-8 """ Manage the input and output history of the interpreter and the frontend. There are 2 different history objects, one that lives in the interpreter, and one that lives in the frontend. They are synced with a diff at each execution of a command, as the interpreter history is a real stack, its existing entries are not mutable. """ __docformat__ = "restructuredtext en" #------------------------------------------------------------------------------- # Copyright (C) 2008 The IPython Development Team # # Distributed under the terms of the BSD License. The full license is in # the file COPYING, distributed as part of this software. #------------------------------------------------------------------------------- #------------------------------------------------------------------------------- # Imports #------------------------------------------------------------------------------- from copy import copy # Local imports. from util import InputList ############################################################################## class History(object): """ An object managing the input and output history. """ def __init__(self, input_cache=None, output_cache=None): # Stuff that IPython adds to the namespace. self.namespace_additions = dict( _ = None, __ = None, ___ = None, ) # A list to store input commands. if input_cache is None: input_cache =InputList([]) self.input_cache = input_cache # A dictionary to store trapped output. if output_cache is None: output_cache = {} self.output_cache = output_cache def get_history_item(self, index): """ Returns the history string at index, where index is the distance from the end (positive). """ if index>0 and index0 and index<(len(self.input_cache)-1): return self.input_cache[-index] def get_input_cache(self): return copy(self.input_cache) def get_input_after(self, index): """ Returns the list of the commands entered after index. """ # We need to call directly list.__getslice__, because this object # is not a real list. return list.__getslice__(self.input_cache, index, len(self.input_cache)) ############################################################################## class FrontEndHistory(History): """ An object managing the input and output history at the frontend. It is used as a local cache to reduce network latency problems and multiple users editing the same thing. """ def add_items(self, item_list): """ Adds the given command list to the stack of executed commands. """ self.input_cache.extend(item_list)