blob: e66bb6791f51d42ba5b2fc80a6620199e6321112 [file] [log] [blame]
Olivier Bourdon99c9bcb2017-09-07 17:58:42 +02001# -*- coding: utf-8 -*-
2'''
3Module for defining new filter for sorting
4host names/alias by FQDN first and alphabetically
5'''
6
7from jinja2 import Undefined
8
azvyagintsev42b64a12018-08-17 11:47:45 +03009def __virtual__():
10 return 'linux_hosts'
11
Olivier Bourdon99c9bcb2017-09-07 17:58:42 +020012def fqdn_sort_fn(n1, n2):
13 l1 = n1.split('.')
14 l2 = n2.split('.')
15 if len(l1) > len(l2):
16 return -1
17 if len(l1) < len(l2):
18 return 1
19 for i1, i2 in zip(l1, l2):
20 if i1 < i2:
21 return -1
22 if i1 > i2:
23 return 1
24 return 0
25
26def fqdn_sort_filter(iterable):
27 if iterable is None or isinstance(iterable, Undefined):
28 return iterable
29 # Do effective custom sorting of iterable here
Olivier Bourdonc8f68822017-09-13 13:35:22 +020030 return sorted(set(iterable), cmp=fqdn_sort_fn)