use raw strings for regexs

This commit is contained in:
Damien Elmes 2019-03-04 17:03:43 +10:00
parent 3464b5fd80
commit 6e077ab414
13 changed files with 26 additions and 26 deletions

View file

@ -301,7 +301,7 @@ class AnkiPackageExporter(AnkiExporter):
if os.path.isdir(mpath):
continue
if os.path.exists(mpath):
if re.search('\.svg$', file, re.IGNORECASE):
if re.search(r'\.svg$', file, re.IGNORECASE):
z.write(mpath, cStr, zipfile.ZIP_DEFLATED)
else:
z.write(mpath, cStr, zipfile.ZIP_STORED)

View file

@ -70,7 +70,7 @@ class TextImporter(NoteImporter):
self.fileobj = open(self.file, "r", encoding='utf-8-sig')
self.data = self.fileobj.read()
def sub(s):
return re.sub("^\#.*$", "__comment", s)
return re.sub(r"^\#.*$", "__comment", s)
self.data = [sub(x)+"\n" for x in self.data.split("\n") if sub(x) != "__comment"]
if self.data:
if self.data[0].startswith("tags:"):

View file

@ -81,7 +81,7 @@ acq_reps+ret_reps, lapses, card_type_id from cards"""):
rem = int((next - time.time())/86400)
c.due = self.col.sched.today+rem
# get ord
m = re.search(".(\d+)$", row[1])
m = re.search(r".(\d+)$", row[1])
ord = int(m.group(1))-1
if 'cards' not in note:
note['cards'] = {}
@ -103,7 +103,7 @@ acq_reps+ret_reps, lapses, card_type_id from cards"""):
# \n -> br
fld = re.sub("\r?\n", "<br>", fld)
# latex differences
fld = re.sub("(?i)<(/?(\$|\$\$|latex))>", "[\\1]", fld)
fld = re.sub(r"(?i)<(/?(\$|\$\$|latex))>", "[\\1]", fld)
# audio differences
fld = re.sub("<audio src=\"(.+?)\">(</audio>)?", "[sound:\\1]", fld)
return fld
@ -178,7 +178,7 @@ acq_reps+ret_reps, lapses, card_type_id from cards"""):
res = ("{{c%d::%s}}" % (state['n'], match.group(1)))
state['n'] += 1
return res
fld = re.sub("\[(.+?)\]", repl, fld)
fld = re.sub(r"\[(.+?)\]", repl, fld)
fld = self._mungeField(fld)
n.fields.append(fld)
n.fields.append("") # extra

View file

@ -231,8 +231,8 @@ class SupermemoXmlImporter(NoteImporter):
# clean things like [999] or [111-2222] from title path, example: xyz / [1000-1200] zyx / xyz
# clean whitespaces
# set Capital letters for first char of the word
tmp = list(set([ re.sub('(\[[0-9]+\])' , ' ' , i ).replace('_',' ') for i in item.lTitle ]))
tmp = list(set([ re.sub('(\W)',' ', i ) for i in tmp ]))
tmp = list(set([ re.sub(r'(\[[0-9]+\])' , ' ' , i ).replace('_',' ') for i in item.lTitle ]))
tmp = list(set([ re.sub(r'(\W)',' ', i ) for i in tmp ]))
tmp = list(set([ re.sub( '^[0-9 ]+$','',i) for i in tmp ]))
tmp = list(set([ capwords(i).replace(' ','') for i in tmp ]))
tags = [ j[0].lower() + j[1:] for j in tmp if j.strip() != '']

View file

@ -111,7 +111,7 @@ def getLang():
def noHint(str):
"Remove translation hint from end of string."
return re.sub("(^.*?)( ?\(.+?\))?$", "\\1", str)
return re.sub(r"(^.*?)( ?\(.+?\))?$", "\\1", str)
if not currentTranslation:
setLang("en_US", local=False)

View file

@ -19,12 +19,12 @@ from anki.lang import _
class MediaManager:
soundRegexps = ["(?i)(\[sound:(?P<fname>[^]]+)\])"]
soundRegexps = [r"(?i)(\[sound:(?P<fname>[^]]+)\])"]
imgRegexps = [
# src element quoted case
"(?i)(<img[^>]* src=(?P<str>[\"'])(?P<fname>[^>]+?)(?P=str)[^>]*>)",
r"(?i)(<img[^>]* src=(?P<str>[\"'])(?P<fname>[^>]+?)(?P=str)[^>]*>)",
# unquoted case
"(?i)(<img[^>]* src=(?!['\"])(?P<fname>[^ >]+)[^>]*?>)",
r"(?i)(<img[^>]* src=(?!['\"])(?P<fname>[^ >]+)[^>]*?>)",
]
regexps = soundRegexps + imgRegexps
@ -34,7 +34,7 @@ class MediaManager:
self._dir = None
return
# media directory
self._dir = re.sub("(?i)\.(anki2)$", ".media", self.col.path)
self._dir = re.sub(r"(?i)\.(anki2)$", ".media", self.col.path)
if not os.path.exists(self._dir):
os.makedirs(self._dir)
try:
@ -183,7 +183,7 @@ create table meta (dirMod int, lastUsn int); insert into meta values (0, 0);
if checksum(f.read()) == csum:
return fname
# otherwise, increment the index in the filename
reg = " \((\d+)\)$"
reg = r" \((\d+)\)$"
if not re.search(reg, root):
root = root + " (1)"
else:
@ -215,7 +215,7 @@ create table meta (dirMod int, lastUsn int); insert into meta values (0, 0);
return l
def _expandClozes(self, string):
ords = set(re.findall("{{c(\d+)::.+?}}", string))
ords = set(re.findall(r"{{c(\d+)::.+?}}", string))
strings = []
from anki.template.template import clozeReg
def qrepl(m):

View file

@ -578,7 +578,7 @@ select id from notes where mid = ?)""" % " ".join(map),
continue
ord = map[fname][0]
ords.update([int(m)-1 for m in re.findall(
"(?s){{c(\d+)::.+?}}", sflds[ord])])
r"(?s){{c(\d+)::.+?}}", sflds[ord])])
if -1 in ords:
ords.remove(-1)
if not ords and allowEmpty:

View file

@ -12,7 +12,7 @@ from anki.lang import _
# Shared utils
##########################################################################
_soundReg = "\[sound:(.*?)\]"
_soundReg = r"\[sound:(.*?)\]"
def playFromText(text):
for match in allSounds(text):

View file

@ -85,7 +85,7 @@ class Template:
# check for cloze
val = None
m = re.match("c[qa]:(\d+):(.+)", section_name)
m = re.match(r"c[qa]:(\d+):(.+)", section_name)
if m:
# get full field text
txt = get_or_attr(context, m.group(2), None)
@ -184,7 +184,7 @@ class Template:
txt = self.clozeText(txt, extra, mod[1]) if txt and extra else ""
else:
# hook-based field modifier
mod, extra = re.search("^(.*?)(?:\((.*)\))?$", mod).groups()
mod, extra = re.search(r"^(.*?)(?:\((.*)\))?$", mod).groups()
txt = runFilter('fmod_' + mod, txt or '', extra or '', context,
tag, tag_name)
if txt is None:
@ -211,7 +211,7 @@ class Template:
return buf
txt = re.sub(reg%ord, repl, txt)
# and display other clozes normally
return re.sub(reg%"\d+", "\\2", txt)
return re.sub(reg%r"\d+", "\\2", txt)
# look for clozes wrapped in mathjax, and change {{cx to {{Cx
def _removeFormattingFromMathjax(self, txt, ord):

View file

@ -130,7 +130,7 @@ reComment = re.compile("(?s)<!--.*?-->")
reStyle = re.compile("(?si)<style.*?>.*?</style>")
reScript = re.compile("(?si)<script.*?>.*?</script>")
reTag = re.compile("(?s)<.*?>")
reEnts = re.compile("&#?\w+;")
reEnts = re.compile(r"&#?\w+;")
reMedia = re.compile("(?i)<img[^>]+src=[\"']?([^\"'>]+)[\"']?[^>]*>")
def stripHTML(s):
@ -161,8 +161,8 @@ def htmlToTextLine(s):
s = s.replace("<br />", " ")
s = s.replace("<div>", " ")
s = s.replace("\n", " ")
s = re.sub("\[sound:[^]]+\]", "", s)
s = re.sub("\[\[type:[^]]+\]\]", "", s)
s = re.sub(r"\[sound:[^]]+\]", "", s)
s = re.sub(r"\[\[type:[^]]+\]\]", "", s)
s = stripHTMLMedia(s)
s = s.strip()
return s

View file

@ -337,7 +337,7 @@ Please create a new card type first."""))
repl = "<center>%s</center>" % repl
else:
repl = answerRepl
return re.sub("\[\[type:.+?\]\]", repl, txt)
return re.sub(r"\[\[type:.+?\]\]", repl, txt)
# Card operations
######################################################################

View file

@ -291,7 +291,7 @@ def importFile(mw, file):
for i in importing.Importers:
if done:
break
for mext in re.findall("[( ]?\*\.(.+?)[) ]", i[0]):
for mext in re.findall(r"[( ]?\*\.(.+?)[) ]", i[0]):
if file.endswith("." + mext):
importerClass = i[1]
done = True

View file

@ -296,7 +296,7 @@ The front of this card is empty. Please run Tools>Empty Cards.""")
# Type in the answer
##########################################################################
typeAnsPat = "\[\[type:(.+?)\]\]"
typeAnsPat = r"\[\[type:(.+?)\]\]"
def typeAnsFilter(self, buf):
if self.state == "question":
@ -379,7 +379,7 @@ Please run Tools>Empty Cards""")
return re.sub(self.typeAnsPat, repl, buf)
def _contentForCloze(self, txt, idx):
matches = re.findall("\{\{c%s::(.+?)\}\}"%idx, txt, re.DOTALL)
matches = re.findall(r"\{\{c%s::(.+?)\}\}"%idx, txt, re.DOTALL)
if not matches:
return None
def noHint(txt):