From a51d51bca606103b75ae7b1f7a6049c5b1057f0e Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Sun, 17 Apr 2011 01:14:12 +0900 Subject: [PATCH] use new libanki temp funcs --- aqt/editor.py | 14 ++++---------- aqt/getshared.py | 3 ++- aqt/main.py | 5 ++--- aqt/share.py | 1 + aqt/stats.py | 6 +++--- aqt/utils.py | 2 +- 6 files changed, 13 insertions(+), 18 deletions(-) diff --git a/aqt/editor.py b/aqt/editor.py index a5b059c80..0d222639d 100644 --- a/aqt/editor.py +++ b/aqt/editor.py @@ -5,8 +5,8 @@ from PyQt4.QtGui import * from PyQt4.QtCore import * from PyQt4.QtWebKit import QWebView -import re, os, sys, tempfile, urllib2, ctypes, simplejson, traceback -from anki.utils import stripHTML, parseTags, isWin +import re, os, sys, urllib2, ctypes, simplejson, traceback +from anki.utils import stripHTML, parseTags, isWin, namedtmp from anki.sound import play from anki.hooks import runHook from aqt.sound import getAudio @@ -727,7 +727,6 @@ class EditorWebView(AnkiWebView): def __init__(self, parent, editor): AnkiWebView.__init__(self, parent) self.editor = editor - self.__tmpDir = None self.errtxt = _("An error occured while opening %s") self.strip = self.editor.mw.config['stripHTML'] @@ -825,7 +824,7 @@ class EditorWebView(AnkiWebView): def _processImage(self, mime): 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()) if im.hasAlphaChannel(): im.save(uname) @@ -848,13 +847,8 @@ class EditorWebView(AnkiWebView): except urllib2.URLError, e: showWarning(self.errtxt % e) return - path = os.path.join(self._tmpDir(), os.path.basename(url)) + path = namedtmp(os.path.basename(url)) file = open(path, "wb") file.write(filecontents) file.close() return self.editor._addMedia(path) - - def _tmpDir(self): - if not self.__tmpDir: - self.__tmpDir = tempfile.mkdtemp(prefix="anki") - return self.__tmpDir diff --git a/aqt/getshared.py b/aqt/getshared.py index f4644b87d..50d6b8ed8 100644 --- a/aqt/getshared.py +++ b/aqt/getshared.py @@ -4,7 +4,7 @@ from PyQt4.QtGui 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 from aqt.ui.utils import saveGeom, restoreGeom, showInfo from anki.utils import fmtTimeSpan @@ -176,6 +176,7 @@ Error was:
%s
""") if self.type == 0: if not self.parent.saveAndClose(hideWelcome=True, parent=self): return QDialog.accept(self) + # fixme: use namedtmp (fd, tmpname) = tempfile.mkstemp(prefix="anki") tmpfile = os.fdopen(fd, "w+b") cnt = 0 diff --git a/aqt/main.py b/aqt/main.py index e9bfa9b80..3ca1004ad 100755 --- a/aqt/main.py +++ b/aqt/main.py @@ -3,7 +3,7 @@ # License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html import os, sys, re, stat, traceback, signal -import shutil, time, tempfile, zipfile +import shutil, time, zipfile from operator import itemgetter from PyQt4.QtCore import * @@ -461,8 +461,7 @@ Debug info:\n%s""") % traceback.format_exc(), help="DeckErrors") self.ensureSyncParams() self.close() # we need a disk-backed file for syncing - dir = unicode(tempfile.mkdtemp(prefix="anki"), sys.getfilesystemencoding()) - path = os.path.join(dir, u"untitled.anki") + path = namedtmp(u"untitled.anki") self.onNew(path=path) # ensure all changes come to us self.deck.modified = 0 diff --git a/aqt/share.py b/aqt/share.py index 8e4c6d22b..7544299b8 100644 --- a/aqt/share.py +++ b/aqt/share.py @@ -1,6 +1,7 @@ # to be moved into libanki def _copyToTmpDeck(self, name="cram.anki", tags="", ids=[]): + # fixme: use namedtmp ndir = tempfile.mkdtemp(prefix="anki") path = os.path.join(ndir, name) from anki.exporting import AnkiExporter diff --git a/aqt/stats.py b/aqt/stats.py index 52ef6bc53..16bde2b60 100644 --- a/aqt/stats.py +++ b/aqt/stats.py @@ -4,9 +4,10 @@ from PyQt4.QtGui import * from PyQt4.QtCore import * -import os, tempfile +import os from aqt.webview import AnkiWebView from aqt.utils import saveGeom, restoreGeom, maybeHideClose +from anki.utils import namedtmp from anki.hooks import addHook import aqt @@ -98,8 +99,7 @@ class DeckStats(QDialog): def browser(self): # dump to a temporary file - tmpdir = tempfile.mkdtemp(prefix="anki") - path = os.path.join(tmpdir, "report.png") + path = namedtmp("report.png") p = self.form.web.page() oldsize = p.viewportSize() p.setViewportSize(p.mainFrame().contentsSize()) diff --git a/aqt/utils.py b/aqt/utils.py index c67ed3177..10fc07096 100644 --- a/aqt/utils.py +++ b/aqt/utils.py @@ -330,7 +330,7 @@ def openFolder(path): QDesktopServices.openUrl(QUrl("file://" + path)) def shortcut(key): - if Mac: + if isMac: return re.sub("(?i)ctrl", "Command", key) return key