From 81d4b77ee147070296b23acb9fd96708e6a59b16 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Mon, 11 Dec 2017 17:25:51 +1000 Subject: [PATCH] more open() and regex strings --- anki/collection.py | 3 ++- aqt/addons.py | 16 ++++++++++------ aqt/main.py | 7 ++++--- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/anki/collection.py b/anki/collection.py index 6cec228db..5ee1584b7 100644 --- a/anki/collection.py +++ b/anki/collection.py @@ -840,7 +840,7 @@ and queue = 0""", intTime(), self.usn()) def _openLog(self): if not self._debugLog: 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: lpath2 = lpath + ".old" if os.path.exists(lpath2): @@ -849,6 +849,7 @@ and queue = 0""", intTime(), self.usn()) self._logHnd = open(lpath, "a", encoding="utf8") def _closeLog(self): + self._logHnd.close() self._logHnd = None # Card Flags diff --git a/aqt/addons.py b/aqt/addons.py index 8900e6443..d0c2f3684 100644 --- a/aqt/addons.py +++ b/aqt/addons.py @@ -39,7 +39,7 @@ class AddonManager: def managedAddons(self): return [d for d in self.allAddons() - if re.match("^\d+$", d)] + if re.match(r"^\d+$", d)] def addonsFolder(self, dir=None): root = self.mw.pm.addonFolder() @@ -76,13 +76,15 @@ When loading '%(name)s': def addonMeta(self, dir): path = self._addonMetaPath(dir) try: - return json.load(open(path, encoding="utf8")) + with open(path, encoding="utf8") as f: + return json.load(f) except: return dict() def writeAddonMeta(self, dir, meta): 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): meta = self.addonMeta(dir) @@ -203,14 +205,16 @@ When loading '%(name)s': def addonConfigDefaults(self, dir): path = os.path.join(self.addonsFolder(dir), "config.json") try: - return json.load(open(path, encoding="utf8")) + with open(path, encoding="utf8") as f: + return json.load(f) except: return None def addonConfigHelp(self, dir): path = os.path.join(self.addonsFolder(dir), "config.md") if os.path.exists(path): - return markdown.markdown(open(path).read()) + with open(path) as f: + return markdown.markdown(f.read()) else: return "" @@ -325,7 +329,7 @@ class AddonsDialog(QDialog): addon = self.onlyOneSelected() if not addon: return - if re.match("^\d+$", addon): + if re.match(r"^\d+$", addon): openLink(aqt.appShared + f"info/{addon}") else: showWarning(_("Add-on was not downloaded from AnkiWeb.")) diff --git a/aqt/main.py b/aqt/main.py index 8dc839934..9c652e90a 100644 --- a/aqt/main.py +++ b/aqt/main.py @@ -405,7 +405,8 @@ from the profile screen.")) # do backup fname = time.strftime("backup-%Y-%m-%d-%H.%M.%S.colpkg", time.localtime(time.time())) 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.start() @@ -413,7 +414,7 @@ from the profile screen.")) backups = [] for file in os.listdir(dir): # 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: continue backups.append(file) @@ -1170,7 +1171,7 @@ will be lost. Continue?""")) tgt = tgt or self for action in tgt.findChildren(QAction): txt = str(action.text()) - m = re.match("^(.+)\(&.+\)(.+)?", txt) + m = re.match(r"^(.+)\(&.+\)(.+)?", txt) if m: action.setText(m.group(1) + (m.group(2) or ""))