Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b7bb752

Browse files
committed
First go an implementing a=!ls and a=%who syntax.
These things used to be in ext_rescapture.py, but they need to be in prefilter. I have a draft, but I will need to do further refactoting of prefilter to get this working.
1 parent c296652 commit b7bb752

3 files changed

Lines changed: 80 additions & 10 deletions

File tree

IPython/core/prefilter.py

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
# Global utilities, errors and constants
4343
#-----------------------------------------------------------------------------
4444

45-
45+
# Warning, these cannot be changed unless various regular expressions
46+
# are updated in a number of places. Not great, but at least we told you.
4647
ESC_SHELL = '!'
4748
ESC_SH_CAP = '!!'
4849
ESC_HELP = '?'
@@ -231,6 +232,7 @@ def get_handler_by_esc(self, esc_str):
231232

232233
def prefilter_line_info(self, line_info):
233234
"""Prefilter a line that has been converted to a LineInfo object."""
235+
# print "prefilter_line_info: ", line_info
234236
handler = self.find_handler(line_info)
235237
return handler.handle(line_info)
236238

@@ -239,12 +241,15 @@ def find_handler(self, line_info):
239241
for checker in self.sorted_checkers:
240242
handler = checker.check(line_info)
241243
if handler:
244+
# print "Used checker: ", checker
245+
# print "Using handler: ", handler
242246
return handler
243247
return self.get_handler_by_name('normal')
244248

245249
def prefilter_line(self, line, continue_prompt):
246250
"""Prefilter a single input line as text."""
247251

252+
# print "prefilter_line: ", line, continue_prompt
248253
# All handlers *must* return a value, even if it's blank ('').
249254

250255
# Lines are NOT logged here. Handlers should process the line as
@@ -254,7 +259,7 @@ def prefilter_line(self, line, continue_prompt):
254259
# growl.notify("_prefilter: ", "line = %s\ncontinue_prompt = %s" % (line, continue_prompt))
255260

256261
# save the line away in case we crash, so the post-mortem handler can
257-
# record it
262+
# record it
258263
self.shell._last_input_line = line
259264

260265
if not line:
@@ -284,7 +289,9 @@ def prefilter_line(self, line, continue_prompt):
284289
if continue_prompt and not self.multi_line_specials:
285290
return normal_handler.handle(line_info)
286291

287-
return self.prefilter_line_info(line_info)
292+
prefiltered = self.prefilter_line_info(line_info)
293+
# print "prefiltered line: %r" % prefiltered
294+
return prefiltered
288295

289296
def prefilter_lines(self, lines, continue_prompt):
290297
"""Prefilter multiple input lines of text.
@@ -330,6 +337,8 @@ def check(self, line_info):
330337
"""Inspect line_info and return a handler or None."""
331338
return None
332339

340+
def __str__(self):
341+
return "<%s(priority=%i)>" % (self.__class__.__name__, self.priority)
333342

334343
class EmacsChecker(PrefilterChecker):
335344

@@ -401,6 +410,10 @@ def check(self, line_info):
401410
return self.prefilter_manager.get_handler_by_esc(line_info.pre_char)
402411

403412

413+
_assign_system_re = re.compile('\s*=\s*!(?P<cmd>.*)')
414+
_assign_magic_re = re.compile('\s*=\s*%(?P<cmd>.*)')
415+
416+
404417
class AssignmentChecker(PrefilterChecker):
405418

406419
priority = Int(600, config=True)
@@ -412,8 +425,15 @@ def check(self, line_info):
412425
This allows users to assign to either alias or magic names true python
413426
variables (the magic/alias systems always take second seat to true
414427
python code). E.g. ls='hi', or ls,that=1,2"""
415-
if line_info.the_rest and line_info.the_rest[0] in '=,':
416-
return self.prefilter_manager.get_handler_by_name('normal')
428+
if line_info.the_rest:
429+
if line_info.the_rest[0] in '=,':
430+
# m = _assign_system_re.match(line_info.the_rest)
431+
# if m is not None:
432+
# return self.prefilter_manager.get_handler_by_name('assign_system')
433+
# m = _assign_magic_re.match(line_info.the_rest)
434+
# if m is not None:
435+
# return self.prefilter_manager.get_handler_by_name('assign_magic')
436+
return self.prefilter_manager.get_handler_by_name('normal')
417437
else:
418438
return None
419439

@@ -529,6 +549,7 @@ def prefilter_manager(self):
529549
return PrefilterManager.get_instances(root=self.root)[0]
530550

531551
def handle(self, line_info):
552+
# print "normal: ", line_info
532553
"""Handle normal input lines. Use as a template for handlers."""
533554

534555
# With autoindent on, we need some way to exit the input loop, and I
@@ -547,11 +568,52 @@ def handle(self, line_info):
547568
self.shell.log(line, line, continue_prompt)
548569
return line
549570

571+
def __str__(self):
572+
return "<%s(name=%s)>" % (self.__class__.__name__, self.handler_name)
573+
574+
class AssignSystemHandler(PrefilterHandler):
575+
576+
handler_name = Str('assign_system')
577+
578+
@auto_attr
579+
def normal_handler(self):
580+
return self.prefilter_manager.get_handler_by_name('normal')
581+
582+
def handle(self, line_info):
583+
new_line = line_info.line
584+
m = _assign_system_re.match(line_info.the_rest)
585+
if m is not None:
586+
cmd = m.group('cmd')
587+
expr = make_quoted_expr("sc -l =%s" % cmd)
588+
new_line = '%s%s = get_ipython().magic(%s)' % (line_info.pre_whitespace,
589+
line_info.ifun, expr)
590+
self.shell.log(line_info.line, new_line, line_info.continue_prompt)
591+
return new_line
592+
593+
594+
class AssignMagicHandler(PrefilterHandler):
595+
596+
handler_name = Str('assign_magic')
597+
598+
@auto_attr
599+
def normal_handler(self):
600+
return self.prefilter_manager.get_handler_by_name('normal')
601+
602+
def handle(self, line_info):
603+
new_line = line_info.line
604+
m = _assign_magic_re.match(line_info.the_rest)
605+
if m is not None:
606+
cmd = m.group('cmd')
607+
expr = make_quoted_expr(cmd)
608+
new_line = '%s%s = get_ipython().magic(%s)' % (line_info.pre_whitespace,
609+
line_info.ifun, expr)
610+
self.shell.log(line_info.line, new_line, line_info.continue_prompt)
611+
return new_line
612+
550613

551614
class AliasHandler(PrefilterHandler):
552615

553616
handler_name = Str('alias')
554-
esc_strings = List([])
555617

556618
@auto_attr
557619
def alias_manager(self):
@@ -768,5 +830,7 @@ def handle(self, line_info):
768830
AutoHandler,
769831
HelpHandler,
770832
EmacsHandler
833+
# AssignSystemHandler,
834+
# AssignMagicHandler
771835
]
772836

IPython/core/splitinput.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,25 @@
4646
r'\s*([\w\.]+)'
4747
r'(\s+.*$|$)')
4848

49+
# r'[\w\.]+'
50+
# r'\s*=\s*%.*'
4951

5052
def split_user_input(line, pattern=None):
51-
"""Split user input into pre-char/whitespace, function part and rest."""
53+
"""Split user input into pre-char/whitespace, function part and rest.
54+
55+
This is currently handles lines with '=' in them in a very inconsistent
56+
manner.
57+
"""
5258

5359
if pattern is None:
5460
pattern = line_split
5561
match = pattern.match(line)
5662
if not match:
57-
#print "match failed for line '%s'" % line
63+
# print "match failed for line '%s'" % line
5864
try:
5965
ifun, the_rest = line.split(None,1)
6066
except ValueError:
61-
#print "split failed for line '%s'" % line
67+
# print "split failed for line '%s'" % line
6268
ifun, the_rest = line,''
6369
pre = re.match('^(\s*)(.*)',line).groups()[0]
6470
else:

IPython/extensions/ext_rescapture.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def init_handlers():
4747

4848
init_handlers()
4949

50-
def regex_prefilter_f(self,line):
50+
def regex_prefilter_f(self,line):
5151
for pat, handler in ip.meta.re_prefilters:
5252
mo = pat.match(line)
5353
if mo:

0 commit comments

Comments
 (0)