From 4d1159745df1b69bfc68368f5f79767ddbf50108 Mon Sep 17 00:00:00 2001 From: Patric Stout Date: Thu, 12 Nov 2020 22:02:57 +0100 Subject: [PATCH] Fix: skip the cache if it somehow got corrupted (#76) --- truewiki/metadata.py | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/truewiki/metadata.py b/truewiki/metadata.py index 1b3d3f1..00b753c 100644 --- a/truewiki/metadata.py +++ b/truewiki/metadata.py @@ -285,6 +285,24 @@ def page_changed(self): return CATEGORIES, FILES, LANGUAGES, LINKS, PAGES, PAGES_LC, TEMPLATES, TRANSLATIONS + def _load_metadata_from_cache(self): + with open(CACHE_FILENAME, "r") as fp: + try: + payload = json.loads(fp.read()) + except json.JSONDecodeError: + log.info("Cache was corrupted; reloading metadata ...") + return + + if payload.get("version", 1) != CACHE_VERSION: + return + + CATEGORIES.update(payload["categories"]) + FILES.update(payload["files"]) + LINKS.update(payload["links"]) + PAGES.update(payload["pages"]) + TEMPLATES.update(payload["templates"]) + TRANSLATIONS.update(payload["translations"]) + def load_metadata(self): start = time.time() log.info("Loading metadata (this can take a while the first run) ...") @@ -298,15 +316,7 @@ def load_metadata(self): TRANSLATIONS.clear() if os.path.exists(CACHE_FILENAME): - with open(CACHE_FILENAME, "r") as fp: - payload = json.loads(fp.read()) - if payload.get("version", 1) == CACHE_VERSION: - CATEGORIES.update(payload["categories"]) - FILES.update(payload["files"]) - LINKS.update(payload["links"]) - PAGES.update(payload["pages"]) - TEMPLATES.update(payload["templates"]) - TRANSLATIONS.update(payload["translations"]) + self._load_metadata_from_cache() # Ensure no file is scanned more than once. notified = set()