storemagic.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. # -*- coding: utf-8 -*-
  2. """
  3. %store magic for lightweight persistence.
  4. Stores variables, aliases and macros in IPython's database.
  5. To automatically restore stored variables at startup, add this to your
  6. :file:`ipython_config.py` file::
  7. c.StoreMagics.autorestore = True
  8. """
  9. # Copyright (c) IPython Development Team.
  10. # Distributed under the terms of the Modified BSD License.
  11. import inspect, os, sys, textwrap
  12. from IPython.core.error import UsageError
  13. from IPython.core.magic import Magics, magics_class, line_magic
  14. from IPython.testing.skipdoctest import skip_doctest
  15. from traitlets import Bool
  16. def restore_aliases(ip, alias=None):
  17. staliases = ip.db.get('stored_aliases', {})
  18. if alias is None:
  19. for k,v in staliases.items():
  20. #print "restore alias",k,v # dbg
  21. #self.alias_table[k] = v
  22. ip.alias_manager.define_alias(k,v)
  23. else:
  24. ip.alias_manager.define_alias(alias, staliases[alias])
  25. def refresh_variables(ip):
  26. db = ip.db
  27. for key in db.keys('autorestore/*'):
  28. # strip autorestore
  29. justkey = os.path.basename(key)
  30. try:
  31. obj = db[key]
  32. except KeyError:
  33. print("Unable to restore variable '%s', ignoring (use %%store -d to forget!)" % justkey)
  34. print("The error was:", sys.exc_info()[0])
  35. else:
  36. #print "restored",justkey,"=",obj #dbg
  37. ip.user_ns[justkey] = obj
  38. def restore_dhist(ip):
  39. ip.user_ns['_dh'] = ip.db.get('dhist',[])
  40. def restore_data(ip):
  41. refresh_variables(ip)
  42. restore_aliases(ip)
  43. restore_dhist(ip)
  44. @magics_class
  45. class StoreMagics(Magics):
  46. """Lightweight persistence for python variables.
  47. Provides the %store magic."""
  48. autorestore = Bool(False, help=
  49. """If True, any %store-d variables will be automatically restored
  50. when IPython starts.
  51. """
  52. ).tag(config=True)
  53. def __init__(self, shell):
  54. super(StoreMagics, self).__init__(shell=shell)
  55. self.shell.configurables.append(self)
  56. if self.autorestore:
  57. restore_data(self.shell)
  58. @skip_doctest
  59. @line_magic
  60. def store(self, parameter_s=''):
  61. """Lightweight persistence for python variables.
  62. Example::
  63. In [1]: l = ['hello',10,'world']
  64. In [2]: %store l
  65. Stored 'l' (list)
  66. In [3]: exit
  67. (IPython session is closed and started again...)
  68. ville@badger:~$ ipython
  69. In [1]: l
  70. NameError: name 'l' is not defined
  71. In [2]: %store -r
  72. In [3]: l
  73. Out[3]: ['hello', 10, 'world']
  74. Usage:
  75. * ``%store`` - Show list of all variables and their current
  76. values
  77. * ``%store spam bar`` - Store the *current* value of the variables spam
  78. and bar to disk
  79. * ``%store -d spam`` - Remove the variable and its value from storage
  80. * ``%store -z`` - Remove all variables from storage
  81. * ``%store -r`` - Refresh all variables, aliases and directory history
  82. from store (overwrite current vals)
  83. * ``%store -r spam bar`` - Refresh specified variables and aliases from store
  84. (delete current val)
  85. * ``%store foo >a.txt`` - Store value of foo to new file a.txt
  86. * ``%store foo >>a.txt`` - Append value of foo to file a.txt
  87. It should be noted that if you change the value of a variable, you
  88. need to %store it again if you want to persist the new value.
  89. Note also that the variables will need to be pickleable; most basic
  90. python types can be safely %store'd.
  91. Also aliases can be %store'd across sessions.
  92. To remove an alias from the storage, use the %unalias magic.
  93. """
  94. opts,argsl = self.parse_options(parameter_s,'drz',mode='string')
  95. args = argsl.split()
  96. ip = self.shell
  97. db = ip.db
  98. # delete
  99. if 'd' in opts:
  100. try:
  101. todel = args[0]
  102. except IndexError as e:
  103. raise UsageError('You must provide the variable to forget') from e
  104. else:
  105. try:
  106. del db['autorestore/' + todel]
  107. except BaseException as e:
  108. raise UsageError("Can't delete variable '%s'" % todel) from e
  109. # reset
  110. elif 'z' in opts:
  111. for k in db.keys('autorestore/*'):
  112. del db[k]
  113. elif 'r' in opts:
  114. if args:
  115. for arg in args:
  116. try:
  117. obj = db['autorestore/' + arg]
  118. except KeyError:
  119. try:
  120. restore_aliases(ip, alias=arg)
  121. except KeyError:
  122. print("no stored variable or alias %s" % arg)
  123. else:
  124. ip.user_ns[arg] = obj
  125. else:
  126. restore_data(ip)
  127. # run without arguments -> list variables & values
  128. elif not args:
  129. vars = db.keys('autorestore/*')
  130. vars.sort()
  131. if vars:
  132. size = max(map(len, vars))
  133. else:
  134. size = 0
  135. print('Stored variables and their in-db values:')
  136. fmt = '%-'+str(size)+'s -> %s'
  137. get = db.get
  138. for var in vars:
  139. justkey = os.path.basename(var)
  140. # print 30 first characters from every var
  141. print(fmt % (justkey, repr(get(var, '<unavailable>'))[:50]))
  142. # default action - store the variable
  143. else:
  144. # %store foo >file.txt or >>file.txt
  145. if len(args) > 1 and args[1].startswith(">"):
  146. fnam = os.path.expanduser(args[1].lstrip(">").lstrip())
  147. if args[1].startswith(">>"):
  148. fil = open(fnam, "a", encoding="utf-8")
  149. else:
  150. fil = open(fnam, "w", encoding="utf-8")
  151. with fil:
  152. obj = ip.ev(args[0])
  153. print("Writing '%s' (%s) to file '%s'." % (args[0],
  154. obj.__class__.__name__, fnam))
  155. if not isinstance (obj, str):
  156. from pprint import pprint
  157. pprint(obj, fil)
  158. else:
  159. fil.write(obj)
  160. if not obj.endswith('\n'):
  161. fil.write('\n')
  162. return
  163. # %store foo
  164. for arg in args:
  165. try:
  166. obj = ip.user_ns[arg]
  167. except KeyError:
  168. # it might be an alias
  169. name = arg
  170. try:
  171. cmd = ip.alias_manager.retrieve_alias(name)
  172. except ValueError as e:
  173. raise UsageError("Unknown variable '%s'" % name) from e
  174. staliases = db.get('stored_aliases',{})
  175. staliases[name] = cmd
  176. db['stored_aliases'] = staliases
  177. print("Alias stored: %s (%s)" % (name, cmd))
  178. return
  179. else:
  180. modname = getattr(inspect.getmodule(obj), '__name__', '')
  181. if modname == '__main__':
  182. print(textwrap.dedent("""\
  183. Warning:%s is %s
  184. Proper storage of interactively declared classes (or instances
  185. of those classes) is not possible! Only instances
  186. of classes in real modules on file system can be %%store'd.
  187. """ % (arg, obj) ))
  188. return
  189. #pickled = pickle.dumps(obj)
  190. db[ 'autorestore/' + arg ] = obj
  191. print("Stored '%s' (%s)" % (arg, obj.__class__.__name__))
  192. def load_ipython_extension(ip):
  193. """Load the extension in IPython."""
  194. ip.register_magics(StoreMagics)