more open() and regex strings

This commit is contained in:
Damien Elmes 2017-12-11 17:25:51 +10:00
parent 3b71f8e44e
commit 81d4b77ee1
3 changed files with 16 additions and 10 deletions

View file

@ -840,7 +840,7 @@ and queue = 0""", intTime(), self.usn())
def _openLog(self): def _openLog(self):
if not self._debugLog: if not self._debugLog:
return return
lpath = re.sub("\.anki2$", ".log", self.path) lpath = re.sub(r"\.anki2$", ".log", self.path)
if os.path.exists(lpath) and os.path.getsize(lpath) > 10*1024*1024: if os.path.exists(lpath) and os.path.getsize(lpath) > 10*1024*1024:
lpath2 = lpath + ".old" lpath2 = lpath + ".old"
if os.path.exists(lpath2): if os.path.exists(lpath2):
@ -849,6 +849,7 @@ and queue = 0""", intTime(), self.usn())
self._logHnd = open(lpath, "a", encoding="utf8") self._logHnd = open(lpath, "a", encoding="utf8")
def _closeLog(self): def _closeLog(self):
self._logHnd.close()
self._logHnd = None self._logHnd = None
# Card Flags # Card Flags

View file

@ -39,7 +39,7 @@ class AddonManager:
def managedAddons(self): def managedAddons(self):
return [d for d in self.allAddons() return [d for d in self.allAddons()
if re.match("^\d+$", d)] if re.match(r"^\d+$", d)]
def addonsFolder(self, dir=None): def addonsFolder(self, dir=None):
root = self.mw.pm.addonFolder() root = self.mw.pm.addonFolder()
@ -76,13 +76,15 @@ When loading '%(name)s':
def addonMeta(self, dir): def addonMeta(self, dir):
path = self._addonMetaPath(dir) path = self._addonMetaPath(dir)
try: try:
return json.load(open(path, encoding="utf8")) with open(path, encoding="utf8") as f:
return json.load(f)
except: except:
return dict() return dict()
def writeAddonMeta(self, dir, meta): def writeAddonMeta(self, dir, meta):
path = self._addonMetaPath(dir) path = self._addonMetaPath(dir)
json.dump(meta, open(path, "w", encoding="utf8")) with open(path, "w", encoding="utf8") as f:
json.dump(meta, f)
def toggleEnabled(self, dir): def toggleEnabled(self, dir):
meta = self.addonMeta(dir) meta = self.addonMeta(dir)
@ -203,14 +205,16 @@ When loading '%(name)s':
def addonConfigDefaults(self, dir): def addonConfigDefaults(self, dir):
path = os.path.join(self.addonsFolder(dir), "config.json") path = os.path.join(self.addonsFolder(dir), "config.json")
try: try:
return json.load(open(path, encoding="utf8")) with open(path, encoding="utf8") as f:
return json.load(f)
except: except:
return None return None
def addonConfigHelp(self, dir): def addonConfigHelp(self, dir):
path = os.path.join(self.addonsFolder(dir), "config.md") path = os.path.join(self.addonsFolder(dir), "config.md")
if os.path.exists(path): if os.path.exists(path):
return markdown.markdown(open(path).read()) with open(path) as f:
return markdown.markdown(f.read())
else: else:
return "" return ""
@ -325,7 +329,7 @@ class AddonsDialog(QDialog):
addon = self.onlyOneSelected() addon = self.onlyOneSelected()
if not addon: if not addon:
return return
if re.match("^\d+$", addon): if re.match(r"^\d+$", addon):
openLink(aqt.appShared + f"info/{addon}") openLink(aqt.appShared + f"info/{addon}")
else: else:
showWarning(_("Add-on was not downloaded from AnkiWeb.")) showWarning(_("Add-on was not downloaded from AnkiWeb."))

View file

@ -405,7 +405,8 @@ from the profile screen."))
# do backup # do backup
fname = time.strftime("backup-%Y-%m-%d-%H.%M.%S.colpkg", time.localtime(time.time())) fname = time.strftime("backup-%Y-%m-%d-%H.%M.%S.colpkg", time.localtime(time.time()))
newpath = os.path.join(dir, fname) newpath = os.path.join(dir, fname)
data = open(path, "rb").read() with open(path, "rb") as f:
data = f.read()
b = self.BackupThread(newpath, data) b = self.BackupThread(newpath, data)
b.start() b.start()
@ -413,7 +414,7 @@ from the profile screen."))
backups = [] backups = []
for file in os.listdir(dir): for file in os.listdir(dir):
# only look for new-style format # only look for new-style format
m = re.match("backup-\d{4}-\d{2}-.+.colpkg", file) m = re.match(r"backup-\d{4}-\d{2}-.+.colpkg", file)
if not m: if not m:
continue continue
backups.append(file) backups.append(file)
@ -1170,7 +1171,7 @@ will be lost. Continue?"""))
tgt = tgt or self tgt = tgt or self
for action in tgt.findChildren(QAction): for action in tgt.findChildren(QAction):
txt = str(action.text()) txt = str(action.text())
m = re.match("^(.+)\(&.+\)(.+)?", txt) m = re.match(r"^(.+)\(&.+\)(.+)?", txt)
if m: if m:
action.setText(m.group(1) + (m.group(2) or "")) action.setText(m.group(1) + (m.group(2) or ""))