Created
September 13, 2021 17:33
-
-
Save sosie-js/0ab8ae859c0c02b1f41d7f438bb6054a to your computer and use it in GitHub Desktop.
Determine Anki Database version
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
################################################# | |
# Determine Anki Database version, usefull to track DB version errors | |
# | |
# | |
# Installation steps for 2.1.44 (can be others versions too): | |
# | |
# 1) Remove previous versions | |
# sudo apt-get remove anki | |
# sudo pip3 uninstall anki | |
# sudo pip3 uninstall aqt | |
# 2) install the good one compatible with anki-cli-unofficial | |
# sudo pip3 install aqt==2.1.44 | |
# sudo pip3 install anki-cli-unofficial | |
# => Successfully installed anki-2.1.44 aqt-2.1.44 | |
# 3) create clean directory (and close anki): | |
# anki -l en -b ~/ankidir-2.1.44 | |
# 4) Prepare scite for pyhton, add in python properties | |
# if PLAT_GTK | |
# command.go.*.py=python3 -u "$(FileNameExt)" | |
# command.build.SConscript=scons --up . | |
# command.build.SConstruct=scons . | |
# command.debug.*.py=python3 -m pdb -u "$(FileNameExt)" | |
# Usage: | |
# 1) Restore clean | |
# rm -r ~/ankidir | |
# cp -r ~/ankidir-2.1.44 ~/ankidir | |
# rm -r ./ankidir | |
# cp -r ./ankidir-2.1.44 ./ankidir | |
# 2) Check database version with sqlite3 tool | |
# sqlite3 ankidir/User\ 1/collection.anki2 "SELECT ver from col" | |
# 3) Run this script in the shell | |
# python3 ankiDBversion.py | |
# 4) Run ths script in scite, Menu Tools > go or [F5] | |
# -> returns 18 instead of 17 | |
# -> db can not be opened, anki.errors.DBError: DBError { info: "", kind: FileTooNew } | |
################################################# | |
import os | |
import csv | |
import sys | |
# Load Anki library | |
sys.path.append("anki") | |
#from anki.collection import Collection | |
from anki.storage import Collection | |
#Path utils | |
import os | |
def folder(dirpath) -> str: | |
folder = os.getenv("FOLDER", dirpath) | |
if not os.path.exists(folder): | |
print("creating", folder) | |
os.mkdir(folder) | |
return folder | |
def col_path(dirpath, name,prefix="collection") -> str: | |
if name !="": | |
name="."+name | |
return os.path.join(folder(dirpath), prefix+name+".anki2") | |
# variables to set: | |
################################################# | |
COLLECTION_DIR=os.path.join(os.path.expanduser('.'), 'ankidir/User 1/') | |
COLLECTION_PREFIX = 'collection' | |
COLLECTION_NAME = '' | |
COLLECTION_PATH=col_path(COLLECTION_DIR, COLLECTION_NAME,COLLECTION_PREFIX) | |
################################################# | |
def get_col(dirpath, name,prefix): | |
col = Collection(col_path(dirpath,name,prefix), server=True, log=True) | |
# don't hold an outer transaction open | |
col.db.rollback() | |
return col | |
col=get_col(COLLECTION_DIR, COLLECTION_NAME,COLLECTION_PREFIX) | |
results=col.db.list("SELECT ver from col") | |
for dbVersion in results: | |
print(dbVersion) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment