1111from logHandler import log # logging
1212
1313# initialize the user preferences tuples
14- user_preferences = dict ([( "" , "" )])
14+ user_preferences = {}
1515#Speech_Language is derived from the folder structure
1616Speech_Impairment = ("LearningDisability" , "Blindness" , "LowVision" )
1717#Speech_SpeechStyle is derived from the yaml files under the selected language
2323#Navigation_OverView is boolean
2424Navigation_NavVerbosity = ("Terse" , "Medium" , "Verbose" )
2525#Navigation_AutoZoomOut is boolean
26-
2726Braille_BrailleNavHighlight = ("Off" , "FirstChar" , "EndPoints" , "All" )
2827Braille_BrailleCode = ("Nemeth" , "UEB" )
2928
30-
3129class UserInterface (MathCATgui .MathCATPreferencesDialog ):
3230 def __init__ (self ,parent ):
3331 #initialize parent class
@@ -41,6 +39,7 @@ def __init__(self,parent):
4139 # load in the system values followed by the user prefs (if any)
4240 UserInterface .load_default_preferences ()
4341 UserInterface .load_user_preferences ()
42+ UserInterface .validate_user_preferences ()
4443
4544 if "MathCATPreferencesLastCategory" in user_preferences :
4645 #set the categories selection to what we used on last run
@@ -96,6 +95,7 @@ def set_ui_values(self):
9695 self .m_choiceLanguage .SetSelection (0 )
9796 #now get the available SpeechStyles from the folder structure and set to the preference setting is possible
9897 self .GetSpeechStyles (user_preferences ["Speech" ]["SpeechStyle" ])
98+ #set the rest of the UI elements
9999 self .m_choiceSpeechAmount .SetSelection (Speech_Verbosity .index (user_preferences ["Speech" ]["Verbosity" ]))
100100 self .m_sliderRelativeSpeed .SetValue (user_preferences ["Speech" ]["MathRate" ])
101101 self .m_choiceSpeechForChemical .SetSelection (Speech_Chemistry .index (user_preferences ["Speech" ]["Chemistry" ]))
@@ -153,10 +153,6 @@ def load_default_preferences():
153153 if os .path .exists (UserInterface .path_to_default_preferences ()):
154154 with open (UserInterface .path_to_default_preferences (), encoding = 'utf-8' ) as f :
155155 user_preferences = yaml .load (f , Loader = yaml .FullLoader )
156- else :
157- #default preferences file is NOT found
158- wx .MessageBox (_ (u"MathCat preferences file not found. The program will now exit." ), "Error" , wx .OK | wx .ICON_ERROR )
159- os .sys .exit (- 1 )
160156
161157 def load_user_preferences ():
162158 global user_preferences
@@ -166,6 +162,65 @@ def load_user_preferences():
166162 # merge with the default preferences, overwriting with the user's values
167163 user_preferences .update (yaml .load (f , Loader = yaml .FullLoader ))
168164
165+ def validate (key1 , key2 , valid_values , default_value ):
166+ global user_preferences
167+ try :
168+ if valid_values == None :
169+ #any value is valid
170+ if user_preferences [key1 ][key2 ] != "" :
171+ return
172+ if (type (valid_values [0 ]) == int ) and (type (valid_values [1 ]) == int ):
173+ #any value between lower and upper bounds is valid
174+ if (user_preferences [key1 ][key2 ] >= valid_values (0 )) and (user_preferences [key1 ][key2 ] <= valid_values (1 )):
175+ return
176+ else :
177+ #any value in the list is valid
178+ if user_preferences [key1 ][key2 ] in valid_values :
179+ return
180+ except :
181+ #the preferences entry does not exist
182+ pass
183+ if not key1 in user_preferences :
184+ user_preferences [key1 ] = {key2 : default_value }
185+ else :
186+ user_preferences [key1 ][key2 ] = default_value
187+
188+ def validate_user_preferences ():
189+ #check each user preference value to ensure it is present and valid, set default value if not
190+ # Speech:
191+ #Impairment: Blindness # LearningDisability, LowVision, Blindness
192+ UserInterface .validate ("Speech" , "Impairment" , ["LearningDisability" , "LowVision" , "Blindness" ], "Blindness" )
193+ # Language: en # any known language code and sub-code -- could be en-uk, etc
194+ UserInterface .validate ("Speech" , "Language" , None , "en" )
195+ # Verbosity: Medium # Terse, Medium, Verbose
196+ UserInterface .validate ("Speech" , "Verbosity" , ["Terse" , "Medium" , "Verbose" ], "Medium" )
197+ # MathRate: 100 # Change from text speech rate (%)
198+ UserInterface .validate ("Speech" , "MathRate" , [0 ,200 ], 100 )
199+ # SpeechStyle: ClearSpeak # Any known speech style (falls back to ClearSpeak)
200+ UserInterface .validate ("Speech" , "SpeechStyle" , None , "ClearSpeak" )
201+ # SubjectArea: General # FIX: still working on this
202+ UserInterface .validate ("Speech" , "SubjectArea" , None , "General" )
203+ # Chemistry: SpellOut # SpellOut (H 2 0), AsCompound (Water), Off (H sub 2 O)
204+ UserInterface .validate ("Speech" , "Chemistry" , ["SpellOut" , "AsCompound" , "Off" ], "SpellOut" )
205+ #Navigation:
206+ # NavMode: Enhanced # Enhanced, Simple, Character
207+ UserInterface .validate ("Navigation" , "NavMode" , ["Enhanced" , "Simple" , "Character" ], "Enhanced" )
208+ # ResetNavMode: false # remember previous value and use it
209+ UserInterface .validate ("Navigation" , "ResetNavMode" , [False , True ], False )
210+ # Overview: false # speak the expression or give a description/overview
211+ UserInterface .validate ("Navigation" , "Overview" , [False , True ] ,False )
212+ # ResetOverview: true # remember previous value and use it
213+ UserInterface .validate ("Navigation" , "ResetOverview" , [False , True ], True )
214+ # NavVerbosity: Medium # Terse, Medium, Full (words to say for nav command)
215+ UserInterface .validate ("Navigation" , "NavVerbosity" , ["Terse" , "Medium" , "Full" ], "Medium" )
216+ # AutoZoomOut: true # Auto zoom out of 2D exprs (use shift-arrow to force zoom out if unchecked)
217+ UserInterface .validate ("Navigation" , "AutoZoomOut" , [False , True ], True )
218+ #Braille:
219+ # BrailleNavHighlight: EndPoints # Highlight with dots 7 & 8 the current nav node -- values are Off, FirstChar, EndPoints, All
220+ UserInterface .validate ("Braille" , "BrailleNavHighlight" , ["Off" , "FirstChar" , "EndPoints" , "All" ], "EndPoints" )
221+ # BrailleCode: "Nemeth" # Any supported braille code (currently Nemeth, UEB)
222+ UserInterface .validate ("Braille" , "BrailleCode" , ["Nemeth" , "UEB" ], "Nemeth" )
223+
169224 def write_user_preferences ():
170225 if not os .path .exists (UserInterface .path_to_user_preferences_folder ()):
171226 #create a folder for the user preferences
@@ -195,6 +250,7 @@ def OnClickApply(self,event):
195250
196251 def OnClickReset (self ,event ):
197252 UserInterface .load_default_preferences ()
253+ UserInterface .validate_user_preferences ()
198254 UserInterface .set_ui_values (self )
199255
200256 def OnClickHelp (self ,event ):
0 commit comments