コード例 #1
0
ファイル: utils.py プロジェクト: modulexcite/elizabeth
def pull(file, locale='en'):
    """
    Open file and get content from file. Memorize result using lru_cache.

    pull - is internal function, please do not use this function outside
    the module 'elizabeth'.

    +------------------------------+--------------+
    | Locale Code                  | Folder       |
    +==============================+==============+
    | da - Danish                  | (data/da)    |
    +------------------------------+--------------+
    | de - German                  | (data/de)    |
    +------------------------------+--------------+
    | en - English                 | (data/en)    |
    +------------------------------+--------------+
    | ru - Russian                 | (data/ru)    |
    +------------------------------+--------------+
    | fa - Farsi                   | (data/fa)    |
    +------------------------------+--------------+
    | fi - Finnish                 | (data/fi)    |
    +------------------------------+--------------+
    | fr - French                  | (data/fr)    |
    +------------------------------+--------------+
    | es - Spanish                 | (data/es)    |
    +------------------------------+--------------+
    | it - Italian                 | (data/it)    |
    +------------------------------+--------------+
    | is - Icelandic               | (data/is)    |
    +------------------------------+--------------+
    | pl - Polish                  | (data/pl)    |
    +------------------------------+--------------+
    | pt - Portuguese              | (data/pt)    |
    +------------------------------+--------------+
    | nl - Dutch                   | (data/nl)    |
    +------------------------------+--------------+
    | no - Norwegian               | (data/no)    |
    +------------------------------+--------------+
    | pt-br - Brazilian Portuguese | (data/pt-br) |
    +------------------------------+--------------+
    | sv - Swedish                 | (data/sv)    |
    +------------------------------+--------------+

    :param file: The name of file.
    :param locale: Locale.
    :returns: The content of the file.
    """

    locale = locale.lower()

    if locale not in SUPPORTED_LOCALES:
        raise UnsupportedLocale("Locale %s does not supported" % locale)

    # Needs explicit encoding for Windows
    with open(join(PATH + '/' + locale, file), 'r', encoding='utf8') as f:
        data = json.load(f)

    return data
コード例 #2
0
ファイル: decorators.py プロジェクト: uvegla/elizabeth
 def wrapper(*args, **kwargs):
     try:
         alphabet = ROMANIZATION_ALPHABETS[locale]
     except KeyError:
         raise UnsupportedLocale(
             'Locale {0} is not supported yet.'.format(locale))
     result = func(*args, **kwargs)
     txt = ''.join([alphabet[i] for i in result if i in alphabet])
     return txt
コード例 #3
0
def locale_information(locale: str) -> str:
    """Return name (in english) or local name of the locale

    :param locale: Locale abbreviation.
    :type locale: str
    :returns: Locale name.
    :rtype: str
    :Example:

    >>> from elizabeth.utils import locale_information
    >>> locale_information('sv')
    'Swedish'
    """
    locale = locale.lower()

    if locale not in SUPPORTED_LOCALES:
        raise UnsupportedLocale("Locale %s does not supported" % locale)

    return SUPPORTED_LOCALES[locale]['name']
コード例 #4
0
ファイル: utils.py プロジェクト: czfmuyu/elizabeth
def pull(file, locale='en') -> dict:
    """Open json file file and get content from file and memorize result using lru_cache.

    .. note:: pull - is internal function, please do not use this function outside
    the module 'elizabeth'.

    :param file: The name of file.
    :param locale: Locale.
    :returns: The content of the file.

    :Example:

        >>> from elizabeth.utils import pull
        >>> en = pull(file='datetime.json', locale='en')
        >>> isinstance(en, dict)
        True
        >>> en['day']['abbr'][0]
        'Mon.'
    """
    def get_data(locale_name):
        """
        Pull JSON data from file.
        :param locale_name: Name of locale to pull.
        :return: Dict of data from file
        """
        file_path = path.join(PATH + '/' + locale_name, file)
        # Needs explicit encoding for Windows
        with open(file_path, 'r', encoding='utf8') as f:
            return json.load(f)

    locale = locale.lower()

    if locale not in SUPPORTED_LOCALES:
        raise UnsupportedLocale("Locale %s is not supported" % locale)

    master_locale = locale.split("-")[0]
    data = get_data(master_locale)

    # Handle sub-locales
    if "-" in locale:
        data = update_dict(data, get_data(locale))

    return data
コード例 #5
0
def _push(**kwargs):
    """
    Dict to json file.
    :param kwargs: Kwargs
    :return: None
    """
    skeleton = {
        'key': {
            'key': 'value',
        }
    }

    locale = kwargs.get('locale', 'en')
    file = kwargs.get('file', 'test.json')
    data = kwargs.get('data', skeleton)

    if locale not in SUPPORTED_LOCALES:
        raise UnsupportedLocale("Locale %s does not supported" % locale)

    with open(join(PATH + '/' + locale, file), 'w') as f:
        json.dump(data, f)
コード例 #6
0
def locale_information(locale, local=False):
    """
    Return name (in english) or local name of the locale
    :param locale: locale abbreviation
    :param local: if True then return local name of the locale
    :type locale: str
    :returns: locale name
    :rtype: str
    :Example:

    >>> from elizabeth.utils import locale_information
    >>> locale_information('sv')
    Swedish
    >>> locale_information('sv', local=True)
    Svenska
    """
    locale = locale.lower()

    if locale not in SUPPORTED_LOCALES:
        raise UnsupportedLocale("Locale %s does not supported" % locale)

    return SUPPORTED_LOCALES[locale]
コード例 #7
0
def pull(file, locale='en') -> dict:
    """Open file and get content from file. Memorize result using lru_cache.

    pull - is internal function, please do not use this function outside
    the module 'elizabeth'.

    +------------------------------+--------------+
    | Locale Code                  | Folder       |
    +==============================+==============+
    | cs - Czech                   | (data/cs)    |
    +------------------------------+--------------+
    | da - Danish                  | (data/da)    |
    +------------------------------+--------------+
    | de - German                  | (data/de)    |
    +------------------------------+--------------+
    | de-at - Austrian german      | (data/de-at) |
    +------------------------------+--------------+
    | en - English                 | (data/en)    |
    +------------------------------+--------------+
    | en-au - Australian English   | (data/en-au) |
    +------------------------------+--------------+
    | en-gb - British English      | (data/en-gb) |
    +------------------------------+--------------+
    | ru - Russian                 | (data/ru)    |
    +------------------------------+--------------+
    | fa - Farsi                   | (data/fa)    |
    +------------------------------+--------------+
    | fi - Finnish                 | (data/fi)    |
    +------------------------------+--------------+
    | fr - French                  | (data/fr)    |
    +------------------------------+--------------+
    | es - Spanish                 | (data/es)    |
    +------------------------------+--------------+
    | hu - Hungarian               | (data/hu)    |
    +------------------------------+--------------+
    | it - Italian                 | (data/it)    |
    +------------------------------+--------------+
    | is - Icelandic               | (data/is)    |
    +------------------------------+--------------+
    | jp - Japanese                | (data/jp)    |
    +------------------------------+--------------+
    | ko - Korean                  | (data/ko)    |
    +------------------------------+--------------+
    | pl - Polish                  | (data/pl)    |
    +------------------------------+--------------+
    | pt - Portuguese              | (data/pt)    |
    +------------------------------+--------------+
    | nl - Dutch                   | (data/nl)    |
    +------------------------------+--------------+
    | no - Norwegian               | (data/no)    |
    +------------------------------+--------------+
    | pt-br - Brazilian Portuguese | (data/pt-br) |
    +------------------------------+--------------+
    | sv - Swedish                 | (data/sv)    |
    +------------------------------+--------------+
    | tr - Turkish                 | (data/tr)    |
    +------------------------------+--------------+

    :param file: The name of file.
    :param locale: Locale.
    :returns: The content of the file.

    :Example:

        >>> from elizabeth.utils import pull
        >>> en = pull(file='datetime.json', locale='en')
        >>> isinstance(en, dict)
        True
        >>> en['day']['abbr'][0]
        'Mon.'
    """

    locale = locale.lower()

    if locale not in SUPPORTED_LOCALES:
        raise UnsupportedLocale("Locale %s does not supported" % locale)

    file_path = path.join(PATH + '/' + locale, file)

    # Needs explicit encoding for Windows
    with open(file_path, 'r', encoding='utf8') as f:
        data = json.load(f)

    return data