diff --git a/IPython/Extensions/ipy_exportdb.py b/IPython/Extensions/ipy_exportdb.py new file mode 100644 index 0000000..6f74ea3 --- /dev/null +++ b/IPython/Extensions/ipy_exportdb.py @@ -0,0 +1,68 @@ +import IPython.ipapi +ip = IPython.ipapi.get() + +import os,pprint + +def export(filename = None): + + lines = ['import IPython.ipapi', 'ip = IPython.ipapi.get()',''] + + vars = ip.db.keys('autorestore/*') + vars.sort() + varstomove = [] + get = ip.db.get + + macros = [] + variables = [] + + for var in vars: + k = os.path.basename(var) + v = get(var) + + if k.startswith('_'): + continue + if isinstance(v, IPython.macro.Macro): + macros.append((k,v)) + if type(v) in [int, str, float]: + variables.append((k,v)) + + + + if macros: + lines.extend(['# === Macros ===' ,'']) + for k,v in macros: + lines.append("ip.defmacro('%s'," % k) + for line in v.value.splitlines(): + lines.append(' ' + repr(line+'\n')) + lines.extend([')', '']) + + if variables: + lines.extend(['','# === Variables ===','']) + for k,v in variables: + varstomove.append(k) + lines.append('%s = %s' % (k,repr(v))) + + lines.append('ip.to_user_ns("%s")' % (' '.join(varstomove))) + + bkms = ip.db.get('bookmarks',{}) + + if bkms: + lines.extend(['','# === Bookmarks ===','']) + lines.append("ip.db['bookmarks'] = %s " % pprint.pformat(bkms, indent = 2) ) + + aliases = ip.db.get('stored_aliases', {} ) + + if aliases: + lines.extend(['','# === Alias definitions ===','']) + for k,v in aliases.items(): + lines.append("ip.defalias('%s', %s)" % (k, repr(v[1]))) + + + + out = '\n'.join(lines) + + if filename: + open(filename,'w').write(out) + else: + print out + diff --git a/IPython/ipapi.py b/IPython/ipapi.py index 7f81feb..7595c68 100644 --- a/IPython/ipapi.py +++ b/IPython/ipapi.py @@ -333,7 +333,7 @@ class IPApi: """ import IPython.macro - print args + if len(args) == 1: return IPython.macro.Macro(args[0]) elif len(args) == 2: diff --git a/doc/ChangeLog b/doc/ChangeLog index 43b819e..31f1723 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -7,9 +7,13 @@ * Extensions/ipy_completers.py: introduced Olivier Lauzanne's safer & faster 'import' completer. - * Introduced new ipapi methods, _ip.defmacro(name, value) + * ipapi.py: Introduced new ipapi methods, _ip.defmacro(name, value) and _ip.defalias(name, command). + * Extensions/ipy_exportdb.py: New extension for exporting all the + %store'd data in a portable format (normal ipapi calls like + defmacro() etc.) + 2007-04-19 Ville Vainio * upgrade_dir.py: skip junk files like *.pyc