from .module_list import ModuleInfoDict
+from .module_meta_info import ModuleMetadata
+
from .puppetfile import Puppetfile, PuppetfileError
from .xlate import XLATOR
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)
--- /dev/null
+#!/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