Skip to content

Commit

Permalink
add list_common_keyboards to public api
Browse files Browse the repository at this point in the history
  • Loading branch information
sdp5 committed Aug 18, 2020
1 parent 48d9fd4 commit 0d234b2
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
2020-08-17 Sundeep ANAND <suanand@redhat.com>

* add list_common_keyboards() to public api

2020-01-14 Mike FABIAN <mfabian@redhat.com>

* Parse stuff in glibc locale names after @ which is not a script as a variant
Expand Down
8 changes: 8 additions & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,19 @@ import langtable

Functions in the public API:

parse_locale()
list_locales()
list_keyboards()
list_common_keyboards()
list_consolefonts()
list_inputmethods()
list_timezones()
list_scripts()
language_name()
territory_name()
timezone_name()
languageId()
territoryId()
supports_ascii()

Some examples to show the usage are found in the documentation
Expand Down
65 changes: 65 additions & 0 deletions langtable/langtable.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
# parse_locale()
# list_locales()
# list_keyboards()
# list_common_keyboards()
# list_consolefonts()
# list_inputmethods()
# list_timezones()
Expand Down Expand Up @@ -112,6 +113,10 @@
# will be replaced by “make install”:
_DATADIR = '/usr/share/langtable'

# Rank threshold to qualify a
# keyboard layout as prevalent
_KEYBOARD_LAYOUT_RANK_THRESHOLD = 500

# For the ICU/CLDR locale pattern see: http://userguide.icu-project.org/locale
# (We ignore the variant code here)
_cldr_locale_pattern = re.compile(
Expand Down Expand Up @@ -2060,6 +2065,66 @@ def list_keyboards(concise=True, show_weights=False, languageId = None, scriptId
else:
return _ranked_list_to_list(ranked_list)

def list_common_keyboards(languageId = None, scriptId = None, territoryId = None):
'''Returns highest ranked keyboard layout(s)
:param languageId: identifier for the language
:type languageId: string
:param scriptId: identifier for the script
:type scriptId: string
:param territoryId: identifier for the territory
:type territoryId: string
:return: list of keyboard layouts
:rtype: list of str(s)
**Examples:**
>>> list_common_keyboards()
['af(ps)', 'al', 'am', 'ara', 'az', 'ba', 'be(oss)', 'bg', 'br', 'bt', 'by', 'ca(eng)', 'ca(ike)', 'ch', 'cn', 'cn(ug)', 'cz', 'de(nodeadkeys)', 'dk', 'ee', 'es', 'es(ast)', 'es(cat)', 'et', 'fi', 'fo', 'fr(bre)', 'fr(oss)', 'gb', 'ge', 'gr', 'hr', 'hu', 'ie(CloGaelach)', 'il', 'in(eng)', 'ir', 'is', 'it', 'jp', 'ke', 'kg', 'kh', 'kr', 'kz', 'la', 'latam', 'lt', 'lv', 'ma(tifinagh)', 'mk', 'mm', 'mn', 'mt', 'mv', 'ng(hausa)', 'ng(igbo)', 'ng(yoruba)', 'no', 'np', 'ph', 'pk', 'pl', 'ro', 'rs', 'rs(latin)', 'ru', 'ru(bak)', 'ru(chm)', 'ru(cv)', 'ru(kom)', 'ru(os_winkeys)', 'ru(sah)', 'ru(tt)', 'ru(udm)', 'ru(xal)', 'se', 'si', 'sk', 'sn', 'syc', 'th', 'tj', 'tm', 'tr', 'tr(crh)', 'tr(ku)', 'tw', 'ua', 'us', 'us(altgr-intl)', 'us(euro)', 'us(intl)', 'uz', 'vn', 'za']
>>> list_common_keyboards(languageId='fr')
['fr(oss)']
>>> list_common_keyboards(territoryId='CA')
['ca(eng)']
>>> list_common_keyboards(territoryId='FR')
['fr(oss)']
>>> list_common_keyboards(languageId='fr', territoryId='CA')
['ca']
>>> list_common_keyboards(languageId='de', territoryId='FR')
['fr(oss)']
>>> list_common_keyboards(languageId='sr', scriptId='Latn')
['rs(latin)']
>>> list_common_keyboards(languageId='zh', scriptId='Hans')
['cn']
>>> list_common_keyboards(languageId='zh', scriptId='Hans', territoryId='TW')
['tw']
'''
high_ranked_keyboards = list()
if not languageId and not scriptId and not territoryId:
for _, language in _languages_db.items():
keyboard_layouts = language.keyboards
selected_layouts = [layout for layout, rank in keyboard_layouts.items()
if rank >= _KEYBOARD_LAYOUT_RANK_THRESHOLD]
if selected_layouts:
high_ranked_keyboards.extend(selected_layouts)
high_ranked_keyboards = list(set(high_ranked_keyboards))

kwargs = dict()
locale = _parse_and_split_languageId(
languageId=languageId, scriptId=scriptId, territoryId=territoryId
)
if locale.language:
kwargs.update(dict(languageId=locale.language))
if locale.script:
kwargs.update(dict(scriptId=locale.script))
if locale.territory:
kwargs.update(dict(territoryId=locale.territory))
common_layouts = list_keyboards(**kwargs)
if common_layouts:
# Picking up first layout from the list
high_ranked_keyboards.append(common_layouts[0])

return sorted(high_ranked_keyboards)

def list_consolefonts(concise=True, show_weights=False, languageId = None, scriptId = None, territoryId = None):
u'''List likely Linux Console fonts
Expand Down
19 changes: 19 additions & 0 deletions test_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def dummy():
>>> from langtable import _test_cldr_locale_pattern
>>> from langtable import supports_ascii
>>> from langtable import languageId
>>> from langtable import list_common_keyboards
######################################################################
# Start of tests to reproduce the results from mangleLocale(inLocale) in anaconda, see:
Expand Down Expand Up @@ -2071,6 +2072,24 @@ def dummy():
>>> print(langtable.timezone_name(timezoneId='Pacific/Pago_Pago', languageIdQuery='ast')) # doctest: +NORMALIZE_WHITESPACE
Océanu Pacíficu/Pago Pago
>>> print(list_common_keyboards()) # doctest: +NORMALIZE_WHITESPACE
['af(ps)', 'al', 'am', 'ara', 'az', 'ba', 'be(oss)', 'bg', 'br', 'bt', 'by', 'ca(eng)', 'ca(ike)', 'ch', 'cn', 'cn(ug)', 'cz', 'de(nodeadkeys)', 'dk', 'ee', 'es', 'es(ast)', 'es(cat)', 'et', 'fi', 'fo', 'fr(bre)', 'fr(oss)', 'gb', 'ge', 'gr', 'hr', 'hu', 'ie(CloGaelach)', 'il', 'in(eng)', 'ir', 'is', 'it', 'jp', 'ke', 'kg', 'kh', 'kr', 'kz', 'la', 'latam', 'lt', 'lv', 'ma(tifinagh)', 'mk', 'mm', 'mn', 'mt', 'mv', 'ng(hausa)', 'ng(igbo)', 'ng(yoruba)', 'no', 'np', 'ph', 'pk', 'pl', 'ro', 'rs', 'rs(latin)', 'ru', 'ru(bak)', 'ru(chm)', 'ru(cv)', 'ru(kom)', 'ru(os_winkeys)', 'ru(sah)', 'ru(tt)', 'ru(udm)', 'ru(xal)', 'se', 'si', 'sk', 'sn', 'syc', 'th', 'tj', 'tm', 'tr', 'tr(crh)', 'tr(ku)', 'tw', 'ua', 'us', 'us(altgr-intl)', 'us(euro)', 'us(intl)', 'uz', 'vn', 'za']
>>> print(list_common_keyboards(languageId='fr')) # doctest: +NORMALIZE_WHITESPACE
['fr(oss)']
>>> print(list_common_keyboards(territoryId='CA')) # doctest: +NORMALIZE_WHITESPACE
['ca(eng)']
>>> print(list_common_keyboards(territoryId='FR')) # doctest: +NORMALIZE_WHITESPACE
['fr(oss)']
>>> print(list_common_keyboards(languageId='fr', territoryId='CA')) # doctest: +NORMALIZE_WHITESPACE
['ca']
>>> print(list_common_keyboards(languageId='de', territoryId='FR')) # doctest: +NORMALIZE_WHITESPACE
['fr(oss)']
>>> print(list_common_keyboards(languageId='sr', scriptId='Latn')) # doctest: +NORMALIZE_WHITESPACE
['rs(latin)']
>>> print(list_common_keyboards(languageId='zh', scriptId='Hans')) # doctest: +NORMALIZE_WHITESPACE
['cn']
>>> print(list_common_keyboards(languageId='zh', scriptId='Hans', territoryId='TW')) # doctest: +NORMALIZE_WHITESPACE
['tw']
'''

if __name__ == "__main__":
Expand Down

0 comments on commit 0d234b2

Please sign in to comment.