From: Frank Brehm Date: Fri, 4 Jan 2019 15:20:05 +0000 (+0100) Subject: Adding property author to class ModuleMetadata X-Git-Tag: 1.6.4^2~72 X-Git-Url: https://git.uhu-banane.de/?a=commitdiff_plain;h=5a52d746870755a336cae5b99219cf040f8ee0fd;p=pixelpark%2Fpuppetmaster-webhooks.git Adding property author to class ModuleMetadata --- diff --git a/lib/webhooks/module_meta_info.py b/lib/webhooks/module_meta_info.py index b2863cc..24282d7 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.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)))