From 6ac46a11f11653e730e711b4231670eb9f679b53 Mon Sep 17 00:00:00 2001 From: LyubomirT Date: Thu, 7 Dec 2023 13:05:06 +0200 Subject: [PATCH] Bugfix in Cache Loading --- lesp/autocorrect.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/lesp/autocorrect.py b/lesp/autocorrect.py index d129640..bfe2097 100644 --- a/lesp/autocorrect.py +++ b/lesp/autocorrect.py @@ -100,9 +100,15 @@ def load_cache(self, cache_file: str = "lesp.cache") -> None: # Validate cache file format and how words are stored temp_cache: dict = json.load(f) # Must follow the format {"word": ["similar", "words"]} - if not all(isinstance(word, str) for word in temp_cache.keys() and not all(word.islower() and word.isalpha() for word in temp_cache.keys())): - raise ValueError("Invalid cache file format. Keys must be strings. Also the strings must be all-lowercase and contain only alphabetic characters.") - self.cache: dict = json.load(f) + for word in temp_cache.keys(): + if not isinstance(word, str) or not word.islower() or not word.isalpha(): + raise ValueError("Invalid cache file format. Keys must be strings. Also, the strings must be all-lowercase and contain only alphabetic characters.") + if not isinstance(temp_cache[word], list): + raise ValueError("Invalid cache file format. Values must be lists.") + if not all(isinstance(w, str) and w.islower() and w.isalpha() for w in temp_cache[word]): + raise ValueError("Invalid cache file format. Values must be lists of strings. Also, the strings must be all-lowercase and contain only alphabetic characters.") + self.cache: dict = temp_cache # Use the loaded data + except FileNotFoundError: # Create the cache file (and directory. also possible if multiple directories are missing) try: @@ -114,6 +120,7 @@ def load_cache(self, cache_file: str = "lesp.cache") -> None: json.dump({}, f) except json.JSONDecodeError: raise ValueError("Invalid cache file format. Must be a valid JSON file.") + def save_cache(self) -> None: """