Skip to content

Commit 6b5910f

Browse files
authored
Docstrings (#8)
This PR adds docstrings to classes, methods, and functions.
1 parent cfe7340 commit 6b5910f

File tree

4 files changed

+314
-30
lines changed

4 files changed

+314
-30
lines changed

addon/globalPlugins/MathCAT/MathCAT.py

Lines changed: 67 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,8 @@ def convertSSMLTextForNVDA(text: str) -> list[str | SpeechCommand]:
212221

213222

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

226237
def __init__(
227238
self,
228-
provider: mathPres.MathPresentationProvider = None,
239+
provider: mathPres.MathPresentationProvider | None = None,
229240
mathMl: str | None = None,
230241
):
242+
"""Initialize the MathCATInteraction object.
243+
244+
:param provider: Optional presentation provider.
245+
:param mathMl: Optional initial MathML string.
246+
"""
231247
super(MathCATInteraction, self).__init__(provider=provider, mathMl=mathMl)
232248
if mathMl is None:
233249
self.initMathML = "<math></math>"
234250
else:
235251
self.initMathML = mathMl
236252

237253
def reportFocus(self) -> None:
254+
"""Calls MathCAT's ZoomIn command and speaks the resulting text."""
238255
super(MathCATInteraction, self).reportFocus()
239256
# try to get around espeak bug where voice slows down
240257
if _synthesizerRate and getSynth().name == "espeak":
@@ -256,6 +273,7 @@ def getBrailleRegions(
256273
self,
257274
review: bool = False,
258275
) -> Generator[braille.Region, None, None]:
276+
"""Yields braille.Region objects for this MathCATInteraction object."""
259277
# log.info("***MathCAT start getBrailleRegions")
260278
yield braille.NVDAObjectRegion(self, appendText=" ")
261279
region: braille.Region = braille.Region()
@@ -276,7 +294,12 @@ def getScript(
276294
self,
277295
gesture: KeyboardInputGesture,
278296
) -> Callable[KeyboardInputGesture, None] | None:
279-
# Pass most keys to MathCAT. Pretty ugly.
297+
"""
298+
Returns the script function bound to the given gesture.
299+
300+
:param gesture: A KeyboardInputGesture sent to this object.
301+
:returns: The script bound to that gesture.
302+
"""
280303
if (
281304
isinstance(gesture, KeyboardInputGesture)
282305
and "NVDA" not in gesture.modifierNames
@@ -309,6 +332,10 @@ def getScript(
309332
return super().getScript(gesture)
310333

311334
def script_navigate(self, gesture: KeyboardInputGesture) -> None:
335+
"""Performs the specified navigation command.
336+
337+
:param gesture: They keyboard command which specified the navigation command to perform.
338+
"""
312339
try:
313340
# try to get around espeak bug where voice slows down
314341
if _synthesizerRate and getSynth().name == "espeak":
@@ -365,6 +392,10 @@ def script_navigate(self, gesture: KeyboardInputGesture) -> None:
365392
gesture="kb:control+c",
366393
)
367394
def script_rawdataToClip(self, gesture: KeyboardInputGesture) -> None:
395+
"""Copies the raw data to the clipboard, either as MathML, ASCII math, or LaTeX, depending on user preferences.
396+
397+
:param gesture: The gesture which activated this script.
398+
"""
368399
try:
369400
copyAs: str = "mathml" # value used even if "CopyAs" pref is invalid
370401
textToCopy: str = ""
@@ -416,7 +447,7 @@ def script_rawdataToClip(self, gesture: KeyboardInputGesture) -> None:
416447
_hasDataAttr: re.Pattern = re.compile(" data-[^=]+='[^']*'")
417448

418449
def _wrapMathMLForClipBoard(self, text: str) -> str:
419-
# cleanup the MathML a little
450+
"""Cleanup the MathML a little."""
420451
text = re.sub(self._hasAddedId, "", text)
421452
mathMLWithNS: str = re.sub(self._hasDataAttr, "", text)
422453
if not re.match(self._mathTagHasNameSpace, mathMLWithNS):
@@ -434,9 +465,10 @@ def _copyToClipAsMathML(
434465
notify: bool | None = False,
435466
) -> bool:
436467
"""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
468+
469+
:param text: the text which will be copied to the clipboard.
470+
:param notify: whether to emit a confirmation message.
471+
:returns: True if it succeeds, False otherwise.
440472
"""
441473
# copied from api.py and modified to use CF_MathML_Presentation
442474
if not isinstance(text, str) or len(text) == 0:
@@ -463,6 +495,12 @@ def _copyToClipAsMathML(
463495
return False
464496

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

486524
class MathCAT(mathPres.MathPresentationProvider):
487525
def __init__(self):
526+
"""Initializes MathCAT, loading the rules specified in the rules directory."""
488527
# super(MathCAT, self).__init__(*args, **kwargs)
489528

490529
try:
@@ -502,6 +541,11 @@ def getSpeechForMathMl(
502541
self,
503542
mathml: str,
504543
) -> list[str | SpeechCommand]:
544+
"""Outputs a MathML string as speech.
545+
546+
:param mathml: A MathML string.
547+
:returns: A list of speech commands and strings representing the given MathML.
548+
"""
505549
global _synthesizerRate
506550
synth: SynthDriver = getSynth()
507551
synthConfig = config.conf["speech"][synth.name]
@@ -557,13 +601,22 @@ def getSpeechForMathMl(
557601
getSynth()._set_rate(_synthesizerRate)
558602

559603
def _addSounds(self) -> bool:
604+
"""Queries the user preferences to determine whether or not sounds should be added.
605+
606+
:returns: True if MathCAT's `SpeechSound` preference is set, and False otherwise.
607+
"""
560608
try:
561609
return libmathcat.GetPreference("SpeechSound") != "None"
562610
except Exception as e:
563611
log.exception(f"MathCAT: An exception occurred in _add_sounds: {e}")
564612
return False
565613

566614
def getBrailleForMathMl(self, mathml: str) -> str:
615+
"""Gets the braille representation of a given MathML string by calling MathCAT's GetBraille function.
616+
617+
:param mathml: A MathML string.
618+
:returns: A braille string representing the input MathML.
619+
"""
567620
# log.info("***MathCAT getBrailleForMathMl")
568621
try:
569622
libmathcat.SetMathML(mathml)
@@ -582,6 +635,10 @@ def getBrailleForMathMl(self, mathml: str) -> str:
582635
return ""
583636

584637
def interactWithMathMl(self, mathml: str) -> None:
638+
"""Interact with a MathML string, creating a MathCATInteraction object.
639+
640+
:param mathml: The MathML representing the math to interact with.
641+
"""
585642
MathCATInteraction(provider=self, mathMl=mathml).setFocus()
586643
MathCATInteraction(provider=self, mathMl=mathml).script_navigate(None)
587644

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

591648

592649
def _monkeyPatchESpeak() -> None:
650+
"""Patches an eSpeak bug where the voice slows down."""
593651
global CACHED_SYNTH
594652
currentSynth: SynthDriver = getSynth()
595653
if currentSynth.name != "espeak" or CACHED_SYNTH == currentSynth:

0 commit comments

Comments
 (0)