]> Frank Brehm's Git Trees - pixelpark/puppet-tools.git/commitdiff
Adding module dpx_puppettools.forge.base_module_info for class BaseForgeModuleInfo.
authorFrank Brehm <frank.brehm@pixelpark.com>
Thu, 9 Feb 2023 13:27:20 +0000 (14:27 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Thu, 9 Feb 2023 13:27:20 +0000 (14:27 +0100)
lib/dpx_puppettools/forge/base_module_info.py [new file with mode: 0644]
lib/dpx_puppettools/forge/owner_info.py

diff --git a/lib/dpx_puppettools/forge/base_module_info.py b/lib/dpx_puppettools/forge/base_module_info.py
new file mode 100644 (file)
index 0000000..e12622a
--- /dev/null
@@ -0,0 +1,181 @@
+#!/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 encapsulating basic information about a Forge module
+"""
+from __future__ import absolute_import
+
+# Standard modules
+import logging
+import copy
+import datetime
+
+# Own modules
+
+from ..xlate import XLATOR
+
+from . import parse_forge_date
+from . import BaseForgeObject
+
+from .owner_info import ForgeOwnerInfo
+
+__version__ = '0.2.0'
+
+LOG = logging.getLogger(__name__)
+
+_ = XLATOR.gettext
+ngettext = XLATOR.ngettext
+
+
+# =============================================================================
+class BaseForgeModuleInfo(BaseForgeObject):
+
+    # -------------------------------------------------------------------------
+    def __init__(
+        self, appname=None, verbose=0, version=__version__, base_dir=None,
+            initialized=None):
+
+        self._deprecated_at = None
+        self._name = None
+        self.owner = None
+
+        super(BaseForgeModuleInfo, self).__init__(
+            appname=appname, verbose=verbose, version=version,
+            base_dir=base_dir, initialized=False)
+
+        if initialized is not None:
+            self.initialized = initialized
+
+    # -------------------------------------------------------------------------
+    def as_dict(self, short=True):
+        """
+        Transforms the elements of the object into a dict
+
+        @return: structure as dict
+        @rtype:  dict
+        """
+
+        res = super(BaseForgeModuleInfo, self).as_dict(short=short)
+
+        res['deprecated_at'] = self.deprecated_at
+        res['name'] = self.name
+
+        return res
+
+    # -------------------------------------------------------------------------
+    @property
+    def deprecated_at(self):
+        """Date of deprecation of this module."""
+        return self._deprecated_at
+
+    @deprecated_at.setter
+    def deprecated_at(self, value):
+        if value is None:
+            self._deprecated_at = None
+            return
+        if isinstance(value, datetime.datetime):
+            self._deprecated_at = value
+            return
+        v = str(value).strip()
+        if v == '':
+            self._deprecated_at = None
+            return
+        self._deprecated_at = parse_forge_date(v)
+
+    # -------------------------------------------------------------------------
+    @property
+    def name(self):
+        """The name of this module."""
+        return self._name
+
+    @name.setter
+    def name(self, value):
+        if value is None:
+            self._name = None
+            return
+        v = str(value).strip()
+        if v == '':
+            self._name = None
+            return
+        self._name = v
+
+    # -------------------------------------------------------------------------
+    def to_data(self):
+        """Returning a dict, which can be used to re-instantiate this module info."""
+
+        res = super(BaseForgeModuleInfo, self).to_data()
+
+        res['deprecated_at'] = None
+        if self.deprecated_at:
+            res['deprecated_at'] = self.deprecated_at.strftime('%Y-%m-%d %H:%M:%S %z')
+
+        res['owner'] = None
+        if self.owner:
+            res['owner'] = self.owner.to_data()
+
+        res['name'] = self.name
+
+        return res
+
+    # -------------------------------------------------------------------------
+    def apply_to(self, new):
+
+        if not isinstance(new, BaseForgeModuleInfo):
+            msg = _("Parameter {p!r} is not of class {c!r}, but of {e!r} instead.").format(
+                p='new', c='BaseForgeModuleInfo', e=new.__class__.__name__)
+            raise TypeError(msg)
+
+        super(BaseForgeModuleInfo, self).apply_to(new)
+        new.deprecated_at = self.deprecated_at
+        new.name = self.name
+        if self.owner:
+            new.owner = copy.copy(self.owner)
+
+    # -------------------------------------------------------------------------
+    def __eq__(self, other):
+
+        if self.verbose > 4:
+            LOG.debug(_("Comparing {} objects ...").format(self.__class__.__name__))
+
+        if not super(BaseForgeModuleInfo, self).__eq__(other):
+            return False
+
+        if not isinstance(other, BaseForgeModuleInfo):
+            return False
+
+        if self.deprecated_at != other.deprecated_at:
+            return False
+        if self.name != other.name:
+            return False
+        if self.owner != other.owner:
+            return False
+
+        return True
+
+    # -------------------------------------------------------------------------
+    def apply_data(self, data):
+
+        super(BaseForgeModuleInfo, self).apply_data(data)
+
+        if 'deprecated_at' in data:
+            self.deprecated_at = data['deprecated_at']
+        if 'name' in data:
+            self.name = data['name']
+
+        if 'owner' in data and data['owner']:
+            self.owner = ForgeOwnerInfo.from_data(
+                data['owner'], appname=self.appname,
+                verbose=self.verbose, base_dir=self.base_dir)
+
+
+# =============================================================================
+if __name__ == "__main__":
+
+    pass
+
+# =============================================================================
+
+# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 list
index 82e5eda8d5311dbd6d50991b2649cc91ed6d3716..ea13039885ff08e1f5d96bde209ddc67eb917b15 100644 (file)
@@ -3,7 +3,7 @@
 """
 @author: Frank Brehm
 @contact: frank.brehm@pixelpark.com
-@copyright: © 2023 by Frank Brehm, Publicies Pixelpark GmbH, Berlin
+@copyright: © 2023 by Frank Brehm, Digitas Pixelpark GmbH, Berlin
 @summary: A module for encapsulating the owner information of a forge module.
 """
 from __future__ import absolute_import