from .obj import PpBaseObject
-__version__ = '0.1.0'
+__version__ = '0.2.2'
LOG = logging.getLogger(__name__)
self.template_vm = self.default_template_vm
self.template_name = self.default_template_name
+ 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
+
+ open_opts = {}
+ if six.PY3 and self.encoding:
+ open_opts['encoding'] = self.encoding
+ open_opts['errors'] = 'surrogateescape'
+
+ 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(cfg_file)
+ msg += str(e)
+ self.handle_error(msg, "Configuration error")
+ 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 if section.lower() == 'global':
+
+ for (key, value) in config.items(section):
+
+ if self.verbose > 2:
+ LOG.debug("Key {k!r}, value {v!r}.".format(k=key, v=value))
+
+ if key.lower() == 'verbose':
+ val = int(value)
+ if val > self.verbose:
+ self.verbose = val
+
+
+ if section.lower() == 'vsphere':
+
+ for (key, value) in config.items(section):
+
+ if self.verbose > 2:
+ LOG.debug("Key {k!r}, value {v!r}.".format(k=key, v=value))
+
+ if key.lower() == 'host':
+ self.vsphere_host = value
+ continue
+ if key.lower() == 'port':
+ self.vsphere_port = int(value)
+ continue
+ if key.lower() == 'user':
+ self.vsphere_user = value
+ continue
+ if key.lower() == 'password':
+ self.password = value
+ continue
+ if key.lower() == 'folder':
+ self.folder = value
+
+ continue
+
+ if section.lower() == 'template':
+
+ for (key, value) in config.items(section):
+ if self.verbose > 2:
+ LOG.debug("Key {k!r}, value {v!r}.".format(k=key, v=value))
+
+ if key.lower() == 'vm':
+ self.template_vm = value
+ continue
+ if key.lower() == 'name':
+ self.template_name = value
+
# =============================================================================