From 7b2167200a2936d0fcc4807c57cc7d96ccfeb29a Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Mon, 27 Aug 2018 15:57:50 +0200 Subject: [PATCH] Finishing lib/webhooks/show_modules.py --- lib/webhooks/__init__.py | 2 +- lib/webhooks/base_app.py | 2 +- lib/webhooks/show_modules.py | 160 +++++++++++++++++++++++++++++++++-- 3 files changed, 157 insertions(+), 7 deletions(-) diff --git a/lib/webhooks/__init__.py b/lib/webhooks/__init__.py index 79baaa9..10cde58 100644 --- a/lib/webhooks/__init__.py +++ b/lib/webhooks/__init__.py @@ -1,6 +1,6 @@ #!/bin/env python3 # -*- coding: utf-8 -*- -__version__ = '0.10.6' +__version__ = '0.10.7' # vim: ts=4 et list diff --git a/lib/webhooks/base_app.py b/lib/webhooks/base_app.py index 45aab26..99825d1 100644 --- a/lib/webhooks/base_app.py +++ b/lib/webhooks/base_app.py @@ -653,7 +653,7 @@ class BaseHookApp(BaseObject): self.send_error_msgs() if self.output_type == 'html': self.print_htlm_end() - LOG.info("Finished.") + LOG.debug("Finished.") sys.exit(0) # ------------------------------------------------------------------------- diff --git a/lib/webhooks/show_modules.py b/lib/webhooks/show_modules.py index e8d8cfa..874c2c3 100644 --- a/lib/webhooks/show_modules.py +++ b/lib/webhooks/show_modules.py @@ -16,6 +16,7 @@ import textwrap import copy import json import datetime +import re # Third party modules import six @@ -113,17 +114,59 @@ class ShowModulesApp(BaseHookApp): if is_sequence(self.query['vendor']): for vendor in self.query['vendor']: v = vendor.strip().lower() - if v: + if v and v not in q_vendors: q_vendors.append(v) else: v = self.query['vendor'].strip().lower() - if v: + if v and v not in q_vendors: q_vendors.append(v) if q_vendors: if self.filters is None: self.filters = {} self.filters['vendor'] = q_vendors + if 'name' in self.query: + q_names = [] + if is_sequence(self.query['name']): + for name in self.query['name']: + v = name.strip().lower() + if v and v not in q_names: + q_names.append(v) + else: + v = self.query['name'].strip().lower() + if v and v not in q_names: + q_names.append(v) + if q_names: + if self.filters is None: + self.filters = {} + self.filters['name'] = q_names + + if 're' in self.query: + q_re = [] + if is_sequence(self.query['re']): + for regex in self.query['re']: + v = regex.strip() + if v: + q_re.append(v) + else: + v = self.query['re'].strip() + if v: + q_re.append(v) + if q_re: + if self.filters is None: + self.filters = {} + for regex in q_re: + regex_compiled = None + try: + regex_compiled = re.compile(regex, re.IGNORECASE) + except Exception as e: + LOG.error("{c}: Invalid regular expression {r!r}: {e}".format( + c=e.__class__.__name__, r=regex, e=e)) + else: + if 're' not in self.filters: + self.filters['re'] = [] + if regex_compiled not in self.filters['re']: + self.filters['re'].append(regex_compiled) # ------------------------------------------------------------------------- def run(self): @@ -152,6 +195,8 @@ class ShowModulesApp(BaseHookApp): self.output_modules_json(module_infos) elif self.output_type == 'html': self.output_modules_html(module_infos) + else: + self.output_modules_txt(module_infos) # ------------------------------------------------------------------------- def output_modules_json(self, module_infos): @@ -170,6 +215,12 @@ class ShowModulesApp(BaseHookApp): # ------------------------------------------------------------------------- def output_modules_html(self, module_infos): + if not module_infos: + self.print_out() + self.print_out('

Keine passenden Module gefunden.

') + self.print_out() + return + self.print_out('') self.print_out(' ') self.print_out(' ') @@ -224,6 +275,88 @@ class ShowModulesApp(BaseHookApp): msg = "
Insgesamt {nr} Modul{e} gefunden.".format(nr=nr_modules, e=plural_e) self.print_out(msg) + # ------------------------------------------------------------------------- + def output_modules_txt(self, module_infos): + + if not module_infos: + self.print_out() + self.print_out('Keine passenden Module gefunden.') + self.print_out() + return + + label = { + 'name': 'Name', + 'full_name': 'Vollständiger Name', + 'repo': "Repository", + 'forge_version': 'Puppet Forge', + 'version_development': 'Development', + 'version_test': 'Test', + 'version_production': 'Production', + 'date_checked': 'Letzter Check', + } + + if self.verbose > 2: + LOG.debug("Labels:\n{}".format(pp(label))) + + width = {} + for key in label.keys(): + width[key] = len(label[key]) + + if self.verbose > 2: + LOG.debug("Breiten vor Ermittlung:\n{}".format(pp(width))) + + nr_modules = 0 + + for module_info in module_infos: + nr_modules += 1 + output_data = self.get_output_data(module_info) + for key in label.keys(): + wi = width[key] + txt = output_data.get(key, '') + if txt is not None and len(txt) > wi: + width[key] = len(txt) + + if self.verbose > 2: + LOG.debug("Breiten nach Ermittlung:\n{}".format(pp(width))) + + tpl = "{{name:<{name}}} | " + tpl += "{{full_name:<{full_name}}} | " + tpl += "{{repo:<{repo}}} | " + tpl += "{{forge_version:^{forge_version}}} | " + tpl += "{{version_development:^{version_development}}} | " + tpl += "{{version_test:^{version_test}}} | " + tpl += "{{version_production:^{version_production}}} | " + tpl += "{{date_checked:<{date_checked}}}" + + tpl = tpl.format(**width) + + if self.verbose > 2: + LOG.debug("Zeilen-Template:\n{}".format(tpl)) + + plural_e = 'e' + if nr_modules == 1: + plural_e = '' + msg = "\nInsgesamt {nr} Modul{e} gefunden:\n".format(nr=nr_modules, e=plural_e) + self.print_out(msg) + + self.print_out() + header = tpl.format(**label) + self.print_out(header) + self.print_out('=' * len(header)) + + for module_info in module_infos: + nr_modules += 1 + output_data = self.get_output_data(module_info) + if not output_data['forge_version']: + output_data['forge_version'] = '~' + if not output_data['forge_homepage_url']: + output_data['forge_homepage_url'] = '~' + if not output_data['repo']: + output_data['repo'] = '~' + self.print_out(tpl.format(**output_data)) + + self.print_out() + # ------------------------------------------------------------------------- def get_output_data(self, module_info): @@ -247,7 +380,7 @@ class ShowModulesApp(BaseHookApp): 'version_test': module_info.local_version_output('test'), 'version_production': module_info.local_version_output('production'), } - if self.verbose > 2: + if self.verbose > 3: LOG.debug("Output data:\n{}".format(pp(output_data))) return output_data @@ -280,8 +413,7 @@ class ShowModulesApp(BaseHookApp): if self.verbose > 3: LOG.debug("Content of {f!r}:\n{c}".format(f=self.cache_file, c=pp(data))) if not data: - raise ShowModulesUncriticalError( - "Cache file {!r} has no content.".format(self.cache_file)) + LOG.debug("Did not found any matching modules.") return data @@ -300,6 +432,24 @@ class ShowModulesApp(BaseHookApp): if not do_display: return False + do_display = False + if 'name' in self.filters: + for name in self.filters['name']: + if name == module_info.name: + do_display = True + break + if not do_display: + return False + + do_display = False + if 're' in self.filters: + for regex in self.filters['re']: + if regex.search(module_info.full_name): + do_display = True + break + if not do_display: + return False + return True # ============================================================================= -- 2.39.5