1
|
|
|
""" |
2
|
|
|
localization_setup - facilitates localization file compilation (from .po to .mo) |
3
|
|
|
""" |
4
|
|
|
import glob |
5
|
|
|
import os |
6
|
|
|
import pathlib |
7
|
|
|
# package to facilitate multiple operation system operations |
8
|
|
|
import platform |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
class CustomizedLocalizationCompiling: |
12
|
|
|
|
13
|
|
|
def compile_localisation_files(self, compilation_needed, domains_to_compile): |
14
|
|
|
if compilation_needed: |
15
|
|
|
for current_domain_to_compile in domains_to_compile: |
16
|
|
|
os.system(self.get_virtual_environment_python_binary() + ' ' |
17
|
|
|
+ os.path.join(os.path.dirname(__file__), 'localizations_setup.py') |
18
|
|
|
+ ' compile_catalog' |
19
|
|
|
+ ' --input-file=' + current_domain_to_compile['input-file'] |
20
|
|
|
+ ' --output-file=' + current_domain_to_compile['output-file'] |
21
|
|
|
+ ' --locale ' + current_domain_to_compile['locale'] |
22
|
|
|
+ ' --statistics') |
23
|
|
|
else: |
24
|
|
|
print('For all Localization source files there is a pair of compiled localization file ' |
25
|
|
|
+ 'which has same date or newer, so no compiling is necessary!') |
26
|
|
|
|
27
|
|
|
@staticmethod |
28
|
|
|
def evaluate_compilation_necessity(in_list_localisation_source_files): |
29
|
|
|
localisation_compilation_is_required = False |
30
|
|
|
file_list_paring_complete = False |
31
|
|
|
file_counter = 0 |
32
|
|
|
domains_to_compile = [] |
33
|
|
|
compiling_files_counter = 0 |
34
|
|
|
while not file_list_paring_complete: |
35
|
|
|
get_domain_details_for_compilation = False |
36
|
|
|
source_localisation_file = in_list_localisation_source_files[file_counter] |
37
|
|
|
compiled_localisation_file = source_localisation_file.replace('.po', '.mo') |
38
|
|
|
folder_parts = pathlib.PurePath(source_localisation_file).parts |
39
|
|
|
current_locale = folder_parts[(len(folder_parts) - 3)] |
40
|
|
|
if os.path.lexists(pathlib.Path(compiled_localisation_file)): |
41
|
|
|
source_last_modified = os.path.getmtime(source_localisation_file) |
42
|
|
|
compiled_last_modified = os.path.getmtime(compiled_localisation_file) |
43
|
|
|
if source_last_modified > compiled_last_modified: |
44
|
|
|
localisation_compilation_is_required = True |
45
|
|
|
get_domain_details_for_compilation = True |
46
|
|
|
print('#' + str(file_counter) + ', ' |
47
|
|
|
+ 'For locale ' + current_locale + ' source file ' |
48
|
|
|
+ os.path.basename(source_localisation_file) |
49
|
|
|
+ ' is newer than compiled one ' |
50
|
|
|
+ os.path.basename(compiled_localisation_file) |
51
|
|
|
+ ' so compilation needs to take place to remedy this') |
52
|
|
|
print('===>' + source_localisation_file + ' has ' + str(source_last_modified) |
53
|
|
|
+ ' vs. ' + str(compiled_last_modified)) |
54
|
|
|
else: |
55
|
|
|
get_domain_details_for_compilation = True |
56
|
|
|
print('#' + str(file_counter) + ', ' |
57
|
|
|
+ 'The file ' + os.path.basename(source_localisation_file) |
58
|
|
|
+ ' does not have compiled file for ' + current_locale |
59
|
|
|
+ ' therefore will require compiling into the same folder: ' |
60
|
|
|
+ os.path.dirname(source_localisation_file)) |
61
|
|
|
localisation_compilation_is_required = True |
62
|
|
|
if get_domain_details_for_compilation: |
63
|
|
|
domains_to_compile.append(compiling_files_counter) |
64
|
|
|
domains_to_compile[compiling_files_counter] = { |
65
|
|
|
'input-file': source_localisation_file, |
66
|
|
|
'output-file': compiled_localisation_file, |
67
|
|
|
'locale': current_locale, |
68
|
|
|
} |
69
|
|
|
compiling_files_counter += 1 |
70
|
|
|
file_counter += 1 |
71
|
|
|
if file_counter == len(in_list_localisation_source_files): |
72
|
|
|
file_list_paring_complete = True |
73
|
|
|
return localisation_compilation_is_required, domains_to_compile |
74
|
|
|
|
75
|
|
|
@staticmethod |
76
|
|
|
def get_project_localisation_source_files(): |
77
|
|
|
file_pattern = os.path.join(os.path.dirname(__file__), '**/*.po') \ |
78
|
|
|
.replace('test', 'sources') |
79
|
|
|
initial_list = glob.glob(file_pattern, recursive=True) |
80
|
|
|
normalizer = lambda x: my_class.path_normalize(x) |
81
|
|
|
return list(map(normalizer, initial_list)) |
82
|
|
|
|
83
|
|
|
@staticmethod |
84
|
|
|
def get_project_root(): |
85
|
|
|
return os.path.normpath(os.path.dirname(__file__).replace('sources', '')) |
86
|
|
|
|
87
|
|
|
@staticmethod |
88
|
|
|
def get_virtual_environment_python_binary(): |
89
|
|
|
python_binary = 'python' |
90
|
|
|
if platform.system() == 'Windows': |
91
|
|
|
python_binary = 'python.exe' |
92
|
|
|
return os.path.normpath(os.path.join( |
93
|
|
|
os.path.dirname(__file__) |
|
|
|
|
94
|
|
|
.replace('sources', 'virtual_environment/Scripts') \ |
|
|
|
|
95
|
|
|
.replace('/', os.altsep), |
96
|
|
|
python_binary)) |
|
|
|
|
97
|
|
|
|
98
|
|
|
def path_normalize(self, in_file_name): |
99
|
|
|
return os.path.join(os.path.normpath(os.path.dirname(in_file_name)), |
100
|
|
|
os.path.basename(in_file_name)) |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
my_class = CustomizedLocalizationCompiling() |
104
|
|
|
|
105
|
|
|
locale_source_files = my_class.get_project_localisation_source_files() |
106
|
|
|
compilation_is_required, compiling_domains = \ |
107
|
|
|
my_class.evaluate_compilation_necessity(locale_source_files) |
108
|
|
|
my_class.compile_localisation_files(compilation_is_required, compiling_domains) |
109
|
|
|
|