LocalizationsCommon.check_file_pairs()   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 31
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 30
nop 2
dl 0
loc 31
rs 9.16
c 0
b 0
f 0
1
"""
2
localizations_common - common class for various localizations tasks
3
"""
4
import glob
5
# package to facilitate operating system project_locale detection
6
import locale
7
# package to handle files/folders and related metadata/operations
8
import os
9
import pathlib
10
# package to facilitate multiple operation system operations
11
import platform
12
13
14
class LocalizationsCommon:
15
    locale_implemented = [
16
        'it_IT',
17
        'ro_RO',
18
    ]
19
    operation_is_required = False
20
21
    def check_file_pairs(self, in_dict):
22
        get_details_to_operate = False
23
        file_situation_verdict = ''
24
        if os.path.lexists(pathlib.Path(in_dict['destination'])):
25
            source_last_modified = os.path.getmtime(in_dict['source'])
26
            compiled_last_modified = os.path.getmtime(in_dict['destination'])
27
            if source_last_modified > compiled_last_modified:
28
                self.operation_is_required = True
29
                get_details_to_operate = True
30
                print('#' + str(in_dict['counter']) + ', '
31
                      + 'For locale ' + in_dict['locale']
32
                      + ' the ' + in_dict['source file type name'] + ' file '
33
                      + os.path.basename(in_dict['source'])
34
                      + ' is newer than compiled one '
35
                      + os.path.basename(in_dict['destination'])
36
                      + ' therefore to remedy this, '
37
                      + in_dict['destination operation name'] + ' is required')
38
                print('===>' + in_dict['source'] + ' has ' + str(source_last_modified)
39
                      + ' vs. ' + str(compiled_last_modified))
40
                file_situation_verdict = 'newer'
41
        else:
42
            self.operation_is_required = True
43
            get_details_to_operate = True
44
            print('#' + str(in_dict['counter']) + ', '
45
                  + 'The file ' + os.path.basename(in_dict['source'])
46
                  + ' does not have compiled file for ' + in_dict['locale']
47
                  + ' therefore will require '
48
                  + in_dict['destination operation name'] + ' into following folder: '
49
                  + os.path.normpath(os.path.dirname(in_dict['destination'])))
50
            file_situation_verdict = 'missing'
51
        return get_details_to_operate, file_situation_verdict
52
53
    @staticmethod
54
    def file_counter_limit(in_file_counter, in_file_list_size):
55
        file_list_paring_complete = False
56
        if in_file_counter == in_file_list_size:
57
            file_list_paring_complete = True
58
        return file_list_paring_complete
59
60
    def get_region_language_to_use_from_operating_system(self):
61
        language_to_use = ''
62
        try:
63
            region_language_to_use = locale.getdefaultlocale('LC_ALL')
64
            if region_language_to_use[0] in self.locale_implemented:
65
                language_to_use = region_language_to_use[0]
66
        except AttributeError as err:
67
            print(err)
68
        except ValueError as err:
69
            print(err)
70
        return language_to_use
71
72
    @staticmethod
73
    def get_this_file_folder():
74
        return os.path.dirname(__file__)
75
76
    def get_project_localisation_source_files(self, in_extension):
77
        file_pattern = os.path.join(os.path.dirname(__file__), '**/*.' + in_extension)
78
        initial_list = glob.glob(file_pattern, recursive=True)
79
        normalizer = lambda x: self.path_normalize(x)
80
        return list(map(normalizer, initial_list))
81
82
    @staticmethod
83
    def get_project_root():
84
        file_parts = os.path.normpath(os.path.abspath(__file__))\
85
            .replace('\\', os.altsep).split(os.altsep)
86
        return os.altsep.join(file_parts[:-3])
87
88
    def get_virtual_environment_python_binary(self):
89
        python_binary = 'python'
90
        if platform.system() == 'Windows':
91
            project_root = self.get_project_root()
92
            join_separator = os.path.altsep
93
            elements_to_join = [
94
                project_root,
95
                'virtual_environment',
96
                'Scripts',
97
            ]
98
            virtual_env_long = join_separator.join(elements_to_join)
99
            elements_to_join[1] = 'venv'
100
            virtual_env_short = join_separator.join(elements_to_join)
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 os.path.normpath(python_binary)
106
107
    def operate_localisation_files(self, in_dict_details_to_operate_with):
108
        if self.operation_is_required:
109
            for current_details_to_operate in in_dict_details_to_operate_with:
110
                os.system(self.get_virtual_environment_python_binary() + ' '
111
                          + os.path.join(os.path.dirname(__file__), 'localizations_setup.py')
112
                          + ' ' + current_details_to_operate['operation']
113
                          + ' --input-file=' + current_details_to_operate['input-file']
114
                          + ' --output-file=' + current_details_to_operate['output-file']
115
                          + ' --locale ' + current_details_to_operate['locale']
116
                          + current_details_to_operate['operation final flags'])
117
118
    @staticmethod
119
    def path_normalize(in_file_name):
120
        return os.path.join(os.path.normpath(os.path.dirname(in_file_name)),
121
                            os.path.basename(in_file_name))
122
123
    def run_localization_compile(self):
124
        command_parts_to_run = [
125
            self.get_virtual_environment_python_binary(),
126
            ' ',
127
            self.get_this_file_folder(),
128
            os.path.altsep,
129
            'localizations_compile.py',
130
        ]
131
        os.system(''.join(command_parts_to_run))
132
133