Test Failed
Push — development/test ( 03fb82...bf576f )
by Daniel
02:03
created

CustomizedLocalizationCompiling.file_counter_limit()   A

Complexity

Conditions 2

Size

Total Lines 6
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nop 2
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
    def evaluate_compilation_necessity(self, in_list_localisation_source_files):
28
        list_size = len(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
            file_list_paring_complete = self.file_counter_limit(file_counter, list_size)
72
        return localisation_compilation_is_required, domains_to_compile
73
74
    @staticmethod
75
    def file_counter_limit(in_file_counter, in_file_list_size):
76
        file_list_paring_complete = False
77
        if in_file_counter == in_file_list_size:
78
            file_list_paring_complete = True
79
        return file_list_paring_complete
80
81
    @staticmethod
82
    def get_project_localisation_source_files():
83
        file_pattern = os.path.join(os.path.dirname(__file__), '**/*.po') \
84
            .replace('test', 'sources')
85
        initial_list = glob.glob(file_pattern, recursive=True)
86
        normalizer = lambda x: my_class.path_normalize(x)
87
        return list(map(normalizer, initial_list))
88
89
    @staticmethod
90
    def get_project_root():
91
        return os.path.normpath(os.path.dirname(__file__).replace('sources', ''))
92
93
    @staticmethod
94
    def get_virtual_environment_python_binary():
95
        python_binary = 'python'
96
        if platform.system() == 'Windows':
97
            virtual_env_long = os.path.normpath(os.path.dirname(__file__)
98
                                                .replace('sources', 'virtual_environment/Scripts'))
99
            virtual_env_short = os.path.normpath(os.path.dirname(__file__)
100
                                                 .replace('sources', 'venv/Scripts'))
101
            if os.path.isdir(virtual_env_long):
102
                python_binary = os.path.join(virtual_env_long, 'python.exe')
103
            elif os.path.isdir(virtual_env_short):
104
                python_binary = os.path.join(virtual_env_short, 'python.exe')
105
        return python_binary
106
107
    def path_normalize(self, in_file_name):
108
        return os.path.join(os.path.normpath(os.path.dirname(in_file_name)),
109
                            os.path.basename(in_file_name))
110
111
112
my_class = CustomizedLocalizationCompiling()
113
114
locale_source_files = my_class.get_project_localisation_source_files()
115
compilation_is_required, compiling_domains = \
116
    my_class.evaluate_compilation_necessity(locale_source_files)
117
my_class.compile_localisation_files(compilation_is_required, compiling_domains)
118