5
5
from __future__ import absolute_import , print_function , unicode_literals
6
6
7
7
# Import python libs
8
+ import copy
8
9
import fnmatch
10
+ import functools
9
11
import logging
10
12
import re
11
- import copy
12
13
13
14
# Try to import range from https://github.com/ytoolshed/range
14
15
HAS_RANGE = False
@@ -31,6 +32,18 @@ def targets(conditioned_raw, tgt, tgt_type, ipv='ipv4'):
31
32
return rmatcher .targets ()
32
33
33
34
35
+ def _tgt_set (tgt ):
36
+ '''
37
+ Return the tgt as a set of literal names
38
+ '''
39
+ try :
40
+ # A comma-delimited string
41
+ return set (tgt .split (',' ))
42
+ except AttributeError :
43
+ # Assume tgt is already a non-string iterable.
44
+ return set (tgt )
45
+
46
+
34
47
class RosterMatcher (object ):
35
48
'''
36
49
Matcher for the roster data structure
@@ -50,59 +63,47 @@ def targets(self):
50
63
except AttributeError :
51
64
return {}
52
65
53
- def ret_glob_minions (self ):
66
+ def _ret_minions (self , filter_ ):
54
67
'''
55
- Return minions that match via glob
68
+ Filter minions by a generic filter.
56
69
'''
57
70
minions = {}
58
- for minion in self .raw :
59
- if fnmatch .fnmatch (minion , self .tgt ):
60
- data = self .get_data (minion )
61
- if data :
62
- minions [minion ] = data .copy ()
71
+ for minion in filter_ (self .raw ):
72
+ data = self .get_data (minion )
73
+ if data :
74
+ minions [minion ] = data .copy ()
63
75
return minions
64
76
77
+ def ret_glob_minions (self ):
78
+ '''
79
+ Return minions that match via glob
80
+ '''
81
+ fnfilter = functools .partial (fnmatch .filter , pat = self .tgt )
82
+ return self ._ret_minions (fnfilter )
83
+
65
84
def ret_pcre_minions (self ):
66
85
'''
67
86
Return minions that match via pcre
68
87
'''
69
- minions = {}
70
- for minion in self .raw :
71
- if re .match (self .tgt , minion ):
72
- data = self .get_data (minion )
73
- if data :
74
- minions [minion ] = data .copy ()
75
- return minions
88
+ tgt = re .compile (self .tgt )
89
+ refilter = functools .partial (filter , tgt .match )
90
+ return self ._ret_minions (refilter )
76
91
77
92
def ret_list_minions (self ):
78
93
'''
79
94
Return minions that match via list
80
95
'''
81
- minions = {}
82
- if not isinstance (self .tgt , list ):
83
- self .tgt = self .tgt .split (',' )
84
- for minion in self .raw :
85
- if minion in self .tgt :
86
- data = self .get_data (minion )
87
- if data :
88
- minions [minion ] = data .copy ()
89
- return minions
96
+ tgt = _tgt_set (self .tgt )
97
+ return self ._ret_minions (tgt .intersection )
90
98
91
99
def ret_nodegroup_minions (self ):
92
100
'''
93
101
Return minions which match the special list-only groups defined by
94
102
ssh_list_nodegroups
95
103
'''
96
- minions = {}
97
104
nodegroup = __opts__ .get ('ssh_list_nodegroups' , {}).get (self .tgt , [])
98
- if not isinstance (nodegroup , list ):
99
- nodegroup = nodegroup .split (',' )
100
- for minion in self .raw :
101
- if minion in nodegroup :
102
- data = self .get_data (minion )
103
- if data :
104
- minions [minion ] = data .copy ()
105
- return minions
105
+ nodegroup = _tgt_set (nodegroup )
106
+ return self ._ret_minions (nodegroup .intersection )
106
107
107
108
def ret_range_minions (self ):
108
109
'''
@@ -113,13 +114,7 @@ def ret_range_minions(self):
113
114
114
115
minions = {}
115
116
range_hosts = _convert_range_to_list (self .tgt , __opts__ ['range_server' ])
116
-
117
- for minion in self .raw :
118
- if minion in range_hosts :
119
- data = self .get_data (minion )
120
- if data :
121
- minions [minion ] = data .copy ()
122
- return minions
117
+ return self ._ret_minions (range_hosts .__contains__ )
123
118
124
119
def get_data (self , minion ):
125
120
'''
0 commit comments