|
1
|
|
|
import fnmatch |
|
2
|
|
|
import os |
|
3
|
|
|
import sys |
|
4
|
|
|
|
|
5
|
|
|
if sys.version_info >= (3, 9): |
|
6
|
|
|
List = list |
|
7
|
|
|
Set = list |
|
8
|
|
|
else: |
|
9
|
|
|
from typing import List |
|
10
|
|
|
from typing import Set |
|
11
|
|
|
|
|
12
|
|
|
from pathlib import Path |
|
13
|
|
|
|
|
14
|
|
|
from .text import stringify_path |
|
15
|
|
|
from .utils import print_out |
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
MESSAGES_DIR_NAME = "LC_MESSAGES" |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
def make_messages_dir_path(locales_dir_path: Path, locale: str) -> Path: |
|
22
|
|
|
return locales_dir_path / locale / MESSAGES_DIR_NAME |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
def make_pot_file_path(locales_dir_path: Path, domain: str) -> Path: |
|
26
|
|
|
return locales_dir_path / f"{domain}.pot" |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
def make_po_file_path( |
|
30
|
|
|
locales_dir_path: Path, |
|
31
|
|
|
locale: str, |
|
32
|
|
|
domain: str, |
|
33
|
|
|
) -> Path: |
|
34
|
|
|
|
|
35
|
|
|
messages_dir_path = make_messages_dir_path(locales_dir_path, locale) |
|
36
|
|
|
messages_file_name = f"{domain}.po" |
|
37
|
|
|
|
|
38
|
|
|
return messages_dir_path / messages_file_name |
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
def make_mo_file_path(po_file_path: Path) -> Path: |
|
42
|
|
|
return po_file_path.parent / f"{po_file_path.stem}.mo" |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
def get_names_of_immediate_subdirectories(root: Path) -> List[str]: |
|
46
|
|
|
return [ |
|
47
|
|
|
node.name |
|
48
|
|
|
for node in root.iterdir() |
|
49
|
|
|
if node.is_dir() |
|
50
|
|
|
] |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
def ensure_dir_exists(path: Path) -> None: |
|
54
|
|
|
path.mkdir(parents=True, exist_ok=True) |
|
55
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
def is_path_ignored(path: Path, ignore_patterns: List[str]) -> bool: |
|
58
|
|
|
for pattern in ignore_patterns: |
|
59
|
|
|
if fnmatch.fnmatchcase(path.name, pattern): |
|
60
|
|
|
return True |
|
61
|
|
|
else: |
|
62
|
|
|
return False |
|
63
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
def normalize_dir_patterns(patterns: List[str]) -> List[str]: |
|
66
|
|
|
dir_suffix = "{os.sep}*" |
|
67
|
|
|
return [ |
|
68
|
|
|
( |
|
69
|
|
|
p[:-len(dir_suffix)] |
|
70
|
|
|
if p.endswith(dir_suffix) |
|
71
|
|
|
else p |
|
72
|
|
|
) |
|
73
|
|
|
for p in patterns |
|
74
|
|
|
] |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
def find_source_files_paths( |
|
78
|
|
|
root_dir_path: Path, |
|
79
|
|
|
ignore_patterns: List[str], |
|
80
|
|
|
extensions: Set[str], |
|
81
|
|
|
follow_links: bool, |
|
82
|
|
|
verbose: bool, |
|
83
|
|
|
) -> List[Path]: |
|
84
|
|
|
|
|
85
|
|
|
result = [] |
|
86
|
|
|
|
|
87
|
|
|
walker = os.walk( |
|
88
|
|
|
top=stringify_path(root_dir_path), |
|
89
|
|
|
topdown=True, |
|
90
|
|
|
followlinks=follow_links, |
|
91
|
|
|
) |
|
92
|
|
|
|
|
93
|
|
|
normalized_ignore_patterns = normalize_dir_patterns(ignore_patterns) |
|
94
|
|
|
|
|
95
|
|
|
for dir_path, dir_names, file_names in walker: |
|
96
|
|
|
|
|
97
|
|
|
for dir_name in dir_names[:]: |
|
98
|
|
|
path = Path(os.path.normpath(os.path.join(dir_path, dir_name))) |
|
99
|
|
|
if is_path_ignored(path, normalized_ignore_patterns): |
|
100
|
|
|
dir_names.remove(dir_name) |
|
101
|
|
|
if verbose: |
|
102
|
|
|
print_out(f"ignoring dir '{stringify_path(path.absolute())}'") |
|
103
|
|
|
|
|
104
|
|
|
for file_name in file_names: |
|
105
|
|
|
path = Path(os.path.normpath(os.path.join(dir_path, file_name))) |
|
106
|
|
|
if is_path_ignored(path, ignore_patterns): |
|
107
|
|
|
if verbose: |
|
108
|
|
|
print_out(f"ignoring file '{stringify_path(path.absolute())}'") |
|
109
|
|
|
elif path.suffix in extensions: |
|
110
|
|
|
result.append(path) |
|
111
|
|
|
|
|
112
|
|
|
return list(sorted(result)) |
|
113
|
|
|
|