From: Frank Brehm Date: Wed, 13 Feb 2019 16:39:07 +0000 (+0100) Subject: Adding class ModuleReleaseList X-Git-Tag: 1.6.4^2~53 X-Git-Url: https://git.uhu-banane.net/?a=commitdiff_plain;h=934c5eda330ac5c4ca5000684524cab46d3b4ccb;p=pixelpark%2Fpuppetmaster-webhooks.git Adding class ModuleReleaseList --- diff --git a/lib/webhooks/forge_module_info.py b/lib/webhooks/forge_module_info.py index d878f89..f606024 100644 --- a/lib/webhooks/forge_module_info.py +++ b/lib/webhooks/forge_module_info.py @@ -15,6 +15,7 @@ import copy import warnings import time import datetime +import collections # Third party modules import requests @@ -28,7 +29,7 @@ from .xlate import XLATOR from .base_module_info import BaseModuleInfoError, BaseModuleInfo -__version__ = '0.3.0' +__version__ = '0.4.0' LOG = logging.getLogger(__name__) @@ -269,6 +270,50 @@ class ModuleReleaseInfo(FbBaseObject): return res + # ------------------------------------------------------------------------- + def __copy__(self): + + release = ModuleReleaseInfo( + appname=self.appname, verbose=self.verbose, base_dir=self.base_dir) + release.created_at = self.created_at + release.deleted_at = self.deleted_at + release.file_size = self.file_size + release.file_uri = self.file_uri + release.slug = self.slug + release.supported = self.supported + release.uri = self.uri + release.release_version = self.release_version + + release.initialized = self.initialized + return release + + # ------------------------------------------------------------------------- + def __eq__(self, other): + + if self.verbose > 4: + LOG.debug(_("Comparing {} objects ...").format(self.__class__.__name__)) + + if not isinstance(other, ModuleReleaseInfo): + return False + + if self.created_at != other.created_at: + return False + if self.deleted_at != other.deleted_at: + return False + if self.file_size != other.file_size: + return False + if self.file_uri != other.file_uri: + return False + if self.slug != other.slug: + return False + if self.supported != other.supported: + return False + if self.uri != other.uri: + return False + if self.release_version != other.release_version: + return False + + return True # ------------------------------------------------------------------------- @classmethod @@ -298,6 +343,180 @@ class ModuleReleaseInfo(FbBaseObject): return release + +# ============================================================================= +class ModuleReleaseList(collections.MutableSequence, FbBaseObject): + + msg_no_release = _("Invalid type {t!r} as an item of a {c}, only {o} objects are allowed.") + + # ------------------------------------------------------------------------- + def __init__( + self, appname=None, verbose=0, version=__version__, base_dir=None, + initialized=None, *releases): + + self._list = [] + + super(ModuleReleaseList, self).__init__( + appname=appname, verbose=verbose, version=version, base_dir=base_dir, initialized=False) + + for release in releases: + self.append(release) + + # ------------------------------------------------------------------------- + def as_dict(self, short=True): + + res = super(ModuleReleaseList, self).as_dict(short=short) + + res['list'] = [] + for release in self: + res['list'].append(release.as_dict(short=short)) + + return res + + # ------------------------------------------------------------------------- + def to_data(self): + """Returning a list, which can be used to re-instantiate this module info.""" + + res = [] + for release in self: + res.append(release.to_data()) + + return res + + # ------------------------------------------------------------------------- + def index(self, release, *args): + + i = None + j = None + + if len(args) > 0: + if len(args) > 2: + raise TypeError(_("{m} takes at most {max} arguments ({n} given).").format( + m='index()', max=3, n=len(args) + 1)) + i = int(args[0]) + if len(args) > 1: + j = int(args[1]) + + index = 0 + if i is not None: + start = i + if i < 0: + start = len(self._list) + i + wrap = False + end = len(self._list) + if j is not None: + if j < 0: + end = len(self._list) + j + if end < index: + wrap = True + else: + end = j + for index in list(range(len(self._list))): + item = self._list[index] + if index < start: + continue + if index >= end and not wrap: + break + if item == release: + return index + + if wrap: + for index in list(range(len(self._list))): + item = self._list[index] + if index >= end: + break + if item == release: + return index + + msg = _("Release {!r} is not in release list.").format(release.release_version) + raise ValueError(msg) + + # ------------------------------------------------------------------------- + def __contains__(self, release): + + if not isinstance(release, ModuleReleaseInfo): + raise TypeError(self.msg_no_release.format( + t=release.__class__.__name__, c=self.__class__.__name__, o='ModuleReleaseInfo')) + + if not self._list: + return False + + for item in self._list: + if item == release: + return True + + return False + + # ------------------------------------------------------------------------- + def count(self, release): + + if not isinstance(release, ModuleReleaseInfo): + raise TypeError(self.msg_no_release.format( + t=release.__class__.__name__, c=self.__class__.__name__, o='ModuleReleaseInfo')) + + if not self._list: + return 0 + + num = 0 + for item in self._list: + if item == release: + num += 1 + return num + + # ------------------------------------------------------------------------- + def __len__(self): + return len(self._list) + + # ------------------------------------------------------------------------- + def __getitem__(self, key): + return self._list.__getitem__(key) + + # ------------------------------------------------------------------------- + def __reversed__(self): + + return reversed(self._list) + + # ------------------------------------------------------------------------- + def __setitem__(self, key, release): + + if not isinstance(release, ModuleReleaseInfo): + raise TypeError(self.msg_no_release.format( + t=release.__class__.__name__, c=self.__class__.__name__, o='ModuleReleaseInfo')) + + self._list.__setitem__(key, release) + + # ------------------------------------------------------------------------- + def __delitem__(self, key): + + del self._list[key] + + # ------------------------------------------------------------------------- + def append(self, release): + + if not isinstance(release, ModuleReleaseInfo): + raise TypeError(self.msg_no_release.format( + t=release.__class__.__name__, c=self.__class__.__name__, o='ModuleReleaseInfo')) + + self._list.append(release) + + # ------------------------------------------------------------------------- + def insert(self, index, release): + + if not isinstance(release, ModuleReleaseInfo): + raise TypeError(self.msg_no_release.format( + t=release.__class__.__name__, c=self.__class__.__name__, o='ModuleReleaseInfo')) + + self._list.insert(index, release) + + # ------------------------------------------------------------------------- + def __copy__(self): + + new_list = self.__class__() + for release in self._list: + new_list.append(copy.copy(release)) + return new_list + + # ============================================================================= class ForgeModuleInfo(BaseModuleInfo): """Class for encapsulating all information about a Puppet module from Puppet Forge.""" @@ -307,7 +526,7 @@ class ForgeModuleInfo(BaseModuleInfo): self, appname=None, verbose=0, version=__version__, base_dir=None, initialized=None, name=None, vendor=None, full_name=None): - self.releases = [] + self.releases = None super(ForgeModuleInfo, self).__init__( appname=appname, verbose=verbose, version=version, base_dir=base_dir, @@ -322,9 +541,21 @@ class ForgeModuleInfo(BaseModuleInfo): res = super(ForgeModuleInfo, self).as_dict(short=short) +# res['releases'] = [] +# for release in self.releases: +# res['releases'].append(release.as_dict(short=short)) + + return res + + # ------------------------------------------------------------------------- + def to_data(self): + """Returning a dict, which can be used to re-instantiate this module info.""" + + res = {} + res['releases'] = [] for release in self.releases: - res['releases'].append(release.as_dict(short=short)) + res['releases'].append(release.to_data()) return res @@ -378,6 +609,9 @@ class ForgeModuleInfo(BaseModuleInfo): if verbose > 4: LOG.debug("Performing forge data:\n" + pp(js_info)) + module_info.releases = ModuleReleaseList( + appname=appname, verbose=verbose, base_dir=base_dir) + if 'releases' in js_info: for rel in js_info['releases']: release = ModuleReleaseInfo.get_from_forge( @@ -385,6 +619,8 @@ class ForgeModuleInfo(BaseModuleInfo): if release: module_info.releases.append(release) + module_info.releases.imnitialized = True + # if 'current_release' in js_info and js_info['current_release']: # if 'version' in js_info['current_release']: # version = js_info['current_release']['version']