From 58378e1e8ef60be41730cf1687a02e4f81abbf5c Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Fri, 7 Sep 2018 11:40:57 +0200 Subject: [PATCH] Moving method read_cache_file() and class attribute open_args into class BaseHookApp --- lib/webhooks/__init__.py | 2 +- lib/webhooks/base_app.py | 61 +++++++++++++++++++++++++++++++ lib/webhooks/get_forge_modules.py | 8 ---- lib/webhooks/show_modules.py | 58 ----------------------------- 4 files changed, 62 insertions(+), 67 deletions(-) diff --git a/lib/webhooks/__init__.py b/lib/webhooks/__init__.py index abf9521..4c06e28 100644 --- a/lib/webhooks/__init__.py +++ b/lib/webhooks/__init__.py @@ -1,6 +1,6 @@ #!/bin/env python3 # -*- coding: utf-8 -*- -__version__ = '0.11.4' +__version__ = '0.12.1' # vim: ts=4 et list diff --git a/lib/webhooks/base_app.py b/lib/webhooks/base_app.py index 0822558..04bea9e 100644 --- a/lib/webhooks/base_app.py +++ b/lib/webhooks/base_app.py @@ -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 + # ============================================================================= diff --git a/lib/webhooks/get_forge_modules.py b/lib/webhooks/get_forge_modules.py index 68ba1da..c5e90ce 100644 --- a/lib/webhooks/get_forge_modules.py +++ b/lib/webhooks/get_forge_modules.py @@ -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 diff --git a/lib/webhooks/show_modules.py b/lib/webhooks/show_modules.py index e83b1d5..ebbcc7c 100644 --- a/lib/webhooks/show_modules.py +++ b/lib/webhooks/show_modules.py @@ -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): -- 2.39.5