22import MathCATgui
33import yaml
44import os
5+ import glob
56import sys
67import webbrowser
78import gettext
89_ = gettext .gettext
910
10- #initialize the languages dictionary
11- Speech_Language = { "en" : "English : en" }
12-
1311# initialize the user preferences tuples
1412user_preferences = dict ([("" , "" )])
13+ #Sepech_Language is derived from the folder structure
1514Speech_Impairment = ("LearningDisability" , "Blindness" , "LowVision" )
15+ #Speech_SpeechStyle is derived from the yaml files under the selected language
1616Speech_Verbosity = ("Terse" , "Medium" , "Verbose" )
17- Speech_SpeechStyle = ("ClearSpeak" , "SimpleSpeak" )
1817Speech_SubjectArea = ("General" )
1918Speech_Chemistry = ("SpellOut" , "AsCompound" , "Off" )
20-
2119Navigation_NavMode = ("Enhanced" , "Simple" , "Character" )
2220#Navigation_ResetNavMode is boolean
2321#Navigation_OverView is boolean
2927
3028def path_to_default_preferences ():
3129 #the default preferences file is: C:\Users\<user-name>AppData\Roaming\\nvda\\addons\\mathCAT\\globalPlugins\\MathCAT\\Rules\\prefs.yaml
32- return os .path .expanduser ('~' )+ "\\ AppData\\ Roaming\\ nvda\\ addons\\ mathCAT\\ globalPlugins\\ MathCAT\\ Rules\\ prefs.yaml" ;
30+ return os .path .expanduser ('~' )+ "\\ AppData\\ Roaming\\ nvda\\ addons\\ mathCAT\\ globalPlugins\\ MathCAT\\ Rules\\ prefs.yaml"
3331
3432def path_to_user_preferences_folder ():
3533 #the user preferences file is stored at: C:\Users\<user-name>AppData\Roaming\MathCAT\prefs.yaml
36- return os .path .expanduser ('~' )+ "\\ AppData\\ Roaming\\ MathCAT" ;
34+ return os .path .expanduser ('~' )+ "\\ AppData\\ Roaming\\ MathCAT"
3735
3836def path_to_user_preferences ():
3937 #the user preferences file is stored at: C:\Users\<user-name>AppData\Roaming\MathCAT\prefs.yaml
40- return path_to_user_preferences_folder () + "\\ prefs.yaml" ;
38+ return path_to_user_preferences_folder () + "\\ prefs.yaml"
4139
4240def load_default_preferences ():
4341 global user_preferences
4442 #load default preferences into the user preferences data structure (overwites existing)
4543 if os .path .exists (path_to_default_preferences ()):
46- with open (path_to_default_preferences ()) as f :
44+ with open (path_to_default_preferences (), encoding = 'utf-8' ) as f :
4745 user_preferences = yaml .load (f , Loader = yaml .FullLoader )
4846 else :
4947 #default preferences file is NOT found
@@ -66,20 +64,44 @@ def write_user_preferences():
6664 #write values to the user preferences file, NOT the default
6765 yaml .dump (user_preferences , f )
6866
69- def GetKey (val ):
70- for key , value in Speech_Language .items ():
71- if val == value :
72- return key
73- return "en"
74-
7567class UserInterface (MathCATgui .MathCATPreferencesDialog ):
7668
69+ def GetLanguages (self ):
70+ #clear the language choices
71+ self .m_choiceLanguage .Clear ()
72+ #populate the language choices
73+ for f in os .listdir (os .path .expanduser ('~' )+ "\\ AppData\\ Roaming\\ nvda\\ addons\\ mathCAT\\ globalPlugins\\ MathCAT\\ Rules\\ Languages" ):
74+ if os .path .isdir (os .path .expanduser ('~' )+ "\\ AppData\\ Roaming\\ nvda\\ addons\\ mathCAT\\ globalPlugins\\ MathCAT\\ Rules\\ Languages\\ " + f ):
75+ self .m_choiceLanguage .Append (f )
76+
77+ def GetSpeechStyles (self , this_SpeechStyle ):
78+ #clear the SpeechStyle choices
79+ self .m_choiceSpeechStyle .Clear ()
80+ #get the currently selected language
81+ this_language = self .m_choiceLanguage .GetStringSelection ()
82+ this_path = os .path .expanduser ('~' )+ "\\ AppData\\ Roaming\\ nvda\\ addons\\ mathCAT\\ globalPlugins\\ MathCAT\\ Rules\\ Languages\\ " + this_language + "\\ *_Rules.yaml"
83+ #populate the m_choiceSpeechStyle choices
84+ for f in glob .glob (this_path ):
85+ fname = os .path .basename (f )
86+ self .m_choiceSpeechStyle .Append ((fname [:fname .find ("_Rules.yaml" )]))
87+ try :
88+ #set the SpeechStyle to the same as previous
89+ self .m_choiceSpeechStyle .SetStringSelection (this_SpeechStyle )
90+ except :
91+ #that didn't work, choose the first in the list
92+ self .m_choiceSpeechStyle .SetSelection (0 )
93+
7794 def set_ui_values (self ):
7895 #set the UI elements to the ones read from the preference file(s)
7996 try :
8097 self .m_choiceImpairment .SetSelection (Speech_Impairment .index (user_preferences ["Speech" ]["Impairment" ]))
81- self .m_choiceLanguage .SetStringSelection (Speech_Language [user_preferences ["Speech" ]["Language" ]])
82- self .m_choiceSpeechStyle .SetSelection (Speech_SpeechStyle .index (user_preferences ["Speech" ]["SpeechStyle" ]))
98+ try :
99+ self .m_choiceLanguage .SetStringSelection (user_preferences ["Speech" ]["Language" ])
100+ except :
101+ #the language in the settings file is not in the folder structure, something went wrong, set to the first in the list
102+ self .m_choiceLanguage .SetSelection (0 )
103+ #now get the available SpeechStyles from the folder structure and set to the preference setting is possible
104+ UserInterface .GetSpeechStyles (self , user_preferences ["Speech" ]["SpeechStyle" ])
83105 self .m_choiceSpeechAmount .SetSelection (Speech_Verbosity .index (user_preferences ["Speech" ]["Verbosity" ]))
84106 self .m_choiceSpeechForChemical .SetSelection (Speech_Chemistry .index (user_preferences ["Speech" ]["Chemistry" ]))
85107 self .m_choiceNavigationMode .SetSelection (Navigation_NavMode .index (user_preferences ["Navigation" ]["NavMode" ]))
@@ -100,8 +122,8 @@ def get_ui_values(self):
100122 global user_preferences
101123 # read the values from the UI and update the user preferences dictionary
102124 user_preferences ["Speech" ]["Impairment" ] = Speech_Impairment [self .m_choiceImpairment .GetSelection ()]
103- user_preferences ["Speech" ]["Language" ] = GetKey ( self .m_choiceLanguage .GetStringSelection () )
104- user_preferences ["Speech" ]["SpeechStyle" ] = Speech_SpeechStyle [ self .m_choiceSpeechStyle .GetSelection ()]
125+ user_preferences ["Speech" ]["Language" ] = self .m_choiceLanguage .GetStringSelection ()
126+ user_preferences ["Speech" ]["SpeechStyle" ] = self .m_choiceSpeechStyle .GetStringSelection ()
105127 user_preferences ["Speech" ]["Verbosity" ] = Speech_Verbosity [self .m_choiceSpeechAmount .GetSelection ()]
106128 user_preferences ["Speech" ]["Chemistry" ] = Speech_Chemistry [self .m_choiceSpeechForChemical .GetSelection ()]
107129 user_preferences ["Navigation" ]["NavMode" ] = Navigation_NavMode [self .m_choiceNavigationMode .GetSelection ()]
@@ -129,15 +151,9 @@ def __init__(self,parent):
129151 #set the categories selection to the first item
130152 self .m_listBoxPreferencesTopic .SetSelection (0 )
131153 user_preferences ["MathCATPreferencesLastCategory" ]= "0"
132- #populate the language choices
133- self .m_choiceLanguage .Clear ()
134- for lang in Speech_Language .values ():
135- self .m_choiceLanguage .Append (lang )
136- #populate the SpeechStyle choices
137- #this will need to be done on change of language
138- self .m_choiceSpeechStyle .Clear ()
139- self .m_choiceSpeechStyle .Append ("ClearSpeak" )
140-
154+ #populate the languages
155+ UserInterface .GetLanguages (self )
156+ #set the ui items to match the preferences
141157 UserInterface .set_ui_values (self )
142158
143159 def OnClickOK (self ,event ):
@@ -160,9 +176,13 @@ def OnClickHelp(self,event):
160176 webbrowser .open ('https://nsoiffer.github.io/MathCAT/users.html' )
161177
162178 def OnListBoxCategories (self ,event ):
163- #show the appropriate dialogue page
179+ #the category changed, now show the appropriate dialogue page
164180 self .m_simplebookPanelsCategories .SetSelection (self .m_listBoxPreferencesTopic .GetSelection ())
165181
182+ def OnLanguage (self ,event ):
183+ #the language changed, get the SpeechStyles for the new language
184+ UserInterface .GetSpeechStyles (self , self .m_choiceSpeechStyle .GetSelection ())
185+
166186 def MathCATPreferencesDialogOnCharHook (self ,event ):
167187 #designed choice is that Enter is the same as clicking OK, and Escape is the same as clicking Cancel
168188 keyCode = event .GetKeyCode ()
0 commit comments