Repository navigation
Expand file tree
/
Copy pathhistory.py
More file actions
137 lines (106 loc) · 4.48 KB
/
Copy pathhistory.py
File metadata and controls
137 lines (106 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# 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 index<len(self.input_cache):
return self.input_cache[index]
##############################################################################
class InterpreterHistory(History):
""" An object managing the input and output history at the interpreter
level.
"""
def setup_namespace(self, namespace):
""" Add the input and output caches into the interpreter's namespace
with IPython-conventional names.
Parameters
----------
namespace : dict
"""
namespace['In'] = self.input_cache
namespace['_ih'] = self.input_cache
namespace['Out'] = self.output_cache
namespace['_oh'] = self.output_cache
def update_history(self, interpreter, python):
""" Update the history objects that this object maintains and the
interpreter's namespace.
Parameters
----------
interpreter : Interpreter
python : str
The real Python code that was translated and actually executed.
"""
number = interpreter.current_cell_number
new_obj = interpreter.display_trap.obj
if new_obj is not None:
self.namespace_additions['___'] = self.namespace_additions['__']
self.namespace_additions['__'] = self.namespace_additions['_']
self.namespace_additions['_'] = new_obj
self.output_cache[number] = new_obj
interpreter.user_ns.update(self.namespace_additions)
self.input_cache.add(number, python)
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 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)