]> Frank Brehm's Git Trees - pixelpark/puppetmaster-webhooks.git/commitdiff
Finishing lib/webhooks/show_modules.py
authorFrank Brehm <frank.brehm@pixelpark.com>
Mon, 27 Aug 2018 13:57:50 +0000 (15:57 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Mon, 27 Aug 2018 13:57:50 +0000 (15:57 +0200)
lib/webhooks/__init__.py
lib/webhooks/base_app.py
lib/webhooks/show_modules.py

index 79baaa97890d73a4add6643c16143432da7ddc07..10cde58b8ca143e19b118cf3bffa6d661529307d 100644 (file)
@@ -1,6 +1,6 @@
 #!/bin/env python3
 # -*- coding: utf-8 -*-
 
-__version__ = '0.10.6'
+__version__ = '0.10.7'
 
 # vim: ts=4 et list
index 45aab26f6fdf204736ac8c49f95967500505f3f6..99825d12c4a94b3d92740719e75021662231e5d4 100644 (file)
@@ -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)
 
     # -------------------------------------------------------------------------
index e8d8cfaa56d45ac7da9a8fc360121c4b6ef036de..874c2c3d31b7a77a21e5ee5230f01870dc935a8b 100644 (file)
@@ -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('<h2>Keine passenden Module gefunden.</h2>')
+            self.print_out()
+            return
+
         self.print_out('<table>')
         self.print_out('  <colgroup span="9"></colgroup>')
         self.print_out('  <thead>')
@@ -224,6 +275,88 @@ class ShowModulesApp(BaseHookApp):
         msg = "<br/>Insgesamt <b>{nr} Modul{e}</b> 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
 
 # =============================================================================