• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

dfint / df-gettext-toolkit / 5871136315

pending completion
5871136315

push

github

insolor
Upgrade style of the type annotations using ruff

157 of 404 branches covered (38.86%)

Branch coverage included in aggregate %.

53 of 53 new or added lines in 18 files covered. (100.0%)

369 of 876 relevant lines covered (42.12%)

1.26 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/df_gettext_toolkit/create_pot/from_steam_text.py
1
from collections import defaultdict
×
2
from collections.abc import Iterable
×
3
from pathlib import Path
×
4

5
import typer
×
6
from loguru import logger
×
7

8
from df_gettext_toolkit.create_pot.from_raw_objects import extract_from_raw_file
×
9
from df_gettext_toolkit.parse.parse_raws import split_tag, tokenize_raw_file
×
10
from df_gettext_toolkit.parse.parse_text_set import extract_from_vanilla_text
×
11
from df_gettext_toolkit.utils.po_utils import save_pot
×
12

13

14
def traverse_vanilla_directories(vanilla_path: Path) -> Iterable[Path]:
×
15
    for directory in sorted(vanilla_path.glob("vanilla_*")):
×
16
        if directory.is_dir():
×
17
            objects = directory / "objects"
×
18
            if objects.is_dir():
×
19
                yield objects
×
20

21

22
def get_raw_object_type(file_name: Path, source_encoding: str) -> str:
×
23
    with file_name.open(encoding=source_encoding) as file:
×
24
        for item in tokenize_raw_file(file):
×
25
            if item.is_tag:
×
26
                object_tag = split_tag(item.text)
×
27
                assert object_tag[0] == "OBJECT"
×
28
                return object_tag[1]
×
29

30

31
def get_translatable_strings(file_path: Path, source_encoding: str) -> tuple[str, Iterable[str]] | None:
×
32
    object_type = get_raw_object_type(file_path, source_encoding)
×
33
    if object_type in dont_translate:
×
34
        return None
×
35
    elif object_type == "TEXT_SET":
×
36
        key = object_type
×
37
        data = extract_from_vanilla_text(file_path, source_encoding)
×
38
    else:
39
        key = "OBJECTS"
×
40
        data = extract_from_raw_file(file_path, source_encoding)
×
41

42
    return key, data
×
43

44

45
def iterable_is_empty(iterable: Iterable):
×
46
    iterator = iter(iterable)
×
47
    try:
×
48
        next(iterator)
×
49
        return False
×
50
    except StopIteration:
×
51
        return True
×
52

53

54
def file_is_translatable(file_path: Path, source_encoding: str) -> bool:
×
55
    result = get_translatable_strings(file_path, source_encoding)
×
56
    return result is not None and not iterable_is_empty(result[1])
×
57

58

59
dont_translate = {"LANGUAGE"}
×
60

61

62
def main(vanilla_path: Path, destination_path: Path, source_encoding: str = "cp437"):
×
63
    assert vanilla_path.exists(), "Source path doesn't exist"
×
64
    assert destination_path.exists(), "Destination path doesn't exist"
×
65

66
    results = defaultdict(list)
×
67

68
    for directory in traverse_vanilla_directories(vanilla_path):
×
69
        logger.info(directory.relative_to(vanilla_path))
×
70
        for file_path in sorted(directory.glob("*.txt")):
×
71
            if file_path.is_file():
×
72
                result = get_translatable_strings(file_path, source_encoding)
×
73
                if not result:
×
74
                    continue
×
75
                else:
76
                    key, data = result
×
77

78
                results[key].extend(data)
×
79

80
        for group, data in results.items():
×
81
            if data:
×
82
                pot_path = destination_path / (group.lower() + ".pot")
×
83
                with pot_path.open("wb") as file_path:
×
84
                    save_pot(file_path, data)
×
85

86

87
if __name__ == "__main__":
88
    typer.run(main)
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc