]> Frank Brehm's Git Trees - pixelpark/puppetmaster-webhooks.git/commitdiff
Adding class ModuleOsSupport, using it for a list of supported operating systems...
authorFrank Brehm <frank.brehm@pixelpark.com>
Fri, 4 Jan 2019 16:42:16 +0000 (17:42 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Fri, 4 Jan 2019 16:42:16 +0000 (17:42 +0100)
lib/webhooks/module_meta_info.py

index 8d31dce66e3bdc461f587a40ac818de5387b6f23..ed438a48cfc082c5a2060fe8505a76608f7426ae 100644 (file)
@@ -27,7 +27,7 @@ from fb_tools.obj import FbBaseObjectError, FbBaseObject
 
 from .xlate import XLATOR
 
-__version__ = '0.4.2'
+__version__ = '0.5.1'
 
 LOG = logging.getLogger(__name__)
 
@@ -87,6 +87,59 @@ class ModuleDependency(object):
         return new
 
 
+# =============================================================================
+class ModuleOsSupport(object):
+    """Class for encapsulating the support of the module to a particular operating system."""
+
+    # -------------------------------------------------------------------------
+    def __init__(self, name, releases):
+
+        self.name = str(name).strip()
+        self.releases = copy.copy(releases)
+
+    # -------------------------------------------------------------------------
+    @classmethod
+    def from_data(cls, data):
+
+        if 'operatingsystem' not in data:
+            msg = _("{} not included in JSON data.").format('operatingsystem')
+            raise MetadataInitError(msg)
+
+        releases = []
+        if 'operatingsystemrelease' in data:
+            if not is_sequence(data['operatingsystemrelease']):
+                msg = _("Invalid item {} - not a list.").format('operatingsystemrelease')
+                raise MetadataInitError(msg)
+            for item in data['operatingsystemrelease']:
+                releases.append(str(item).strip())
+
+        os_supp = cls(name=data['operatingsystem'], releases=releases)
+        return os_supp
+
+    # -------------------------------------------------------------------------
+    def as_dict(self):
+
+        ret = {
+            'name': self.name,
+            'releases': copy.copy(self.releases),
+        }
+        return ret
+
+    # -------------------------------------------------------------------------
+    def to_data(self):
+
+        ret = {'operatingsystem': self.name}
+        if self.releases:
+            ret['operatingsystemrelease'] = copy.copy(self.releases)
+        return ret
+
+    # -------------------------------------------------------------------------
+    def __copy__(self):
+
+        new = cls(name=self.name, releases=copy.copy(self.releases))
+        return new
+
+
 # =============================================================================
 class ModuleMetadata(FbBaseObject):
     """Class for encapsulating information about a Puppet module."""
@@ -108,6 +161,7 @@ class ModuleMetadata(FbBaseObject):
         self.requirements = []
         self._project_page = None
         self._issues_url = None
+        self.operatingsystem_support = []
 
         super(ModuleMetadata, self).__init__(
             appname=appname, verbose=verbose, version=version,
@@ -247,6 +301,9 @@ class ModuleMetadata(FbBaseObject):
             res['requirements'].append(req.as_dict())
         res['project_page'] = self.project_page
         res['issues_url'] = self.issues_url
+        res['operatingsystem_support'] = []
+        for supp in self.operatingsystem_support:
+            res['operatingsystem_support'].append(supp.as_dict())
 
         return res
 
@@ -258,6 +315,10 @@ class ModuleMetadata(FbBaseObject):
             See https://puppet.com/docs/puppet/5.2/modules_metadata.html for syntax.
         """
 
+        if verbose > 3:
+            LOG.debug("Trying to create a {c} object from:\n{d}".format(
+                c=cls.__name__, d=pp(json_data)))
+
         required_props = {
             'name': 'Name',
             'version': 'Version',
@@ -282,6 +343,11 @@ class ModuleMetadata(FbBaseObject):
                 msg = _("Invalid item {} - not a list.").format('requirements')
                 raise MetadataInitError(msg)
 
+        if 'operatingsystem_support' in json_data:
+            if not is_sequence(json_data['operatingsystem_support']):
+                msg = _("Invalid item {} - not a list.").format('operatingsystem_support')
+                raise MetadataInitError(msg)
+
         metadata = cls(appname=appname, verbose=verbose, base_dir=base_dir)
 
         metadata.name = json_data['name']
@@ -304,6 +370,10 @@ class ModuleMetadata(FbBaseObject):
             metadata.project_page = json_data['project_page']
         if 'issues_url' in json_data:
             metadata.issues_url = json_data['issues_url']
+        if 'operatingsystem_support' in json_data:
+            for item in json_data['operatingsystem_support']:
+                supp = ModuleOsSupport.from_data(item)
+                metadata.operatingsystem_support.append(supp)
 
         metadata.initialized = True
         if verbose > 3:
@@ -328,6 +398,8 @@ class ModuleMetadata(FbBaseObject):
             new.requirements.append(copy.copy(req))
         new.project_page = self.project_page
         new.issues_url = self.issues_url
+        for supp in self.operatingsystem_support:
+            new.operatingsystem_support.append(copy.copy(supp))
 
         new.initialized = True
 
@@ -354,6 +426,10 @@ class ModuleMetadata(FbBaseObject):
             data['project_page'] = self.project_page
         if self.issues_url:
             data['issues_url'] = self.issues_url
+        if self.operatingsystem_support:
+            data['operatingsystem_support'] = []
+            for supp in self.operatingsystem_support:
+                data['operatingsystem_support'].append(supp.to_data())
 
         if self.verbose > 4:
             LOG.debug("ModuleMetadata:\n{}".format(pp(data)))