]> Frank Brehm's Git Trees - pixelpark/puppet-tools.git/commitdiff
Adding module dpx_puppettools.forge.mod_release_list for class ModuleReleaseList.
authorFrank Brehm <frank.brehm@pixelpark.com>
Fri, 10 Feb 2023 09:53:54 +0000 (10:53 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Fri, 10 Feb 2023 09:53:54 +0000 (10:53 +0100)
lib/dpx_puppettools/forge/mod_release_list.py [new file with mode: 0644]

diff --git a/lib/dpx_puppettools/forge/mod_release_list.py b/lib/dpx_puppettools/forge/mod_release_list.py
new file mode 100644 (file)
index 0000000..33b8442
--- /dev/null
@@ -0,0 +1,218 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+"""
+@author: Frank Brehm
+@contact: frank.brehm@pixelpark.com
+@copyright: © 2023 by Frank Brehm, Digitas Pixelpark GmbH, Berlin
+@summary: A module for a list of ModuleReleaseInfo objects.
+"""
+from __future__ import absolute_import
+
+# Standard modules
+import logging
+import copy
+
+try:
+    from collections.abc import MutableSequence
+except ImportError:
+    from collections import MutableSequence
+
+# Third party modules
+from fb_tools.obj import FbBaseObject
+
+# Own modules
+from ..xlate import XLATOR
+
+from .mod_release_info import ModuleReleaseInfo
+
+__version__ = '0.2.0'
+
+LOG = logging.getLogger(__name__)
+
+_ = XLATOR.gettext
+ngettext = XLATOR.ngettext
+
+
+# =============================================================================
+class ModuleReleaseList(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__(
+            appname=self.appname, verbose=self.verbose, base_dir=self.base_dir)
+        for release in self._list:
+            new_list.append(copy.copy(release))
+        return new_list
+
+
+# =============================================================================
+if __name__ == "__main__":
+
+    pass
+
+# =============================================================================
+
+# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 list