File: codec.py

package info (click to toggle)
python-precis-i18n 1.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,836 kB
  • sloc: python: 1,825; sh: 28; makefile: 3
file content (41 lines) | stat: -rw-r--r-- 894 bytes parent folder | download
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
38
39
40
41
"""Registers precis_i18n codec."""

import codecs

from precis_i18n import get_profile


def _make_encode(profile):
    def _encode(input, errors="strict"):
        if errors != "strict":
            raise ValueError("invalid errors argument")
        return (profile.enforce(input).encode("utf-8"), len(input))

    return _encode


def _not_supported(input, errors="strict"):
    # pylint: disable=unused-argument
    raise NotImplementedError("decode not supported")


def search(name):
    """Search function registered for PRECIS codecs.

    Args:
        name (str): Codec name.

    Returns:
        CodecInfo: Encode/decode information or None if not found.
    """
    try:
        profile = get_profile(name)
    except KeyError:
        return None

    return codecs.CodecInfo(
        name=name, encode=_make_encode(profile), decode=_not_supported
    )


codecs.register(search)