Skip to content

Commit 97d8d30

Browse files
authored
Merge 66e6b62 into ba74374
2 parents ba74374 + 66e6b62 commit 97d8d30

1 file changed

Lines changed: 32 additions & 28 deletions

File tree

source/characterProcessing.py

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# A part of NonVisual Desktop Access (NVDA)
2-
# Copyright (C) 2010-2021 NV Access Limited, World Light Information Limited,
2+
# Copyright (C) 2010-2022 NV Access Limited, World Light Information Limited,
33
# Hong Kong Blind Union, Babbage B.V., Julien Cochuyt
44
# This file is covered by the GNU General Public License.
55
# See the file COPYING for more details.
@@ -9,29 +9,35 @@
99
import codecs
1010
import collections
1111
import re
12+
from typing import Callable, Dict, Generic, List, Optional, Tuple, TypeVar
1213
from logHandler import log
1314
import globalVars
1415
import config
1516

16-
class LocaleDataMap(object):
17+
18+
_LocaleDataT = TypeVar("_LocaleDataT")
19+
20+
21+
class LocaleDataMap(Generic[_LocaleDataT], object):
1722
"""Allows access to locale-specific data objects, dynamically loading them if needed on request"""
1823

19-
def __init__(self,localeDataFactory):
24+
def __init__(
25+
self,
26+
localeDataFactory: Callable[[str], Generic[_LocaleDataT]]
27+
):
2028
"""
2129
@param localeDataFactory: the factory to create data objects for the requested locale.
2230
"""
23-
self._localeDataFactory=localeDataFactory
24-
self._dataMap={}
31+
self._localeDataFactory: Callable[[str], _LocaleDataT] = localeDataFactory
32+
self._dataMap: Dict[str, _LocaleDataT] = {}
2533

26-
def fetchLocaleData(self,locale,fallback=True):
34+
def fetchLocaleData(self, locale: str, fallback: bool = True) -> _LocaleDataT:
2735
"""
2836
Fetches a data object for the given locale.
2937
This may mean that the data object is first created and stored if it does not yet exist in the map.
3038
The locale is also simplified (country is dropped) if the fallback argument is True and the full locale can not be used to create a data object.
3139
@param locale: the locale of the data object requested
32-
@type locale: string
3340
@param fallback: if true and there is no data for the locale, then the country (if it exists) is stripped and just the language is tried.
34-
@type fallback: boolean
3541
@return: the data object for the given locale
3642
"""
3743
localeList=[locale]
@@ -49,11 +55,10 @@ def fetchLocaleData(self,locale,fallback=True):
4955
return data
5056
raise LookupError(locale)
5157

52-
def invalidateLocaleData(self, locale):
58+
def invalidateLocaleData(self, locale: str) -> None:
5359
"""Invalidate the data object (if any) for the given locale.
5460
This will cause a new data object to be created when this locale is next requested.
5561
@param locale: The locale for which the data object should be invalidated.
56-
@type locale: str
5762
"""
5863
try:
5964
del self._dataMap[locale]
@@ -72,12 +77,11 @@ class CharacterDescriptions(object):
7277
The data is loaded from a file from the requested locale.
7378
"""
7479

75-
def __init__(self,locale):
80+
def __init__(self, locale: str):
7681
"""
7782
@param locale: The characterDescriptions.dic file will be found by using this locale.
78-
@type locale: string
7983
"""
80-
self._entries = {}
84+
self._entries: Dict[str, List[str]] = {}
8185
fileName = os.path.join(globalVars.appDir, 'locale', locale, 'characterDescriptions.dic')
8286
if not os.path.isfile(fileName):
8387
raise LookupError(fileName)
@@ -95,23 +99,22 @@ def __init__(self,locale):
9599
log.debug("Loaded %d entries." % len(self._entries))
96100
f.close()
97101

98-
def getCharacterDescription(self, character):
102+
def getCharacterDescription(self, character: str) -> Optional[List[str]]:
99103
"""
100104
Looks up the given character and returns a list containing all the description strings found.
101105
"""
102106
return self._entries.get(character)
103107

104-
_charDescLocaleDataMap=LocaleDataMap(CharacterDescriptions)
105108

106-
def getCharacterDescription(locale,character):
109+
_charDescLocaleDataMap: LocaleDataMap[CharacterDescriptions] = LocaleDataMap(CharacterDescriptions)
110+
111+
112+
def getCharacterDescription(locale: str, character: str) -> Optional[List[str]]:
107113
"""
108-
Finds a description or examples for the given character, which makes sence in the given locale.
114+
Finds a description or examples for the given character, which makes sense in the given locale.
109115
@param locale: the locale (language[_COUNTRY]) the description should be for.
110-
@type locale: string
111-
@param character: the character who's description should be retreaved.
112-
@type character: string
113-
@return: the found description for the given character
114-
@rtype: list of strings
116+
@param character: the character to fetch the description for.
117+
@return: the found description for the given character
115118
"""
116119
try:
117120
l=_charDescLocaleDataMap.fetchLocaleData(locale)
@@ -363,7 +366,9 @@ def _saveSymbol(self, symbol):
363366
return u"\t".join(fields)
364367

365368
_noSymbolLocalesCache = set()
366-
def _getSpeechSymbolsForLocale(locale):
369+
370+
371+
def _getSpeechSymbolsForLocale(locale: str) -> Tuple[SpeechSymbols, SpeechSymbols]:
367372
if locale in _noSymbolLocalesCache:
368373
raise LookupError
369374
builtin = SpeechSymbols()
@@ -401,7 +406,7 @@ class SpeechSymbolProcessor(object):
401406
"""
402407

403408
#: Caches symbol data for locales.
404-
localeSymbols = LocaleDataMap(_getSpeechSymbolsForLocale)
409+
localeSymbols: LocaleDataMap[Tuple[SpeechSymbols, SpeechSymbols]] = LocaleDataMap(_getSpeechSymbolsForLocale)
405410

406411
def __init__(self, locale):
407412
"""Constructor.
@@ -653,17 +658,16 @@ def deleteSymbol(self, symbol):
653658
except KeyError:
654659
pass
655660

656-
def isBuiltin(self, symbolIdentifier):
661+
def isBuiltin(self, symbolIdentifier: str) -> bool:
657662
"""Determine whether a symbol is built in.
658663
@param symbolIdentifier: The identifier of the symbol in question.
659-
@type symbolIdentifier: str
660664
@return: C{True} if the symbol is built in,
661665
C{False} if it was added by the user.
662-
@rtype: bool
663666
"""
664667
return any(symbolIdentifier in source.symbols for source in self.builtinSources)
665668

666-
_localeSpeechSymbolProcessors = LocaleDataMap(SpeechSymbolProcessor)
669+
670+
_localeSpeechSymbolProcessors: LocaleDataMap[SpeechSymbolProcessor] = LocaleDataMap(SpeechSymbolProcessor)
667671

668672

669673
def processSpeechSymbols(locale: str, text: str, level: SymbolLevel):

0 commit comments

Comments
 (0)