public
Created

  • Download Gist
migrate_history.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
"""
Migrate old IPython history (readline) to new IPython history (sqlite)
 
Author: Min RK (benjaminrk@gmail.com)
License: Public Domain
"""
import os
 
from IPython.core.interactiveshell import InteractiveShell
from IPython.core.history import HistoryManager
from IPython.utils.path import locate_profile, get_ipython_dir
 
def migrate_history(old_histfile, new_histfile):
"""populate an IPython sqlite history from an old readline history file"""
# shell = InteractiveShell.instance()
history = HistoryManager(hist_file=new_histfile,
# this line shouldn't be necessary, but it is in 0.13
shell = InteractiveShell.instance()
)
with open(old_histfile) as f:
# iterate through readline history,
# and write to the new history database
for linenum, line in enumerate(f):
history.store_inputs(linenum, line)
 
if __name__ == '__main__':
# the default location of old history file
old = os.path.join(get_ipython_dir(), "history")
# the default location of the new history file
new = os.path.join(locate_profile(), "history.sqlite")
migrate_history(old, new)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.