don't allow invalid chars in file export (#694)

This commit is contained in:
Damien Elmes 2013-02-20 15:12:07 +09:00
parent 7e74248901
commit fa07268763
4 changed files with 32 additions and 18 deletions

View File

@ -320,9 +320,13 @@ def call(argv, wait=True, **kwargs):
isMac = sys.platform.startswith("darwin")
isWin = sys.platform.startswith("win32")
invalidFilenameChars = "\\/:*?\"<>|"
invalidFilenameChars = ":*?\"<>|"
def invalidFilename(str):
def invalidFilename(str, dirsep=True):
for c in invalidFilenameChars:
if c in str:
return True
return c
if (dirsep or isWin) and "/" in str:
return "/"
elif (dirsep or not isWin) and "\\" in str:
return "\\"

View File

@ -4,7 +4,8 @@
import os
from aqt.qt import *
import aqt
from aqt.utils import getSaveFile, tooltip, showWarning, askUser
from aqt.utils import getSaveFile, tooltip, showWarning, askUser, \
checkInvalidFilename
from anki.exporting import exporters
class ExportDialog(QDialog):
@ -65,11 +66,15 @@ class ExportDialog(QDialog):
return
else:
verbatim = False
while 1:
file = getSaveFile(
self, _("Export"), "export",
self.exporter.key, self.exporter.ext)
if not file:
return
if checkInvalidFilename(file, dirsep=False):
continue
break
self.hide()
if file:
self.mw.progress.start(immediate=True)

View File

@ -14,7 +14,8 @@ from anki.hooks import runHook, addHook
import aqt, aqt.progress, aqt.webview, aqt.toolbar, aqt.stats
from aqt.utils import restoreGeom, showInfo, showWarning,\
restoreState, getOnlyText, askUser, applyStyles, showText, tooltip, openHelp, openLink
restoreState, getOnlyText, askUser, applyStyles, showText, tooltip, \
openHelp, openLink, checkInvalidFilename
class AnkiQt(QMainWindow):
def __init__(self, app, profileManager, args):
@ -151,12 +152,7 @@ class AnkiQt(QMainWindow):
def profileNameOk(self, str):
from anki.utils import invalidFilename, invalidFilenameChars
if invalidFilename(str):
showWarning(
_("A profile name cannot contain these characters: %s") %
" ".join(invalidFilenameChars))
return
return True
return not checkInvalidFilename(str)
def onAddProfile(self):
name = getOnlyText(_("Name:"))

View File

@ -5,7 +5,7 @@ from aqt.qt import *
import re, os, sys, urllib, subprocess
import aqt
from anki.sound import stripSounds
from anki.utils import isWin, isMac
from anki.utils import isWin, isMac, invalidFilename
def openHelp(section):
link = aqt.appHelpSite
@ -411,3 +411,12 @@ def closeTooltip():
if _tooltipTimer:
_tooltipTimer.stop()
_tooltipTimer = None
# true if invalid; print warning
def checkInvalidFilename(str, dirsep=True):
bad = invalidFilename(str, dirsep)
if bad:
showWarning(_("The following character can not be used: %s") %
bad)
return True
return False