def take_action(self, opts):
        for template in opts.scaffold_name:
            template_filename = None
            template_relpath = None
            if template.endswith('.template'):
                # Template is a path, use it as it is.
                template_filename = template
                template_relpath = '.'
            else:
                # Not a template path, look it up in subfolders
                template_filename = self._lookup(template, '.')
                if template_filename:
                    template_relpath = os.path.dirname(template_filename)

                if template_filename is None and opts.lookup:
                    template_filename = self._lookup(template, opts.lookup)
                    if template_filename:
                        template_relpath = os.path.relpath(os.path.dirname(template_filename), opts.lookup)

            if not template_filename or not os.path.exists(template_filename):
                print('Template %s Not Found!' % (template))
                continue

            print('Using %s for %s' % (template_filename, opts.target))
            template_with_ext, __ = os.path.splitext(template_filename)
            __, output_ext = os.path.splitext(template_with_ext)

            output_dir = opts.path
            if not output_dir:
                output_dir = template_relpath

            if opts.subdir:
                output_dir = os.path.join(output_dir, opts.subdir)

            if not os.path.exists(output_dir):
                os.makedirs(output_dir)
                if opts.subdir:
                    package_init = os.path.join(output_dir, '__init__.py')
                    if not os.path.exists(package_init):
                        with open(package_init, 'w') as pif:
                            pif.write('# -*- coding: utf-8 -*-\n')

            output_path = os.path.join(output_dir, opts.target) + output_ext
            print('Creating %s...' % output_path)

            with open(template_filename, 'r') as tf:
                try:
                    subdir_as_package = opts.subdir.replace(os.sep, '.') if opts.subdir else ''
                    text = GearBoxTemplate().template_renderer(tf.read(), {
                        'target': opts.target,
                        'subdir': opts.subdir,
                        'subpackage': subdir_as_package,
                        'dotted_subpackage': '.' + subdir_as_package,
                        'packages': self._find_toplevel_packages()
                    })
                except NameError as e:
                    print('!! Error while processing template: %s' % e)
                    continue

                with open(output_path, 'w') as of:
                    of.write(text)
dataset/ETHPy150Open TurboGears/gearbox/gearbox/commands/scaffold.py/ScaffoldCommand.take_action/original