klieret / AnkiPandas

Analyze and manipulate your Anki collection using pandas! 🌠🐼

Home Page:https://ankipandas.rtfd.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[request] need read only access to locked database

thiswillbeyourgithub opened this issue · comments

Hi,

I'm the dev behind AnnA an addition to anki that allows to use machine learning on your queue to reduce unnecessary reviews.

I was thinking of a new feature that would make great use of ankipandas, but unfortunately there is no way to open an anki database if anki is already opened.

Given that my use case needs only "read only" access, could you consider adding this feature?

Thanks a lot!

Hi @thiswillbeyourgithub AnnA looks really cool!

How about a quick & dirty approach: You can probably simply copy the database file to a temporary directory and open that. This will basically give you read only access.
So something like this (not tested at all, from top of my head):

from pathlib import Path
import shutil
import tmpfile
import ankipandas as ap
db_path = ap.paths.find_db()
with tempfile.TemporaryDirectory() as tmpdirname:
    new_loc = Path(tempdirname) / db_path.name
    shutil.copy(db_path, new_loc)
    col = ap.Collection(new_loc)
    # do your stuff with the collection
# Copy will get deleted

Thanks a bunch, I thought about doing this but was not sure about how to create temporary file on all platforms.

Thanks a lot!

Sure, let me know if it works!

I'll try to, but I won't try it for quite some time. Don't wait for me :)

For reference : I'm using some more naive code :

from ankipandas import Collection, find_db
from shutil import copy
from pathlib import Path

original_db = find_db(user=profile_name)  # profile name supplied by user, None to use the first found
Path.mkdir(Path("cache"), exist_ok=True)  # create cache folder
temp_db = copy(original_db, f"cache/{deckname.replace(' ', '_')}")  # copying database to cache

col = Collection(path=temp_db)  # load ankipandas on the temporary directory

I'll use this in the future but I think it's still a very valuable feature to implement (for example with an argument like read_only=True)

Thanks a lot for being so reactive and helpful last time, it was very useful and I'm really appreciative of it.

Have a nice day!