Example #1
0
def to_language(locale):
    """Turns a locale name (en_US) into a language name (en-us)."""
    p = locale.find('_')
    if p >= 0:
        return locale[:p].lower()+'-'+locale[p+1:].lower()
    else:
        return locale.lower()
Example #2
0
def parse(filename):

    f = open(filename)
    lines = f.read().splitlines()
    data = {}
    for line in lines:
        line = line.strip()
        if not line:
            continue
        if line[:1] == '#':
            continue
        locale, alias = line.split()
        # Strip ':'
        if locale[-1] == ':':
            locale = locale[:-1]
        # Lower-case locale
        locale = locale.lower()
        # Ignore one letter locale mappings (except for 'c')
        if len(locale) == 1 and locale != 'c':
            continue
        # Normalize encoding, if given
        if '.' in locale:
            lang, encoding = locale.split('.')[:2]
            encoding = encoding.replace('-', '')
            encoding = encoding.replace('_', '')
            locale = lang + '.' + encoding
            if encoding.lower() == 'utf8':
                # Ignore UTF-8 mappings - this encoding should be
                # available for all locales
                continue
        data[locale] = alias
    return data
Example #3
0
def _find_usable_locale() -> str:
    try:
        locales = subprocess.Popen(
            ['locale', '-a'], stdout=subprocess.PIPE,
            stderr=subprocess.PIPE).communicate()[0].decode(
                'ascii', 'replace')
    except OSError:
        locales = ''

    usable_locales: List[str] = []
    for line in locales.splitlines():
        locale = line.strip()
        locale_name = locale.lower().replace('-', '')

        # C.UTF-8 is the best option, if supported
        if locale_name == 'c.utf8':
            return locale

        if locale_name.endswith('.utf8'):
            # Make a preference of english locales
            if locale.startswith('en_'):
                usable_locales.insert(0, locale)
            else:
                usable_locales.append(locale)

    if not usable_locales:
        raise FatalError(
            'Support for Unicode filenames is required, but no suitable UTF-8 locale was found on your system.'
            ' Please refer to the manual for your operating system for details on locale reconfiguration.'
        )

    return usable_locales[0]
Example #4
0
def parse(filename):

    f = open(filename)
    lines = f.read().splitlines()
    data = {}
    for line in lines:
        line = line.strip()
        if not line:
            continue
        if line[:1] == "#":
            continue
        locale, alias = line.split()
        # Strip ':'
        if locale[-1] == ":":
            locale = locale[:-1]
        # Lower-case locale
        locale = locale.lower()
        # Ignore one letter locale mappings (except for 'c')
        if len(locale) == 1 and locale != "c":
            continue
        # Normalize encoding, if given
        if "." in locale:
            lang, encoding = locale.split(".")[:2]
            encoding = encoding.replace("-", "")
            encoding = encoding.replace("_", "")
            locale = lang + "." + encoding
            if encoding.lower() == "utf8":
                # Ignore UTF-8 mappings - this encoding should be
                # available for all locales
                continue
        data[locale] = alias
    return data
Example #5
0
def parse(filename):

    with open(filename, encoding='latin1') as f:
        lines = list(f)
    data = {}
    for line in lines:
        line = line.strip()
        if not line:
            continue
        if line[:1] == '#':
            continue
        locale, alias = line.split()
        # Fix non-standard locale names, e.g. [email protected]
        if '@' in alias:
            alias_lang, _, alias_mod = alias.partition('@')
            if '.' in alias_mod:
                alias_mod, _, alias_enc = alias_mod.partition('.')
                alias = alias_lang + '.' + alias_enc + '@' + alias_mod
        # Strip ':'
        if locale[-1] == ':':
            locale = locale[:-1]
        # Lower-case locale
        locale = locale.lower()
        # Ignore one letter locale mappings (except for 'c')
        if len(locale) == 1 and locale != 'c':
            continue
        # Normalize encoding, if given
        if '.' in locale:
            lang, encoding = locale.split('.')[:2]
            encoding = encoding.replace('-', '')
            encoding = encoding.replace('_', '')
            locale = lang + '.' + encoding
        data[locale] = alias
    return data
Example #6
0
def to_language(locale):
    """Turns a locale name (en_US) into a language name (en-us)."""
    p = locale.find('_')
    if p >= 0:
        return locale[:p].lower() + '-' + locale[p + 1:].lower()
    else:
        return locale.lower()
Example #7
0
def parse(filename):

    f = open(filename)
    lines = f.read().splitlines()
    data = {}
    for line in lines:
        line = line.strip()
        if not line:
            continue
        if line[:1] == '#':
            continue
        locale, alias = line.split()
        # Strip ':'
        if locale[-1] == ':':
            locale = locale[:-1]
        # Lower-case locale
        locale = locale.lower()
        # Ignore one letter locale mappings (except for 'c')
        if len(locale) == 1 and locale != 'c':
            continue
        # Normalize encoding, if given
        if '.' in locale:
            lang, encoding = locale.split('.')[:2]
            encoding = encoding.replace('-', '')
            encoding = encoding.replace('_', '')
            locale = lang + '.' + encoding
            if encoding.lower() == 'utf8':
                # Ignore UTF-8 mappings - this encoding should be
                # available for all locales
                continue
        data[locale] = alias
    return data
Example #8
0
def _find_usable_locale():
    try:
        locales = subprocess.Popen(["locale", "-a"],
                                   stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE).communicate()[0]
    except OSError:
        locales = ""
    if isinstance(locales, bytes):
        locales = locales.decode("ascii", "replace")

    usable_locales = []
    for line in locales.splitlines():
        locale = line.strip()
        locale_name = locale.lower().replace("-", "")

        # C.UTF-8 is the best option, if supported
        if locale_name == "c.utf8":
            return locale

        if locale_name.endswith(".utf8"):
            # Make a preference of english locales
            if locale.startswith("en_"):
                usable_locales.insert(0, locale)
            else:
                usable_locales.append(locale)

    if not usable_locales:
        raise FatalError(
            "Support for Unicode filenames is required, but no suitable UTF-8 locale was found on your system."
            " Please refer to the manual for your operating system for details on locale reconfiguration."
        )

    return usable_locales[0]
Example #9
0
def to_language(locale):
    "Turns a locale name (en_US) into a language name (en-us)."
    p = locale.find("_")
    if p >= 0:
        return locale[:p].lower() + "-" + locale[p + 1 :].lower()
    else:
        return locale.lower()
def parse(filename):

    with open(filename, encoding='latin1') as f:
        lines = list(f)
    data = {}
    for line in lines:
        line = line.strip()
        if not line:
            continue
        if line[:1] == '#':
            continue
        locale, alias = line.split()
        # Fix non-standard locale names, e.g. [email protected]
        if '@' in alias:
            alias_lang, _, alias_mod = alias.partition('@')
            if '.' in alias_mod:
                alias_mod, _, alias_enc = alias_mod.partition('.')
                alias = alias_lang + '.' + alias_enc + '@' + alias_mod
        # Strip ':'
        if locale[-1] == ':':
            locale = locale[:-1]
        # Lower-case locale
        locale = locale.lower()
        # Ignore one letter locale mappings (except for 'c')
        if len(locale) == 1 and locale != 'c':
            continue
        # Normalize encoding, if given
        if '.' in locale:
            lang, encoding = locale.split('.')[:2]
            encoding = encoding.replace('-', '')
            encoding = encoding.replace('_', '')
            locale = lang + '.' + encoding
        data[locale] = alias
    return data
def _verify_python_env():
    M = '.utf8'
    L = '.utf-8'
    J = None
    I = 'ascii'
    try:
        import locale as A
        G = codecs.lookup(A.getpreferredencoding()).name
    except Exception:
        G = I
    if G != I: return
    B = ''
    if os.name == 'posix':
        import subprocess as D
        try:
            C = D.Popen(['locale', '-a'], stdout=D.PIPE,
                        stderr=D.PIPE).communicate()[0]
        except OSError:
            C = b''
        E = set()
        H = False
        if isinstance(C, bytes): C = C.decode(I, 'replace')
        for K in C.splitlines():
            A = K.strip()
            if A.lower().endswith((L, M)):
                E.add(A)
                if A.lower() in ('c.utf8', 'c.utf-8'): H = True
        B += '\n\n'
        if not E:
            B += 'Additional information: on this system no suitable UTF-8 locales were discovered. This most likely requires resolving by reconfiguring the locale system.'
        elif H:
            B += 'This system supports the C.UTF-8 locale which is recommended. You might be able to resolve your issue by exporting the following environment variables:\n\n    export LC_ALL=C.UTF-8\n    export LANG=C.UTF-8'
        else:
            B += f"This system lists some UTF-8 supporting locales that you can pick from. The following suitable locales were discovered: {', '.join(sorted(E))}"
        F = J
        for A in (os.environ.get('LC_ALL'), os.environ.get('LANG')):
            if A and A.lower().endswith((L, M)): F = A
            if A is not J: break
        if F is not J:
            B += f"\n\nClick discovered that you exported a UTF-8 locale but the locale system could not pick up from it because it does not exist. The exported locale is {F!r} but it is not supported"
    raise RuntimeError(
        f"Click will abort further execution because Python was configured to use ASCII as encoding for the environment. Consult https://click.palletsprojects.com/unicode-support/ for mitigation steps.{B}"
    )
Example #12
0
def fontsByLocale(locale):
    result = []
    locale = locale.lower().replace("_", "-")

    # get all fonts
    libfc.FcFontList.restype = POINTER(FcFontSet)
    libfc.FcFontList.argtypes = [c_void_p] * 3
    libfc.FcLangSetGetLangs.restype = POINTER(FcStrSet)
    libfc.FcStrListNext.restype = c_char_p
    libfc.FcPatternFormat.restype = c_char_p

    fontSet = libfc.FcFontList(None, pattern, objectSet)
    fontCount = fontSet.contents.nfont

    # iterate on the fonts
    for i in range(fontCount):
        _pattern = fontSet.contents.fonts[i]

        # _family = c_char_p()
        # _familyLang = c_char_p()
        # langSet = POINTER(FcLangSet)()

        # libfc.FcPatternGetString(_pattern, "family", 0, byref(_family))
        # libfc.FcPatternGetString(_pattern, "familylang", 0, byref(_familyLang))
        # libfc.FcPatternGetLangSet(_pattern, "lang", 0, byref(langSet))

        # langStrSet = libfc.FcLangSetGetLangs(langSet)
        # langStrList = libfc.FcStrListCreate(langStrSet)
        # libfc.FcStrListFirst(langStrList)
        # _lang = libfc.FcStrListNext(langStrList)
        # while (_lang):
        #   if _lang.startswith(lang):
        #       print _family.value, _familyLang.value
        #       result.append(_family.value)
        #       break
        #   _lang = libfc.FcStrListNext(langStrList)
        # libfc.FcStrListDone(langStrList)

        family = libfc.FcPatternFormat(_pattern, c_char_p("%{family}"))
        familyLang = libfc.FcPatternFormat(_pattern, c_char_p("%{familylang}"))
        lang = libfc.FcPatternFormat(_pattern, c_char_p("%{lang}"))
        spacing = libfc.FcPatternFormat(_pattern, c_char_p("%{spacing}"))

        isMono = spacing == "100" or "mono" in family.lower()
        isCurrentLangSupport = filter(lambda x: x.startswith(locale),
                                      lang.split("|"))
        if isMono or isCurrentLangSupport:
            try:
                en, localized = _familyNameInfo(family, familyLang, locale)
                if not filter(lambda (x, y): x == en, result):
                    result.append([en, localized])
            except:
                pass

    return result
def fontsByLocale(locale):
	result = []
	locale = locale.lower().replace("_", "-")

	# get all fonts
	libfc.FcFontList.restype = POINTER(FcFontSet)
	libfc.FcLangSetGetLangs.restype = POINTER(FcStrSet)
	libfc.FcStrListNext.restype = c_char_p
	libfc.FcPatternFormat.restype = c_char_p

	fontSet = libfc.FcFontList(None, pattern, objectSet)
	fontCount = fontSet.contents.nfont

	# iterate on the fonts
	for i in range(fontCount):
		_pattern = fontSet.contents.fonts[i]

		# _family = c_char_p()
		# _familyLang = c_char_p()
		# langSet = POINTER(FcLangSet)()

		# libfc.FcPatternGetString(_pattern, "family", 0, byref(_family))
		# libfc.FcPatternGetString(_pattern, "familylang", 0, byref(_familyLang))
		# libfc.FcPatternGetLangSet(_pattern, "lang", 0, byref(langSet))

		# langStrSet = libfc.FcLangSetGetLangs(langSet)
		# langStrList = libfc.FcStrListCreate(langStrSet)
		# libfc.FcStrListFirst(langStrList)
		# _lang = libfc.FcStrListNext(langStrList)
		# while (_lang):
		# 	if _lang.startswith(lang):
		# 		print _family.value, _familyLang.value
		# 		result.append(_family.value)
		# 		break
		# 	_lang = libfc.FcStrListNext(langStrList)
		# libfc.FcStrListDone(langStrList)

		family = libfc.FcPatternFormat(_pattern, c_char_p("%{family}"))
		familyLang = libfc.FcPatternFormat(_pattern, c_char_p("%{familylang}"))
		lang = libfc.FcPatternFormat(_pattern, c_char_p("%{lang}"))
		spacing = libfc.FcPatternFormat(_pattern, c_char_p("%{spacing}"))

		isMono = spacing == "100" or "mono" in family.lower()
		isCurrentLangSupport = filter(lambda x: x.startswith(locale), lang.split("|"))
		if isMono or isCurrentLangSupport:
			try:
				en, localized = _familyNameInfo(family, familyLang, locale)
				if not filter(lambda (x, y): x == en, result):
					result.append([en, localized])
			except:
				pass

	return result
Example #14
0
def parse(filename):

    f = open(filename)
    lines = f.read().splitlines()
    data = {}
    for line in lines:
        line = line.strip()
        if not line:
            continue
        if line[:1] == '#':
            continue
        locale, alias = line.split()
        # Fix non-standard locale names, e.g. [email protected]
        if '@' in alias:
            alias_lang, _, alias_mod = alias.partition('@')
            if '.' in alias_mod:
                alias_mod, _, alias_enc = alias_mod.partition('.')
                alias = alias_lang + '.' + alias_enc + '@' + alias_mod
        # Strip ':'
        if locale[-1] == ':':
            locale = locale[:-1]
        # Lower-case locale
        locale = locale.lower()
        # Ignore one letter locale mappings (except for 'c')
        if len(locale) == 1 and locale != 'c':
            continue
        # Normalize encoding, if given
        if '.' in locale:
            lang, encoding = locale.split('.')[:2]
            encoding = encoding.replace('-', '')
            encoding = encoding.replace('_', '')
            locale = lang + '.' + encoding
            if encoding.lower() == 'utf8':
                # Ignore UTF-8 mappings - this encoding should be
                # available for all locales
                continue
        data[locale] = alias
    return data
Example #15
0
def parse(filename):

    f = open(filename)
    lines = f.read().splitlines()
    data = {}
    for line in lines:
        line = line.strip()
        if not line:
            continue
        if line[:1] == '#':
            continue
        locale, alias = line.split()
        # Fix non-standard locale names, e.g. [email protected]
        if '@' in alias:
            alias_lang, _, alias_mod = alias.partition('@')
            if '.' in alias_mod:
                alias_mod, _, alias_enc = alias_mod.partition('.')
                alias = alias_lang + '.' + alias_enc + '@' + alias_mod
        # Strip ':'
        if locale[-1] == ':':
            locale = locale[:-1]
        # Lower-case locale
        locale = locale.lower()
        # Ignore one letter locale mappings (except for 'c')
        if len(locale) == 1 and locale != 'c':
            continue
        # Normalize encoding, if given
        if '.' in locale:
            lang, encoding = locale.split('.')[:2]
            encoding = encoding.replace('-', '')
            encoding = encoding.replace('_', '')
            locale = lang + '.' + encoding
            if encoding.lower() == 'utf8':
                # Ignore UTF-8 mappings - this encoding should be
                # available for all locales
                continue
        data[locale] = alias
    return data
Example #16
0
def _verify_python3_env():
    """Ensures that the environment is good for unicode on Python 3."""
    if PY2:
        return
    try:
        import locale
        fs_enc = codecs.lookup(locale.getpreferredencoding()).name
    except Exception:
        fs_enc = 'ascii'
    if fs_enc != 'ascii':
        return

    extra = ''
    if os.name == 'posix':
        import subprocess
        rv = subprocess.Popen(['locale', '-a'], stdout=subprocess.PIPE,
                              stderr=subprocess.PIPE).communicate()[0]
        good_locales = set()
        has_c_utf8 = False

        # Make sure we're operating on text here.
        if isinstance(rv, bytes):
            rv = rv.decode('ascii', 'replace')

        for line in rv.splitlines():
            locale = line.strip()
            if locale.lower().endswith(('.utf-8', '.utf8')):
                good_locales.add(locale)
                if locale.lower() in ('c.utf8', 'c.utf-8'):
                    has_c_utf8 = True

        extra += '\n\n'
        if not good_locales:
            extra += (
                'Additional information: on this system no suitable UTF-8\n'
                'locales were discovered.  This most likely requires resolving\n'
                'by reconfiguring the locale system.'
            )
        elif has_c_utf8:
            extra += (
                'This system supports the C.UTF-8 locale which is recommended.\n'
                'You might be able to resolve your issue by exporting the\n'
                'following environment variables:\n\n'
                '    export LC_ALL=C.UTF-8\n'
                '    export LANG=C.UTF-8'
            )
        else:
            extra += (
                'This system lists a couple of UTF-8 supporting locales that\n'
                'you can pick from.  The following suitable locales were\n'
                'discovered: %s'
            ) % ', '.join(sorted(good_locales))

        bad_locale = None
        for locale in os.environ.get('LC_ALL'), os.environ.get('LANG'):
            if locale and locale.lower().endswith(('.utf-8', '.utf8')):
                bad_locale = locale
            if locale is not None:
                break
        if bad_locale is not None:
            extra += (
                '\n\nClick discovered that you exported a UTF-8 locale\n'
                'but the locale system could not pick up from it because\n'
                'it does not exist.  The exported locale is "%s" but it\n'
                'is not supported'
            ) % bad_locale

    raise RuntimeError('Click will abort further execution because Python 3 '
                       'was configured to use ASCII as encoding for the '
                       'environment.  Consult http://click.pocoo.org/python3/'
                       'for mitigation steps.' + extra)
Example #17
0
def _verify_python3_env():
    """Ensures that the environment is good for unicode on Python 3."""
    if PY2:
        return
    try:
        import locale
        fs_enc = codecs.lookup(locale.getpreferredencoding()).name
    except Exception:
        fs_enc = 'ascii'
    if fs_enc != 'ascii':
        return

    extra = ''
    if os.name == 'posix':
        import subprocess
        rv = subprocess.Popen(['locale', '-a'],
                              stdout=subprocess.PIPE,
                              stderr=subprocess.PIPE).communicate()[0]
        good_locales = set()
        has_c_utf8 = False

        # Make sure we're operating on text here.
        if isinstance(rv, bytes):
            rv = rv.decode('ascii', 'replace')

        for line in rv.splitlines():
            locale = line.strip()
            if locale.lower().endswith(('.utf-8', '.utf8')):
                good_locales.add(locale)
                if locale.lower() in ('c.utf8', 'c.utf-8'):
                    has_c_utf8 = True

        extra += '\n\n'
        if not good_locales:
            extra += (
                'Additional information: on this system no suitable UTF-8\n'
                'locales were discovered.  This most likely requires resolving\n'
                'by reconfiguring the locale system.')
        elif has_c_utf8:
            extra += (
                'This system supports the C.UTF-8 locale which is recommended.\n'
                'You might be able to resolve your issue by exporting the\n'
                'following environment variables:\n\n'
                '    export LC_ALL=C.UTF-8\n'
                '    export LANG=C.UTF-8')
        else:
            extra += (
                'This system lists a couple of UTF-8 supporting locales that\n'
                'you can pick from.  The following suitable locales were\n'
                'discovered: %s') % ', '.join(sorted(good_locales))

        bad_locale = None
        for locale in os.environ.get('LC_ALL'), os.environ.get('LANG'):
            if locale and locale.lower().endswith(('.utf-8', '.utf8')):
                bad_locale = locale
            if locale is not None:
                break
        if bad_locale is not None:
            extra += (
                '\n\nClick discovered that you exported a UTF-8 locale\n'
                'but the locale system could not pick up from it because\n'
                'it does not exist.  The exported locale is "%s" but it\n'
                'is not supported') % bad_locale

    raise RuntimeError('Click will abort further execution because Python 3 '
                       'was configured to use ASCII as encoding for the '
                       'environment.  Consult http://click.pocoo.org/python3/'
                       'for mitigation steps.' + extra)
Example #18
0
def _verify_python3_env():
    """Ensures that the environment is good for unicode on Python 3."""
    if PY2:
        return
    try:
        import locale

        fs_enc = codecs.lookup(locale.getpreferredencoding()).name
    except Exception:
        fs_enc = "ascii"
    if fs_enc != "ascii":
        return

    extra = ""
    if os.name == "posix":
        import subprocess

        try:
            rv = subprocess.Popen(["locale", "-a"],
                                  stdout=subprocess.PIPE,
                                  stderr=subprocess.PIPE).communicate()[0]
        except OSError:
            rv = b""
        good_locales = set()
        has_c_utf8 = False

        # Make sure we're operating on text here.
        if isinstance(rv, bytes):
            rv = rv.decode("ascii", "replace")

        for line in rv.splitlines():
            locale = line.strip()
            if locale.lower().endswith((".utf-8", ".utf8")):
                good_locales.add(locale)
                if locale.lower() in ("c.utf8", "c.utf-8"):
                    has_c_utf8 = True

        extra += "\n\n"
        if not good_locales:
            extra += (
                "Additional information: on this system no suitable UTF-8\n"
                "locales were discovered.  This most likely requires resolving\n"
                "by reconfiguring the locale system.")
        elif has_c_utf8:
            extra += (
                "This system supports the C.UTF-8 locale which is recommended.\n"
                "You might be able to resolve your issue by exporting the\n"
                "following environment variables:\n\n"
                "    export LC_ALL=C.UTF-8\n"
                "    export LANG=C.UTF-8")
        else:
            extra += (
                "This system lists a couple of UTF-8 supporting locales that\n"
                "you can pick from.  The following suitable locales were\n"
                "discovered: %s") % ", ".join(sorted(good_locales))

        bad_locale = None
        for locale in os.environ.get("LC_ALL"), os.environ.get("LANG"):
            if locale and locale.lower().endswith((".utf-8", ".utf8")):
                bad_locale = locale
            if locale is not None:
                break
        if bad_locale is not None:
            extra += (
                "\n\nClick discovered that you exported a UTF-8 locale\n"
                "but the locale system could not pick up from it because\n"
                'it does not exist.  The exported locale is "%s" but it\n'
                "is not supported") % bad_locale

    raise RuntimeError(
        "Click will abort further execution because Python 3 was"
        " configured to use ASCII as encoding for the environment."
        " Consult https://click.palletsprojects.com/en/7.x/python3/ for"
        " mitigation steps." + extra)
 def change_locale(locale):
     LOCALE = locale.lower()
     return TIMEZONES.get(LOCALE, 'UTC')