]> Frank Brehm's Git Trees - pixelpark/puppetmaster-webhooks.git/commitdiff
Adding cmdline parameter for sort order of mudule lists
authorFrank Brehm <frank.brehm@pixelpark.com>
Tue, 6 Nov 2018 10:53:20 +0000 (11:53 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Tue, 6 Nov 2018 10:53:20 +0000 (11:53 +0100)
lib/webhooks/__init__.py
lib/webhooks/base_app.py
lib/webhooks/get_forge_modules.py
lib/webhooks/module_list.py

index 77a1597213a6db2c8c221434c1ddb384ff82a0db..8e714de3d98fe27d6bbb70d29c76b35c0c6c8510 100644 (file)
@@ -1,6 +1,6 @@
 #!/bin/env python3
 # -*- coding: utf-8 -*-
 
-__version__ = '1.1.2'
+__version__ = '1.2.1'
 
 # vim: ts=4 et list
index 63f02afd8786e19146f2fb3cecc586840406591b..c7575cace1f9d9d763a46ea76ec945421d1eaf92 100644 (file)
@@ -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:
index aa31eea4d1fa1a5a18914bd229e4c65619a3b52a..87834095d094d9cecaa37f8a366be2fb4a167247 100644 (file)
@@ -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))
index aaa646c84d9a1801499545fdcf3adc3fd99d7567..3666d0a3aec06ea13b82634b1c4e4a6f4624b072 100644 (file)
@@ -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