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.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

View file

@ -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:<pre>%s</pre>""")
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

View file

@ -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

View file

@ -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

View file

@ -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())

View file

@ -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