]> Frank Brehm's Git Trees - pixelpark/puppetmaster-webhooks.git/commitdiff
Moving method read_cache_file() and class attribute open_args into class BaseHookApp
authorFrank Brehm <frank.brehm@pixelpark.com>
Fri, 7 Sep 2018 09:40:57 +0000 (11:40 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Fri, 7 Sep 2018 09:40:57 +0000 (11:40 +0200)
lib/webhooks/__init__.py
lib/webhooks/base_app.py
lib/webhooks/get_forge_modules.py
lib/webhooks/show_modules.py

index abf952173cde5cff5760063015f31042cc852b1e..4c06e28d37cbd9ef25e650860552bbd228e7f76a 100644 (file)
@@ -1,6 +1,6 @@
 #!/bin/env python3
 # -*- coding: utf-8 -*-
 
-__version__ = '0.11.4'
+__version__ = '0.12.1'
 
 # vim: ts=4 et list
index 082255886fd285795b5bda97d60911972977500b..04bea9e223b51f547c3d5a0a34c80f869ed4d949 100644 (file)
@@ -25,6 +25,7 @@ import textwrap
 from email.message import EmailMessage
 
 # Third party modules
+import six
 import yaml
 import pytz
 
@@ -37,6 +38,10 @@ from .obj import BaseObjectError, BaseObject
 
 from .handler import BaseHandler
 
+from .module_info import ModuleInfo
+
+from .module_list import ModuleInfoDict
+
 __version__ = webhooks.__version__
 
 LOG = logging.getLogger(__name__)
@@ -81,6 +86,13 @@ class BaseHookApp(BaseObject):
     default_output_type = 'txt'
     default_mime_type = valid_output_types[default_output_type]
 
+    open_args = {}
+    if six.PY3:
+        open_args = {
+            'encoding': 'utf-8',
+            'errors': 'surrogateescape',
+        }
+
     # -------------------------------------------------------------------------
     def __init__(self, appname=None, base_dir=None, verbose=0, version=__version__):
         """Constructor."""
@@ -295,6 +307,7 @@ class BaseHookApp(BaseObject):
         res['mime_type'] = self.mime_type
         res['html_title'] = self.html_title
         res['no_error_mail'] = self.no_error_mail
+        res['open_args'] = self.open_args
 
         return res
 
@@ -910,6 +923,54 @@ class BaseHookApp(BaseObject):
             self.__class__.__name__)
         raise NotImplementedError(msg)
 
+    # -------------------------------------------------------------------------
+    def read_cache_file(self, only_main_branches=True):
+
+        LOG.debug("Searching for {!r} ...".format(self.cachefile))
+        if not os.path.exists(self.cachefile):
+            raise ShowModulesUncriticalError(
+                "Cache file {!r} not found.".format(self.cachefile))
+
+        if not os.access(self.cachefile, os.R_OK):
+            raise ShowModulesUncriticalError(
+                "Cache file {!r} not readable.".format(self.cachefile))
+
+        modules = ModuleInfoDict(
+            appname=self.appname, verbose=self.verbose, base_dir=self.base_dir)
+
+        LOG.debug("Reading {!r} ...".format(self.cachefile))
+        try:
+            with open(self.cachefile, 'r', **self.open_args) as fh:
+                for struct in yaml.load(fh):
+                    module_info = ModuleInfo.init_from_data(
+                        struct, appname=self.appname, verbose=self.verbose,
+                        base_dir=self.base_dir)
+                    if module_info:
+                        if only_main_branches:
+                            in_main_branches = False
+                            for branch in self.main_branches:
+                                if (branch in module_info.local_versions and
+                                        module_info.local_versions[branch]):
+                                    in_main_branches = True
+                                elif (branch in module_info.expected_versions and
+                                        module_info.expected_versions[branch]):
+                                    in_main_branches = True
+                                if in_main_branches:
+                                    break
+                            if not in_main_branches:
+                                continue
+                        if self.should_display(module_info):
+                            modules.append(module_info)
+        except yaml.YAMLError as e:
+            raise ShowModulesUncriticalError(
+                "Could not evaluate content of {f!r}: {e}".format(f=self.cachefile, e=e))
+        if self.verbose > 3:
+            LOG.debug("Content of {f!r}:\n{c}".format(f=self.cachefile, c=pp(modules.as_list())))
+        if not len(modules):
+            LOG.debug("Did not found any matching modules.")
+
+        return modules
+
 
 # =============================================================================
 
index 68ba1da826553405e23bddffc0c6f7a78d69239f..c5e90ce3b358be2133395d4f1f10644109048cb5 100644 (file)
@@ -63,13 +63,6 @@ class GetForgeModulesApp(BaseHookApp):
     default_http_timeout = 30
     max_http_timeout = 600
 
-    open_args = {}
-    if six.PY3:
-        open_args = {
-            'encoding': 'utf-8',
-            'errors': 'surrogateescape',
-        }
-
     # -------------------------------------------------------------------------
     def __init__(self, appname=None, verbose=0, version=__version__):
         """Constructor."""
@@ -142,7 +135,6 @@ class GetForgeModulesApp(BaseHookApp):
         res['default_http_timeout'] = self.default_http_timeout
         res['max_http_timeout'] = self.max_http_timeout
         res['default_puppet_root_env_dir'] = self.default_puppet_root_env_dir
-        res['open_args'] = self.open_args
         res['do_forge'] = self.do_forge
 
         return res
index e83b1d5b452bd6eb73c6350fbbd95809ddf12493..ebbcc7c92035f0068cb83a09278df18a626d09b4 100644 (file)
@@ -54,13 +54,6 @@ class ShowModulesApp(BaseHookApp):
     Class for the application objects.
     """
 
-    open_args = {}
-    if six.PY3:
-        open_args = {
-            'encoding': 'utf-8',
-            'errors': 'surrogateescape',
-        }
-
     main_branches = ('development', 'test', 'production')
 
     # -------------------------------------------------------------------------
@@ -71,7 +64,6 @@ class ShowModulesApp(BaseHookApp):
             returns a list with all used Puppet modules
             ''').strip()
 
-        #self.cache_file = None
         self._output_type = output_type
         self.filters = None
 
@@ -91,7 +83,6 @@ class ShowModulesApp(BaseHookApp):
 
         res = super(ShowModulesApp, self).as_dict(short=short)
 
-        res['open_args'] = self.open_args
         res['main_branches'] = copy.copy(self.main_branches)
 
         return res
@@ -105,7 +96,6 @@ class ShowModulesApp(BaseHookApp):
     def post_init(self):
 
         self.read_stdin = False
-        #self.cache_file = os.path.join(self.data_dir, 'modules-info.yaml')
         self.init_filters()
         self.no_error_mail = True
 
@@ -423,54 +413,6 @@ class ShowModulesApp(BaseHookApp):
             LOG.debug("Output data:\n{}".format(pp(output_data)))
         return output_data
 
-    # -------------------------------------------------------------------------
-    def read_cache_file(self, only_main_branches=True):
-
-        LOG.debug("Searching for {!r} ...".format(self.cachefile))
-        if not os.path.exists(self.cachefile):
-            raise ShowModulesUncriticalError(
-                "Cache file {!r} not found.".format(self.cachefile))
-
-        if not os.access(self.cachefile, os.R_OK):
-            raise ShowModulesUncriticalError(
-                "Cache file {!r} not readable.".format(self.cachefile))
-
-        modules = ModuleInfoDict(
-            appname=self.appname, verbose=self.verbose, base_dir=self.base_dir)
-
-        LOG.debug("Reading {!r} ...".format(self.cachefile))
-        try:
-            with open(self.cachefile, 'r', **self.open_args) as fh:
-                for struct in yaml.load(fh):
-                    module_info = ModuleInfo.init_from_data(
-                        struct, appname=self.appname, verbose=self.verbose,
-                        base_dir=self.base_dir)
-                    if module_info:
-                        if only_main_branches:
-                            in_main_branches = False
-                            for branch in self.main_branches:
-                                if (branch in module_info.local_versions and
-                                        module_info.local_versions[branch]):
-                                    in_main_branches = True
-                                elif (branch in module_info.expected_versions and
-                                        module_info.expected_versions[branch]):
-                                    in_main_branches = True
-                                if in_main_branches:
-                                    break
-                            if not in_main_branches:
-                                continue
-                        if self.should_display(module_info):
-                            modules.append(module_info)
-        except yaml.YAMLError as e:
-            raise ShowModulesUncriticalError(
-                "Could not evaluate content of {f!r}: {e}".format(f=self.cachefile, e=e))
-        if self.verbose > 3:
-            LOG.debug("Content of {f!r}:\n{c}".format(f=self.cachefile, c=pp(modules.as_list())))
-        if not len(modules):
-            LOG.debug("Did not found any matching modules.")
-
-        return modules
-
     # -------------------------------------------------------------------------
     def should_display(self, module_info):