From a61d8240355d9e12216add394966238c80a6ce73 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Fri, 4 Jan 2019 16:50:16 +0100 Subject: [PATCH] Adding class ModuleDependency, using it for a list of dependencies in ModuleMetadata --- lib/webhooks/module_meta_info.py | 66 +++++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) diff --git a/lib/webhooks/module_meta_info.py b/lib/webhooks/module_meta_info.py index 80e303a..98c3f7e 100644 --- a/lib/webhooks/module_meta_info.py +++ b/lib/webhooks/module_meta_info.py @@ -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))) -- 2.39.5