From e4464472541893172dfc4bcab5a3aac485033014 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Wed, 8 Nov 2017 11:04:00 +0100 Subject: [PATCH] Adding function compare_fqdn() --- pp_lib/common.py | 56 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/pp_lib/common.py b/pp_lib/common.py index 22700e8..cfcaa3e 100644 --- a/pp_lib/common.py +++ b/pp_lib/common.py @@ -21,7 +21,7 @@ import six # Own modules -__version__ = '0.4.2' +__version__ = '0.5.1' LOG = logging.getLogger(__name__) @@ -32,6 +32,9 @@ RE_TO_BOOL_TRUE = re.compile(PAT_TO_BOOL_TRUE) PAT_TO_BOOL_FALSE = locale.nl_langinfo(locale.NOEXPR) RE_TO_BOOL_FALSE = re.compile(PAT_TO_BOOL_FALSE) +RE_DOT = re.compile(r'\.') +RE_DOT_AT_END = re.compile(r'(\.)*$') + # ============================================================================= def pp(value, indent=4, width=99, depth=None): @@ -286,6 +289,57 @@ def caller_search_path(): return path_list +# ============================================================================= +def compare_fqdn(x, y): + + # First check for None values + if x is None and y is None: + return 0 + if x is None: + return -1 + if y is None: + return 1 + + xs = str(x).strip().lower() + ys = str(y).strip().lower() + + if xs == '' and ys == '': + return 0 + if xs == '': + return -1 + if ys == '': + return 1 + + xs = RE_DOT_AT_END.sub('.', xs) + ys = RE_DOT_AT_END.sub('.', ys) + + if xs == ys: + return 0 + + xa = RE_DOT.split(xs) + xa.reverse() + xa.pop(0) + + ya = RE_DOT.split(ys) + ya.reverse() + ya.pop(0) + + nr_tokens = min(len(xa), len(ya)) + while nr_tokens > 0: + token_x = xa.pop(0) + token_y = ya.pop(0) + if token_x < token_y: + return -1 + elif token_x > token_y: + return 1 + nr_tokens -= 1 + + if len(xa): + return 1 + if len(ya): + return -1 + + return 0 # ============================================================================= -- 2.39.5