File size: 11,890 Bytes
27867f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
"""
MIT License

Copyright (C) 2023 ROCKY4546
https://github.com/rocky4546

This file is part of Cabernet

Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the “Software”), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
"""

import importlib
import json
import logging
import threading
from importlib import resources

import lib.common.utils as utils
from lib.db.db_config_defn import DBConfigDefn

CONFIG_DEFN_PATH = 'lib.resources.config_defn'
PLUGIN_DATA = 'AOEPFQGLIKMCNRJSBTHUDV'


def load_default_config_defns():
    """ loads all definition files from the default
        folder and returns the ConfigDefn object
    """
    defn_obj = ConfigDefn()
    for defn_file in sorted(
            importlib.resources.contents(CONFIG_DEFN_PATH)):
        if str(defn_file).endswith('.json'):
            defn_obj.merge_defn_file(CONFIG_DEFN_PATH, defn_file)
    return defn_obj


class ConfigDefn:

    def __init__(self, _defn_path=None, _defn_file=None, _config=None, _is_instance=False):
        self.logger = None
        self.config_defn = {}
        self.config = None
        self.db = None
        self.is_instance_defn = _is_instance
        self.restricted_items = []
        if _config:
            self.set_config(_config)
        if _defn_file and _defn_path:
            self.merge_defn_file(_defn_path, _defn_file)

    def set_config(self, _config):
        self.config = _config
        if self.db is None:
            self.db = DBConfigDefn(self.config)
            if self.config_defn:
                self.save_defn_to_db()
        self.logger = logging.getLogger(__name__)

    def merge_defn_file(self, _defn_path, _defn_file):
        """ Merges a definition file into the current object
        """
        json_file = resources.read_text(_defn_path, _defn_file)
        defn = json.loads(json_file)
        self.call_ondefnload(defn)
        self.merge_defn_dict(defn)

    def merge_defn_dict(self, _defn_dict):
        """ Merges a definition file into the current object
        """
        self.config_defn = utils.merge_dict(self.config_defn, _defn_dict)
        self.update_restricted_items(_defn_dict)
        if self.db is not None:
            self.save_defn_to_db(_defn_dict)

    def merge_defn_obj(self, _defn_obj):
        """ will merge and terminate defn object
        """
        self.config_defn = utils.merge_dict(self.config_defn, _defn_obj.config_defn, ignore_conflicts=True)
        self.update_restricted_items(_defn_obj.config_defn)

    def garbage_collect(self):
        self.logger.debug('garbage collecting for Thread:{}'.format(threading.get_ident()))
        self.config_defn = None

    def get_default_config(self):
        """
        JSON format: [module]['sections'][section]['settings'][setting][metadata]
        section is the section in the ini file
        setting is the name in the ini file
        """
        config_defaults = {}
        if self.db is not None:
            areas = self.get_areas()
            for area in areas:
                area_dict = self.get_defn(area)
                defaults_dict = self.get_default_config_area(area, area_dict)
                config_defaults = utils.merge_dict(config_defaults, defaults_dict)
        else:
            for area, area_dict in self.config_defn.items():
                defaults_dict = self.get_default_config_area(area, area_dict)
                config_defaults = utils.merge_dict(config_defaults, defaults_dict)
        return config_defaults

    def get_default_config_area(self, _area, _area_dict=None):
        config_defaults = {}
        if _area_dict is None:
            area_dict = self.get_defn(_area)
        else:
            area_dict = _area_dict

        for section in list(area_dict['sections'].keys()):
            if section not in list(config_defaults.keys()):
                config_defaults[section] = {}
            for setting in list(area_dict['sections'][section]['settings'].keys()):
                value = area_dict['sections'][section]['settings'][setting]['default']
                config_defaults[section][setting] = value
        return config_defaults

    def get_defn(self, _area):
        area_dict = self.db.get_area_dict(_area)
        if not area_dict:
            return
        area_dict = area_dict[0]
        sections = self.db.get_sections_dict(_area)
        area_dict['sections'] = sections
        return area_dict

    def get_areas(self):
        return self.db.get_areas()

    def call_oninit(self, _config_obj):
        for module in list(self.config_defn.keys()):
            for section in list(self.config_defn[module]['sections'].keys()):
                for key, settings in list(self.config_defn[module]['sections'][section]['settings'].items()):
                    if 'onInit' in settings:
                        self.call_function(settings['onInit'], section, key, _config_obj)

    def call_onchange(self, _area, _updated_data, _config_obj):
        results = ''
        area_data = self.get_defn(_area)
        for section, section_data in area_data['sections'].items():
            if section in _updated_data:
                for key, setting_data in section_data['settings'].items():
                    if key in _updated_data[section] and \
                            _updated_data[section][key][1] and \
                            'onChange' in setting_data:
                        status = self.call_function(setting_data['onChange'], section, key, _config_obj)
                        if status is None:
                            results += '<li>[{}][{}] implemented</li>'.format(section, key)
                        else:
                            results += '<li>[{}][{}] {}</li>'.format(section, key, status)
        return results

    def call_ondefnload(self, _defn):
        for module in list(_defn.keys()):
            for section in list(_defn[module]['sections'].keys()):
                for key, settings in list(_defn[module]['sections'][section]['settings'].items()):
                    if 'onDefnLoad' in settings:
                        self.call_ondefnload_function(settings['onDefnLoad'], section, key, self.config, _defn)

    def call_function(self, _func_str, _section, _key, _config_obj):
        """ calls the function in the definition.  If relative path
            then assume module is relative to the plugins directory
        """
        mod_name, func_name = _func_str.rsplit('.', 1)
        if mod_name.startswith('.'):
            try:
                mod = importlib.import_module(
                    mod_name,
                    package=_config_obj.data['paths']['internal_plugins_pkg'])
            except ModuleNotFoundError as e:
                mod = importlib.import_module(
                    mod_name,
                    package=_config_obj.data['paths']['external_plugins_pkg'])
        else:
            mod = importlib.import_module(mod_name)
        func = getattr(mod, func_name)
        return func(_config_obj, _section, _key)

    def call_ondefnload_function(self, _func_str, _section, _key, _config, _defn):
        """ calls the function in the definition.  If relative path
            then assume module is relative to the plugins directory
        """
        mod_name, func_name = _func_str.rsplit('.', 1)
        if mod_name.startswith('.'):
            try:
                mod = importlib.import_module(
                    mod_name,
                    package=_config['paths']['internal_plugins_pkg'])
            except ModuleNotFoundError as e:
                mod = importlib.import_module(
                    mod_name,
                    package=_config['paths']['external_plugins_pkg'])
        else:
            mod = importlib.import_module(mod_name)
        func = getattr(mod, func_name)
        return func(_defn, _config, _section, _key)

    def save_defn_to_db(self, _delta_defn=None):
        if _delta_defn:
            delta_defn = _delta_defn
        else:
            delta_defn = self.config_defn
        for area, area_data in delta_defn.items():
            if 'icon' in area_data:
                self.db.add_area(area, area_data)
            for section, section_data in area_data['sections'].items():
                if self.is_instance_defn:
                    self.db.add_instance(area, section, section_data)
                else:
                    self.db.add_section(area, section, section_data)

    def save_instance_defn_to_db(self, _delta_defn=None):
        if _delta_defn:
            delta_defn = _delta_defn
        else:
            delta_defn = self.config_defn
        for area, area_data in delta_defn.items():
            if 'icon' in area_data:
                self.db.add_area(area, area_data)
            for section, section_data in area_data['sections'].items():
                self.db.add_instance(area, section, section_data)

    def get_type(self, _section, _key, _value):
        """ Returns the expected type of the setting
        """
        for module in list(self.config_defn.keys()):
            for section in list(self.config_defn[module]['sections'].keys()):
                if section == _section:
                    for setting in list(self.config_defn[module]['sections'][section]['settings'].keys()):
                        if setting == _key:
                            return self.config_defn[module]['sections'][section]['settings'][setting]['type']
        return None

    def validate_list_item(self, _section, _key, _value):
        """ for list settings, will determine if the value
            is in the list
        """
        for module in list(self.config_defn.keys()):
            for section in list(self.config_defn[module]['sections'].keys()):
                if section == _section:
                    for setting in list(self.config_defn[module]['sections'][section]['settings'].keys()):
                        if setting == _key:
                            if _value in str(
                                    self.config_defn[module]['sections'][section]['settings'][setting]['values']):
                                return True
                            else:
                                return False
        return None

    def update_restricted_items(self, _defn_file):
        for area, area_data in _defn_file.items():
            self.update_restricted_items_area(area_data)

    def update_restricted_items_area(self, _defn_area):
        for section, section_data in _defn_area['sections'].items():
            for key, settings in section_data['settings'].items():
                if settings['level'] == 4:
                    self.restricted_items.append([section, key])
                elif 'hidden' in settings and settings['hidden']:
                    self.restricted_items.append([section, key])

    def get_restricted_items(self):
        if not self.restricted_items:
            area_list = self.db.get_areas()
            for area in area_list:
                area_dict = self.get_defn(area)
                self.update_restricted_items_area(area_dict)
        return self.restricted_items

    @property
    def defn_path(self):
        return CONFIG_DEFN_PATH

    def terminate(self):
        self.db.close()
        self.config_defn = None
        self.config = None
        self.db = None
        self.restricted_items = None
        self.logger.debug('Database terminated for thread:{}'.format(threading.get_ident()))