Refactor NGLcm init method 42/13042/3
authorPatricia Reinoso <patricia.reinoso@canonical.com>
Thu, 2 Mar 2023 08:53:58 +0000 (08:53 +0000)
committerGulsum Atici <gulsum.atici@canonical.com>
Fri, 3 Mar 2023 09:32:48 +0000 (12:32 +0300)
Creation of inner methods
to increase readability.

Change-Id: I542ee2752fb5b870ccf20513393dc696bc8e5939
Signed-off-by: Patricia Reinoso <patricia.reinoso@canonical.com>
osm_lcm/nglcm.py

index 6972477..ee758cb 100644 (file)
@@ -2,7 +2,6 @@
 # -*- coding: utf-8 -*-
 
 ##
-# Copyright 2018 Telefonica S.A.
 #
 # Licensed under the Apache License, Version 2.0 (the "License"); you may
 # not use this file except in compliance with the License. You may obtain
@@ -17,7 +16,6 @@
 # under the License.
 ##
 
-
 import asyncio
 import getopt
 import logging
@@ -25,11 +23,10 @@ import logging.handlers
 import sys
 import yaml
 
-from osm_lcm.lcm_utils import LcmException
-
 from osm_common.dbbase import DbException
 from osm_lcm.data_utils.database.database import Database
 from osm_lcm.data_utils.lcm_config import LcmCfg
+from osm_lcm.lcm_utils import LcmException
 from os import path
 from temporalio import workflow
 from temporalio.client import Client
@@ -39,66 +36,82 @@ from temporalio.worker import Worker
 class NGLcm:
     main_config = LcmCfg()
 
-    def __init__(self, config_file, loop=None):
+    def __init__(self, config_file):
         """
         Init, Connect to database, filesystem storage, and messaging
-        :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
+        :param config_file: two level dictionary with configuration. Top level should contain 'database', 'storage',
         :return: None
         """
         self.db = None
-
-        # logging
         self.logger = logging.getLogger("lcm")
-        # load configuration
-        config = self.read_config_file(config_file)
+        self._load_configuration(config_file)
+        self._configure_logging()
+
+        try:
+            self.db = Database(self.main_config.to_dict()).instance.db
+        except DbException as e:
+            self.logger.critical(str(e), exc_info=True)
+            raise LcmException(str(e))
+
+    def _load_configuration(self, config_file):
+        config = self._read_config_file(config_file)
         self.main_config.set_from_dict(config)
         self.main_config.transform()
         self.main_config.load_from_env()
         self.logger.critical("Loaded configuration:" + str(self.main_config.to_dict()))
 
-        # logging
+    def _read_config_file(self, config_file):
+        try:
+            with open(config_file) as f:
+                return yaml.safe_load(f)
+        except Exception as e:
+            self.logger.critical("At config file '{}': {}".format(config_file, e))
+            exit(1)
+
+    @staticmethod
+    def _get_log_formatter_simple():
         log_format_simple = (
             "%(asctime)s %(levelname)s %(name)s %(filename)s:%(lineno)s %(message)s"
         )
-        log_formatter_simple = logging.Formatter(
-            log_format_simple, datefmt="%Y-%m-%dT%H:%M:%S"
+        return logging.Formatter(log_format_simple, datefmt="%Y-%m-%dT%H:%M:%S")
+
+    def _create_file_handler(self):
+        return logging.handlers.RotatingFileHandler(
+            self.main_config.globalConfig.logfile,
+            maxBytes=100e6,
+            backupCount=9,
+            delay=0,
         )
-        if self.main_config.globalConfig.logfile:
-            file_handler = logging.handlers.RotatingFileHandler(
-                self.main_config.globalConfig.logfile,
-                maxBytes=100e6,
-                backupCount=9,
-                delay=0,
-            )
-            file_handler.setFormatter(log_formatter_simple)
-            self.logger.addHandler(file_handler)
-        if not self.main_config.globalConfig.to_dict()["nologging"]:
-            str_handler = logging.StreamHandler()
-            str_handler.setFormatter(log_formatter_simple)
-            self.logger.addHandler(str_handler)
 
-        if self.main_config.globalConfig.to_dict()["loglevel"]:
-            self.logger.setLevel(self.main_config.globalConfig.loglevel)
-
-        # logging other modules
-        for logger in ("message", "database", "storage", "tsdb"):
+    def _log_other_modules(self):
+        for logger in ("message", "database", "storage", "tsdb", "temporal"):
             logger_config = self.main_config.to_dict()[logger]
             logger_module = logging.getLogger(logger_config["logger_name"])
             if logger_config["logfile"]:
                 file_handler = logging.handlers.RotatingFileHandler(
                     logger_config["logfile"], maxBytes=100e6, backupCount=9, delay=0
                 )
-                file_handler.setFormatter(log_formatter_simple)
+                file_handler.setFormatter(self._get_log_formatter_simple())
                 logger_module.addHandler(file_handler)
             if logger_config["loglevel"]:
                 logger_module.setLevel(logger_config["loglevel"])
-        self.logger.critical("starting osm/nglcm")
 
-        try:
-            self.db = Database(self.main_config.to_dict()).instance.db
-        except DbException as e:
-            self.logger.critical(str(e), exc_info=True)
-            raise LcmException(str(e))
+    def _configure_logging(self):
+        if self.main_config.globalConfig.logfile:
+            file_handler = self._create_file_handler()
+            file_handler.setFormatter(self._get_log_formatter_simple())
+            self.logger.addHandler(file_handler)
+
+        if not self.main_config.globalConfig.to_dict()["nologging"]:
+            str_handler = logging.StreamHandler()
+            str_handler.setFormatter(self._get_log_formatter_simple())
+            self.logger.addHandler(str_handler)
+
+        if self.main_config.globalConfig.to_dict()["loglevel"]:
+            self.logger.setLevel(self.main_config.globalConfig.loglevel)
+
+        self._log_other_modules()
+        self.logger.critical("starting osm/nglcm")
 
     async def start(self):
         # do some temporal stuff here
@@ -124,14 +137,6 @@ class NGLcm:
 
         await worker.run()
 
-    def read_config_file(self, config_file):
-        try:
-            with open(config_file) as f:
-                return yaml.safe_load(f)
-        except Exception as e:
-            self.logger.critical("At config file '{}': {}".format(config_file, e))
-            exit(1)
-
 
 @workflow.defn
 class Heartbeat: