pass utf8 to sqlite but don't clobber our original format

This commit is contained in:
Damien Elmes 2012-07-19 17:37:47 +09:00
parent 5ddc4c639e
commit f56c2156a2
3 changed files with 8 additions and 12 deletions

View file

@ -12,9 +12,10 @@ from anki.hooks import runHook
class DB(object):
def __init__(self, path, text=None, timeout=0):
if isinstance(path, unicode):
path = path.encode("utf-8")
self._db = sqlite.connect(path, timeout=timeout)
encpath = path
if isinstance(encpath, unicode):
encpath = path.encode("utf-8")
self._db = sqlite.connect(encpath, timeout=timeout)
if text:
self._db.text_factory = text
self._path = path

View file

@ -37,7 +37,7 @@ class Upgrader(object):
def check(self, path):
"True if deck looks ok."
with DB(self._utf8(path)) as db:
with DB(path) as db:
return self._check(db)
def _check(self, db):
@ -111,17 +111,12 @@ f.id = cards.factId)"""):
def _openDB(self, path):
self.tmppath = tmpfile(suffix=".anki2")
shutil.copy(path, self._utf8(self.tmppath))
shutil.copy(path, self.tmppath)
self.db = DB(self.tmppath)
def _openCol(self):
self.col = _Collection(self.db)
def _utf8(self, txt):
if isinstance(txt, unicode):
return txt.encode("utf8")
return txt
# Schema upgrade
######################################################################

View file

@ -245,7 +245,7 @@ def tmpdir():
shutil.rmtree(_tmpdir)
import atexit
atexit.register(cleanup)
_tmpdir = os.path.join(tempfile.gettempdir(), "anki_temp")
_tmpdir = unicode(os.path.join(tempfile.gettempdir(), "anki_temp"), sys.getfilesystemencoding())
if not os.path.exists(_tmpdir):
os.mkdir(_tmpdir)
return _tmpdir
@ -253,7 +253,7 @@ def tmpdir():
def tmpfile(prefix="", suffix=""):
(fd, name) = tempfile.mkstemp(dir=tmpdir(), prefix=prefix, suffix=suffix)
os.close(fd)
return unicode(name, sys.getfilesystemencoding())
return name
def namedtmp(name, rm=True):
"Return tmpdir+name. Deletes any existing file."