From: Frank Brehm Date: Tue, 20 Mar 2018 17:05:08 +0000 (+0100) Subject: Reading configuration X-Git-Tag: 0.1.1~104 X-Git-Url: https://git.uhu-banane.org/?a=commitdiff_plain;h=754ae2508402d3bc642028dbc98af86e2b3b7ac7;p=pixelpark%2Fcreate-vmware-tpl.git Reading configuration --- diff --git a/lib/cr_vmware_tpl/app.py b/lib/cr_vmware_tpl/app.py index f0e7372..67dfc67 100644 --- a/lib/cr_vmware_tpl/app.py +++ b/lib/cr_vmware_tpl/app.py @@ -32,7 +32,7 @@ from .obj import PpBaseObject from .config import CrTplConfiguration -__version__ = '0.2.2' +__version__ = '0.2.3' LOG = logging.getLogger(__name__) @@ -388,8 +388,12 @@ class CrTplApplication(PpBaseObject): """ - self.perform_arg_parser() self.init_logging() + self.config.read() + if self.config.verbose > self.verbose: + self.verbose = self.config.verbose + + self.perform_arg_parser() if not self.config.password: prompt = 'Enter password for host {h!r} and user {u!r}: '.format( diff --git a/lib/cr_vmware_tpl/config.py b/lib/cr_vmware_tpl/config.py index ee56dc6..0e3b444 100644 --- a/lib/cr_vmware_tpl/config.py +++ b/lib/cr_vmware_tpl/config.py @@ -26,7 +26,7 @@ from .errors import FunctionNotImplementedError, PpError from .obj import PpBaseObject -__version__ = '0.1.0' +__version__ = '0.2.2' LOG = logging.getLogger(__name__) @@ -65,6 +65,8 @@ class CrTplConfiguration(PpBaseObject): 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, @@ -73,6 +75,9 @@ 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' @@ -102,6 +107,98 @@ 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 + + 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 + # =============================================================================