Module: check_mk
Branch: master
Commit: 56e47ea3187954187a8fd59250a4ef1a10a91411
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=56e47ea3187954…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Mon Feb 19 08:02:22 2018 +0100
Moved num_split() from lib to utils
Change-Id: I3dd5cdcdced4f73f9e7e6c7f6bfb869e25bbf9b3
---
web/htdocs/availability.py | 7 +++--
web/htdocs/lib.py | 15 ---------
web/htdocs/table.py | 6 ++--
web/htdocs/utils.py | 78 ++++++++++++++++++++++++++++++++++++++++++++++
web/htdocs/views.py | 5 ++-
web/htdocs/wato.py | 3 +-
6 files changed, 89 insertions(+), 25 deletions(-)
diff --git a/web/htdocs/availability.py b/web/htdocs/availability.py
index 057a9a1..9bce3d9 100644
--- a/web/htdocs/availability.py
+++ b/web/htdocs/availability.py
@@ -24,6 +24,7 @@
# to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
# Boston, MA 02110-1301 USA.
+import utils
import bi, views, visuals
import sites
# TODO: Get rid of import of views
@@ -1985,10 +1986,10 @@ def get_av_groups(availability_table, avoptions):
# Sort according to host and service. First after site, then
# host (natural sort), then service
def cmp_av_entry(a, b):
- return cmp(a["site"], b["site"]) or \
- cmp(num_split(a["host"]) + (a["host"],),
num_split(b["host"]) + (b["host"],)) or \
+ return utils.cmp_num_split(a["site"], b["site"]) or \
+ utils.cmp_num_split(a["host"], b["host"]) or \
cmp(views.cmp_service_name_equiv(a["service"]),
views.cmp_service_name_equiv(b["service"])) or \
- cmp(a["service"], b["service"])
+ utils.cmp_num_split(a["service"], b["service"])
def history_url_of(av_object, time_range):
diff --git a/web/htdocs/lib.py b/web/htdocs/lib.py
index 3c37a73..cb5c7d4 100644
--- a/web/htdocs/lib.py
+++ b/web/htdocs/lib.py
@@ -141,18 +141,3 @@ def tryint(x):
return int(x)
except:
return x
-
-
-# Splits a word into sequences of numbers and non-numbers.
-# Creates a tuple from these where the number are converted
-# into int datatype. That way a naturual sort can be
-# implemented.
-def num_split(s):
- parts = []
- for part in re.split('(\d+)', s):
- try:
- parts.append(int(part))
- except ValueError:
- parts.append(part)
-
- return tuple(parts)
diff --git a/web/htdocs/table.py b/web/htdocs/table.py
index 0d9e75a..a6abf10 100644
--- a/web/htdocs/table.py
+++ b/web/htdocs/table.py
@@ -27,8 +27,8 @@
import re
from contextlib import contextmanager
+import utils
import config
-from lib import num_split
tables = []
@@ -521,8 +521,8 @@ def _sort_rows(rows, sort_col, sort_reverse):
# sorting. This gives the user the chance to change the sorting and
# see the table in the first place.
try:
- rows.sort(cmp=lambda a, b: cmp(num_split(a[0][sort_col][0]),
- num_split(b[0][sort_col][0])),
+ rows.sort(cmp=lambda a, b: utils.cmp_num_split(a[0][sort_col][0],
+ b[0][sort_col][0]),
reverse=sort_reverse==1)
except IndexError:
pass
diff --git a/web/htdocs/utils.py b/web/htdocs/utils.py
new file mode 100644
index 0000000..55adb65
--- /dev/null
+++ b/web/htdocs/utils.py
@@ -0,0 +1,78 @@
+#!/usr/bin/python
+# -*- encoding: utf-8; py-indent-offset: 4 -*-
+# +------------------------------------------------------------------+
+# | ____ _ _ __ __ _ __ |
+# | / ___| |__ ___ ___| | __ | \/ | |/ / |
+# | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
+# | | |___| | | | __/ (__| < | | | | . \ |
+# | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
+# | |
+# | Copyright Mathias Kettner 2014 mk(a)mathias-kettner.de |
+# +------------------------------------------------------------------+
+#
+# This file is part of Check_MK.
+# The official homepage is at
http://mathias-kettner.de/check_mk.
+#
+# check_mk is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation in version 2. check_mk is distributed
+# in the hope that it will be useful, but WITHOUT ANY WARRANTY; with-
+# out even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+# PARTICULAR PURPOSE. See the GNU General Public License for more de-
+# tails. You should have received a copy of the GNU General Public
+# License along with GNU Make; see the file COPYING. If not, write
+# to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+# Boston, MA 02110-1301 USA.
+
+"""This is an unsorted collection of small unrelated helper functions
which are
+usable in all components of the Web GUI of Check_MK
+
+Please try to find a better place for the things you want to put here."""
+
+import re
+
+
+def drop_dotzero(v, digits=2):
+ """Renders a number as a floating point number and drops useless
+ zeroes at the end of the fraction
+
+ 45.1 -> "45.1"
+ 45.0 -> "45"
+ """
+ t = "%%.%df" % digits % v
+ if "." in t:
+ return t.rstrip("0").rstrip(".")
+ else:
+ return t
+
+
+def num_split(s):
+ """Splits a word into sequences of numbers and non-numbers.
+
+ Creates a tuple from these where the number are converted into int datatype.
+ That way a naturual sort can be implemented.
+ """
+ parts = []
+ for part in re.split('(\d+)', s):
+ try:
+ parts.append(int(part))
+ except ValueError:
+ parts.append(part)
+
+ return tuple(parts)
+
+
+def cmp_num_split(a, b):
+ """Compare two strings, separate numbers and non-numbers from
before."""
+ return cmp(num_split(a), num_split(b))
+
+
+def cmp_version(a, b):
+ """Compare two version numbers with each other
+ Allow numeric version numbers, but also characters.
+ """
+ if a == None or b == None:
+ return cmp(a, b)
+ aa = map(num_split, a.split("."))
+ bb = map(num_split, b.split("."))
+ return cmp(aa, bb)
diff --git a/web/htdocs/views.py b/web/htdocs/views.py
index 415f496..259ff52 100644
--- a/web/htdocs/views.py
+++ b/web/htdocs/views.py
@@ -24,6 +24,7 @@
# to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
# Boston, MA 02110-1301 USA.
+import utils
import config, time, os, re, pprint
import weblib, traceback, forms, valuespec, inventory, visuals, metrics
import sites
@@ -3038,9 +3039,7 @@ def cmp_simple_string(column, r1, r2):
return cmp_insensitive_string(v1, v2)
def cmp_num_split(column, r1, r2):
- c1 = num_split(r1[column].lower())
- c2 = num_split(r2[column].lower())
- return cmp(c1, c2)
+ return utils.cmp_num_split(r1[column].lower(), r2[column].lower())
def cmp_string_list(column, r1, r2):
v1 = ''.join(r1.get(column, []))
diff --git a/web/htdocs/wato.py b/web/htdocs/wato.py
index f07c51e..405d986 100644
--- a/web/htdocs/wato.py
+++ b/web/htdocs/wato.py
@@ -101,6 +101,7 @@ import time
import traceback
from hashlib import sha256
+import utils
import i18n
import config
import table
@@ -811,7 +812,7 @@ class ModeFolder(WatoMode):
show_checkboxes = html.var('show_checkboxes', '0') ==
'1'
hostnames = self._folder.hosts().keys()
- hostnames.sort(cmp = lambda a, b: cmp(num_split(a), num_split(b)))
+ hostnames.sort(cmp=utils.cmp_num_split)
search_text = html.var("search")
# Helper function for showing bulk actions. This is needed at the bottom