From 84a77afbef86aab624ec09139722039355bda27f Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Tue, 6 Nov 2018 11:53:20 +0100 Subject: [PATCH] Adding cmdline parameter for sort order of mudule lists --- lib/webhooks/__init__.py | 2 +- lib/webhooks/base_app.py | 26 ++++++++++++++++- lib/webhooks/get_forge_modules.py | 6 ++-- lib/webhooks/module_list.py | 46 +++++++++++++++++++++++-------- 4 files changed, 65 insertions(+), 15 deletions(-) diff --git a/lib/webhooks/__init__.py b/lib/webhooks/__init__.py index 77a1597..8e714de 100644 --- a/lib/webhooks/__init__.py +++ b/lib/webhooks/__init__.py @@ -1,6 +1,6 @@ #!/bin/env python3 # -*- coding: utf-8 -*- -__version__ = '1.1.2' +__version__ = '1.2.1' # vim: ts=4 et list diff --git a/lib/webhooks/base_app.py b/lib/webhooks/base_app.py index 63f02af..c7575ca 100644 --- a/lib/webhooks/base_app.py +++ b/lib/webhooks/base_app.py @@ -124,6 +124,7 @@ class BaseHookApp(BaseObject): self._cachefile = self.default_cachefile_stem self._mail_subject = None self._mail_headline = None + self._sort_by_name = False if not hasattr(self, '_output_type'): self._output_type = self.default_output_type @@ -231,6 +232,12 @@ class BaseHookApp(BaseObject): def read_stdin(self, value): self._read_stdin = to_bool(value) + # ----------------------------------------------------------- + @property + def sort_by_name(self): + """Sorting all lists of modules by name and vendor, instead of the full name.""" + return self._sort_by_name + # ----------------------------------------------------------- @property def html_title(self): @@ -371,6 +378,7 @@ class BaseHookApp(BaseObject): res['main_branches'] = copy.copy(self.main_branches) res['mail_subject'] = self.mail_subject res['mail_headline'] = self.mail_headline + res['sort_by_name'] = self.sort_by_name return res @@ -408,6 +416,18 @@ class BaseHookApp(BaseObject): help="Data directory, default: {!r}.".format(self.data_dir), ) + sort_group = general_group.add_mutually_exclusive_group() + + sort_group.add_argument( + '-o', '--sort-by-name', action="store_true", dest='sort_by_name', + help="Sorting all lists of modules by name and vendor, in this order." + ) + + sort_group.add_argument( + '-O', '--sort-by-fullname', action="store_false", dest='sort_by_name', + help="Sorting all lists of modules by the full name of the module (default)." + ) + general_group.add_argument( "-v", "--verbose", action="count", dest='verbose', help='Increase the verbosity level', @@ -480,6 +500,9 @@ class BaseHookApp(BaseObject): sys.stderr.write("\nSimulation mode - nothing is really done.\n\n") self.simulate = True + if self.cmdline_args.sort_by_name: + self._sort_by_name = True + self._get_query() if 'output_type' in self.query: try: @@ -1043,7 +1066,8 @@ class BaseHookApp(BaseObject): "Cache file {!r} not readable.".format(self.cachefile)) modules = ModuleInfoDict( - appname=self.appname, verbose=self.verbose, base_dir=self.base_dir) + appname=self.appname, verbose=self.verbose, base_dir=self.base_dir, + sort_by_name=self.sort_by_name) LOG.debug("Reading {!r} ...".format(self.cachefile)) try: diff --git a/lib/webhooks/get_forge_modules.py b/lib/webhooks/get_forge_modules.py index aa31eea..8783409 100644 --- a/lib/webhooks/get_forge_modules.py +++ b/lib/webhooks/get_forge_modules.py @@ -84,7 +84,8 @@ class GetForgeModulesApp(BaseHookApp): appname=appname, verbose=verbose, version=version) self.modules = ModuleInfoDict( - appname=self.appname, verbose=self.verbose, base_dir=self.base_dir) + appname=self.appname, verbose=self.verbose, base_dir=self.base_dir, + sort_by_name=self.sort_by_name) # ----------------------------------------------------------- @property @@ -203,7 +204,8 @@ class GetForgeModulesApp(BaseHookApp): def collect_local_modules(self): self.modules = ModuleInfoDict( - appname=self.appname, verbose=self.verbose, base_dir=self.base_dir) + appname=self.appname, verbose=self.verbose, base_dir=self.base_dir, + sort_by_name=self.sort_by_name) for env in self.environments: LOG.debug("Evaluating environment {!r} ..".format(env)) diff --git a/lib/webhooks/module_list.py b/lib/webhooks/module_list.py index aaa646c..3666d0a 100644 --- a/lib/webhooks/module_list.py +++ b/lib/webhooks/module_list.py @@ -19,10 +19,11 @@ from functools import cmp_to_key # Own modules from .obj import BaseObject +from .common import to_bool from .module_info import ModuleInfo -__version__ = '1.0.0' +__version__ = '1.1.0' LOG = logging.getLogger(__name__) @@ -46,18 +47,32 @@ class ModuleInfoDict(MutableMapping, BaseObject): # ------------------------------------------------------------------------- # __init__() method required to create instance from class. def __init__( - self, appname=None, verbose=0, version=__version__, base_dir=None, *args, **kwargs): + self, appname=None, verbose=0, version=__version__, base_dir=None, + sort_by_name=False, *args, **kwargs): self._map = dict() + self._sort_by_name = False super(ModuleInfoDict, self).__init__( appname=appname, verbose=verbose, version=version, base_dir=base_dir, initialized=False, ) + self.sort_by_name = sort_by_name + for arg in args: self.append(arg) + # ----------------------------------------------------------- + @property + def sort_by_name(self): + """Sorting modules by name and vendor, instead of the full name.""" + return self._sort_by_name + + @sort_by_name.setter + def sort_by_name(self, value): + self._sort_by_name = to_bool(value) + # ------------------------------------------------------------------------- def _set_item(self, key, module_info): @@ -82,6 +97,7 @@ class ModuleInfoDict(MutableMapping, BaseObject): res = super(ModuleInfoDict, self).as_dict(short=short) + res['sort_by_name'] = self.sort_by_name res['items'] = {} res['keys'] = [] for full_name in self.keys(): @@ -184,16 +200,24 @@ class ModuleInfoDict(MutableMapping, BaseObject): def keys(self): def compare_items(x, y): + if self.sort_by_name: + if self.verbose > 4: + LOG.debug("Comparing names {!r} > {!r}".format(x.name, y.name)) + if x.name != y.name: + if x.name > y.name: + return 1 + return -1 + if self.verbose > 4: + LOG.debug("Comparing vendor {!r} > {!r}".format(x.vendor, y.vendor)) + if x.vendor != y.vendor: + if x.vendor > y.vendor: + return 1 + return -1 + return 0 if self.verbose > 4: - LOG.debug("Comparing names {!r} > {!r}".format(x.name, y.name)) - if x.name != y.name: - if x.name > y.name: - return 1 - return -1 - if self.verbose > 4: - LOG.debug("Comparing vendor {!r} > {!r}".format(x.vendor, y.vendor)) - if x.vendor != y.vendor: - if x.vendor > y.vendor: + LOG.debug("Comparing full names {!r} > {!r}".format(x.full_name, y.full_name)) + if x.full_name != y.full_name: + if x.full_name > y.full_name: return 1 return -1 return 0 -- 2.39.5