##// END OF EJS Templates
%lsmagic returns a RichRepr of the magics dict...
MinRK -
Show More
@@ -327,22 +327,6 b' class MagicsManager(Configurable):'
327 """Return descriptive string with automagic status."""
327 """Return descriptive string with automagic status."""
328 return self._auto_status[self.auto_magic]
328 return self._auto_status[self.auto_magic]
329
329
330 def lsmagic_info(self):
331 """Return the magics as a list of dicts"""
332 magic_list = []
333 for m_type in self.magics:
334 for m_name,mgc in self.magics[m_type].items():
335 try:
336 magic_list.append({'name':m_name,'type':m_type,'class':mgc.im_class.__name__})
337 except AttributeError:
338 magic_list.append({'name':m_name,'type':m_type,'class':'Other'})
339 return magic_list
340
341 def lsmagic_json(self):
342 """Wrap lsmagic_info() in a JSON object"""
343 from IPython.display import JSON
344 return JSON(json.dumps(self.lsmagic_info()))
345
346 def lsmagic(self):
330 def lsmagic(self):
347 """Return a dict of currently available magic functions.
331 """Return a dict of currently available magic functions.
348
332
@@ -15,6 +15,7 b' from __future__ import print_function'
15
15
16 # Stdlib
16 # Stdlib
17 import io
17 import io
18 import json
18 import sys
19 import sys
19 from pprint import pformat
20 from pprint import pformat
20
21
@@ -33,6 +34,55 b' from IPython.utils.warn import warn, error'
33 # Magics class implementation
34 # Magics class implementation
34 #-----------------------------------------------------------------------------
35 #-----------------------------------------------------------------------------
35
36
37 class MagicsDisplay(object):
38 def __init__(self, magics_manager):
39 self.magics_manager = magics_manager
40
41 def _lsmagic(self):
42 """The main implementation of the %lsmagic"""
43 mesc = magic_escapes['line']
44 cesc = magic_escapes['cell']
45 mman = self.magics_manager
46 magics = mman.lsmagic()
47 out = ['Available line magics:',
48 mesc + (' '+mesc).join(sorted(magics['line'])),
49 '',
50 'Available cell magics:',
51 cesc + (' '+cesc).join(sorted(magics['cell'])),
52 '',
53 mman.auto_status()]
54 return '\n'.join(out)
55
56 def _repr_pretty_(self, p, cycle):
57 p.text(self._lsmagic())
58
59 def __str__(self):
60 return self._lsmagic()
61
62 def _jsonable(self):
63 """turn magics dict into jsonable dict of the same structure
64
65 replaces object instances with their class names as strings
66 """
67 magic_dict = {}
68 mman = self.magics_manager
69 magics = mman.lsmagic()
70 for key, subdict in magics.items():
71 d = {}
72 magic_dict[key] = d
73 for name, obj in subdict.items():
74 try:
75 classname = obj.im_class.__name__
76 except AttributeError:
77 classname = 'Other'
78
79 d[name] = classname
80 return magic_dict
81
82 def _repr_json_(self):
83 return json.dumps(self._jsonable())
84
85
36 @magics_class
86 @magics_class
37 class BasicMagics(Magics):
87 class BasicMagics(Magics):
38 """Magics that provide central IPython functionality.
88 """Magics that provide central IPython functionality.
@@ -124,24 +174,10 b' class BasicMagics(Magics):'
124 magic_escapes['cell'], name,
174 magic_escapes['cell'], name,
125 magic_escapes['cell'], target))
175 magic_escapes['cell'], target))
126
176
127 def _lsmagic(self):
128 mesc = magic_escapes['line']
129 cesc = magic_escapes['cell']
130 mman = self.shell.magics_manager
131 magics = mman.lsmagic()
132 out = ['Available line magics:',
133 mesc + (' '+mesc).join(sorted(magics['line'])),
134 '',
135 'Available cell magics:',
136 cesc + (' '+cesc).join(sorted(magics['cell'])),
137 '',
138 mman.auto_status()]
139 return '\n'.join(out)
140
141 @line_magic
177 @line_magic
142 def lsmagic(self, parameter_s=''):
178 def lsmagic(self, parameter_s=''):
143 """List currently available magic functions."""
179 """List currently available magic functions."""
144 print(self._lsmagic())
180 return MagicsDisplay(self.shell.magics_manager)
145
181
146 def _magic_docs(self, brief=False, rest=False):
182 def _magic_docs(self, brief=False, rest=False):
147 """Return docstrings from magic functions."""
183 """Return docstrings from magic functions."""
@@ -29,6 +29,8 b' from threading import Thread'
29 # System library imports
29 # System library imports
30 from IPython.external.qt import QtGui,QtCore
30 from IPython.external.qt import QtGui,QtCore
31
31
32 from IPython.core.magic import magic_escapes
33
32 def background(f):
34 def background(f):
33 """call a function in a simple thread, to prevent blocking"""
35 """call a function in a simple thread, to prevent blocking"""
34 t = Thread(target=f)
36 t = Thread(target=f)
@@ -621,8 +623,8 b' class MainWindow(QtGui.QMainWindow):'
621 Parameters
623 Parameters
622 ----------
624 ----------
623 display_data : dict,
625 display_data : dict,
624 dict of display_data for the magics list.
626 dict of display_data for the magics dict of a MagicsManager.
625 Expects json data, as the result of MagicsManager.lsmagic_json()
627 Expects json data, as the result of %lsmagic
626
628
627 """
629 """
628 for k,v in self._magic_menu_dict.items():
630 for k,v in self._magic_menu_dict.items():
@@ -631,27 +633,26 b' class MainWindow(QtGui.QMainWindow):'
631
633
632 if not display_data:
634 if not display_data:
633 return
635 return
634
636
635 mlist = json.loads(display_data['data'].get('application/json', []))
637 if display_data['status'] != 'ok':
636
638 self.log.warn("%%lsmagic user-expression failed: %s" % display_data)
637 for magic in mlist:
639 return
638 cell = (magic['type'] == 'cell')
640
639 name = magic['name']
641 mdict = json.loads(display_data['data'].get('application/json', {}))
640 mclass = magic['class']
642
641 if cell :
643 for mtype in sorted(mdict):
642 prefix='%%'
644 subdict = mdict[mtype]
643 else :
645 prefix = magic_escapes[mtype]
644 prefix='%'
646 for name in sorted(subdict):
645 magic_menu = self._get_magic_menu(mclass)
647 mclass = subdict[name]
646
648 magic_menu = self._get_magic_menu(mclass)
647 pmagic = '%s%s'%(prefix,name)
649 pmagic = prefix + name
648
650 xaction = QtGui.QAction(pmagic,
649 xaction = QtGui.QAction(pmagic,
651 self,
650 self,
652 triggered=self._make_dynamic_magic(pmagic)
651 triggered=self._make_dynamic_magic(pmagic)
653 )
652 )
654 magic_menu.addAction(xaction)
653 magic_menu.addAction(xaction)
655 self.all_magic_menu.addAction(xaction)
654 self.all_magic_menu.addAction(xaction)
655
656
656 def update_all_magic_menu(self):
657 def update_all_magic_menu(self):
657 """ Update the list of magics in the "All Magics..." Menu
658 """ Update the list of magics in the "All Magics..." Menu
@@ -660,7 +661,7 b' class MainWindow(QtGui.QMainWindow):'
660 menu with the list received back
661 menu with the list received back
661
662
662 """
663 """
663 self.active_frontend._silent_exec_callback('get_ipython().magics_manager.lsmagic_json()',
664 self.active_frontend._silent_exec_callback('get_ipython().magic("lsmagic")',
664 self.populate_all_magic_menu)
665 self.populate_all_magic_menu)
665
666
666 def _get_magic_menu(self,menuidentifier, menulabel=None):
667 def _get_magic_menu(self,menuidentifier, menulabel=None):
General Comments 0
You need to be logged in to leave comments. Login now