]> Frank Brehm's Git Trees - pixelpark/create-vmware-tpl.git/commitdiff
Moving CrTplApplication in module cr_vmware_tpl.app to a FbConfigApplication.
authorFrank Brehm <frank@brehm-online.com>
Tue, 21 Jun 2022 16:15:24 +0000 (18:15 +0200)
committerFrank Brehm <frank@brehm-online.com>
Tue, 21 Jun 2022 16:15:24 +0000 (18:15 +0200)
lib/cr_vmware_tpl/app.py

index 302cdd5ddaa1d3376735db66f02a2678da9052cb..86967955c1cce255fc7b5d8381ad75cf1c2a4e85 100644 (file)
@@ -25,7 +25,7 @@ from fb_logging.colored import ColoredFormatter
 
 from fb_tools.common import pp
 
-from fb_tools.app import BaseApplication
+from fb_tools.cfg_app import FbConfigApplication
 
 from fb_tools.errors import FbAppError, ExpectedHandlerError
 
@@ -38,7 +38,7 @@ from .xlate import __base_dir__ as __xlate_base_dir__
 from .xlate import __mo_file__ as __xlate_mo_file__
 from .xlate import XLATOR, LOCALE_DIR, DOMAIN
 
-__version__ = '1.4.0'
+__version__ = '1.5.0'
 LOG = logging.getLogger(__name__)
 
 _ = XLATOR.gettext
@@ -105,7 +105,7 @@ class CfgFileOptionAction(argparse.Action):
 
 
 # =============================================================================
-class CrTplApplication(BaseApplication):
+class CrTplApplication(FbConfigApplication):
     """
     Class for the application objects.
     """
@@ -123,22 +123,13 @@ class CrTplApplication(BaseApplication):
             "Creates in the given VSphere environment and cluster a template object, "
             "which can be used to spawn different virtual machines.")
 
-        self._cfg_file = None
-        self.config = None
-
         super(CrTplApplication, self).__init__(
             appname=appname, verbose=verbose, version=version, base_dir=base_dir,
-            description=desc, initialized=False,
+            cfg_class=CrTplConfiguration, description=desc, initialized=False,
         )
 
         self.initialized = True
 
-    # -------------------------------------------------------------------------
-    @property
-    def cfg_file(self):
-        """Configuration file."""
-        return self._cfg_file
-
     # -------------------------------------------------------------------------
     def as_dict(self, short=True):
         """
@@ -152,7 +143,6 @@ class CrTplApplication(BaseApplication):
         """
 
         res = super(CrTplApplication, self).as_dict(short=short)
-        res['cfg_file'] = self.cfg_file
 
         if 'xlate' not in res:
             res['xlate'] = {}
@@ -242,32 +232,18 @@ class CrTplApplication(BaseApplication):
 
         self.initialized = False
 
-        self.init_logging()
-
-        self.perform_arg_parser()
-
-        self.config = CrTplConfiguration(
-            appname=self.appname, verbose=self.verbose, base_dir=self.base_dir,
-            config_file=self.cfg_file)
-
-        self.config.read()
-        if self.config.verbose > self.verbose:
-            self.verbose = self.config.verbose
-        self.config.initialized = True
-
-        if self.verbose > 3:
-            LOG.debug(_("Read configuration:") + '\n' + pp(self.config.as_dict()))
+        super(CrTplApplication, self).post_init()
 
         self.perform_arg_parser_vmware()
 
-        if not self.config.vsphere_info.password:
+        if not self.cfg.vsphere_info.password:
             prompt = (_('Enter password for host {h!r} and user {u!r}:') + ' ').format(
-                h=self.config.vsphere_info.host, u=self.config.vsphere_info.user)
-            self.config.vsphere_info.password = getpass.getpass(prompt=prompt)
+                h=self.cfg.vsphere_info.host, u=self.cfg.vsphere_info.user)
+            self.cfg.vsphere_info.password = getpass.getpass(prompt=prompt)
 
         self.handler = CrTplHandler(
             appname=self.appname, verbose=self.verbose, base_dir=self.base_dir,
-            simulate=self.simulate, force=self.force, config=self.config,
+            simulate=self.simulate, force=self.force, cfg=self.cfg,
             terminal_has_colors=self.terminal_has_colors)
 
         if self.args.rotate:
@@ -287,20 +263,12 @@ class CrTplApplication(BaseApplication):
 
         super(CrTplApplication, self).init_arg_parser()
 
-        default_cfg_file = self.base_dir.joinpath('etc').joinpath(self.appname + '.ini')
-
         self.arg_parser.add_argument(
             '-A', '--abort', dest='abort', action='store_true',
             help=_(
                 "Abort creation of VMWare template after successsful creation of template VM."),
         )
 
-        self.arg_parser.add_argument(
-            '-c', '--config', '--config-file', dest='cfg_file', metavar=_('FILE'),
-            action=CfgFileOptionAction,
-            help=_("Configuration file (default: {!r}).").format(str(default_cfg_file))
-        )
-
         vmware_group = self.arg_parser.add_argument_group(_('VMWare options'))
 
         vmware_group.add_argument(
@@ -333,7 +301,7 @@ class CrTplApplication(BaseApplication):
         )
 
         vmware_group.add_argument(
-            '-C', '--cluster', dest='cluster',
+            '-c', '--cluster', dest='cluster',
             help=_(
                 "Host cluster in VSphere, where to create the template (Default: {!r}).").format(
                 CrTplConfiguration.default_vsphere_cluster)
@@ -364,8 +332,8 @@ class CrTplApplication(BaseApplication):
     # -------------------------------------------------------------------------
     def perform_arg_parser(self):
 
-        if self.args.cfg_file:
-            self._cfg_file = self.args.cfg_file
+        if self.verbose > 2:
+            LOG.debug(_("Got command line arguments:") + '\n' + pp(self.args))
 
     # -------------------------------------------------------------------------
     def perform_arg_parser_vmware(self):
@@ -375,25 +343,25 @@ class CrTplApplication(BaseApplication):
         """
 
         if self.args.host:
-            self.config.vsphere_info.host = self.args.host
+            self.cfg.vsphere_info.host = self.args.host
         if self.args.port:
-            self.config.vsphere_info.port = self.args.port
+            self.cfg.vsphere_info.port = self.args.port
         if self.args.user:
-            self.config.vsphere_info.user = self.args.user
+            self.cfg.vsphere_info.user = self.args.user
         if self.args.password:
-            self.config.vsphere_info.password = self.args.password
+            self.cfg.vsphere_info.password = self.args.password
         if self.args.cluster:
-            self.config.vsphere_cluster = self.args.cluster
+            self.cfg.vsphere_cluster = self.args.cluster
         if self.args.folder:
-            self.config.folder = self.args.folder
+            self.cfg.folder = self.args.folder
         if self.args.os_id:
-            self.config.os_id = self.args.os_id
-            if not self.config.template_name_given:
-                self.config.template_name = self.args.os_id + '-template'
-            if not self.config.cobbler_profile_given:
-                self.config.cobbler_profile = 'vmware-template-' + self.args.os_id
+            self.cfg.os_id = self.args.os_id
+            if not self.cfg.template_name_given:
+                self.cfg.template_name = self.args.os_id + '-template'
+            if not self.cfg.cobbler_profile_given:
+                self.cfg.cobbler_profile = 'vmware-template-' + self.args.os_id
         if self.args.number is not None:
-            self.config.max_nr_templates_stay = self.args.number
+            self.cfg.max_nr_templates_stay = self.args.number
 
     # -------------------------------------------------------------------------
     def _run(self):