Skip to content

Commit

Permalink
Added fallbacks to the crossword loading system
Browse files Browse the repository at this point in the history
The definitions loader in `cword_gen.py` and the info loader in `main.py` will now access the base crossword data if they cannot find data for the translated crossword that was requested.
  • Loading branch information
tomasvana10 committed Feb 28, 2024
1 parent 7585403 commit 0d12cc1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
7 changes: 5 additions & 2 deletions src/cword_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,11 @@ def load_definitions(category: str,
language: str = "en",
) -> Dict[str, str]:
'''Load a definitions json for a given crossword.'''
path = os.path.join(Paths.LOCALES_PATH, language, "cwords", category, name, "definitions.json")
if not os.path.exists(path): # Fallback to the base crossword
path = os.path.join(Paths.BASE_CWORDS_PATH, category, name, "definitions.json")
try:
with open(os.path.join(Paths.LOCALES_PATH, language, "cwords", category, name, "definitions.json"), "r") as file:
with open(path) as file:
return json.load(file)
except json.decoder.JSONDecodeError:
raise EmptyDefinitions
Expand All @@ -413,7 +416,7 @@ def _load_attempts_db() -> Dict[str, int]:
'''Load a json that specifies the amount of attempts a crossword should be recreated based
on the amount of words that crossword will contain.
'''
with open(Paths.ATTEMPTS_DB_PATH, "r") as file:
with open(Paths.ATTEMPTS_DB_PATH) as file:
return json.load(file)


Expand Down
11 changes: 8 additions & 3 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,13 @@ def _place_content(self) -> None:
def open_cword_browser(self) -> None:
'''Remove all homescreen widgets and instantiate the `CrosswordBrowser` class'''
self.container.pack_forget()
self.title(_("Crossword Browser"))
self.cword_browser: object = CrosswordBrowser(self)

def close_cword_browser(self) -> None:
'''Remove all `CrosswordBrowser` widgets and regenerate the main screen.'''
self.cword_browser.pack_forget()
self.title(_("Crossword Puzzle"))
self.generate_screen()

def generate_screen(self, inst=None) -> None:
Expand Down Expand Up @@ -646,7 +648,7 @@ def __init__(self,
self.category_object = category_object
self.value = value
self.info = AppHelper._load_cword_info(self.category, self.name, self.master.master.locale.language)
self.translated_name = self.info["translated_name"]
self.translated_name = self.info["translated_name"] if self.info["translated_name"] else self.info["name"]
self.difficulty = _(CrosswordDifficulties.DIFFICULTIES[self.info["difficulty"]])

self._make_content()
Expand All @@ -657,7 +659,7 @@ def _make_content(self) -> None:
wrap="word", fg_color=(Colour.Light.SUB, Colour.Dark.SUB),
scrollbar_button_color=(Colour.Light.MAIN, Colour.Dark.MAIN))
self.tb_name.tag_config("center", justify="center")
self.tb_name.insert("end", f"{chr(int(self.info['symbol'], 16))} {self.info['translated_name']}", "center")
self.tb_name.insert("end", f"{chr(int(self.info['symbol'], 16))} {self.translated_name}", "center")
self.tb_name.configure(state="disabled")

self.l_total_words = ctk.CTkLabel(self, font=self.master.master.TEXT_FONT,
Expand Down Expand Up @@ -788,7 +790,10 @@ def _load_cword_info(category: str,
language: str = "en"
) -> Dict[str, Union[str, int]]:
'''Load the `info.json` file for a crossword. Called by an instance of `CrosswordInfoBlock`.'''
with open(os.path.join(Paths.LOCALES_PATH, language, "cwords", category, name, "info.json")) as file:
path = os.path.join(Paths.LOCALES_PATH, language, "cwords", category, name, "info.json")
if not os.path.exists(path): # Fallback to base crossword info
path = os.path.join(Paths.BASE_CWORDS_PATH, category, name, "info.json")
with open(path) as file:
return json.load(file)

@staticmethod
Expand Down
15 changes: 7 additions & 8 deletions src/translation_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,9 @@ def _get_cword_data(category: str,
) -> CrosswordData:
base_cword_path = os.path.join(Paths.BASE_CWORDS_PATH, category, name)
'''Load the `definitions.json` and `info.json` files from a given base crossword.'''
with open(os.path.join(base_cword_path, "definitions.json"), "r") as file:
definitions = json.load(file)
with open(os.path.join(base_cword_path, "info.json"), "r") as file:
info = json.load(file)
with open(os.path.join(base_cword_path, "definitions.json"), "r") as def_file, \
open(os.path.join(base_cword_path, "info.json"), "r") as info_file:
definitions, info = json.load(def_file), json.load(info_file)

return definitions, info

Expand All @@ -193,10 +192,10 @@ def _write_translated_cword_data(path: str,
definitions: Dict[str, str],
info: Dict[str, Union[str, int]]
) -> None:
with open(os.path.join(path, "definitions.json"), "w") as file:
json.dump(definitions, file, indent=4)
with open(os.path.join(path, "info.json"), "w") as file:
json.dump(info, file, indent=4)
with open(os.path.join(path, "definitions.json"), "w") as def_file, \
open(os.path.join(path, "info.json"), "w") as info_file:
json.dump(definitions, def_file, indent=4)
json.dump(info, info_file, indent=4)


if __name__ == "__main__":
Expand Down

0 comments on commit 0d12cc1

Please sign in to comment.