""" 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 += '