-
Notifications
You must be signed in to change notification settings - Fork 279
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move
get_emoji_unicode_dict()
,get_aliases_unicode_dict()
to testu…
…tils.py
- Loading branch information
Showing
4 changed files
with
62 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,33 @@ | ||
from typing import Any, Dict, Optional | ||
from typing import Optional | ||
from functools import lru_cache | ||
from emoji.unicode_codes.data_dict import * | ||
from emoji.unicode_codes.data_dict import EMOJI_DATA, STATUS, LANGUAGES | ||
|
||
__all__ = [ | ||
'get_emoji_by_name', 'get_emoji_unicode_dict', 'get_aliases_unicode_dict', | ||
'get_emoji_by_name', | ||
'EMOJI_DATA', 'STATUS', 'LANGUAGES' | ||
] | ||
|
||
|
||
_EMOJI_UNICODE: Dict[str, Any] = {lang: None for lang in LANGUAGES} # Cache for the language dicts | ||
|
||
_ALIASES_UNICODE: Dict[str, str] = {} # Cache for the aliases dict | ||
|
||
|
||
@lru_cache(maxsize=4000) | ||
def get_emoji_by_name(name: str, lang: str) -> Optional[str]: | ||
"""Find emoji in a specific language or return None if not found""" | ||
def get_emoji_by_name(name: str, language: str) -> Optional[str]: | ||
""" | ||
Find emoji by short-name in a specific language. | ||
Returns None if not found | ||
:param name: emoji short code e.g. ":banana:" | ||
:param language: language-code e.g. 'es', 'de', etc. or 'alias' | ||
""" | ||
|
||
fully_qualified = STATUS['fully_qualified'] | ||
|
||
if lang == 'alias': | ||
if language == 'alias': | ||
for emj, data in EMOJI_DATA.items(): | ||
if name in data.get('alias', []) and data['status'] <= fully_qualified: | ||
return emj | ||
lang = 'en' | ||
language = 'en' | ||
|
||
for emj, data in EMOJI_DATA.items(): | ||
if data.get(lang) == name and data['status'] <= fully_qualified: | ||
if data.get(language) == name and data['status'] <= fully_qualified: | ||
return emj | ||
|
||
return None | ||
|
||
|
||
def get_emoji_unicode_dict(lang: str) -> Dict[str, Any]: | ||
"""Generate dict containing all fully-qualified and component emoji name for a language | ||
The dict is only generated once per language and then cached in _EMOJI_UNICODE[lang]""" | ||
|
||
if _EMOJI_UNICODE[lang] is None: | ||
_EMOJI_UNICODE[lang] = {data[lang]: emj for emj, data in EMOJI_DATA.items() | ||
if lang in data and data['status'] <= STATUS['fully_qualified']} | ||
|
||
return _EMOJI_UNICODE[lang] | ||
|
||
|
||
def get_aliases_unicode_dict() -> Dict[str, str]: | ||
"""Generate dict containing all fully-qualified and component aliases | ||
The dict is only generated once and then cached in _ALIASES_UNICODE""" | ||
|
||
if not _ALIASES_UNICODE: | ||
_ALIASES_UNICODE.update(get_emoji_unicode_dict('en')) | ||
for emj, data in EMOJI_DATA.items(): | ||
if 'alias' in data and data['status'] <= STATUS['fully_qualified']: | ||
for alias in data['alias']: | ||
_ALIASES_UNICODE[alias] = emj | ||
|
||
return _ALIASES_UNICODE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42c27f5
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm saddened by this change, as I was relying on emoji.unicode_codes.get_aliases_unicode_dict() in my code. Please make it accessible again.
42c27f5
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can just copy the code for this function into your program if you really need it:
emoji/utils/testutils.py
Lines 29 to 51 in e83cf4a