ipy_exportdb.py
77 lines
| 2.0 KiB
| text/x-python
|
PythonLexer
Brian Granger
|
r2027 | from IPython.core import ipapi | ||
Brian Granger
|
r2033 | from IPython.core import macro | ||
Brian Granger
|
r2027 | ip = ipapi.get() | ||
vivainio
|
r680 | |||
import os,pprint | ||||
def export(filename = None): | ||||
Brian Granger
|
r2027 | lines = ['import IPython.core.ipapi', 'ip = IPython.core.ipapi.get()',''] | ||
vivainio
|
r680 | |||
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 | ||||
Brian Granger
|
r2033 | if isinstance(v, macro.Macro): | ||
vivainio
|
r680 | 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(): | ||||
vivainio
|
r809 | try: | ||
lines.append("ip.defalias('%s', %s)" % (k, repr(v[1]))) | ||||
except (AttributeError, TypeError): | ||||
pass | ||||
vivainio
|
r680 | |||
env = ip.db.get('stored_env') | ||||
if env: | ||||
lines.extend(['','# === Stored env vars ===','']) | ||||
lines.append("ip.db['stored_env'] = %s " % pprint.pformat(env, indent = 2) ) | ||||
out = '\n'.join(lines) | ||||
if filename: | ||||
open(filename,'w').write(out) | ||||
else: | ||||
print out | ||||