From fa0726876301c56a676f6df8239d0eba842e9b42 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Wed, 20 Feb 2013 15:12:07 +0900 Subject: [PATCH] don't allow invalid chars in file export (#694) --- anki/utils.py | 10 +++++++--- aqt/exporting.py | 17 +++++++++++------ aqt/main.py | 10 +++------- aqt/utils.py | 13 +++++++++++-- 4 files changed, 32 insertions(+), 18 deletions(-) diff --git a/anki/utils.py b/anki/utils.py index eef69e8ce..cc0b1b9b2 100644 --- a/anki/utils.py +++ b/anki/utils.py @@ -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 "\\" diff --git a/aqt/exporting.py b/aqt/exporting.py index bcdf3010d..55b361318 100644 --- a/aqt/exporting.py +++ b/aqt/exporting.py @@ -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 - file = getSaveFile( - self, _("Export"), "export", - self.exporter.key, self.exporter.ext) - if not file: - return + 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) diff --git a/aqt/main.py b/aqt/main.py index c1195c841..456d9622e 100644 --- a/aqt/main.py +++ b/aqt/main.py @@ -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:")) diff --git a/aqt/utils.py b/aqt/utils.py index 18907b2b8..bc08638cf 100644 --- a/aqt/utils.py +++ b/aqt/utils.py @@ -4,8 +4,8 @@ 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.sound import stripSounds +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