Skip to content

Commit faaf98b

Browse files
committed
Added docstrings to MathCAT.py
1 parent e1aed55 commit faaf98b

File tree

1 file changed

+65
-9
lines changed

1 file changed

+65
-9
lines changed

addon/globalPlugins/MathCAT/MathCAT.py

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,11 @@
9292

9393

9494
def getLanguageToUse(mathMl: str = "") -> str:
95-
"""Get the language specified in a math tag if the language pref is Auto, else the language preference."""
95+
"""Get the language specified in a math tag if the language pref is Auto, else the language preference.
96+
97+
:param mathMl: The MathML string to examine for language. Defaults to an empty string.
98+
:returns: The language string to use.
99+
"""
96100
mathCATLanguageSetting: str = "Auto"
97101
try:
98102
# ignore regional differences if the MathCAT language setting doesn't have it.
@@ -117,8 +121,13 @@ def getLanguageToUse(mathMl: str = "") -> str:
117121

118122

119123
def convertSSMLTextForNVDA(text: str) -> list[str | SpeechCommand]:
120-
"""Change the SSML in the text into NVDA's command structure.
121-
The environment is examined to determine whether a language switch is needed"""
124+
"""
125+
Change the SSML in the text into NVDA's command structure.
126+
The environment is examined to determine whether a language switch is needed.
127+
128+
:param text: The SSML text to convert.
129+
:returns: A list of strings and SpeechCommand objects.
130+
"""
122131
# MathCAT's default rate is 180 wpm.
123132
# Assume that 0% is 80 wpm and 100% is 450 wpm and scale accordingly.
124133
# log.info(f"\nSpeech str: '{text}'")
@@ -212,6 +221,7 @@ def convertSSMLTextForNVDA(text: str) -> list[str | SpeechCommand]:
212221

213222

214223
class MathCATInteraction(mathPres.MathInteractionNVDAObject):
224+
"""An NVDA object used to interact with MathML."""
215225
# Put MathML or other formats on the clipboard.
216226
# MathML is put on the clipboard using the two formats below (defined by MathML spec)
217227
# We use both formats because some apps may only use one or the other
@@ -225,16 +235,22 @@ class MathCATInteraction(mathPres.MathInteractionNVDAObject):
225235

226236
def __init__(
227237
self,
228-
provider: mathPres.MathPresentationProvider = None,
238+
provider: mathPres.MathPresentationProvider | None = None,
229239
mathMl: str | None = None,
230240
):
241+
"""Initialize the MathCATInteraction object.
242+
243+
:param provider: Optional presentation provider.
244+
:param mathMl: Optional initial MathML string.
245+
"""
231246
super(MathCATInteraction, self).__init__(provider=provider, mathMl=mathMl)
232247
if mathMl is None:
233248
self.initMathML = "<math></math>"
234249
else:
235250
self.initMathML = mathMl
236251

237252
def reportFocus(self) -> None:
253+
"""Calls MathCAT's ZoomIn command and speaks the resulting text."""
238254
super(MathCATInteraction, self).reportFocus()
239255
# try to get around espeak bug where voice slows down
240256
if _synthesizerRate and getSynth().name == "espeak":
@@ -256,6 +272,7 @@ def getBrailleRegions(
256272
self,
257273
review: bool = False,
258274
) -> Generator[braille.Region, None, None]:
275+
"""Yields braille.Region objects for this MathCATInteraction object."""
259276
# log.info("***MathCAT start getBrailleRegions")
260277
yield braille.NVDAObjectRegion(self, appendText=" ")
261278
region: braille.Region = braille.Region()
@@ -276,7 +293,12 @@ def getScript(
276293
self,
277294
gesture: KeyboardInputGesture,
278295
) -> Callable[KeyboardInputGesture, None] | None:
279-
# Pass most keys to MathCAT. Pretty ugly.
296+
"""
297+
Returns the script function bound to the given gesture.
298+
299+
:param gesture: A KeyboardInputGesture sent to this object.
300+
:returns: The script bound to that gesture.
301+
"""
280302
if (
281303
isinstance(gesture, KeyboardInputGesture)
282304
and "NVDA" not in gesture.modifierNames
@@ -309,6 +331,10 @@ def getScript(
309331
return super().getScript(gesture)
310332

311333
def script_navigate(self, gesture: KeyboardInputGesture) -> None:
334+
"""Performs the specified navigation command.
335+
336+
:param gesture: They keyboard command which specified the navigation command to perform.
337+
"""
312338
try:
313339
# try to get around espeak bug where voice slows down
314340
if _synthesizerRate and getSynth().name == "espeak":
@@ -365,6 +391,10 @@ def script_navigate(self, gesture: KeyboardInputGesture) -> None:
365391
gesture="kb:control+c",
366392
)
367393
def script_rawdataToClip(self, gesture: KeyboardInputGesture) -> None:
394+
"""Copies the raw data to the clipboard, either as MathML, ASCII math, or LaTeX, depending on user preferences.
395+
396+
:param gesture: The gesture which activated this script.
397+
"""
368398
try:
369399
copyAs: str = "mathml" # value used even if "CopyAs" pref is invalid
370400
textToCopy: str = ""
@@ -416,7 +446,7 @@ def script_rawdataToClip(self, gesture: KeyboardInputGesture) -> None:
416446
_hasDataAttr: re.Pattern = re.compile(" data-[^=]+='[^']*'")
417447

418448
def _wrapMathMLForClipBoard(self, text: str) -> str:
419-
# cleanup the MathML a little
449+
"""Cleanup the MathML a little."""
420450
text = re.sub(self._hasAddedId, "", text)
421451
mathMLWithNS: str = re.sub(self._hasDataAttr, "", text)
422452
if not re.match(self._mathTagHasNameSpace, mathMLWithNS):
@@ -434,9 +464,10 @@ def _copyToClipAsMathML(
434464
notify: bool | None = False,
435465
) -> bool:
436466
"""Copies the given text to the windows clipboard.
437-
@returns: True if it succeeds, False otherwise.
438-
@param text: the text which will be copied to the clipboard
439-
@param notify: whether to emit a confirmation message
467+
468+
:param text: the text which will be copied to the clipboard.
469+
:param notify: whether to emit a confirmation message.
470+
:returns: True if it succeeds, False otherwise.
440471
"""
441472
# copied from api.py and modified to use CF_MathML_Presentation
442473
if not isinstance(text, str) or len(text) == 0:
@@ -463,6 +494,11 @@ def _copyToClipAsMathML(
463494
return False
464495

465496
def _setClipboardData(self, format: int, data: str) -> None:
497+
"""Sets the clipboard data to the given data with the specified format.
498+
499+
:param format: The format for the clipboard data. This is an integer format code returned by windll.user32.RegisterClipboardFormatW.
500+
:param data: The data to set on the clipboard.
501+
"""
466502
# Need to support MathML Presentation, so this copied from winUser.py and the first two lines are commented out
467503
# For now only unicode is a supported format
468504
# if format!=CF_UNICODETEXT:
@@ -485,6 +521,7 @@ def _setClipboardData(self, format: int, data: str) -> None:
485521

486522
class MathCAT(mathPres.MathPresentationProvider):
487523
def __init__(self):
524+
"""Initializes MathCAT, loading the rules specified in the rules directory."""
488525
# super(MathCAT, self).__init__(*args, **kwargs)
489526

490527
try:
@@ -502,6 +539,11 @@ def getSpeechForMathMl(
502539
self,
503540
mathml: str,
504541
) -> list[str | SpeechCommand]:
542+
"""Outputs a MathML string as speech.
543+
544+
:param mathml: A MathML string.
545+
:returns: A list of speech commands and strings representing the given MathML.
546+
"""
505547
global _synthesizerRate
506548
synth: SynthDriver = getSynth()
507549
synthConfig = config.conf["speech"][synth.name]
@@ -557,13 +599,22 @@ def getSpeechForMathMl(
557599
getSynth()._set_rate(_synthesizerRate)
558600

559601
def _addSounds(self) -> bool:
602+
"""Queries the user preferences to determine whether or not sounds should be added.
603+
604+
:returns: True if MathCAT's `SpeechSound` preference is set, and False otherwise.
605+
"""
560606
try:
561607
return libmathcat.GetPreference("SpeechSound") != "None"
562608
except Exception as e:
563609
log.exception(f"MathCAT: An exception occurred in _add_sounds: {e}")
564610
return False
565611

566612
def getBrailleForMathMl(self, mathml: str) -> str:
613+
"""Gets the braille representation of a given MathML string by calling MathCAT's GetBraille function.
614+
615+
:param mathml: A MathML string.
616+
:returns: A braille string representing the input MathML.
617+
"""
567618
# log.info("***MathCAT getBrailleForMathMl")
568619
try:
569620
libmathcat.SetMathML(mathml)
@@ -582,6 +633,10 @@ def getBrailleForMathMl(self, mathml: str) -> str:
582633
return ""
583634

584635
def interactWithMathMl(self, mathml: str) -> None:
636+
"""Interact with a MathML string, creating a MathCATInteraction object.
637+
638+
:param mathml: The MathML representing the math to interact with.
639+
"""
585640
MathCATInteraction(provider=self, mathMl=mathml).setFocus()
586641
MathCATInteraction(provider=self, mathMl=mathml).script_navigate(None)
587642

@@ -590,6 +645,7 @@ def interactWithMathMl(self, mathml: str) -> None:
590645

591646

592647
def _monkeyPatchESpeak() -> None:
648+
"""Patches an eSpeak bug where the voice slows down."""
593649
global CACHED_SYNTH
594650
currentSynth: SynthDriver = getSynth()
595651
if currentSynth.name != "espeak" or CACHED_SYNTH == currentSynth:

0 commit comments

Comments
 (0)