mirror of
https://github.com/ankitects/anki.git
synced 2025-09-25 01:06:35 -04:00
don't allow invalid chars in file export (#694)
This commit is contained in:
parent
7e74248901
commit
fa07268763
4 changed files with 32 additions and 18 deletions
|
@ -320,9 +320,13 @@ def call(argv, wait=True, **kwargs):
|
||||||
isMac = sys.platform.startswith("darwin")
|
isMac = sys.platform.startswith("darwin")
|
||||||
isWin = sys.platform.startswith("win32")
|
isWin = sys.platform.startswith("win32")
|
||||||
|
|
||||||
invalidFilenameChars = "\\/:*?\"<>|"
|
invalidFilenameChars = ":*?\"<>|"
|
||||||
|
|
||||||
def invalidFilename(str):
|
def invalidFilename(str, dirsep=True):
|
||||||
for c in invalidFilenameChars:
|
for c in invalidFilenameChars:
|
||||||
if c in str:
|
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 "\\"
|
||||||
|
|
|
@ -4,7 +4,8 @@
|
||||||
import os
|
import os
|
||||||
from aqt.qt import *
|
from aqt.qt import *
|
||||||
import aqt
|
import aqt
|
||||||
from aqt.utils import getSaveFile, tooltip, showWarning, askUser
|
from aqt.utils import getSaveFile, tooltip, showWarning, askUser, \
|
||||||
|
checkInvalidFilename
|
||||||
from anki.exporting import exporters
|
from anki.exporting import exporters
|
||||||
|
|
||||||
class ExportDialog(QDialog):
|
class ExportDialog(QDialog):
|
||||||
|
@ -65,11 +66,15 @@ class ExportDialog(QDialog):
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
verbatim = False
|
verbatim = False
|
||||||
file = getSaveFile(
|
while 1:
|
||||||
self, _("Export"), "export",
|
file = getSaveFile(
|
||||||
self.exporter.key, self.exporter.ext)
|
self, _("Export"), "export",
|
||||||
if not file:
|
self.exporter.key, self.exporter.ext)
|
||||||
return
|
if not file:
|
||||||
|
return
|
||||||
|
if checkInvalidFilename(file, dirsep=False):
|
||||||
|
continue
|
||||||
|
break
|
||||||
self.hide()
|
self.hide()
|
||||||
if file:
|
if file:
|
||||||
self.mw.progress.start(immediate=True)
|
self.mw.progress.start(immediate=True)
|
||||||
|
|
10
aqt/main.py
10
aqt/main.py
|
@ -14,7 +14,8 @@ from anki.hooks import runHook, addHook
|
||||||
|
|
||||||
import aqt, aqt.progress, aqt.webview, aqt.toolbar, aqt.stats
|
import aqt, aqt.progress, aqt.webview, aqt.toolbar, aqt.stats
|
||||||
from aqt.utils import restoreGeom, showInfo, showWarning,\
|
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):
|
class AnkiQt(QMainWindow):
|
||||||
def __init__(self, app, profileManager, args):
|
def __init__(self, app, profileManager, args):
|
||||||
|
@ -151,12 +152,7 @@ class AnkiQt(QMainWindow):
|
||||||
|
|
||||||
def profileNameOk(self, str):
|
def profileNameOk(self, str):
|
||||||
from anki.utils import invalidFilename, invalidFilenameChars
|
from anki.utils import invalidFilename, invalidFilenameChars
|
||||||
if invalidFilename(str):
|
return not checkInvalidFilename(str)
|
||||||
showWarning(
|
|
||||||
_("A profile name cannot contain these characters: %s") %
|
|
||||||
" ".join(invalidFilenameChars))
|
|
||||||
return
|
|
||||||
return True
|
|
||||||
|
|
||||||
def onAddProfile(self):
|
def onAddProfile(self):
|
||||||
name = getOnlyText(_("Name:"))
|
name = getOnlyText(_("Name:"))
|
||||||
|
|
13
aqt/utils.py
13
aqt/utils.py
|
@ -4,8 +4,8 @@
|
||||||
from aqt.qt import *
|
from aqt.qt import *
|
||||||
import re, os, sys, urllib, subprocess
|
import re, os, sys, urllib, subprocess
|
||||||
import aqt
|
import aqt
|
||||||
from anki.sound import stripSounds
|
from anki.sound import stripSounds
|
||||||
from anki.utils import isWin, isMac
|
from anki.utils import isWin, isMac, invalidFilename
|
||||||
|
|
||||||
def openHelp(section):
|
def openHelp(section):
|
||||||
link = aqt.appHelpSite
|
link = aqt.appHelpSite
|
||||||
|
@ -411,3 +411,12 @@ def closeTooltip():
|
||||||
if _tooltipTimer:
|
if _tooltipTimer:
|
||||||
_tooltipTimer.stop()
|
_tooltipTimer.stop()
|
||||||
_tooltipTimer = None
|
_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
|
||||||
|
|
Loading…
Reference in a new issue