Passed
Push — master ( 04fbbf...5a831b )
by Daniel
01:17
created

LocalizationsCommon.get_project_root()   A

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nop 0
dl 0
loc 5
rs 10
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
        try:
62
            region_language_to_use = locale.getdefaultlocale('LC_ALL')
63
            if region_language_to_use[0] not in self.locale_implemented:
64
                language_to_use = self.locale_implemented[0]
65
            else:
66
                language_to_use = region_language_to_use[0]
67
        except AttributeError as err:
68
            print(err)
69
            language_to_use = self.locale_implemented[0]
70
        except ValueError as err:
71
            print(err)
72
            language_to_use = self.locale_implemented[0]
73
        return language_to_use
74
75
    @staticmethod
76
    def get_this_file_folder():
77
        return os.path.dirname(__file__)
78
79
    def get_project_localisation_source_files(self, in_extension):
80
        file_pattern = os.path.join(os.path.dirname(__file__), '**/*.' + in_extension)
81
        initial_list = glob.glob(file_pattern, recursive=True)
82
        normalizer = lambda x: self.path_normalize(x)
83
        return list(map(normalizer, initial_list))
84
85
    @staticmethod
86
    def get_project_root():
87
        file_parts = os.path.normpath(os.path.abspath(__file__))\
88
            .replace('\\', os.altsep).split(os.altsep)
89
        return os.altsep.join(file_parts[:-3])
90
91
    def get_virtual_environment_python_binary(self):
92
        python_binary = 'python'
93
        if platform.system() == 'Windows':
94
            project_root = self.get_project_root()
95
            join_separator = os.path.altsep
96
            elements_to_join = [
97
                project_root,
98
                'virtual_environment',
99
                'Scripts',
100
            ]
101
            virtual_env_long = join_separator.join(elements_to_join)
102
            elements_to_join[1] = 'venv'
103
            virtual_env_short = join_separator.join(elements_to_join)
104
            if os.path.isdir(virtual_env_long):
105
                python_binary = os.path.join(virtual_env_long, 'python.exe')
106
            elif os.path.isdir(virtual_env_short):
107
                python_binary = os.path.join(virtual_env_short, 'python.exe')
108
        return os.path.normpath(python_binary)
109
110
    def operate_localisation_files(self, in_dict_details_to_operate_with):
111
        if self.operation_is_required:
112
            for current_details_to_operate in in_dict_details_to_operate_with:
113
                os.system(self.get_virtual_environment_python_binary() + ' '
114
                          + os.path.join(os.path.dirname(__file__), 'localizations_setup.py')
115
                          + ' ' + current_details_to_operate['operation']
116
                          + ' --input-file=' + current_details_to_operate['input-file']
117
                          + ' --output-file=' + current_details_to_operate['output-file']
118
                          + ' --locale ' + current_details_to_operate['locale']
119
                          + current_details_to_operate['operation final flags'])
120
121
    @staticmethod
122
    def path_normalize(in_file_name):
123
        return os.path.join(os.path.normpath(os.path.dirname(in_file_name)),
124
                            os.path.basename(in_file_name))
125
126
    def run_localization_compile(self):
127
        command_parts_to_run = [
128
            self.get_virtual_environment_python_binary(),
129
            ' ',
130
            self.get_this_file_folder(),
131
            os.path.altsep,
132
            'localizations_compile.py',
133
        ]
134
        os.system(''.join(command_parts_to_run))
135
136