# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.

class Group(object):
    ''' a group of ansible hosts '''

    __slots__ = [ 'name', 'hosts', 'vars', 'child_groups', 'parent_groups', 'depth' ]

    def __init__(self, name=None):

        self.depth = 0
        self.name = name
        self.hosts = []
        self.vars = {}
        self.child_groups = []
        self.parent_groups = []
        if self.name is None:
            raise Exception("group name is required")

    def add_child_group(self, group):

        if self == group:
            raise Exception("can't add group to itself")

        # don't add if it's already there
        if not group in self.child_groups:
            self.child_groups.append(group)
            group.depth = max([self.depth+1, group.depth])
            group.parent_groups.append(self)

    def add_host(self, host):

        self.hosts.append(host)
        host.add_group(self)

    def set_variable(self, key, value):

        self.vars[key] = value

    def get_hosts(self):

        hosts = []
        for kid in self.child_groups:
            kid_hosts = kid.get_hosts()
            for kk in kid_hosts:
                if kk not in hosts:
                    hosts.append(kk)
        for mine in self.hosts:
            if mine not in hosts:
                hosts.append(mine)
        return hosts

    def get_variables(self):
        return self.vars.copy()

    def _get_ancestors(self):

        results = {}
        for g in self.parent_groups:
            results[g.name] = g
            results.update(g._get_ancestors())
        return results

    def get_ancestors(self):

        return self._get_ancestors().values()


@@ -844,16 +844,32 @@
  'depth'
+, '_hosts_cache'
  %5D%0A%0A    
@@ -1050,32 +1050,65 @@
 ent_groups = %5B%5D%0A
+        self.clear_hosts_cache()%0A
         if self.
@@ -1527,24 +1527,61 @@
 append(self)
+%0A            self.clear_hosts_cache()
 %0A%0A    def ad
@@ -1661,16 +1661,49 @@
 up(self)
+%0A        self.clear_hosts_cache()
 %0A%0A    de
@@ -1773,24 +1773,313 @@
 ue%0A%0A    def 
+clear_hosts_cache(self):%0A%0A        self._hosts_cache = None%0A        for g in self.parent_groups:%0A            g.clear_hosts_cache()%0A%0A    def get_hosts(self):%0A%0A        if self._hosts_cache is None:%0A            self._hosts_cache = self._get_hosts()%0A%0A        return self._hosts_cache%0A%0A    def _
 get_hosts(se
@@ -2095,32 +2095,50 @@
      hosts = %5B%5D%0A
+        seen = %7B%7D%0A
         for kid 
@@ -2253,38 +2253,70 @@
    if kk not in 
-hosts:
+seen:%0A                    seen%5Bkk%5D = 1
 %0A               
@@ -2396,22 +2396,52 @@
  not in 
-hosts:
+seen:%0A                seen%5Bmine%5D = 1
 %0A       

Improve Group.get_hosts() performance.