]> Frank Brehm's Git Trees - pixelpark/puppetmaster-webhooks.git/commitdiff
Adding property author to class ModuleMetadata
authorFrank Brehm <frank.brehm@pixelpark.com>
Fri, 4 Jan 2019 15:20:05 +0000 (16:20 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Fri, 4 Jan 2019 15:20:05 +0000 (16:20 +0100)
lib/webhooks/module_meta_info.py

index b2863cc3c083c13bab526c57fe75c8851b4f98b8..24282d7f2613021136e0e484b2eef6a6f3da4d47 100644 (file)
@@ -27,7 +27,7 @@ from fb_tools.obj import FbBaseObjectError, FbBaseObject
 
 from .xlate import XLATOR
 
-__version__ = '0.1.1'
+__version__ = '0.2.2'
 
 LOG = logging.getLogger(__name__)
 
@@ -44,12 +44,16 @@ class MetadataInitError(FbBaseObjectError):
 class ModuleMetadata(FbBaseObject):
     """Class for encapsulating information about a Puppet module."""
 
+    re_author_from_name = re.compile(r'^([^-]+)-')
+
     # -------------------------------------------------------------------------
     def __init__(
         self, appname=None, verbose=0, version=__version__, base_dir=None,
             initialized=None):
 
         self._name = None
+        self._version = None
+        self._author = None
 
         super(ModuleMetadata, self).__init__(
             appname=appname, verbose=verbose, version=version,
@@ -69,6 +73,36 @@ class ModuleMetadata(FbBaseObject):
             return
         self._name = to_str(value).strip().lower()
 
+    # -------------------------------------------------------------------------
+    @property
+    def version(self):
+        """The version of the module."""
+        return self._version
+
+    @version.setter
+    def version(self, value):
+        if value is None:
+            self._version = None
+            return
+        self._version = to_str(value).strip()
+
+    # -------------------------------------------------------------------------
+    @property
+    def author(self):
+        """The author of the module."""
+        if not self._author:
+            match = self.re_author_from_name.match(self.name)
+            if match:
+                return match.group(1)
+        return self._author
+
+    @author.setter
+    def author(self, value):
+        if value is None:
+            self._author = None
+            return
+        self._author = to_str(value).strip()
+
     # -------------------------------------------------------------------------
     def as_dict(self, short=True):
         """
@@ -81,6 +115,8 @@ class ModuleMetadata(FbBaseObject):
         res = super(ModuleMetadata, self).as_dict(short=short)
 
         res['name'] = self.name
+        res['version'] = self.version
+        res['author'] = self.author
 
         return res
 
@@ -92,13 +128,27 @@ class ModuleMetadata(FbBaseObject):
             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)
+        required_props = {
+            'name': 'Name',
+            'version': 'Version',
+        }
+
+        for prop in required_props.keys():
+            desc = required_props[prop]
+            if prop not in json_data:
+                msg = _("{} not included in JSON data.").format(desc)
+                raise MetadataInitError(msg)
 
         metadata = cls(appname=appname, verbose=verbose, base_dir=base_dir)
 
         metadata.name = json_data['name']
+        metadata.version = json_data['version']
+        if 'author' in json_data:
+            metadata.author = json_data['author']
+        else:
+            metadata.author = metadata.author
+
+        metadata.initialized = True
         if verbose > 3:
             LOG.debug("ModuleMetadata:\n{}".format(pp(metadata.as_dict())))
 
@@ -108,7 +158,12 @@ class ModuleMetadata(FbBaseObject):
     def __copy__(self):
 
         new = self.__class__(appname=self.appname, verbose=self.verbose, base_dir=self.base_dir)
+
         new.name = self.name
+        new.version = self.version
+        new.author = self.author
+
+        new.initialized = True
 
         return new
 
@@ -117,6 +172,8 @@ class ModuleMetadata(FbBaseObject):
 
         data = {}
         data['name'] = self.name
+        data['version'] = self.version
+        data['author'] = self.author
 
         if self.verbose > 4:
             LOG.debug("ModuleMetadata:\n{}".format(pp(data)))