'A semi-generic function that will take an object
with a template_files dict {source:destiation}, and
generate a rendered file.  The write_file option
allows for generating of the rendered output without
actually creating any files.
The return value is a dict of the destination file
names (after variable substitution is done) and the
data in the file.'

def write_templates(self, obj, write_file=False, path=None):

    self.logger.info(('Writing    template    files    for    %s' % obj.name))
    results = {}
    try:
        templates = obj.template_files
    except:
        return results
    blended = utils.blender(self.api, False, obj)
    if (obj.COLLECTION_TYPE == 'distro'):
        if (re.search('esxi[56]', obj.os_version) is not None):
            realbootcfg = open(os.path.join(os.path.dirname(obj.kernel), 'boot.cfg')).read()
            bootmodules = re.findall('modules=(.*)', realbootcfg)
            for modules in bootmodules:
                blended['esx_modules'] = modules.replace('/', '')
    autoinstall_meta = blended.get('autoinstall_meta', {})
    try:
        del blended['autoinstall_meta']
    except:
        pass
    blended.update(autoinstall_meta)
    templates = blended.get('template_files', {})
    try:
        del blended['template_files']
    except:
        pass
    blended.update(templates)
    (success, templates) = utils.input_string_or_dict(templates)
    if (not success):
        return results
    if blended['distro_name']:
        blended['img_path'] = os.path.join('/images', blended['distro_name'])
        blended['local_img_path'] = os.path.join(utils.tftpboot_location(), 'images', blended['distro_name'])
    for template in templates.keys():
        dest = templates[template]
        if (dest is None):
            continue
        template = self.templar.render(template, blended, None).strip()
        dest = os.path.normpath(self.templar.render(dest, blended, None).strip())
        dest_dir = os.path.normpath(os.path.dirname(dest))
        if ((path is not None) and (path != dest)):
            continue
        if (os.path.isabs(dest_dir) and write_file):
            if (dest_dir.find(utils.tftpboot_location()) != 0):
                raise CX(('    warning:    template    destination    (%s)    is    outside    %s,    skipping.' % (dest_dir, utils.tftpboot_location())))
                continue
        elif write_file:
            dest_dir = os.path.join(self.settings.webdir, 'rendered', dest_dir)
            dest = os.path.join(dest_dir, os.path.basename(dest))
            if (not os.path.exists(dest_dir)):
                utils.mkdir(dest_dir)
        if (not os.path.exists(template)):
            raise CX(('template    source    %s    does    not    exist' % template))
            continue
        elif (write_file and (not os.path.isdir(dest_dir))):
            raise CX(('template    destination    (%s)    is    invalid' % dest_dir))
            continue
        elif (write_file and os.path.exists(dest)):
            raise CX(('template    destination    (%s)    already    exists' % dest))
            continue
        elif (write_file and os.path.isdir(dest)):
            raise CX(('template    destination    (%s)    is    a    directory' % dest))
            continue
        elif ((template == '') or (dest == '')):
            raise CX(('either    the    template    source    or    destination    was    blank    (unknown    variable    used?)' % dest))
            continue
        template_fh = open(template)
        template_data = template_fh.read()
        template_fh.close()
        buffer = self.templar.render(template_data, blended, None)
        results[dest] = buffer
        if write_file:
            self.logger.info(('generating:    %s' % dest))
            fd = open(dest, 'w')
            fd.write(buffer)
            fd.close()
    return results
