From 32b5c90ccc6f01050cf4bfd114fcbabf479547f9 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Tue, 23 Oct 2018 12:29:58 +0200 Subject: [PATCH] Changing lib/cr_vmware_tpl/config.py to base on BaseConfiguration in fb_tools.config --- lib/cr_vmware_tpl/app.py | 20 +++++--- lib/cr_vmware_tpl/config.py | 99 +++++++------------------------------ python_fb_tools | 2 +- 3 files changed, 33 insertions(+), 88 deletions(-) diff --git a/lib/cr_vmware_tpl/app.py b/lib/cr_vmware_tpl/app.py index 284e554..542c9c2 100644 --- a/lib/cr_vmware_tpl/app.py +++ b/lib/cr_vmware_tpl/app.py @@ -67,6 +67,8 @@ class CrTplApplication(BaseApplication): which can be used to spawn different virtual machines. """).strip() + self.config = None + super(CrTplApplication, self).__init__( appname=appname, verbose=verbose, @@ -78,9 +80,6 @@ class CrTplApplication(BaseApplication): #self.initialized = False - #self.config = CrTplConfiguration( - # appname=self.appname, verbose=self.verbose, base_dir=self.base_dir) - #self.handler = CrTplHandler( # appname=self.appname, verbose=self.verbose, base_dir=self.base_dir) @@ -167,10 +166,17 @@ class CrTplApplication(BaseApplication): """ + self.initialized = False + self.init_logging() -# self.config.read() -# if self.config.verbose > self.verbose: -# self.verbose = self.config.verbose + + self.config = CrTplConfiguration( + appname=self.appname, verbose=self.verbose, base_dir=self.base_dir) + + self.config.read() + if self.config.verbose > self.verbose: + self.verbose = self.config.verbose + self.config.initialized = True self.perform_arg_parser() @@ -186,7 +192,7 @@ class CrTplApplication(BaseApplication): # self.handler.abort = True # self.handler.initialized = True -# self.initialized = True + self.initialized = True # ------------------------------------------------------------------------- def init_arg_parser(self): diff --git a/lib/cr_vmware_tpl/config.py b/lib/cr_vmware_tpl/config.py index 38eab36..735c340 100644 --- a/lib/cr_vmware_tpl/config.py +++ b/lib/cr_vmware_tpl/config.py @@ -16,22 +16,15 @@ import re # Third party modules import six -from six import StringIO -from six.moves import configparser - -from configparser import Error as ConfigParseError - # Own modules -from .errors import PpError - -from .obj import PpBaseObject +from fb_tools.config import ConfigError, BaseConfiguration -__version__ = '0.5.7' +__version__ = '0.6.1' LOG = logging.getLogger(__name__) # ============================================================================= -class ConfigError(PpError): +class CrTplConfigError(ConfigError): """Base error class for all exceptions happened during execution this configured application""" @@ -39,7 +32,7 @@ class ConfigError(PpError): # ============================================================================= -class CrTplConfiguration(PpBaseObject): +class CrTplConfiguration(BaseConfiguration): """ A class for providing a configuration for the CrTplApplication class and methods to read it from configuration files. @@ -87,8 +80,6 @@ class CrTplConfiguration(PpBaseObject): self.excluded_datastores = [] - self.encoding = 'utf-8' - super(CrTplConfiguration, self).__init__( appname=appname, verbose=verbose, @@ -97,9 +88,6 @@ class CrTplConfiguration(PpBaseObject): initialized=False, ) - self.config_dir = os.path.join(self.base_dir, 'etc') - self.config_file = os.path.join(self.config_dir, self.appname + '.ini') - # Workaround, bis das Lesen der Config implementiert ist self.vsphere_host = 'test-vcsa01.pixelpark.net' self.vsphere_user = 'Administrator@vsphere.local' @@ -169,79 +157,30 @@ class CrTplConfiguration(PpBaseObject): return res # ------------------------------------------------------------------------- - def read(self): - """Reading the configuration file.""" - - if self.verbose > 2: - LOG.debug("Searching for {!r} ...".format(self.config_file)) - if not os.path.isfile(self.config_file): - LOG.warn("Config file {!r} not found.".format(self.config_file)) - return + def eval_config_section(self, config, section_name): - open_opts = {} - if six.PY3 and self.encoding: - open_opts['encoding'] = self.encoding - open_opts['errors'] = 'surrogateescape' + super(CrTplConfiguration, self).eval_config_section(config, section_name) - if self.verbose > 1: - LOG.debug("Reading {!r} ...".format(self.config_file)) - - config = configparser.ConfigParser() - try: - with open(self.config_file, 'r', **open_opts) as fh: - stream = StringIO("[default]\n" + fh.read()) - if six.PY2: - config.readfp(stream) - else: - config.read_file(stream) - except ConfigParseError as e: - msg = "Wrong configuration in {!r} found: ".format(self.config_file) - msg += str(e) - self.handle_error(msg, "Configuration error") + if section_name.lower() == 'vsphere': + self._eval_config_vsphere(config, section_name) + return + if section_name.lower() == 'template': + self._eval_config_template(config, section_name) return - self.eval_config(config) - - # ------------------------------------------------------------------------- - def eval_config(self, config): - """Evaluating of all found configuration options.""" - - for section in config.sections(): - - if self.verbose > 2: - LOG.debug("Options of section {!r} ...".format(section)) - - if section.lower() == 'default' or section.lower() == 'global': - self._eval_config_global(config, section) - elif section.lower() == 'vsphere': - self._eval_config_vsphere(config, section) - elif section.lower() == 'template': - self._eval_config_template(config, section) - - # ------------------------------------------------------------------------- - def _eval_config_global(self, config, section): - - if self.verbose > 2: - LOG.debug("Checking config section {!r} ...".format(section)) - - for (key, value) in config.items(section): - if key.lower() == 'verbose': - val = int(value) - if val > self.verbose: - self.verbose = val - - return + if self.verbose > 1: + LOG.debug("Unhandled configuration section {!r}.".format(section_name)) # ------------------------------------------------------------------------- - def _eval_config_vsphere(self, config, section): + def _eval_config_vsphere(self, config, section_name): if self.verbose > 2: - LOG.debug("Checking config section {!r} ...".format(section)) + LOG.debug("Checking config section {!r} ...".format(section_name)) re_excl_ds = re.compile(r'^\s*excluded?[-_]datastores?\s*$', re.IGNORECASE) re_split_ds = re.compile(r'[,;\s]+') - for (key, value) in config.items(section): + for (key, value) in config.items(section_name): if key.lower() == 'host': self.vsphere_host = value @@ -284,12 +223,12 @@ class CrTplConfiguration(PpBaseObject): return # ------------------------------------------------------------------------- - def _eval_config_template(self, config, section): + def _eval_config_template(self, config, section_name): if self.verbose > 2: - LOG.debug("Checking config section {!r} ...".format(section)) + LOG.debug("Checking config section {!r} ...".format(section_name)) - for (key, value) in config.items(section): + for (key, value) in config.items(section_name): if key.lower() == 'vm': self.template_vm = value continue diff --git a/python_fb_tools b/python_fb_tools index 9e480d2..6411197 160000 --- a/python_fb_tools +++ b/python_fb_tools @@ -1 +1 @@ -Subproject commit 9e480d2af8e47fac04e349dcde092a036f583134 +Subproject commit 64111976cd55a439b5b63de14945acc75041b90c -- 2.39.5