From: Frank Brehm Date: Tue, 26 Feb 2019 17:12:29 +0000 (+0100) Subject: Adding class ForgeModulesList X-Git-Tag: 1.6.4^2~40 X-Git-Url: https://git.uhu-banane.net/?a=commitdiff_plain;h=f5894b30652fb18c04d62999792aaed54df7660d;p=pixelpark%2Fpuppetmaster-webhooks.git Adding class ForgeModulesList --- diff --git a/lib/webhooks/forge_module_info.py b/lib/webhooks/forge_module_info.py index 2a3c9e7..8adda96 100644 --- a/lib/webhooks/forge_module_info.py +++ b/lib/webhooks/forge_module_info.py @@ -31,7 +31,7 @@ from .base_module_info import BaseModuleInfoError, BaseModuleInfo from .module_meta_info import ModuleMetadata -__version__ = '0.6.4' +__version__ = '0.7.1' LOG = logging.getLogger(__name__) @@ -1747,6 +1747,179 @@ class ForgeModuleInfo(BaseModuleInfo): return module_info +# ============================================================================= +class ForgeModulesList(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, *modules): + + self._list = [] + + super(ForgeModulesList, self).__init__( + appname=appname, verbose=verbose, version=version, base_dir=base_dir, initialized=False) + + for mod in modules: + self.append(mod) + + # ------------------------------------------------------------------------- + def as_dict(self, short=True): + + res = super(ForgeModulesList, self).as_dict(short=short) + + res['list'] = [] + for mod in self: + res['list'].append(mod.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 mod in self: + res.append(mod.to_data()) + + return res + + # ------------------------------------------------------------------------- + def index(self, mod, *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 == mod: + return index + + if wrap: + for index in list(range(len(self._list))): + item = self._list[index] + if index >= end: + break + if item == mod: + return index + + msg = _("Module {!r} is not in modules list.").format(mod.full_name) + raise ValueError(msg) + + # ------------------------------------------------------------------------- + def __contains__(self, mod): + + if not isinstance(mod, ForgeModuleInfo): + raise TypeError(self.msg_no_release.format( + t=mod.__class__.__name__, c=self.__class__.__name__, o='ForgeModuleInfo')) + + if not self._list: + return False + + for item in self._list: + if item == mod: + return True + + return False + + # ------------------------------------------------------------------------- + def count(self, mod): + + if not isinstance(mod, ForgeModuleInfo): + raise TypeError(self.msg_no_release.format( + t=mod.__class__.__name__, c=self.__class__.__name__, o='ForgeModuleInfo')) + + if not self._list: + return 0 + + num = 0 + for item in self._list: + if item == mod: + 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, mod): + + if not isinstance(mod, ForgeModuleInfo): + raise TypeError(self.msg_no_release.format( + t=mod.__class__.__name__, c=self.__class__.__name__, o='ForgeModuleInfo')) + + self._list.__setitem__(key, mod) + + # ------------------------------------------------------------------------- + def __delitem__(self, key): + + del self._list[key] + + # ------------------------------------------------------------------------- + def append(self, mod): + + if not isinstance(mod, ForgeModuleInfo): + raise TypeError(self.msg_no_release.format( + t=mod.__class__.__name__, c=self.__class__.__name__, o='ForgeModuleInfo')) + + self._list.append(mod) + + # ------------------------------------------------------------------------- + def insert(self, index, mod): + + if not isinstance(mod, ForgeModuleInfo): + raise TypeError(self.msg_no_release.format( + t=mod.__class__.__name__, c=self.__class__.__name__, o='ForgeModuleInfo')) + + self._list.insert(index, mod) + + # ------------------------------------------------------------------------- + def __copy__(self): + + new_list = self.__class__() + for mod in self._list: + new_list.append(copy.copy(mod)) + return new_list + + # ============================================================================= if __name__ == "__main__": diff --git a/lib/webhooks/get_forge_modules.py b/lib/webhooks/get_forge_modules.py index 09e8bc7..ef2b89a 100644 --- a/lib/webhooks/get_forge_modules.py +++ b/lib/webhooks/get_forge_modules.py @@ -38,7 +38,7 @@ from . import __version__ from .base_app import BaseHookError, BaseHookApp -from .forge_module_info import ForgeModuleInfo +from .forge_module_info import ForgeModuleInfo, ForgeModulesList from .module_info import ModuleInfo @@ -94,9 +94,7 @@ class GetForgeModulesApp(BaseHookApp): appname=appname, base_dir=base_dir, verbose=verbose, version=version, description=description) - self.forge_modules = ModuleInfoDict( - appname=self.appname, verbose=self.verbose, base_dir=self.base_dir, - sort_by_name=self.sort_by_name) + self._init_forge_module_list() # ----------------------------------------------------------- @property @@ -118,6 +116,12 @@ class GetForgeModulesApp(BaseHookApp): help=_("Don't get module information from Puppet forge."), ) + # ------------------------------------------------------------------------- + def _init_forge_module_list(self): + + self.forge_modules = ForgeModulesList( + appname=self.appname, verbose=self.verbose, base_dir=self.base_dir) + # ------------------------------------------------------------------------- def perform_arg_parser(self): @@ -237,7 +241,8 @@ class GetForgeModulesApp(BaseHookApp): LOG.info(msg) self.print_out(msg) modules_done = [] - self.forge_modules = [] + + self._init_forge_module_list() for env in self.environments: