File: lang_sample_text.py

package info (click to toggle)
python-gflanguages 0.7.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,004 kB
  • sloc: python: 818; sh: 8; makefile: 6
file content (65 lines) | stat: -rwxr-xr-x 1,562 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python3
"""
lang-sample-text

Adds sample text for a given language using the specified UDHR translation.

Usage:

lang-sample-text -l ./languages/en.textproto ./udhr_translations/en.xml

"""

from gflanguages import LoadLanguages, languages_public_pb2
from gftools.util.google_fonts import ReadProto, WriteProto
from gflanguages.udhr import Udhr
from lxml import etree
import os
import re
import argparse


def main(argv=None):
    parser = argparse.ArgumentParser(
        description="Update UDHR sample text for a given language"
    )
    parser.add_argument(
        "-l",
        "--lang",
        help="Language proto file to update",
        required=True,
    )
    parser.add_argument(
        "-u",
        "--udhr",
        help="Path to UDHR translation (XML)",
        required=True,
    )
    args = parser.parse_args(argv)

    language = ReadProto(languages_public_pb2.LanguageProto(), args.lang)

    udhr_data = etree.parse(args.udhr)
    head = udhr_data.getroot()
    for name, value in head.attrib.items():
        if re.search(r"\{.*\}lang", name):
            bcp47 = value.replace("-", "_")
    udhr = Udhr(
        key=head.get("key"),
        iso639_3=head.get("iso639-3"),
        iso15924=head.get("iso15924"),
        bcp47=bcp47,
        direction=head.get("dir"),
        ohchr=None,
        stage=4,
        loc=None,
        name=head.get("n"),
    )
    udhr.Parse(udhr_data)

    language.sample_text.MergeFrom(udhr.GetSampleTexts())
    WriteProto(language, args.lang)


if __name__ == "__main__":
    main()