From: Frank Brehm Date: Fri, 4 Jan 2019 15:05:08 +0000 (+0100) Subject: Starting with lib/webhooks/module_meta_info.py X-Git-Tag: 1.6.4^2~73 X-Git-Url: https://git.uhu-banane.de/?a=commitdiff_plain;h=2f66e9df78f08a9f975cf48166d5be5ca62b03ea;p=pixelpark%2Fpuppetmaster-webhooks.git Starting with lib/webhooks/module_meta_info.py --- diff --git a/lib/webhooks/get_forge_modules.py b/lib/webhooks/get_forge_modules.py index fad0de9..7a5b3d4 100644 --- a/lib/webhooks/get_forge_modules.py +++ b/lib/webhooks/get_forge_modules.py @@ -42,6 +42,8 @@ from .module_info import ModuleInfo from .module_list import ModuleInfoDict +from .module_meta_info import ModuleMetadata + from .puppetfile import Puppetfile, PuppetfileError from .xlate import XLATOR @@ -316,6 +318,14 @@ class GetForgeModulesApp(BaseHookApp): LOG.warn(_("Did not found any data in {!r}.").format(str(metadata_file))) return None + metadata = ModuleMetadata.from_json_data( + json_data, appname=self.appname, verbose=self.verbose, base_dir=self.base_dir) + if self.verbose > 3: + LOG.debug("ModuleMetadata:\n{}".format(pp(metadata.as_dict()))) + if self.verbose > 2: + LOG.debug("ModuleMetadata: got a {c} object:\n{s}".format( + c=metadata.__class__.__name__, s=metadata)) + return ModuleInfo.init_from_json( json_data, env, appname=self.appname, verbose=self.verbose, base_dir=self.base_dir) diff --git a/lib/webhooks/module_info.py b/lib/webhooks/module_info.py index 8aeac7b..1adff92 100644 --- a/lib/webhooks/module_info.py +++ b/lib/webhooks/module_info.py @@ -26,7 +26,7 @@ from fb_tools.obj import FbBaseObjectError, FbBaseObject from .xlate import XLATOR -__version__ = '1.2.1' +__version__ = '1.3.1' LOG = logging.getLogger(__name__) diff --git a/lib/webhooks/module_meta_info.py b/lib/webhooks/module_meta_info.py new file mode 100644 index 0000000..b2863cc --- /dev/null +++ b/lib/webhooks/module_meta_info.py @@ -0,0 +1,147 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@author: Frank Brehm +@contact: frank.brehm@pixelpark.com +@copyright: © 2019 by Frank Brehm, Publicies Pixelpark GmbH, Berlin +@summary: A module for encapsulating all information from a metadata.json file + of a Puppet module +""" +from __future__ import absolute_import + +# Standard modules +import logging +import re +import copy +import warnings +import time +import datetime +import copy +import json + +# Third party modules + +# Own modules +from fb_tools.common import pp, to_str, to_bool, is_sequence +from fb_tools.obj import FbBaseObjectError, FbBaseObject + +from .xlate import XLATOR + +__version__ = '0.1.1' + +LOG = logging.getLogger(__name__) + +_ = XLATOR.gettext +ngettext = XLATOR.ngettext + + +# ============================================================================= +class MetadataInitError(FbBaseObjectError): + pass + + +# ============================================================================= +class ModuleMetadata(FbBaseObject): + """Class for encapsulating information about a Puppet module.""" + + # ------------------------------------------------------------------------- + def __init__( + self, appname=None, verbose=0, version=__version__, base_dir=None, + initialized=None): + + self._name = None + + super(ModuleMetadata, self).__init__( + appname=appname, verbose=verbose, version=version, + base_dir=base_dir, initialized=False, + ) + + # ------------------------------------------------------------------------- + @property + def name(self): + """The name of the module.""" + return self._name + + @name.setter + def name(self, value): + if value is None: + self._name = None + return + self._name = to_str(value).strip().lower() + + # ------------------------------------------------------------------------- + def as_dict(self, short=True): + """ + Transforms the elements of the object into a dict + + @return: structure as dict + @rtype: dict + """ + + res = super(ModuleMetadata, self).as_dict(short=short) + + res['name'] = self.name + + return res + + # ------------------------------------------------------------------------- + @classmethod + def from_json_data(cls, json_data, appname=None, verbose=0, base_dir=None): + """ Tries to instantiate a new ModuleMetadata object from data + read from a metadata.json file. + See https://puppet.com/docs/puppet/5.2/modules_metadata.html for syntax. + """ + + if 'name' not in json_data: + msg = _("Name of the module not included in JSON data.") + raise MetadataInitError(msg) + + metadata = cls(appname=appname, verbose=verbose, base_dir=base_dir) + + metadata.name = json_data['name'] + if verbose > 3: + LOG.debug("ModuleMetadata:\n{}".format(pp(metadata.as_dict()))) + + return metadata + + # ------------------------------------------------------------------------- + def __copy__(self): + + new = self.__class__(appname=self.appname, verbose=self.verbose, base_dir=self.base_dir) + new.name = self.name + + return new + + # ------------------------------------------------------------------------- + def to_data_dict(self): + + data = {} + data['name'] = self.name + + if self.verbose > 4: + LOG.debug("ModuleMetadata:\n{}".format(pp(data))) + + return data + + # ------------------------------------------------------------------------- + def to_json(self, indent=None): + + data = self.to_data_dict() + ret = json.dumps(data, indent=indent, sort_keys=True) + if self.verbose > 4: + LOG.debug("ModuleMetadata as JSON:\n{}".format(ret)) + return ret + + # ------------------------------------------------------------------------- + def __str__(self): + return self.to_json(indent=4) + + +# ============================================================================= +if __name__ == "__main__": + + pass + +# ============================================================================= + +# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 list