'Make calls to cloudforms and save the output in a cache'

def update_cache(self):

    self.groups = dict()
    self.hosts = dict()
    if self.args.debug:
        print('Updating    cache...')
    for host in self._get_hosts():
        if (host['power_state'] != 'on'):
            if self.args.debug:
                print(('Skipping    %s    because    power_state    =    %s' % (host['name'], host['power_state'])))
            continue
        if (self.cloudforms_purge_actions and ('actions' in host)):
            del host['actions']
        if ('tags' in host):
            if ('tags' not in self.inventory):
                self.inventory['tags'] = dict(children=[], vars={}, hosts=[])
            if (not self.cloudforms_nest_tags):
                for group in host['tags']:
                    safe_key = self.to_safe(group['name'])
                    if safe_key:
                        if self.args.debug:
                            print(("Adding    sub-group    '%s'    to    parent    'tags'" % safe_key))
                        if (safe_key not in self.inventory['tags']['children']):
                            self.push(self.inventory['tags'], 'children', safe_key)
                        self.push(self.inventory, safe_key, host['name'])
                        if self.args.debug:
                            print(('Found    tag    [%s]    for    host    which    will    be    mapped    to    [%s]' % (group['name'], safe_key)))
            else:
                safe_parent_tag_name = 'tags'
                for tag in host['tags']:
                    tag_hierarchy = tag['name'][1:].split('/')
                    if self.args.debug:
                        print(('Working    on    list    %s' % tag_hierarchy))
                    for tag_name in tag_hierarchy:
                        if self.args.debug:
                            print(('Working    on    tag_name    =    %s' % tag_name))
                        safe_tag_name = self.to_safe(tag_name)
                        if self.args.debug:
                            print(('Using    sanitized    name    %s' % safe_tag_name))
                        if (safe_tag_name not in self.inventory):
                            self.inventory[safe_tag_name] = dict(children=[], vars={}, hosts=[])
                        if safe_parent_tag_name:
                            if self.args.debug:
                                print(("Adding    sub-group    '%s'    to    parent    '%s'" % (safe_tag_name, safe_parent_tag_name)))
                            if (safe_tag_name not in self.inventory[safe_parent_tag_name]['children']):
                                self.push(self.inventory[safe_parent_tag_name], 'children', safe_tag_name)
                        safe_parent_tag_name = safe_tag_name
                    self.push(self.inventory[safe_parent_tag_name], 'hosts', host['name'])
        if (('ipaddresses' in host) and host['ipaddresses'] and isinstance(host['ipaddresses'], list)):
            host['ansible_ssh_host'] = host['ipaddresses'][0]
        for key in ('location', 'type', 'vendor'):
            safe_key = self.to_safe(host[key])
            if (key not in self.inventory):
                self.inventory[key] = dict(children=[], vars={}, hosts=[])
            if (safe_key not in self.inventory):
                self.inventory[safe_key] = dict(children=[], vars={}, hosts=[])
            if (safe_key not in self.inventory[key]['children']):
                self.push(self.inventory[key], 'children', safe_key)
            if (key in host):
                self.push(self.inventory[safe_key], 'hosts', host['name'])
        self.hosts[host['name']] = host
        self.push(self.inventory, 'all', host['name'])
    if self.args.debug:
        print('Saving    cached    data')
    self.write_to_cache(self.hosts, self.cache_path_hosts)
    self.write_to_cache(self.inventory, self.cache_path_inventory)
