-
Notifications
You must be signed in to change notification settings - Fork 29
/
searchpaths.py
42 lines (41 loc) · 1.83 KB
/
searchpaths.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
def searchpaths(scope):
"""Iterator over search paths.
>>> searchpaths(dict(host="titi"))
['host/titi', 'common']
>>> searchpaths(dict(host="titi.sk1", shorthost="titi", location="sk1"))
['host/titi.sk1', 'host/sk1/titi', 'groups/sk1', 'common']
>>> searchpaths(dict(groups=["tor", "tor-bgp"], location="sk1", continent="oc"))
['groups/tor-bgp-sk1', 'groups/tor-sk1', 'groups/tor-bgp-oc', 'groups/tor-oc', 'groups/tor-bgp', 'groups/tor', 'groups/sk1', 'common']
"""
paths = [
"host/{scope[host]}",
"host/{scope[environment]}.{scope[location]}/{scope[shorthost]}",
"host/{scope[location]}/{scope[shorthost]}",
*[f"groups/{group}{path}" for path in [
"-{scope[os]}-{scope[model]}-member{scope[member]}",
"-member{scope[member]}",
"-{scope[environment]}.{scope[location]}-pod{scope[pod]}",
"-{scope[location]}-pod{scope[pod]}",
"-{scope[environment]}.{scope[location]}-{scope[sublocation]}",
"-{scope[location]}-{scope[sublocation]}",
"-{scope[environment]}.{scope[location]}",
"-{scope[location]}",
"-{scope[continent]}",
"-{scope[os]}-{scope[model]}",
"",
] for group in scope.get('groups', [])[::-1]],
"groups/{scope[environment]}.{scope[location]}-{scope[sublocation]}",
"groups/{scope[location]}-{scope[sublocation]}",
"groups/{scope[environment]}.{scope[location]}",
"groups/{scope[location]}",
"os/{scope[os]}-{scope[model]}",
"os/{scope[os]}-{scope[location]}",
"os/{scope[os]}",
'common'
]
for idx in range(len(paths)):
try:
paths[idx] = paths[idx].format(scope=scope)
except KeyError:
paths[idx] = None
return [path for path in paths if path]