]> Frank Brehm's Git Trees - pixelpark/puppet-tools.git/commitdiff
Adding module dpx_puppettools.app for class BaseDPXPuppetApplication
authorFrank Brehm <frank.brehm@pixelpark.com>
Mon, 6 Feb 2023 09:12:35 +0000 (10:12 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Mon, 6 Feb 2023 09:12:35 +0000 (10:12 +0100)
lib/dpx_puppettools/app/__init__.py [new file with mode: 0644]

diff --git a/lib/dpx_puppettools/app/__init__.py b/lib/dpx_puppettools/app/__init__.py
new file mode 100644 (file)
index 0000000..a4eb4bd
--- /dev/null
@@ -0,0 +1,119 @@
+# -*- coding: utf-8 -*-
+"""
+@author: Frank Brehm
+@contact: frank.brehm@pixelpark.com
+@copyright: © 2023 by Frank Brehm, Berlin
+@summary: A base module for all DPX puppet tools application classes
+"""
+from __future__ import absolute_import
+
+# Standard modules
+import logging
+import shutil
+
+# Third party modules
+from fb_tools.cfg_app import FbConfigApplication
+from fb_tools.errors import FbAppError
+from fb_tools.multi_config import BaseMultiConfig
+
+# Own modules
+from .. import __version__ as GLOBAL_VERSION
+from .. import DEFAULT_CONFIG_DIR
+from .. import DEFAULT_TERMINAL_WIDTH, DEFAULT_TERMINAL_HEIGHT
+
+from ..xlate import XLATOR
+
+LOG = logging.getLogger(__name__)
+
+_ = XLATOR.gettext
+ngettext = XLATOR.ngettext
+
+__version__ = '0.1.0'
+
+
+# =============================================================================
+class DPXPuppetAppError(FbAppError):
+    """ Base exception class for all exceptions in all application classes."""
+    pass
+
+
+# =============================================================================
+class AbortAppError(DPXAppError):
+    """Special exception class interrupting the application."""
+    pass
+
+
+# =============================================================================
+class BaseDPXPuppetApplication(FbConfigApplication):
+    """
+    Base class for all DPX application classes.
+    """
+
+    show_assume_options = True
+    show_console_timeout_option = True
+    # show_force_option = False
+    show_simulate_option = True
+
+    # -------------------------------------------------------------------------
+    def __init__(
+        self, appname=None, verbose=0, version=GLOBAL_VERSION, base_dir=None,
+            cfg_class=BaseMultiConfig, initialized=False, usage=None, description=None,
+            argparse_epilog=None, argparse_prefix_chars='-', env_prefix=None,
+            config_dir=DEFAULT_CONFIG_DIR):
+
+        super(BaseDPXPuppetApplication, self).__init__(
+            appname=appname, verbose=verbose, version=version, base_dir=base_dir,
+            description=description, cfg_class=cfg_class, initialized=False,
+            argparse_epilog=argparse_epilog, argparse_prefix_chars=argparse_prefix_chars,
+            env_prefix=env_prefix, config_dir=config_dir
+        )
+
+    # -------------------------------------------------------------------------
+    def line(self, width=None, linechar='-', color=None):
+        """Print out an line on stdout, if not in quiet mode."""
+        if self.quiet:
+            return
+
+        lchar = str(linechar).strip()
+        if not lchar:
+            lchar = '-'
+
+        if not width:
+            term_size = shutil.get_terminal_size((DEFAULT_TERMINAL_WIDTH, DEFAULT_TERMINAL_HEIGHT))
+            width = term_size.columns
+
+        lin = (lchar * width)[0:width]
+        if color:
+            lin = self.colored(lin, color)
+        print(lin)
+
+    # -------------------------------------------------------------------------
+    def empty_line(self):
+        """Print out an empty line on stdout, if not in quiet mode."""
+        if not self.quiet:
+            print()
+
+    # -------------------------------------------------------------------------
+    def post_init(self):
+        """
+        Method to execute before calling run(). Here could be done some
+        finishing actions after reading in commandline parameters,
+        configuration a.s.o.
+
+        This method could be overwritten by descendant classes, these
+        methhods should allways include a call to post_init() of the
+        parent class.
+
+        """
+
+        self.initialized = False
+
+        super(BaseDPXPuppetApplication, self).post_init()
+
+        if self.logfile:
+            LOG.debug(_("Using logfile {!r}.").format(str(self.logfile)))
+        else:
+            LOG.debug(_("Don't using a logfile."))
+
+
+# vim: ts=4 et list