mirror of
https://github.com/ankitects/anki.git
synced 2025-09-18 22:12:21 -04:00
move storage logic into collection.py; fix export bug
https://anki.tenderapp.com/discussions/ankidesktop/41495-using-file-export-closes-the-collection-on-mwcoldb-if-the-browser-window-is-open
This commit is contained in:
parent
eb0c9f6e0a
commit
c49c378296
4 changed files with 36 additions and 40 deletions
|
@ -23,7 +23,7 @@ from anki.dbproxy import DBProxy
|
||||||
from anki.decks import DeckManager
|
from anki.decks import DeckManager
|
||||||
from anki.errors import AnkiError
|
from anki.errors import AnkiError
|
||||||
from anki.lang import _
|
from anki.lang import _
|
||||||
from anki.media import MediaManager
|
from anki.media import MediaManager, media_paths_from_col_path
|
||||||
from anki.models import ModelManager
|
from anki.models import ModelManager
|
||||||
from anki.notes import Note
|
from anki.notes import Note
|
||||||
from anki.rsbackend import TR, DBError, RustBackend
|
from anki.rsbackend import TR, DBError, RustBackend
|
||||||
|
@ -41,18 +41,19 @@ class _Collection:
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
db: DBProxy,
|
path: str,
|
||||||
backend: RustBackend,
|
backend: Optional[RustBackend],
|
||||||
server: bool = False,
|
server: bool = False,
|
||||||
log: bool = False,
|
log: bool = False,
|
||||||
) -> None:
|
) -> None:
|
||||||
self.backend = backend
|
self.backend = backend or RustBackend(server=server)
|
||||||
self._debugLog = log
|
self.db = None
|
||||||
self.db = db
|
self._should_log = log
|
||||||
self.path = db._path
|
|
||||||
self._openLog()
|
|
||||||
self.log(self.path, anki.version)
|
|
||||||
self.server = server
|
self.server = server
|
||||||
|
self.path = os.path.abspath(path)
|
||||||
|
self.reopen()
|
||||||
|
|
||||||
|
self.log(self.path, anki.version)
|
||||||
self._lastSave = time.time()
|
self._lastSave = time.time()
|
||||||
self.clearUndo()
|
self.clearUndo()
|
||||||
self.media = MediaManager(self, server)
|
self.media = MediaManager(self, server)
|
||||||
|
@ -219,6 +220,24 @@ class _Collection:
|
||||||
self.db.rollback()
|
self.db.rollback()
|
||||||
self.db.begin()
|
self.db.begin()
|
||||||
|
|
||||||
|
def reopen(self) -> None:
|
||||||
|
assert not self.db
|
||||||
|
assert self.path.endswith(".anki2")
|
||||||
|
|
||||||
|
(media_dir, media_db) = media_paths_from_col_path(self.path)
|
||||||
|
|
||||||
|
log_path = ""
|
||||||
|
should_log = not self.server and self._should_log
|
||||||
|
if should_log:
|
||||||
|
log_path = self.path.replace(".anki2", "2.log")
|
||||||
|
|
||||||
|
# connect
|
||||||
|
self.backend.open_collection(self.path, media_dir, media_db, log_path)
|
||||||
|
self.db = DBProxy(weakref.proxy(self.backend))
|
||||||
|
self.db.begin()
|
||||||
|
|
||||||
|
self._openLog()
|
||||||
|
|
||||||
def modSchema(self, check: bool) -> None:
|
def modSchema(self, check: bool) -> None:
|
||||||
"Mark schema modified. Call this first so user can abort if necessary."
|
"Mark schema modified. Call this first so user can abort if necessary."
|
||||||
if not self.schemaChanged():
|
if not self.schemaChanged():
|
||||||
|
@ -586,7 +605,7 @@ select id from notes where mid = ?) limit 1"""
|
||||||
##########################################################################
|
##########################################################################
|
||||||
|
|
||||||
def log(self, *args, **kwargs) -> None:
|
def log(self, *args, **kwargs) -> None:
|
||||||
if not self._debugLog:
|
if not self._should_log:
|
||||||
return
|
return
|
||||||
|
|
||||||
def customRepr(x):
|
def customRepr(x):
|
||||||
|
@ -606,7 +625,7 @@ select id from notes where mid = ?) limit 1"""
|
||||||
print(buf)
|
print(buf)
|
||||||
|
|
||||||
def _openLog(self) -> None:
|
def _openLog(self) -> None:
|
||||||
if not self._debugLog:
|
if not self._should_log:
|
||||||
return
|
return
|
||||||
lpath = re.sub(r"\.anki2$", ".log", self.path)
|
lpath = re.sub(r"\.anki2$", ".log", self.path)
|
||||||
if os.path.exists(lpath) and os.path.getsize(lpath) > 10 * 1024 * 1024:
|
if os.path.exists(lpath) and os.path.getsize(lpath) > 10 * 1024 * 1024:
|
||||||
|
@ -617,7 +636,7 @@ select id from notes where mid = ?) limit 1"""
|
||||||
self._logHnd = open(lpath, "a", encoding="utf8")
|
self._logHnd = open(lpath, "a", encoding="utf8")
|
||||||
|
|
||||||
def _closeLog(self) -> None:
|
def _closeLog(self) -> None:
|
||||||
if not self._debugLog:
|
if not self._should_log:
|
||||||
return
|
return
|
||||||
self._logHnd.close()
|
self._logHnd.close()
|
||||||
self._logHnd = None
|
self._logHnd = None
|
||||||
|
|
|
@ -21,9 +21,8 @@ class DBProxy:
|
||||||
# Lifecycle
|
# Lifecycle
|
||||||
###############
|
###############
|
||||||
|
|
||||||
def __init__(self, backend: anki.rsbackend.RustBackend, path: str) -> None:
|
def __init__(self, backend: anki.rsbackend.RustBackend) -> None:
|
||||||
self._backend = backend
|
self._backend = backend
|
||||||
self._path = path
|
|
||||||
self.mod = False
|
self.mod = False
|
||||||
self.last_begin_at = 0
|
self.last_begin_at = 0
|
||||||
|
|
||||||
|
|
|
@ -1,13 +1,9 @@
|
||||||
# Copyright: Ankitects Pty Ltd and contributors
|
# Copyright: Ankitects Pty Ltd and contributors
|
||||||
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||||
|
|
||||||
import os
|
|
||||||
import weakref
|
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
from anki.collection import _Collection
|
from anki.collection import _Collection
|
||||||
from anki.dbproxy import DBProxy
|
|
||||||
from anki.media import media_paths_from_col_path
|
|
||||||
from anki.rsbackend import RustBackend
|
from anki.rsbackend import RustBackend
|
||||||
|
|
||||||
|
|
||||||
|
@ -18,22 +14,4 @@ def Collection(
|
||||||
log: bool = False,
|
log: bool = False,
|
||||||
) -> _Collection:
|
) -> _Collection:
|
||||||
"Open a new or existing collection. Path must be unicode."
|
"Open a new or existing collection. Path must be unicode."
|
||||||
assert path.endswith(".anki2")
|
return _Collection(path, backend, server, log)
|
||||||
if backend is None:
|
|
||||||
backend = RustBackend(server=server)
|
|
||||||
|
|
||||||
(media_dir, media_db) = media_paths_from_col_path(path)
|
|
||||||
log_path = ""
|
|
||||||
should_log = not server and log
|
|
||||||
if should_log:
|
|
||||||
log_path = path.replace(".anki2", "2.log")
|
|
||||||
path = os.path.abspath(path)
|
|
||||||
|
|
||||||
# connect
|
|
||||||
backend.open_collection(path, media_dir, media_db, log_path)
|
|
||||||
db = DBProxy(weakref.proxy(backend), path)
|
|
||||||
|
|
||||||
# add db to col and do any remaining upgrades
|
|
||||||
col = _Collection(db, backend=backend, server=server)
|
|
||||||
db.begin()
|
|
||||||
return col
|
|
||||||
|
|
|
@ -505,12 +505,12 @@ close the profile or restart Anki."""
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def _loadCollection(self):
|
def _loadCollection(self):
|
||||||
self.reopen()
|
cpath = self.pm.collectionPath()
|
||||||
|
self.col = Collection(cpath, backend=self.backend, log=True)
|
||||||
self.setEnabled(True)
|
self.setEnabled(True)
|
||||||
|
|
||||||
def reopen(self):
|
def reopen(self):
|
||||||
cpath = self.pm.collectionPath()
|
self.col.reopen()
|
||||||
self.col = Collection(cpath, backend=self.backend, log=True)
|
|
||||||
|
|
||||||
def unloadCollection(self, onsuccess: Callable) -> None:
|
def unloadCollection(self, onsuccess: Callable) -> None:
|
||||||
def callback():
|
def callback():
|
||||||
|
|
Loading…
Reference in a new issue