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