Rename some internal functions, clarify what they do

get_hosts → used externally, not changed
_get_hosts → _evaluate_patterns (takes a list, evaluates ! and &)
__get_hosts → _match_one_pattern (takes one pattern only, ignores !&)
pull/11958/head
Abhijit Menon-Sen 9 years ago committed by James Cammarata
parent eff33bac49
commit 6bf6b9fdbb

@ -174,8 +174,9 @@ class Inventory(object):
def get_hosts(self, pattern="all"): def get_hosts(self, pattern="all"):
""" """
find all host names matching a pattern string, taking into account any inventory restrictions or Takes a pattern or list of patterns and returns a list of matching
applied subsets. inventory host names, taking into account any active restrictions
or applied subsets
""" """
# Enumerate all hosts matching the given pattern (which may be # Enumerate all hosts matching the given pattern (which may be
@ -183,11 +184,11 @@ class Inventory(object):
if isinstance(pattern, list): if isinstance(pattern, list):
pattern = ':'.join(pattern) pattern = ':'.join(pattern)
patterns = self._split_pattern(pattern) patterns = self._split_pattern(pattern)
hosts = self._get_hosts(patterns) hosts = self._evaluate_patterns(patterns)
# exclude hosts not in a subset, if defined # exclude hosts not in a subset, if defined
if self._subset: if self._subset:
subset = self._get_hosts(self._subset) subset = self._evaluate_patterns(self._subset)
hosts = [ h for h in hosts if h in subset ] hosts = [ h for h in hosts if h in subset ]
# exclude hosts mentioned in any restriction (ex: failed hosts) # exclude hosts mentioned in any restriction (ex: failed hosts)
@ -196,10 +197,10 @@ class Inventory(object):
return hosts return hosts
def _get_hosts(self, patterns): def _evaluate_patterns(self, patterns):
""" """
finds hosts that match a list of patterns. Handles negative Takes a list of patterns and returns a list of matching host names,
matches as well as intersection matches. taking into account any negative and intersection patterns.
""" """
# Host specifiers should be sorted to ensure consistent behavior # Host specifiers should be sorted to ensure consistent behavior
@ -230,7 +231,7 @@ class Inventory(object):
if p in self._hosts_cache: if p in self._hosts_cache:
hosts.append(self.get_host(p)) hosts.append(self.get_host(p))
else: else:
that = self.__get_hosts(p) that = self._match_one_pattern(p)
if p.startswith("!"): if p.startswith("!"):
hosts = [ h for h in hosts if h not in that ] hosts = [ h for h in hosts if h not in that ]
elif p.startswith("&"): elif p.startswith("&"):
@ -240,10 +241,11 @@ class Inventory(object):
hosts.extend(to_append) hosts.extend(to_append)
return hosts return hosts
def __get_hosts(self, pattern): def _match_one_pattern(self, pattern):
""" """
finds hosts that positively match a particular pattern. Does not Takes a single pattern (i.e., not "p1:p2") and returns a list of
take into account negative matches. matching hosts names. Does not take negatives or intersections
into account.
""" """
if pattern in self._pattern_cache: if pattern in self._pattern_cache:

Loading…
Cancel
Save