| 18 | |
| 19 | |
| 20 | class MagicsDisplay(object): |
| 21 | def __init__(self, magics_manager, ignore=None): |
| 22 | self.ignore = ignore if ignore else [] |
| 23 | self.magics_manager = magics_manager |
| 24 | |
| 25 | def _lsmagic(self): |
| 26 | """The main implementation of the %lsmagic""" |
| 27 | mesc = magic_escapes['line'] |
| 28 | cesc = magic_escapes['cell'] |
| 29 | mman = self.magics_manager |
| 30 | magics = mman.lsmagic() |
| 31 | out = ['Available line magics:', |
| 32 | mesc + (' '+mesc).join(sorted([m for m,v in magics['line'].items() if (v not in self.ignore)])), |
| 33 | '', |
| 34 | 'Available cell magics:', |
| 35 | cesc + (' '+cesc).join(sorted([m for m,v in magics['cell'].items() if (v not in self.ignore)])), |
| 36 | '', |
| 37 | mman.auto_status()] |
| 38 | return '\n'.join(out) |
| 39 | |
| 40 | def _repr_pretty_(self, p, cycle): |
| 41 | p.text(self._lsmagic()) |
| 42 | |
| 43 | def __str__(self): |
| 44 | return self._lsmagic() |
| 45 | |
| 46 | def _jsonable(self): |
| 47 | """turn magics dict into jsonable dict of the same structure |
| 48 | |
| 49 | replaces object instances with their class names as strings |
| 50 | """ |
| 51 | magic_dict = {} |
| 52 | mman = self.magics_manager |
| 53 | magics = mman.lsmagic() |
| 54 | for key, subdict in magics.items(): |
| 55 | d = {} |
| 56 | magic_dict[key] = d |
| 57 | for name, obj in subdict.items(): |
| 58 | try: |
| 59 | classname = obj.__self__.__class__.__name__ |
| 60 | except AttributeError: |
| 61 | classname = 'Other' |
| 62 | |
| 63 | d[name] = classname |
| 64 | return magic_dict |
| 65 | |
| 66 | def _repr_json_(self): |
| 67 | return self._jsonable() |
| 68 | |
| 69 | |
| 70 | @magics_class |