]> Frank Brehm's Git Trees - pixelpark/puppetmaster-webhooks.git/commitdiff
Adding class ModuleDependency, using it for a list of dependencies in ModuleMetadata
authorFrank Brehm <frank.brehm@pixelpark.com>
Fri, 4 Jan 2019 15:50:16 +0000 (16:50 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Fri, 4 Jan 2019 15:50:16 +0000 (16:50 +0100)
lib/webhooks/module_meta_info.py

index 80e303a607667a16d60ccb8dd1be668ad538bbc0..98c3f7e6937852fbf594777e7aae1d196ba59c0a 100644 (file)
@@ -27,7 +27,7 @@ from fb_tools.obj import FbBaseObjectError, FbBaseObject
 
 from .xlate import XLATOR
 
-__version__ = '0.2.2'
+__version__ = '0.3.1'
 
 LOG = logging.getLogger(__name__)
 
@@ -40,6 +40,53 @@ class MetadataInitError(FbBaseObjectError):
     pass
 
 
+# =============================================================================
+class ModuleDependency(object):
+    """Class for encapsulating a dependency to another Puppet module."""
+
+    # -------------------------------------------------------------------------
+    def __init__(self, name, requirement):
+
+        self.name = str(name).strip().lower()
+        self.requirement = str(requirement).strip()
+
+    # -------------------------------------------------------------------------
+    @classmethod
+    def from_data(cls, data):
+
+        for prop in ('name', 'version_requirement'):
+            if prop not in data:
+                msg = _("{} not included in JSON data.").format(prop)
+                raise MetadataInitError(msg)
+
+        dep = cls(name=data['name'], requirement=data['version_requirement'])
+        return dep
+
+    # -------------------------------------------------------------------------
+    def as_dict(self):
+
+        ret = {
+            'name': self.name,
+            'requirement': self.requirement,
+        }
+        return ret
+
+    # -------------------------------------------------------------------------
+    def to_data(self):
+
+        ret = {
+            'name': self.name,
+            'version_requirement': self.requirement,
+        }
+        return ret
+
+    # -------------------------------------------------------------------------
+    def __copy__(self):
+
+        new = cls(name=self.name, requirement=self.requirement)
+        return new
+
+
 # =============================================================================
 class ModuleMetadata(FbBaseObject):
     """Class for encapsulating information about a Puppet module."""
@@ -57,6 +104,7 @@ class ModuleMetadata(FbBaseObject):
         self._license = None
         self._summary = None
         self._source = None
+        self.dependencies = []
 
         super(ModuleMetadata, self).__init__(
             appname=appname, verbose=verbose, version=version,
@@ -162,6 +210,9 @@ class ModuleMetadata(FbBaseObject):
         res['license'] = self.license
         res['summary'] = self.summary
         res['source'] = self.source
+        res['dependencies'] = []
+        for dep in self.dependencies:
+            res['dependencies'].append(dep.as_dict())
 
         return res
 
@@ -179,6 +230,7 @@ class ModuleMetadata(FbBaseObject):
             'license': 'License',
             'summary': 'Summary',
             'source': 'Source',
+            'dependencies': 'Dependencies',
         }
 
         for prop in required_props.keys():
@@ -187,6 +239,10 @@ class ModuleMetadata(FbBaseObject):
                 msg = _("{} not included in JSON data.").format(desc)
                 raise MetadataInitError(msg)
 
+        if not is_sequence(json_data['dependencies']):
+            msg = _("Invalid dependencies - not a list.")
+            raise MetadataInitError(msg)
+
         metadata = cls(appname=appname, verbose=verbose, base_dir=base_dir)
 
         metadata.name = json_data['name']
@@ -198,6 +254,9 @@ class ModuleMetadata(FbBaseObject):
         metadata.license = json_data['license']
         metadata.summary = json_data['summary']
         metadata.source = json_data['source']
+        for item in json_data['dependencies']:
+            dep = ModuleDependency.from_data(item)
+            metadata.dependencies.append(dep)
 
         metadata.initialized = True
         if verbose > 3:
@@ -216,6 +275,8 @@ class ModuleMetadata(FbBaseObject):
         new.license = self.license
         new.summary = self.summary
         new.source = self.source
+        for dep in self.dependencies:
+            new.dependencies.append(copy.copy(dep))
 
         new.initialized = True
 
@@ -231,6 +292,9 @@ class ModuleMetadata(FbBaseObject):
         data['license'] = self.license
         data['summary'] = self.summary
         data['source'] = self.source
+        data['dependencies'] = []
+        for dep in self.dependencies:
+            data['dependencies'].append(dep.to_data())
 
         if self.verbose > 4:
             LOG.debug("ModuleMetadata:\n{}".format(pp(data)))