use new libanki temp funcs

This commit is contained in:
Damien Elmes 2011-04-17 01:14:12 +09:00
parent f245c7651c
commit a51d51bca6
6 changed files with 13 additions and 18 deletions

View file

@ -5,8 +5,8 @@
from PyQt4.QtGui import * from PyQt4.QtGui import *
from PyQt4.QtCore import * from PyQt4.QtCore import *
from PyQt4.QtWebKit import QWebView from PyQt4.QtWebKit import QWebView
import re, os, sys, tempfile, urllib2, ctypes, simplejson, traceback import re, os, sys, urllib2, ctypes, simplejson, traceback
from anki.utils import stripHTML, parseTags, isWin from anki.utils import stripHTML, parseTags, isWin, namedtmp
from anki.sound import play from anki.sound import play
from anki.hooks import runHook from anki.hooks import runHook
from aqt.sound import getAudio from aqt.sound import getAudio
@ -727,7 +727,6 @@ class EditorWebView(AnkiWebView):
def __init__(self, parent, editor): def __init__(self, parent, editor):
AnkiWebView.__init__(self, parent) AnkiWebView.__init__(self, parent)
self.editor = editor self.editor = editor
self.__tmpDir = None
self.errtxt = _("An error occured while opening %s") self.errtxt = _("An error occured while opening %s")
self.strip = self.editor.mw.config['stripHTML'] self.strip = self.editor.mw.config['stripHTML']
@ -825,7 +824,7 @@ class EditorWebView(AnkiWebView):
def _processImage(self, mime): def _processImage(self, mime):
im = QImage(mime.imageData()) im = QImage(mime.imageData())
name = os.path.join(self._tmpDir(), "paste-%d.png" % im.cacheKey()) name = namedtmp("paste-%d.png" % im.cacheKey())
uname = unicode(name, sys.getfilesystemencoding()) uname = unicode(name, sys.getfilesystemencoding())
if im.hasAlphaChannel(): if im.hasAlphaChannel():
im.save(uname) im.save(uname)
@ -848,13 +847,8 @@ class EditorWebView(AnkiWebView):
except urllib2.URLError, e: except urllib2.URLError, e:
showWarning(self.errtxt % e) showWarning(self.errtxt % e)
return return
path = os.path.join(self._tmpDir(), os.path.basename(url)) path = namedtmp(os.path.basename(url))
file = open(path, "wb") file = open(path, "wb")
file.write(filecontents) file.write(filecontents)
file.close() file.close()
return self.editor._addMedia(path) return self.editor._addMedia(path)
def _tmpDir(self):
if not self.__tmpDir:
self.__tmpDir = tempfile.mkdtemp(prefix="anki")
return self.__tmpDir

View file

@ -4,7 +4,7 @@
from PyQt4.QtGui import * from PyQt4.QtGui import *
from PyQt4.QtCore import * from PyQt4.QtCore import *
import aqt, simplejson, time, cStringIO, zipfile, tempfile, os, re, gzip import aqt, simplejson, time, cStringIO, zipfile, os, re, gzip
import traceback, urllib2, socket, cgi import traceback, urllib2, socket, cgi
from aqt.ui.utils import saveGeom, restoreGeom, showInfo from aqt.ui.utils import saveGeom, restoreGeom, showInfo
from anki.utils import fmtTimeSpan from anki.utils import fmtTimeSpan
@ -176,6 +176,7 @@ Error was:<pre>%s</pre>""")
if self.type == 0: if self.type == 0:
if not self.parent.saveAndClose(hideWelcome=True, parent=self): if not self.parent.saveAndClose(hideWelcome=True, parent=self):
return QDialog.accept(self) return QDialog.accept(self)
# fixme: use namedtmp
(fd, tmpname) = tempfile.mkstemp(prefix="anki") (fd, tmpname) = tempfile.mkstemp(prefix="anki")
tmpfile = os.fdopen(fd, "w+b") tmpfile = os.fdopen(fd, "w+b")
cnt = 0 cnt = 0

View file

@ -3,7 +3,7 @@
# 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, sys, re, stat, traceback, signal import os, sys, re, stat, traceback, signal
import shutil, time, tempfile, zipfile import shutil, time, zipfile
from operator import itemgetter from operator import itemgetter
from PyQt4.QtCore import * from PyQt4.QtCore import *
@ -461,8 +461,7 @@ Debug info:\n%s""") % traceback.format_exc(), help="DeckErrors")
self.ensureSyncParams() self.ensureSyncParams()
self.close() self.close()
# we need a disk-backed file for syncing # we need a disk-backed file for syncing
dir = unicode(tempfile.mkdtemp(prefix="anki"), sys.getfilesystemencoding()) path = namedtmp(u"untitled.anki")
path = os.path.join(dir, u"untitled.anki")
self.onNew(path=path) self.onNew(path=path)
# ensure all changes come to us # ensure all changes come to us
self.deck.modified = 0 self.deck.modified = 0

View file

@ -1,6 +1,7 @@
# to be moved into libanki # to be moved into libanki
def _copyToTmpDeck(self, name="cram.anki", tags="", ids=[]): def _copyToTmpDeck(self, name="cram.anki", tags="", ids=[]):
# fixme: use namedtmp
ndir = tempfile.mkdtemp(prefix="anki") ndir = tempfile.mkdtemp(prefix="anki")
path = os.path.join(ndir, name) path = os.path.join(ndir, name)
from anki.exporting import AnkiExporter from anki.exporting import AnkiExporter

View file

@ -4,9 +4,10 @@
from PyQt4.QtGui import * from PyQt4.QtGui import *
from PyQt4.QtCore import * from PyQt4.QtCore import *
import os, tempfile import os
from aqt.webview import AnkiWebView from aqt.webview import AnkiWebView
from aqt.utils import saveGeom, restoreGeom, maybeHideClose from aqt.utils import saveGeom, restoreGeom, maybeHideClose
from anki.utils import namedtmp
from anki.hooks import addHook from anki.hooks import addHook
import aqt import aqt
@ -98,8 +99,7 @@ class DeckStats(QDialog):
def browser(self): def browser(self):
# dump to a temporary file # dump to a temporary file
tmpdir = tempfile.mkdtemp(prefix="anki") path = namedtmp("report.png")
path = os.path.join(tmpdir, "report.png")
p = self.form.web.page() p = self.form.web.page()
oldsize = p.viewportSize() oldsize = p.viewportSize()
p.setViewportSize(p.mainFrame().contentsSize()) p.setViewportSize(p.mainFrame().contentsSize())

View file

@ -330,7 +330,7 @@ def openFolder(path):
QDesktopServices.openUrl(QUrl("file://" + path)) QDesktopServices.openUrl(QUrl("file://" + path))
def shortcut(key): def shortcut(key):
if Mac: if isMac:
return re.sub("(?i)ctrl", "Command", key) return re.sub("(?i)ctrl", "Command", key)
return key return key