From 7ebb5c5d2bf6a16802d79ceba51fdc5156facadd Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Tue, 14 Feb 2023 15:27:35 +0100 Subject: [PATCH] Unifying output --- lib/dpx_puppettools/__init__.py | 24 +++++++- lib/dpx_puppettools/app/__init__.py | 58 +++++++++--------- lib/dpx_puppettools/app/get_forge_module.py | 8 ++- lib/dpx_puppettools/forge/__init__.py | 7 ++- lib/dpx_puppettools/forge/base_module_info.py | 5 +- .../forge/cur_mod_release_info.py | 10 +++- lib/dpx_puppettools/forge/mod_info.py | 60 +++++++++---------- lib/dpx_puppettools/forge/mod_release_info.py | 5 +- lib/dpx_puppettools/forge/owner_info.py | 5 +- 9 files changed, 116 insertions(+), 66 deletions(-) diff --git a/lib/dpx_puppettools/__init__.py b/lib/dpx_puppettools/__init__.py index 0df5255..d0a1815 100644 --- a/lib/dpx_puppettools/__init__.py +++ b/lib/dpx_puppettools/__init__.py @@ -10,7 +10,7 @@ from pathlib import Path # Own modules import fb_tools.common -__version__ = '0.2.1' +__version__ = '0.2.2' MAX_PORT_NUMBER = (2 ** 16) - 1 DEFAULT_CONFIG_DIR = 'pixelpark' @@ -46,6 +46,28 @@ def pp(value, indent=4, width=None, depth=None): return fb_tools.common.pp(value, indent=indent, width=width, depth=depth) +# ============================================================================= +def print_out(msg, end='\n', file=None, flush=False): + + print(msg, end=end, file=file, flush=flush) + + +# ============================================================================= +def line(width=None, linechar='-', end='\n', file=None, flush=False): + + 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] + print(lin, end=end, file=file, flush=flush) + + # ============================================================================= # vim: ts=4 et list diff --git a/lib/dpx_puppettools/app/__init__.py b/lib/dpx_puppettools/app/__init__.py index 188c649..75c743d 100644 --- a/lib/dpx_puppettools/app/__init__.py +++ b/lib/dpx_puppettools/app/__init__.py @@ -9,7 +9,6 @@ from __future__ import absolute_import # Standard modules import logging -import shutil import copy import pipes import os @@ -45,7 +44,7 @@ LOG = logging.getLogger(__name__) _ = XLATOR.gettext ngettext = XLATOR.ngettext -__version__ = '0.3.1' +__version__ = '0.3.2' # ============================================================================= @@ -90,31 +89,6 @@ class BaseDPXPuppetApplication(FbConfigApplication): env_prefix=env_prefix, config_dir=config_dir ) - # ------------------------------------------------------------------------- - def line(self, width=None, linechar='-', color=None, end='\n', file=None, flush=False): - """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, end=end, file=file, flush=flush) - - # ------------------------------------------------------------------------- - def empty_line(self, file=None, flush=False): - """Print out an empty line on stdout, if not in quiet mode.""" - if not self.quiet: - print('', file=file, flush=flush) - # ------------------------------------------------------------------------- def post_init(self): """ @@ -378,6 +352,36 @@ class BaseDPXPuppetApplication(FbConfigApplication): p = Popen(cmd, stdin=PIPE, universal_newlines=True) p.communicate(mail.as_string()) + # ------------------------------------------------------------------------- + def print(self, msg, color=None, end='\n', file=None, flush=False): + + if color: + msg = self.colored(msg, color) + print_out(msg, end=end, file=file, flush=flush) + + # ------------------------------------------------------------------------- + def empty_line(self, end='\n', file=None, flush=False): + + print('', end=end, file=file, flush=flush) + + # ------------------------------------------------------------------------- + def line(self, width=None, linechar='-', color=None, end='\n', file=None, flush=False): + + 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, end=end, file=file, flush=flush) + + # ============================================================================= if __name__ == "__main__": diff --git a/lib/dpx_puppettools/app/get_forge_module.py b/lib/dpx_puppettools/app/get_forge_module.py index 5205b35..9d6eb0a 100644 --- a/lib/dpx_puppettools/app/get_forge_module.py +++ b/lib/dpx_puppettools/app/get_forge_module.py @@ -32,7 +32,7 @@ LOG = logging.getLogger(__name__) _ = XLATOR.gettext ngettext = XLATOR.ngettext -__version__ = '0.6.1' +__version__ = '0.6.2' # ============================================================================= @@ -160,10 +160,16 @@ class GetForgeModuleApplication(BaseDPXPuppetApplication): self.module_name = self.args.module_name[0] + LOG.debug(_("Creating a {} object ...").format('ForgeModuleInfo')) + self.module_info = ForgeModuleInfo( appname=self.appname, verbose=self.verbose, base_dir=self.base_dir, full_name=self.module_name, var_dir=var_dir, initialized=True,) + LOG.debug(_("Finished creating a {} object.").format('ForgeModuleInfo')) + + self.initialized = True + # ------------------------------------------------------------------------- def _run(self): diff --git a/lib/dpx_puppettools/forge/__init__.py b/lib/dpx_puppettools/forge/__init__.py index 1ea1084..0dc2b59 100644 --- a/lib/dpx_puppettools/forge/__init__.py +++ b/lib/dpx_puppettools/forge/__init__.py @@ -23,7 +23,7 @@ from ..xlate import XLATOR from ..errors import BaseModuleInfoError -__version__ = '0.3.0' +__version__ = '0.4.1' LOG = logging.getLogger(__name__) @@ -180,7 +180,7 @@ class BaseForgeObject(FbBaseObject): @classmethod def from_data(cls, data, appname=None, verbose=0, base_dir=None): - if verbose > 3: + if verbose > 2: LOG.debug(_("Trying to get data for {} from:").format( cls.__name__) + '\n' + pp(data)) @@ -198,6 +198,9 @@ class BaseForgeObject(FbBaseObject): # ------------------------------------------------------------------------- def apply_data(self, data): + if self.verbose > 2: + LOG.debug(_("Applying data ...")) + if 'slug' in data: self.slug = data['slug'] if 'uri' in data: diff --git a/lib/dpx_puppettools/forge/base_module_info.py b/lib/dpx_puppettools/forge/base_module_info.py index e12622a..15f19d7 100644 --- a/lib/dpx_puppettools/forge/base_module_info.py +++ b/lib/dpx_puppettools/forge/base_module_info.py @@ -22,7 +22,7 @@ from . import BaseForgeObject from .owner_info import ForgeOwnerInfo -__version__ = '0.2.0' +__version__ = '0.2.1' LOG = logging.getLogger(__name__) @@ -158,6 +158,9 @@ class BaseForgeModuleInfo(BaseForgeObject): # ------------------------------------------------------------------------- def apply_data(self, data): + if self.verbose > 2: + LOG.debug(_("Applying data ...")) + super(BaseForgeModuleInfo, self).apply_data(data) if 'deprecated_at' in data: diff --git a/lib/dpx_puppettools/forge/cur_mod_release_info.py b/lib/dpx_puppettools/forge/cur_mod_release_info.py index d24f757..a05d756 100644 --- a/lib/dpx_puppettools/forge/cur_mod_release_info.py +++ b/lib/dpx_puppettools/forge/cur_mod_release_info.py @@ -12,12 +12,15 @@ from __future__ import absolute_import import logging import copy import datetime +import textwrap # Thisrd party modules from fb_tools.common import to_bool # Own modules from .. import pp +from .. import DEFAULT_TERMINAL_WIDTH, DEFAULT_TERMINAL_HEIGHT + from ..xlate import XLATOR from . import parse_forge_date @@ -28,7 +31,7 @@ from .mod_release_info import ModuleReleaseInfo from .base_module_info import BaseForgeModuleInfo -__version__ = '0.2.0' +__version__ = '0.2.1' LOG = logging.getLogger(__name__) @@ -388,6 +391,9 @@ class CurrentModuleReleaseInfo(ModuleReleaseInfo): # ------------------------------------------------------------------------- def apply_data(self, data): + if self.verbose > 2: + LOG.debug(_("Applying data ...")) + super(CurrentModuleReleaseInfo, self).apply_data(data) if 'changelog' in data and data['changelog']: @@ -439,6 +445,8 @@ class CurrentModuleReleaseInfo(ModuleReleaseInfo): for task in data['tasks']: self.tasks.append(copy.copy(task)) + # ------------------------------------------------------------------------- + # ============================================================================= if __name__ == "__main__": diff --git a/lib/dpx_puppettools/forge/mod_info.py b/lib/dpx_puppettools/forge/mod_info.py index 14fc079..7cab176 100644 --- a/lib/dpx_puppettools/forge/mod_info.py +++ b/lib/dpx_puppettools/forge/mod_info.py @@ -17,7 +17,7 @@ import datetime import collections import time import os -import shutil +import inspect from pathlib import Path @@ -37,9 +37,9 @@ from requests.exceptions import ConnectionError, ReadTimeout, ConnectTimeout from fb_tools.common import to_bool # Own modules -from .. import pp, DEFAULT_FORGE_API_URL, DEFAULT_HTTP_TIMEOUT, MAX_HTTP_TIMEOUT +from .. import pp, print_out, line +from .. import DEFAULT_FORGE_API_URL, DEFAULT_HTTP_TIMEOUT, MAX_HTTP_TIMEOUT from .. import DEFAULT_VAR_DIR -from .. import DEFAULT_TERMINAL_WIDTH, DEFAULT_TERMINAL_HEIGHT from ..errors import BaseHookError, BaseModuleInfoError @@ -47,15 +47,14 @@ from ..xlate import XLATOR from ..base_moduleinfo import BaseModuleInfo -from . import parse_forge_date, ForgeModuleInfoError +from . import parse_forge_date, ForgeModuleInfoError, BaseForgeObject from .mod_release_info import ModuleReleaseInfo from .mod_release_list import ModuleReleaseList from .cur_mod_release_info import CurrentModuleReleaseInfo from .owner_info import ForgeOwnerInfo - -__version__ = '0.6.0' +__version__ = '0.6.1' LOG = logging.getLogger(__name__) @@ -668,6 +667,9 @@ class ForgeModuleInfo(BaseModuleInfo): # ------------------------------------------------------------------------- def apply_data(self, data, from_cache=True): + if self.verbose > 2: + LOG.debug(_("Applying data ...")) + fd = data if from_cache: @@ -762,7 +764,7 @@ class ForgeModuleInfo(BaseModuleInfo): @classmethod def from_data(cls, data, appname=None, verbose=0, base_dir=None): - if verbose > 3: + if verbose > 2: LOG.debug(_( "Trying to instantiate a {}-object from:").format( cls.__name__) + '\n' + pp(data)) @@ -780,7 +782,7 @@ class ForgeModuleInfo(BaseModuleInfo): LOG.error(msg) return None - module_info = cls(appname=appname, verbose=verbose, base_dir=base_dir, full_name=full_name) + module_info = ForgeModuleInfo(appname=appname, verbose=verbose, base_dir=base_dir, full_name=full_name) module_info.apply_data(data) return module_info @@ -868,7 +870,7 @@ class ForgeModuleInfo(BaseModuleInfo): cls, full_name, forge_uri=DEFAULT_FORGE_API_URL, http_timeout=DEFAULT_HTTP_TIMEOUT, appname=None, verbose=0, base_dir=None): - module_info = cls( + module_info = ForgeModuleInfo( appname=appname, verbose=verbose, base_dir=base_dir, full_name=full_name, ) @@ -878,33 +880,28 @@ class ForgeModuleInfo(BaseModuleInfo): # ------------------------------------------------------------------------- - def show(self, file=None, flush=False): - - def my_print(message): - print(message, file=file, flush=flush) - - def my_line(width=None, linechar='-', end='\n'): - 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 + def show(self, detail=False, file=None, flush=False): - lin = (lchar * width)[0:width] - print(lin, end=end, file=file, flush=flush) + if self.verbose > 2: + msg = "Base classes of {}:".format(BaseForgeObject) + msg += '\n' + pp(inspect.getmro(BaseForgeObject)) + LOG.debug(msg) + msg = "Base classes of {}:".format(BaseModuleInfo) + msg += '\n' + pp(inspect.getmro(BaseModuleInfo)) + LOG.debug(msg) + msg = "Base classes of {}:".format(self.__class__.__name__) + msg += '\n' + pp(inspect.getmro(self.__class__)) + LOG.debug(msg) if not self.exists_on_forge: msg = _("Module {!r} not found on Puppet forge.").format(self.full_name) - my_print(msg) + print_out(msg, file=file, flush=flush) return msg = _("Information about {!r} from Puppet forge:").format(self.full_name) - my_print(msg) - my_line(width=len(msg)) - my_print('') + print_out(msg, file=file, flush=flush) + line(width=len(msg), file=file, flush=flush) + print_out('', file=file, flush=flush) labels = {} max_len_label = 1 @@ -935,8 +932,9 @@ class ForgeModuleInfo(BaseModuleInfo): if val is None: val = '~' msg = "{lbl:<{max}} {val}".format(lbl=lbl, max=max_len_label, val=val) - my_print(msg) - my_print('') + print_out(msg, file=file, flush=flush) + print_out('', file=file, flush=flush) + # ============================================================================= if __name__ == "__main__": diff --git a/lib/dpx_puppettools/forge/mod_release_info.py b/lib/dpx_puppettools/forge/mod_release_info.py index f05c980..64a3315 100644 --- a/lib/dpx_puppettools/forge/mod_release_info.py +++ b/lib/dpx_puppettools/forge/mod_release_info.py @@ -21,7 +21,7 @@ from ..xlate import XLATOR from . import parse_forge_date from . import BaseForgeObject -__version__ = '0.2.0' +__version__ = '0.2.1' LOG = logging.getLogger(__name__) @@ -245,6 +245,9 @@ class ModuleReleaseInfo(BaseForgeObject): # ------------------------------------------------------------------------- def apply_data(self, data): + if self.verbose > 2: + LOG.debug(_("Applying data ...")) + super(ModuleReleaseInfo, self).apply_data(data) if 'created_at' in data and data['created_at']: diff --git a/lib/dpx_puppettools/forge/owner_info.py b/lib/dpx_puppettools/forge/owner_info.py index ea13039..8398e2c 100644 --- a/lib/dpx_puppettools/forge/owner_info.py +++ b/lib/dpx_puppettools/forge/owner_info.py @@ -17,7 +17,7 @@ from ..xlate import XLATOR from . import BaseForgeObject -__version__ = '0.3.0' +__version__ = '0.3.1' LOG = logging.getLogger(__name__) @@ -141,6 +141,9 @@ class ForgeOwnerInfo(BaseForgeObject): # ------------------------------------------------------------------------- def apply_data(self, data): + if self.verbose > 2: + LOG.debug(_("Applying data ...")) + super(ForgeOwnerInfo, self).apply_data(data) if 'gravatar_id' in data: -- 2.39.5