Skip to content

How to convert xonsh history from json to sqlite? #4132

Answered by anki-code
anki-code asked this question in Q&A
Discussion options

You must be logged in to vote

My solution for xonsh 0.9.24 on Ubuntu:

  1. You have json backend for history.
  2. Add $XONSH_HISTORY_BACKEND = 'sqlite' to .xonshrc
  3. Run xonsh to create ~/.local/share/xonsh/xonsh-history.sqlite and exit from all xonsh sessions.
  4. Open bash and run cd ~/.local/share/xonsh, run vim json2sqlite.xsh, copy and paste:
import json, sqlite3

conn = sqlite3.connect('xonsh-history.sqlite')
cur = conn.cursor()
sql = 'INSERT INTO xonsh_history ({}) VALUES ({});'

i = 0
for f in g`*.json`:
    sessionid = f.replace('xonsh-','').replace('.json', '')

    try:
        j = json.load(open(f))
    except:
        print(f'Skip {f}: json error')
        continue

    for c in j['data']['cmds']:
        row = {
    …

Replies: 1 comment 1 reply

Comment options

anki-code
Mar 2, 2021
Maintainer Author

You must be logged in to vote
1 reply
@eugenesvk
Comment options

Answer selected by anki-code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants