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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
|
"""
Base front end class for all line-oriented frontends, rather than
block-oriented.
Currently this focuses on synchronous frontends.
"""
__docformat__ = "restructuredtext en"
#-------------------------------------------------------------------------------
# Copyright (C) 2008-2011 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
#-------------------------------------------------------------------------------
import re
import sys
import codeop
from frontendbase import FrontEndBase
from IPython.kernel.core.interpreter import Interpreter
def common_prefix(strings):
""" Given a list of strings, return the common prefix between all
these strings.
"""
ref = strings[0]
prefix = ''
for size in range(len(ref)):
test_prefix = ref[:size+1]
for string in strings[1:]:
if not string.startswith(test_prefix):
return prefix
prefix = test_prefix
return prefix
#-----------------------------------------------------------------------------
# Base class for the line-oriented front ends
#-----------------------------------------------------------------------------
class LineFrontEndBase(FrontEndBase):
""" Concrete implementation of the FrontEndBase class. This is meant
to be the base class behind all the frontend that are line-oriented,
rather than block-oriented.
"""
# We need to keep the prompt number, to be able to increment
# it when there is an exception.
prompt_number = 1
# We keep a reference to the last result: it helps testing and
# programatic control of the frontend.
last_result = dict(number=0)
# The last prompt displayed. Useful for continuation prompts.
last_prompt = ''
# The input buffer being edited
input_buffer = ''
# Set to true for debug output
debug = False
# A banner to print at startup
banner = None
#--------------------------------------------------------------------------
# FrontEndBase interface
#--------------------------------------------------------------------------
def __init__(self, shell=None, history=None, banner=None, *args, **kwargs):
if shell is None:
shell = Interpreter()
FrontEndBase.__init__(self, shell=shell, history=history)
if banner is not None:
self.banner = banner
def start(self):
""" Put the frontend in a state where it is ready for user
interaction.
"""
if self.banner is not None:
self.write(self.banner, refresh=False)
self.new_prompt(self.input_prompt_template.substitute(number=1))
def complete(self, line):
"""Complete line in engine's user_ns
Parameters
----------
line : string
Returns
-------
The replacement for the line and the list of possible completions.
"""
completions = self.shell.complete(line)
complete_sep = re.compile('[\s\{\}\[\]\(\)\=]')
if completions:
prefix = common_prefix(completions)
residual = complete_sep.split(line)[:-1]
line = line[:-len(residual)] + prefix
return line, completions
def render_result(self, result):
""" Frontend-specific rendering of the result of a calculation
that has been sent to an engine.
"""
if 'stdout' in result and result['stdout']:
self.write('\n' + result['stdout'])
if 'display' in result and result['display']:
self.write("%s%s\n" % (
self.output_prompt_template.substitute(
number=result['number']),
result['display']['pprint']
) )
def render_error(self, failure):
""" Frontend-specific rendering of error.
"""
self.write('\n\n'+str(failure)+'\n\n')
return failure
def is_complete(self, string):
""" Check if a string forms a complete, executable set of
commands.
For the line-oriented frontend, multi-line code is not executed
as soon as it is complete: the users has to enter two line
returns.
"""
if string in ('', '\n'):
# Prefiltering, eg through ipython0, may return an empty
# string although some operations have been accomplished. We
# thus want to consider an empty string as a complete
# statement.
return True
elif ( len(self.input_buffer.split('\n'))>2
and not re.findall(r"\n[\t ]*\n[\t ]*$", string)):
return False
else:
self.capture_output()
try:
# Add line returns here, to make sure that the statement is
# complete (except if '\' was used).
# This should probably be done in a different place (like
# maybe 'prefilter_input' method? For now, this works.
clean_string = string.rstrip('\n')
if not clean_string.endswith('\\'): clean_string +='\n\n'
is_complete = codeop.compile_command(clean_string,
"<string>", "exec")
self.release_output()
except Exception, e:
# XXX: Hack: return True so that the
# code gets executed and the error captured.
is_complete = True
return is_complete
def write(self, string, refresh=True):
""" Write some characters to the display.
Subclass should overide this method.
The refresh keyword argument is used in frontends with an
event loop, to choose whether the write should trigget an UI
refresh, and thus be syncrhonous, or not.
"""
print >>sys.__stderr__, string
def execute(self, python_string, raw_string=None):
""" Stores the raw_string in the history, and sends the
python string to the interpreter.
"""
if raw_string is None:
raw_string = python_string
# Create a false result, in case there is an exception
self.last_result = dict(number=self.prompt_number)
try:
try:
self.history.input_cache[-1] = raw_string.rstrip()
result = self.shell.execute(python_string)
self.last_result = result
self.render_result(result)
except:
self.show_traceback()
finally:
self.after_execute()
#--------------------------------------------------------------------------
# LineFrontEndBase interface
#--------------------------------------------------------------------------
def prefilter_input(self, string):
""" Prefilter the input to turn it in valid python.
"""
string = string.replace('\r\n', '\n')
string = string.replace('\t', 4*' ')
# Clean the trailing whitespace
string = '\n'.join(l.rstrip() for l in string.split('\n'))
return string
def after_execute(self):
""" All the operations required after an execution to put the
terminal back in a shape where it is usable.
"""
self.prompt_number += 1
self.new_prompt(self.input_prompt_template.substitute(
number=(self.last_result['number'] + 1)))
# Start a new empty history entry
self._add_history(None, '')
self.history_cursor = len(self.history.input_cache) - 1
def complete_current_input(self):
""" Do code completion on current line.
"""
if self.debug:
print >>sys.__stdout__, "complete_current_input",
line = self.input_buffer
new_line, completions = self.complete(line)
if len(completions)>1:
self.write_completion(completions, new_line=new_line)
elif not line == new_line:
self.input_buffer = new_line
if self.debug:
print >>sys.__stdout__, 'line', line
print >>sys.__stdout__, 'new_line', new_line
print >>sys.__stdout__, completions
def get_line_width(self):
""" Return the width of the line in characters.
"""
return 80
def write_completion(self, possibilities, new_line=None):
""" Write the list of possible completions.
new_line is the completed input line that should be displayed
after the completion are writen. If None, the input_buffer
before the completion is used.
"""
if new_line is None:
new_line = self.input_buffer
self.write('\n')
max_len = len(max(possibilities, key=len)) + 1
# Now we check how much symbol we can put on a line...
chars_per_line = self.get_line_width()
symbols_per_line = max(1, chars_per_line/max_len)
pos = 1
completion_string = []
for symbol in possibilities:
if pos < symbols_per_line:
completion_string.append(symbol.ljust(max_len))
pos += 1
else:
completion_string.append(symbol.rstrip() + '\n')
pos = 1
self.write(''.join(completion_string))
self.new_prompt(self.input_prompt_template.substitute(
number=self.last_result['number'] + 1))
self.input_buffer = new_line
def new_prompt(self, prompt):
""" Prints a prompt and starts a new editing buffer.
Subclasses should use this method to make sure that the
terminal is put in a state favorable for a new line
input.
"""
self.input_buffer = ''
self.write(prompt)
def continuation_prompt(self):
"""Returns the current continuation prompt.
"""
return ("."*(len(self.last_prompt)-2) + ': ')
def execute_command(self, command, hidden=False):
""" Execute a command, not only in the model, but also in the
view, if any.
"""
return self.shell.execute(command)
#--------------------------------------------------------------------------
# Private API
#--------------------------------------------------------------------------
def _on_enter(self, new_line_pos=0):
""" Called when the return key is pressed in a line editing
buffer.
Parameters
----------
new_line_pos : integer, optional
Position of the new line to add, starting from the
end (0 adds a new line after the last line, -1 before
the last line...)
Returns
-------
True if execution is triggered
"""
current_buffer = self.input_buffer
# XXX: This string replace is ugly, but there should be no way it
# fails.
prompt_less_buffer = re.sub('^' + self.continuation_prompt(),
'', current_buffer).replace('\n' + self.continuation_prompt(),
'\n')
cleaned_buffer = self.prefilter_input(prompt_less_buffer)
if self.is_complete(cleaned_buffer):
self.execute(cleaned_buffer, raw_string=current_buffer)
return True
else:
# Start a new line.
new_line_pos = -new_line_pos
lines = current_buffer.split('\n')[:-1]
prompt_less_lines = prompt_less_buffer.split('\n')
# Create the new line, with the continuation prompt, and the
# same amount of indent than the line above it.
new_line = self.continuation_prompt() + \
self._get_indent_string('\n'.join(
prompt_less_lines[:new_line_pos-1]))
if len(lines) == 1:
# We are starting a first continuation line. Indent it.
new_line += '\t'
elif current_buffer[:-1].split('\n')[-1].rstrip().endswith(':'):
# The last line ends with ":", autoindent the new line.
new_line += '\t'
if new_line_pos == 0:
lines.append(new_line)
else:
lines.insert(new_line_pos, new_line)
self.input_buffer = '\n'.join(lines)
def _get_indent_string(self, string):
""" Return the string of whitespace that prefixes a line. Used to
add the right amount of indendation when creating a new line.
"""
string = string.replace('\t', ' '*4)
string = string.split('\n')[-1]
indent_chars = len(string) - len(string.lstrip())
indent_string = '\t'*(indent_chars // 4) + \
' '*(indent_chars % 4)
return indent_string
|