Skip to content

Commit 134ae49

Browse files
committed
The braille dialog wasn't picking up the zipped up braille codes.
I merged the way finding the rules files are handled between languages and braille. I probably should do the same for style files, but I'm leaving that for another time.
1 parent ce285b0 commit 134ae49

File tree

1 file changed

+43
-41
lines changed

1 file changed

+43
-41
lines changed

addon/globalPlugins/MathCAT/MathCATPreferences.py

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import gettext
1111
import addonHandler
1212
from logHandler import log # logging
13-
from typing import List, Dict, Union
13+
from typing import List, Dict, Union, Callable
1414
from .MathCAT import ConvertSSMLTextForNVDA
1515
from speech import speak
1616
from zipfile import ZipFile
@@ -85,7 +85,7 @@ def path_to_languages_folder():
8585
return os.path.join(os.path.dirname(os.path.abspath(__file__)), "Rules", "Languages")
8686

8787
@staticmethod
88-
def path_to_braille_folder():
88+
def pathToBrailleFolder():
8989
# the user preferences file is stored at: MathCAT\Rules\Languages
9090
return os.path.join(os.path.dirname(os.path.abspath(__file__)), "Rules", "Braille")
9191

@@ -267,28 +267,46 @@ def LanguagesDict() -> Dict[str, str]:
267267
}
268268
return languages
269269

270+
def get_rules_files(self, pathToDir: str, processSubDirs: Callable[[str, str], List[str]] | None) -> List[str]:
271+
language = os.path.basename(pathToDir)
272+
ruleFiles = [os.path.basename(file) for file in glob.glob(os.path.join(pathToDir, "*_Rules.yaml"))]
273+
for dir in os.listdir(pathToDir):
274+
if os.path.isdir(os.path.join(pathToDir, dir)):
275+
if processSubDirs:
276+
ruleFiles.extend(processSubDirs(dir, language))
277+
278+
if len(ruleFiles) == 0:
279+
# look in the .zip file for the style files, including regional subdirs -- it might not have been unzipped
280+
try:
281+
zip_file = ZipFile(f"{pathToDir}\\{language}.zip", "r")
282+
for file in zip_file.namelist():
283+
if file.endswith('_Rules.yaml'):
284+
ruleFiles.append(file)
285+
elif zip_file.getinfo(file).is_dir() and processSubDirs:
286+
ruleFiles.extend(processSubDirs(dir, language))
287+
except Exception as e:
288+
log.debugWarning(f"MathCAT Dialog: didn't find zip file {zip_file}. Error: {e}")
289+
290+
return ruleFiles
291+
270292
def GetLanguages(self):
271293

272-
def addRegionalLanguages(subDir: str, language: str) -> bool:
294+
def addRegionalLanguages(subDir: str, language: str) -> List[str]:
273295
# the language variants are in folders named using ISO 3166-1 alpha-2
274296
# codes https://en.wikipedia.org/wiki/ISO_3166-2
275297
# check if there are language variants in the language folder
276298
if subDir != "SharedRules":
277299
languagesDict = UserInterface.LanguagesDict()
278300
# add to the listbox the text for this language variant together with the code
279-
if languagesDict.get(language + "-" + subDir.upper(), "missing") != "missing":
280-
self.m_choiceLanguage.Append(
281-
languagesDict[language + "-" + subDir.upper()] + " (" + language + "-" + subDir + ")"
282-
)
301+
regionalCode = language + "-" + subDir.upper()
302+
if languagesDict.get(regionalCode, "missing") != "missing":
303+
self.m_choiceLanguage.Append(f"{languagesDict[regionalCode]} ({language}-{subDir})")
283304
elif languagesDict.get(language, "missing") != "missing":
284-
self.m_choiceLanguage.Append(
285-
languagesDict[language] + " (" + language + "-" + subDir + ")"
286-
)
305+
self.m_choiceLanguage.Append(f"{languagesDict[language]} ({regionalCode})")
287306
else:
288-
self.m_choiceLanguage.Append(
289-
language + " (" + language + "-" + subDir + ")"
290-
)
291-
return subDir != "SharedRules"
307+
self.m_choiceLanguage.Append(f"{language} ({regionalCode})")
308+
return [os.path.basename(file) for file in glob.glob(os.path.join(subDir, "*_Rules.yaml"))]
309+
return []
292310

293311
# initialise the language list
294312
languagesDict = UserInterface.LanguagesDict()
@@ -300,27 +318,12 @@ def addRegionalLanguages(subDir: str, language: str) -> bool:
300318
# populate the available language names in the dialog
301319
# the implemented languages are in folders named using the relevant ISO 639-1
302320
# code https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
303-
for language in os.listdir(UserInterface.path_to_languages_folder()):
321+
languageDir = UserInterface.path_to_languages_folder()
322+
for language in os.listdir(languageDir):
304323
pathToLanguageDir = os.path.join(UserInterface.path_to_languages_folder(), language)
305324
if os.path.isdir(pathToLanguageDir):
306325
# only add this language if there is a xxx_Rules.yaml file
307-
ruleFilesWereFound = len(glob.glob(os.path.join(pathToLanguageDir, "*_Rules.yaml"))) > 0
308-
for dir in os.listdir(pathToLanguageDir):
309-
if os.path.isdir(os.path.join(pathToLanguageDir, dir)):
310-
ruleFilesWereFound |= addRegionalLanguages(dir, language)
311-
312-
if not ruleFilesWereFound:
313-
# look in the .zip file for the style files, including regional subdirs -- it might not have been unzipped
314-
try:
315-
zip_file = ZipFile(f"{pathToLanguageDir}\\{language}.zip", "r")
316-
for file in zip_file.namelist():
317-
if file.endswith('_Rules.yaml'):
318-
ruleFilesWereFound = True
319-
elif zip_file.getinfo(file).is_dir():
320-
ruleFilesWereFound |= addRegionalLanguages(dir, language)
321-
except Exception as e:
322-
log.debugWarning(f"MathCAT Dialog: didn't find zip file {zip_file}. Error: {e}")
323-
if ruleFilesWereFound:
326+
if len(self.get_rules_files(pathToLanguageDir, addRegionalLanguages)) > 0:
324327
# add to the listbox the text for this language together with the code
325328
if languagesDict.get(language, "missing") != "missing":
326329
self.m_choiceLanguage.Append(languagesDict[language] + " (" + language + ")")
@@ -374,7 +377,7 @@ def getSpeechStyleFromDirectory(dir: str, lang: str) -> list[str]:
374377
languageCode = languageCode.replace("-", "\\")
375378

376379
languagePath = UserInterface.path_to_languages_folder() + "\\"
377-
log.info(f"languagePath={languagePath}")
380+
# log.info(f"languagePath={languagePath}")
378381
# populate the m_choiceSpeechStyle choices
379382
all_style_files = [
380383
# remove "_Rules.yaml" from the list
@@ -396,14 +399,13 @@ def GetBrailleCodes(self):
396399
# initialise the braille code list
397400
self.m_choiceBrailleMathCode.Clear()
398401
# populate the available braille codes in the dialog
399-
for braille_code in os.listdir(UserInterface.path_to_braille_folder()):
400-
if os.path.isdir(os.path.join(UserInterface.path_to_braille_folder(), braille_code)):
401-
path_to_braille_folder = os.path.join(UserInterface.path_to_braille_folder(), braille_code)
402-
# only add this language if there is a xxx_Rules.yaml file
403-
for file in glob.glob(os.path.join(path_to_braille_folder, "*_Rules.yaml")):
404-
name = file.split('\\')[-1] # get the last component in the path
405-
name = name.split("_Rules.yaml")[0] # get the part before "_Rules.yaml"
406-
self.m_choiceBrailleMathCode.Append(name)
402+
# the dir names are used, not the rule file names because the dir names have to be unique
403+
pathToBrailleFolder = UserInterface.pathToBrailleFolder()
404+
for braille_code in os.listdir(pathToBrailleFolder):
405+
pathToBrailleCode = os.path.join(pathToBrailleFolder, braille_code)
406+
if os.path.isdir(pathToBrailleCode):
407+
if len(self.get_rules_files(pathToBrailleCode, None)) > 0:
408+
self.m_choiceBrailleMathCode.Append(braille_code)
407409

408410
def set_ui_values(self):
409411
# set the UI elements to the ones read from the preference file(s)

0 commit comments

Comments
 (0)