blob: 6d0ab4081f90ef312b58c218dc5c12c52d06187e [file] [log] [blame]
Ales Komarek8f8eee72017-10-27 08:56:52 +02001
2from collections import OrderedDict
3
4
5def __virtual__():
6 return True
7
8
9def _set_subtree(node, relationships):
10 return {
11 v: _set_subtree(v, relationships)
12 for v in [x['id'] for x in relationships if node in x['require']]
13 }
14
15
16def _traverse_subtree(output, data):
17 for key, value in data.items():
18 output.append(key)
19 _traverse_subtree(output, value)
20 return output
21
22
23def order_by_requisites(data):
24 '''
25 Returns dictionary ordered by require and require_by
26
27 CLI Examples:
28
29 .. code-block:: bash
30
31 salt-call modelutils.order_by_requisites "{'dict':'value'}""
32
33 Sample data
34
35 passed_data:
36 syslog2:
37 pattern: 'syslog.*'
38 syslog_tele1:
39 type: parser
40 require:
41 - syslog1
42 syslog1:
43 pattern: 'syslog.*'
44 require_in:
45 - syslog2
46 syslog_tele2:
47 require:
48 - syslog_tele1
49
50 '''
51 raw_key_list = []
52 ordered_key_list = []
53 output_dict = OrderedDict()
54
55 for datum_id, datum in data.items():
56 if 'require_in' in datum:
57 for req in datum['require_in']:
58 if 'require' not in data[req]:
59 data[req]['require'] = []
60 data[req]['require'].append(datum_id)
61 datum.pop('require_in')
62
63 for datum_id, datum in data.items():
64 if 'require' not in datum:
65 datum['require'] = ['top']
66 datum['id'] = datum_id
67 raw_key_list.append(datum)
68
69 tree_data = _set_subtree('top', raw_key_list)
70 _traverse_subtree(ordered_key_list, tree_data)
71 for key in ordered_key_list:
72 output_dict[key] = data[key]
73
74 return output_dict
75