which can be used to spawn different virtual machines.
""").strip()
+ self.config = None
+
super(CrTplApplication, self).__init__(
appname=appname,
verbose=verbose,
#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)
"""
+ 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()
# self.handler.abort = True
# self.handler.initialized = True
-# self.initialized = True
+ self.initialized = True
# -------------------------------------------------------------------------
def init_arg_parser(self):
# 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"""
# =============================================================================
-class CrTplConfiguration(PpBaseObject):
+class CrTplConfiguration(BaseConfiguration):
"""
A class for providing a configuration for the CrTplApplication class
and methods to read it from configuration files.
self.excluded_datastores = []
- self.encoding = 'utf-8'
-
super(CrTplConfiguration, self).__init__(
appname=appname,
verbose=verbose,
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'
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
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