##// END OF EJS Templates
Refactor %magic into a lsmagic_docs API function.
Bradley M. Froehle -
Show More
@@ -340,6 +340,30 b' class MagicsManager(Configurable):'
340 """
340 """
341 return self.magics
341 return self.magics
342
342
343 def lsmagic_docs(self, brief=False, missing=''):
344 """Return dict of documentation of magic functions.
345
346 The return dict has the keys 'line' and 'cell', corresponding to the
347 two types of magics we support. Each value is a dict keyed by magic
348 name whose value is the function docstring. If a docstring is
349 unavailable, the value of `missing` is used instead.
350
351 If brief is True, only the first line of each docstring will be returned.
352 """
353 docs = {}
354 for m_type in self.magics:
355 m_docs = {}
356 for m_name, m_func in self.magics[m_type].iteritems():
357 if m_func.__doc__:
358 if brief:
359 m_docs[m_name] = m_func.__doc__.split('\n', 1)[0]
360 else:
361 m_docs[m_name] = m_func.__doc__.rstrip()
362 else:
363 m_docs[m_name] = missing
364 docs[m_type] = m_docs
365 return docs
366
343 def register(self, *magic_objects):
367 def register(self, *magic_objects):
344 """Register one or more instances of Magics.
368 """Register one or more instances of Magics.
345
369
@@ -59,6 +59,24 b' class BasicMagics(Magics):'
59 """List currently available magic functions."""
59 """List currently available magic functions."""
60 print(self._lsmagic())
60 print(self._lsmagic())
61
61
62 def _magic_docs(self, brief=False, rest=False):
63 """Return docstrings from magic functions."""
64 mman = self.shell.magics_manager
65 docs = mman.lsmagic_docs(brief, missing='No documentation')
66
67 if rest:
68 format_string = '**%s%s**::\n\n\t%s\n\n'
69 else:
70 format_string = '%s%s:\n\t%s\n'
71
72 return ''.join(
73 [format_string % (ESC_MAGIC, fname, fndoc)
74 for fname, fndoc in sorted(docs['line'].items())]
75 +
76 [format_string % (ESC_MAGIC*2, fname, fndoc)
77 for fname, fndoc in sorted(docs['cell'].items())]
78 )
79
62 @line_magic
80 @line_magic
63 def magic(self, parameter_s=''):
81 def magic(self, parameter_s=''):
64 """Print information about the magic function system.
82 """Print information about the magic function system.
@@ -74,37 +92,9 b' class BasicMagics(Magics):'
74 except IndexError:
92 except IndexError:
75 pass
93 pass
76
94
77 magic_docs = []
95 brief = (mode == 'brief')
78 escapes = dict(line=ESC_MAGIC, cell=ESC_MAGIC*2)
96 rest = (mode == 'rest')
79 magics = self.shell.magics_manager.magics
97 magic_docs = self._magic_docs(brief, rest)
80
81 for mtype in ('line', 'cell'):
82 escape = escapes[mtype]
83 for fname, fn in sorted(magics[mtype].items()):
84
85 if mode == 'brief':
86 # only first line
87 if fn.__doc__:
88 fndoc = fn.__doc__.split('\n',1)[0]
89 else:
90 fndoc = 'No documentation'
91 else:
92 if fn.__doc__:
93 fndoc = fn.__doc__.rstrip()
94 else:
95 fndoc = 'No documentation'
96
97 if mode == 'rest':
98 rest_docs.append('**%s%s**::\n\n\t%s\n\n' %
99 (escape, fname, fndoc))
100 else:
101 magic_docs.append('%s%s:\n\t%s\n' %
102 (escape, fname, fndoc))
103
104 magic_docs = ''.join(magic_docs)
105
106 if mode == 'rest':
107 return "".join(rest_docs)
108
98
109 if mode == 'latex':
99 if mode == 'latex':
110 print(self.format_latex(magic_docs))
100 print(self.format_latex(magic_docs))
General Comments 0
You need to be logged in to leave comments. Login now