99import webbrowser
1010import gettext
1111import addonHandler
12- # from logHandler import log # logging
12+ from logHandler import log # logging
1313from typing import List , Dict , Union
1414from .MathCAT import ConvertSSMLTextForNVDA
1515from speech import speak
3535# Navigation_OverView is boolean
3636Navigation_NavVerbosity = ("Terse" , "Medium" , "Verbose" )
3737# Navigation_AutoZoomOut is boolean
38- Navigation_CopyMathAs = ("MathML" , "LaTeX" , "ASCIIMath" )
38+ Navigation_CopyAs = ("MathML" , "LaTeX" , "ASCIIMath" )
3939Braille_BrailleNavHighlight = ("Off" , "FirstChar" , "EndPoints" , "All" )
4040
4141
@@ -54,6 +54,10 @@ def __init__(self, parent):
5454 # load in the system values followed by the user prefs (if any)
5555 UserInterface .load_default_preferences ()
5656 UserInterface .load_user_preferences ()
57+
58+ # hack for "CopyAs" because its location in the prefs is not yet fixed
59+ if "CopyAs" not in user_preferences ["Navigation" ]:
60+ user_preferences ["Navigation" ]["CopyAs" ] = user_preferences ["Other" ]["CopyAs" ] if "CopyAs" in user_preferences ["Other" ] else "MathML"
5761 UserInterface .validate_user_preferences ()
5862
5963 if "NVDAAddOn" in user_preferences :
@@ -338,7 +342,7 @@ def GetSpeechStyles(self, this_SpeechStyle: str):
338342 # set the SpeechStyle to the same as previous
339343 self .m_choiceSpeechStyle .SetStringSelection (this_SpeechStyle )
340344 except Exception as e :
341- print (f"An exception occurred: { e } " )
345+ log . error (f"MathCAT: An exception occurred in GetSpeechStyles evaluating set SetStringSelection : { e } " )
342346 # that didn't work, choose the first in the list
343347 self .m_choiceSpeechStyle .SetSelection (0 )
344348
@@ -363,25 +367,23 @@ def set_ui_values(self):
363367 )
364368 try :
365369 lang_pref = user_preferences ["Speech" ]["Language" ]
366- i = 0
367- while f"({ lang_pref } )" not in self .m_choiceLanguage .GetString (i ):
368- i = i + 1
369- if i == self .m_choiceLanguage .GetCount ():
370+ self .m_choiceLanguage .SetSelection (0 )
371+ i = 1 # no need to test i == 0
372+ while i < self .m_choiceLanguage .GetCount ():
373+ if f"({ lang_pref } )" in self .m_choiceLanguage .GetString (i ):
374+ self .m_choiceLanguage .SetSelection (i )
370375 break
371- if f"({ lang_pref } )" in self .m_choiceLanguage .GetString (i ):
372- self .m_choiceLanguage .SetSelection (i )
373- else :
374- self .m_choiceLanguage .SetSelection (0 )
376+ i += 1
375377 except Exception as e :
376- print (f"An exception occurred while trying to set the Language: { e } " )
378+ log . error (f"MathCAT: An exception occurred in set_ui_values (' { user_preferences [ 'Speech' ][ ' Language' ] } ') : { e } " )
377379 # the language in the settings file is not in the folder structure, something went wrong,
378380 # set to the first in the list
379381 self .m_choiceLanguage .SetSelection (0 )
380382 try :
381383 # now get the available SpeechStyles from the folder structure and set to the preference setting is possible
382384 self .GetSpeechStyles (str (user_preferences ["Speech" ]["SpeechStyle" ]))
383385 except Exception as e :
384- print (f"An exception occurred: { e } " )
386+ log . error (f"MathCAT: An exception occurred in set_ui_values (getting SpeechStyle) : { e } " )
385387 self .m_choiceSpeechStyle .Append (
386388 "Error when setting SpeechStyle for " + self .m_choiceLanguage .GetStringSelection ()
387389 )
@@ -413,7 +415,7 @@ def set_ui_values(self):
413415 self .m_choiceNavigationSpeech .SetSelection (0 )
414416 self .m_checkBoxResetNavigationSpeech .SetValue (user_preferences ["Navigation" ]["ResetOverview" ])
415417 self .m_checkBoxAutomaticZoom .SetValue (user_preferences ["Navigation" ]["AutoZoomOut" ])
416- self .m_choiceCopyMathAs .SetSelection (Navigation_CopyMathAs .index (user_preferences ["Navigation" ]["CopyMathAs " ]))
418+ self .m_choiceCopyAs .SetSelection (Navigation_CopyAs .index (user_preferences ["Navigation" ]["CopyAs " ]))
417419
418420 self .m_choiceBrailleHighlights .SetSelection (
419421 Braille_BrailleNavHighlight .index (user_preferences ["Braille" ]["BrailleNavHighlight" ])
@@ -430,7 +432,7 @@ def set_ui_values(self):
430432 else :
431433 self .m_choiceBrailleMathCode .SetSelection (0 )
432434 except Exception as e :
433- print (f"An exception occurred while trying to set the Braille code: { e } " )
435+ log . error (f"MathCAT: An exception occurred while trying to set the Braille code: { e } " )
434436 # the braille code in the settings file is not in the folder structure, something went wrong,
435437 # set to the first in the list
436438 self .m_choiceBrailleMathCode .SetSelection (0 )
@@ -465,7 +467,7 @@ def get_ui_values(self):
465467 user_preferences ["Navigation" ]["Overview" ] = self .m_choiceNavigationSpeech .GetSelection () != 0
466468 user_preferences ["Navigation" ]["ResetOverview" ] = self .m_checkBoxResetNavigationSpeech .GetValue ()
467469 user_preferences ["Navigation" ]["AutoZoomOut" ] = self .m_checkBoxAutomaticZoom .GetValue ()
468- user_preferences ["Navigation" ]["CopyMathAs " ] = Navigation_CopyMathAs [self .m_choiceCopyMathAs .GetSelection ()]
470+ user_preferences ["Navigation" ]["CopyAs " ] = Navigation_CopyAs [self .m_choiceCopyAs .GetSelection ()]
469471
470472 user_preferences ["Braille" ]["BrailleNavHighlight" ] = (
471473 Braille_BrailleNavHighlight [self .m_choiceBrailleHighlights .GetSelection ()]
@@ -524,7 +526,7 @@ def validate(key1: str, key2: str, valid_values: List[Union[str, bool]], default
524526 if user_preferences [key1 ][key2 ] in valid_values :
525527 return
526528 except Exception as e :
527- print (f"An exception occurred: { e } " )
529+ log . error (f"MathCAT: An exception occurred in validate : { e } " )
528530 # the preferences entry does not exist
529531 if key1 not in user_preferences :
530532 user_preferences [key1 ] = {key2 : default_value }
@@ -539,7 +541,7 @@ def validate_int(key1: str, key2: str, valid_values: List[int], default_value: i
539541 if int (user_preferences [key1 ][key2 ]) >= valid_values [0 ] and int (user_preferences [key1 ][key2 ]) <= valid_values [1 ]:
540542 return
541543 except Exception as e :
542- print (f"An exception occurred: { e } " )
544+ log . error (f"MathCAT: An exception occurred in validate_int : { e } " )
543545 # the preferences entry does not exist
544546 if key1 not in user_preferences :
545547 user_preferences [key1 ] = {key2 : default_value }
@@ -581,8 +583,8 @@ def validate_user_preferences():
581583 UserInterface .validate ("Navigation" , "NavVerbosity" , ["Terse" , "Medium" , "Full" ], "Medium" )
582584 # AutoZoomOut: true # Auto zoom out of 2D exprs (use shift-arrow to force zoom out if unchecked)
583585 UserInterface .validate ("Navigation" , "AutoZoomOut" , [False , True ], True )
584- # NavCopyMathAs : MathML # MathML, LaTeX, ASCIIMath
585- UserInterface .validate ("Navigation" , "CopyMathAs " , ["MathML" , "LaTeX" , "ASCIIMath" ], "MathML" )
586+ # CopyAs : MathML # MathML, LaTeX, ASCIIMath
587+ UserInterface .validate ("Navigation" , "CopyAs " , ["MathML" , "LaTeX" , "ASCIIMath" ], "MathML" )
586588 # Braille:
587589 # BrailleNavHighlight: EndPoints
588590 # Highlight with dots 7 & 8 the current nav node -- values are Off, FirstChar, EndPoints, All
@@ -594,8 +596,10 @@ def validate_user_preferences():
594596 def write_user_preferences ():
595597 # Language is special because it is set elsewhere by SetPreference which overrides the user_prefs -- so set it here
596598 from . import libmathcat # type: ignore
597-
598- libmathcat .SetPreference ("Language" , user_preferences ["Speech" ]["Language" ])
599+ try :
600+ libmathcat .SetPreference ("Language" , user_preferences ["Speech" ]["Language" ])
601+ except Exception as e :
602+ log .error (f'Error in trying to set MathCAT "Language" preference to "{ user_preferences ["Speech" ]["Language" ]} ": { e } ' )
599603 if not os .path .exists (UserInterface .path_to_user_preferences_folder ()):
600604 # create a folder for the user preferences
601605 os .mkdir (UserInterface .path_to_user_preferences_folder ())
0 commit comments