File: locale_utils.py

package info (click to toggle)
libbytesize 2.11-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 784 kB
  • sloc: python: 1,850; ansic: 994; sh: 666; makefile: 258
file content (37 lines) | stat: -rw-r--r-- 1,065 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

import subprocess

"""Helper functions, decorators, ... for working with locales"""

def get_avail_locales():
    try:
        return {loc.decode(errors="replace").strip() for loc in subprocess.check_output(["locale", "-a"]).split()}
    except FileNotFoundError:
        return {"C"}

def missing_locales(required, available):
    canon_locales = {loc.replace("UTF-8", "utf8") for loc in required}
    return canon_locales - set(available)


def requires_locales(locales):
    """A decorator factory to skip tests that require unavailable locales

    :param set locales: set of required locales

    **Requires the test to have the set of available locales defined as its
    ``avail_locales`` attribute.**

    """

    def decorator(test_method):
        def decorated(test, *args):
            missing = missing_locales(locales, test.avail_locales)
            if missing:
                test.skipTest("requires missing locales: %s" % missing)
            else:
                return test_method(test, *args)

        return decorated

    return decorator