File: i18n_updater.py

package info (click to toggle)
python-djangorestframework-simplejwt 5.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 956 kB
  • sloc: python: 3,783; makefile: 213; javascript: 40; sh: 6
file content (58 lines) | stat: -rw-r--r-- 1,530 bytes parent folder | download | duplicates (3)
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
import contextlib
import os
import subprocess


def get_list_of_files(dir_name: str, extension: str):
    file_list = os.listdir(dir_name)

    result = []
    for entry in file_list:
        full_path = os.path.join(dir_name, entry)
        if os.path.isdir(full_path):
            result = result + get_list_of_files(full_path, extension)
        else:
            if entry[-len(extension) : len(entry)] == extension:
                result.append(full_path)

    return result


@contextlib.contextmanager
def cache_creation():
    # DO NOT cache the line number; the file may change
    cache: dict[str, str] = {}
    for file in get_list_of_files("./", ".po"):
        if os.path.isdir(file):
            continue

        with open(file) as f:
            for line in f.readlines():
                if line.startswith('"POT-Creation-Date: '):
                    cache[file] = line
                    break
    yield
    for file, line_cache in cache.items():
        with open(file, "r+") as f:
            lines = f.readlines()
            # clear file
            f.seek(0)
            f.truncate()

            # find line
            index = [
                lines.index(x) for x in lines if x.startswith('"POT-Creation-Date: ')
            ][0]

            lines[index] = line_cache
            f.writelines(lines)


def main():
    with cache_creation():
        subprocess.run(["django-admin", "makemessages", "-a"])
    subprocess.run(["django-admin", "compilemessages"])


if __name__ == "__main__":
    main()