|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +| **@created on:** 09/08/17, |
| 4 | +| **@author:** prathyushsp, |
| 5 | +| **@version:** v0.0.1 |
| 6 | +| |
| 7 | +| **Description:** |
| 8 | +| |
| 9 | +| |
| 10 | +| **Sphinx Documentation Status:** Complete |
| 11 | +| |
| 12 | +..todo:: |
| 13 | + -- |
| 14 | +""" |
| 15 | + |
| 16 | +from typeguard import typechecked |
| 17 | +from collections import OrderedDict |
| 18 | +import os |
| 19 | +from typing import Union |
| 20 | +import logging |
| 21 | +import json |
| 22 | +from . import setup_logging |
| 23 | +from module import Singleton |
| 24 | +from module import string_constants as constants |
| 25 | + |
| 26 | +logger = logging.getLogger(__name__) |
| 27 | + |
| 28 | + |
| 29 | +class CommonConfig(object): |
| 30 | + """ |
| 31 | + | **@author:** Prathyush SP |
| 32 | + | Common Configuration |
| 33 | + """ |
| 34 | + |
| 35 | + # todo: Prathyush SP: Convert keys to string constants |
| 36 | + @typechecked |
| 37 | + def __init__(self, common_config: dict): |
| 38 | + try: |
| 39 | + |
| 40 | + self.LOG_LEVEL = common_config['log_level'] |
| 41 | + self.PATH_LOG = common_config['path']['logs'] |
| 42 | + self._GLOBAL_LOGGING_CONFIG_FILE_PATH = os.path.join("/".join(__file__.split('/')[:-1]), 'config', |
| 43 | + 'module_logging.yaml') |
| 44 | + self.PYTHON_OPTIMISE = common_config['python_optimise'] |
| 45 | + os.environ['PYTHONOPTIMIZE'] = str(self.PYTHON_OPTIMISE) |
| 46 | + except KeyError as ke: |
| 47 | + raise Exception('Key Error. Config Error', ke) |
| 48 | + |
| 49 | + |
| 50 | +class MetadataConfig(object): |
| 51 | + """ |
| 52 | + | **@author:** Prathyush SP |
| 53 | + | |
| 54 | + | Metadata Configuration Manager |
| 55 | + """ |
| 56 | + |
| 57 | + @typechecked |
| 58 | + def __init__(self, metadata_config: Union[dict, OrderedDict]): |
| 59 | + self.METADATA = metadata_config |
| 60 | + |
| 61 | + |
| 62 | +class ConfigManager(metaclass=Singleton): |
| 63 | + """ |
| 64 | + | **@author:** Prathyush SP |
| 65 | + | |
| 66 | + | DL Configuration Manager |
| 67 | + """ |
| 68 | + |
| 69 | + @typechecked |
| 70 | + def __init__(self, config_file_path: str): |
| 71 | + # todo: Test Support for multiple dl frameworks |
| 72 | + global MODULE_CONFIG_DATA |
| 73 | + try: |
| 74 | + MODULE_CONFIG_DATA = json.load(open(config_file_path), object_pairs_hook=OrderedDict) |
| 75 | + except Exception as e: |
| 76 | + logger.critical( |
| 77 | + 'Configuration file path error. Please provide configuration file path: {}'.format(config_file_path)) |
| 78 | + raise Exception( |
| 79 | + 'Configuration file path error. Please provide configuration file path: ' + config_file_path, e) |
| 80 | + try: |
| 81 | + self.CommonConfig = CommonConfig(MODULE_CONFIG_DATA['common_config']) |
| 82 | + self.MetadataConfig = MetadataConfig(MODULE_CONFIG_DATA['metadata_config']) |
| 83 | + except KeyError as ke: |
| 84 | + raise Exception('Key not found. ', ke) |
| 85 | + |
| 86 | + def get_config_manager(self): |
| 87 | + """ |
| 88 | + | **@author:** Prathyush SP |
| 89 | + | |
| 90 | + | Get Configuration Manager |
| 91 | + :return: Configuration Manager |
| 92 | + """ |
| 93 | + return self |
| 94 | + |
| 95 | + @typechecked |
| 96 | + def update_config_manager(self, config_file_path: str): |
| 97 | + """ |
| 98 | + | **@author:** Prathyush SP |
| 99 | + | |
| 100 | + | Update Configuration Manager |
| 101 | + :param config_file_path: Configuration file path |
| 102 | + """ |
| 103 | + logger.info("Updating Module Configuration - Config File: {}".format(config_file_path)) |
| 104 | + self.__init__(config_file_path=config_file_path) |
| 105 | + |
| 106 | + def update_logging_config_manager(self, config_file_path: str): |
| 107 | + """ |
| 108 | + | **@author:** Prathyush SP |
| 109 | + | |
| 110 | + | Update Logging Configuration |
| 111 | + :param config_file_path: Configuration file path |
| 112 | + """ |
| 113 | + logger.info("Updating Library Logging configuration - Config File:{}".format(config_file_path)) |
| 114 | + setup_logging(default_path=config_file_path) |
| 115 | + self.CommonConfig._GLOBAL_LOGGING_CONFIG_FILE_PATH = config_file_path |
| 116 | + |
| 117 | + |
| 118 | +ConfigPath = os.path.join("/".join(__file__.split('/')[:-1]), 'config', 'module_config.json') |
| 119 | +MODULE_CONFIG = ConfigManager(config_file_path=ConfigPath).get_config_manager() |
| 120 | +MODULE_CONFIG_DATA = OrderedDict() |
0 commit comments