Pull templates from the AWS templates folder
323944
def get_template(self, template_name, **parameters):
        """"""
        template_path = pathlib.Path(self.template_dir).joinpath(template_name)
        return get_template(template_path, **parameters)
101914
def from_template(args):
    
    project_name = args.name
    template = args.template

    with tarfile.open(template) as tar:
        prefix = os.path.commonprefix(tar.getnames())
        check_template(tar.getnames(), prefix)
        tar.extractall(project_name, members=get_members(tar, prefix))
571586
def template_from_filename(filename):
    """"""
    ext = filename.split(os.path.extsep)[-1]
    if not ext in TEMPLATES_MAP:
        raise ValueError("No template for file extension {}".format(ext))
    return TEMPLATES_MAP[ext]
1265750
def get_templates(options={})
      uri = build_uri("/accounts/#{acct_id}/templates")
      uri.query = URI.encode_www_form(options[:params]) if options[:params]

      http = initialize_net_http_ssl(uri)
      request = Net::HTTP::Get.new(uri.request_uri, headers({ 'Content-Type' => 'application/json' }))
      response = http.request(request)
      generate_log(request, response, uri)
      JSON.parse(response.body)
    end
36893
def templates_in(path):
    """"""
    ext = '.cpp'
    return (
        Template(f[0:-len(ext)], load_file(os.path.join(path, f)))
        for f in os.listdir(path) if f.endswith(ext)
    )
970860
def get_fortpy_templates_dir():
    """"""
    import fortpy
    from os import path
    fortdir = path.dirname(fortpy.__file__)
    return path.join(fortdir, "templates")
523449
def deploy_templates():
    
    
    deployed = None
    if not hasattr(env, 'project_template_dir'):
        #the normal pattern would mean the shortest path is the main one.
        #its probably the last listed
        length = 1000   
        for dir in env.TEMPLATE_DIRS:
            if dir:
                len_dir = len(dir)
                if len_dir < length:
                    length = len_dir
                    env.project_template_dir = dir
    
    if hasattr(env,'project_template_dir'):
        remote_dir = '/'.join([deployment_root(),'env',env.project_fullname,'templates'])
        if env.verbosity:
            print env.host,"DEPLOYING templates", remote_dir
        deployed = deploy_files(env.project_template_dir,remote_dir)
    return deployed
306382
def getTemplates(self, workitems, template_folder=None,
                     template_names=None, keep=False, encoding="UTF-8"):
        

        self.templater.getTemplates(workitems,
                                    template_folder=template_folder,
                                    template_names=template_names,
                                    keep=keep,
                                    encoding=encoding)
528698
async function getTemplateFile(templateDir, stackName) {
  const f = await Promise.any(
    _.map(['.yml', '.yaml', '.json', ''], async(ext) =>
      await utils.fileExists(`${path.join(templateDir, stackName)}${ext}`)));
  if (f) {
    return f;
  }
  throw new Error(`Stack template "${stackName}" not found!`);
}
1127326
def get_template_dir():
    """"""
    try:
        template_dir = os.path.expanduser(os.environ["NET_TEXTFSM"])
        index = os.path.join(template_dir, "index")
        if not os.path.isfile(index):
            # Assume only base ./ntc-templates specified
            template_dir = os.path.join(template_dir, "templates")
    except KeyError:
        # Construct path ~/ntc-templates/templates
        home_dir = os.path.expanduser("~")
        template_dir = os.path.join(home_dir, "ntc-templates", "templates")

    index = os.path.join(template_dir, "index")
    if not os.path.isdir(template_dir) or not os.path.isfile(index):
        msg = """
Valid ntc-templates not found, please install https://github.com/networktocode/ntc-templates
and then set the NET_TEXTFSM environment variable to point to the ./ntc-templates/templates
directory."""
        raise ValueError(msg)
    return os.path.abspath(template_dir)
889801
protected void downloadTemplates(ProgressMonitor... monitors) throws IOException {
        File templateDir = getTemplateDir();
        if (!templateDir.exists()) {
            if (!templateDir.mkdirs())
                throw new IOException("Unable to create directory: " + templateDir.getAbsolutePath());
            String templatesUrl = getTemplatesUrl();
            getOut().println("Retrieving templates: " + templatesUrl);
            File tempZip = Files.createTempFile("mdw-templates-", ".zip").toFile();
            new Download(new URL(templatesUrl), tempZip).run(monitors);
            File tempDir = Files.createTempDirectory("mdw-templates-").toFile();
            new Unzip(tempZip, tempDir, false).run();
            File codegenDir = new File(templateDir + "/codegen");
            new Copy(new File(tempDir + "/codegen"), codegenDir, true).run();
            File assetsDir = new File(templateDir + "/assets");
            new Copy(new File(tempDir + "/assets"), assetsDir, true).run();
        }
    }
648873
def retrieve_template(self):
        
        links = self.retrieve_instance_links()
        self.log.debug('Links is \n%s', pformat(links))
        self.pipeline_config['instance_links'].update(links)
        jsondata = get_template(
            template_file='infrastructure/app_data.json.j2',
            appinfo=self.appinfo,
            pipeline_config=self.pipeline_config,
            formats=self.generated,
            run_as_user=DEFAULT_RUN_AS_USER)
        self.log.debug('jsondata is %s', pformat(jsondata))
        return jsondata
148316
function(options, cb) {

        function renderTemplate(callback) {
            options.soajs.log.debug('Rendering template ...');
            engine.render(options, function(error, output) {
                if(error) return callback({error: error.value, code: error.code});

                return callback(null, { render: output.render });
            });
        }

        function writeTemplate(result, callback) {
            options.soajs.log.debug('Writing template to temp folder ...');
            let templateFolderPath = path.join(__dirname, config.terraform.tempFolderPrefix);
            let templateFiles = [
                { name: config.terraform.templateDefaultName, content: result.renderTemplate.render }
            ];

            //if input includes a state object, add it as well
            if(options.params && options.params.templateState && Object.keys(options.params.templateState).length  > 0) {
                templateFiles.push({ name: config.terraform.stateFileName, content: JSON.stringify(options.params.templateState, null, 2) });
            }

            options.params.path = templateFolderPath;
            options.params.files = templateFiles;
            helper.writeToTempFolder(options, function(error, output) {
                if (error) return callback({error, code: 728});
                return callback(null, output);
            });
        }

        function applyTemplate(result, callback) {
            options.soajs.log.debug('Applying template ...');
            options.params.command = config.terraform.applyCommand;
            options.params.cwd = result.writeTemplate.tempFolderPath;
            helper.runChildProcess(options, function(error, output) {
                if(error) {
                    options.soajs.log.error('Applying template FAILED ...');
                    return callback(null, error); // pass error to readOutput and cleanup, then trigger rollback if applicable
                }
                return callback(null, output);
            });
        }

        function readOutput(result, callback) {
            options.soajs.log.debug('Reading output state file ...');
            let stateFilePath = path.join(result.writeTemplate.tempFolderPath, config.terraform.stateFileName);

            function readFile(counter, readFileCb) {
                fs.readFile(stateFilePath, { encoding: 'utf8' }, function(error, stateFileData) {
                    if(error) {
                        if(counter < config.terraform.stateFileReadMaxAttempts) {
                            return setTimeout(readFile.bind(null, ++counter, readFileCb), config.terraform.stateFileReadTimeout);
                        }

                        options.soajs.log.error('Unable to read template state file ...');
                        options.soajs.log.error(error);

                        if(result.applyTemplate && result.applyTemplate.error) {
                            // apply failed + no state file could be read means the template failed because of a syntax error
                            // return an empty object for the state file
                            return readFileCb(null, '{}');
                        }

                        let readFileError = { code: 730, msg: errors[730], debug: error };
                        return readFileCb({ error: readFileError });
                    }

                    return readFileCb(null, stateFileData);
                });
            }

            readFile(0, function(error, stateFileData) {
                if(error) {
                    stateFileData = error; // do not stop execution, return state file read error instead
                }
                else {
                    try {
                        stateFileData = JSON.parse(stateFileData);
                    }
                    catch(e) {
                        let parseError = { code: 730, msg: errors[730], debug: e };
                        return callback(null, { error: parseError }); // do not stop execution, return state file parse error instead
                    }
                }

                return callback(null, { stateFileData });
            });
        }

        function cleanup(result, callback) {
            options.soajs.log.debug('Cleaing up temp folder ...');
            helper.cleanup({ tempFolderPath: result.writeTemplate.tempFolderPath }, function(error) {
                if(error) options.soajs.log.error('Cleanup failed after applying terraform template!', error);
                return callback(null, true);
            });
        }

        function rollback(options, result, terraformErrors, callback) {
            if(!result.readOutput || !result.readOutput.stateFileData || (typeof(result.readOutput.stateFileData) === 'object' && Object.keys(result.readOutput.stateFileData).length === 0)) {
                options.soajs.log.debug('Terraform state file not found, rollback is not possible ...');
                return callback({
                    source: 'driver',
                    value: terraformErrors.applyError,
                    code: 729,
                    msg: terraformErrors.applyError
                });
            }

            options.soajs.log.debug('Terraform deployment failed, rolling back ...');
            // options.params.layerName and options.params.exclude are already set
            options.params.template = result.renderTemplate.render;
            options.params.templateState = result.readOutput.stateFileData;
            return engine.destroy(options, function(destroyError, destroyResult) {
                if(destroyError) {
                    options.soajs.log.debug('Terraform rollback failed ...');
                    terraformErrors.destroyError = destroyError;
                }

                return callback({
                    source: 'driver',
                    value: terraformErrors.applyError,
                    code: 729,
                    msg: terraformErrors.applyError
                });
            });
        }

        async.auto({
            // render template + input using handlebars
            renderTemplate,
            // create temp folder and write template to folder
            writeTemplate: ['renderTemplate', writeTemplate],
            // terraform init and terraform apply
            applyTemplate: ['writeTemplate', applyTemplate],
            // read terraform.tfstate file
            readOutput: ['writeTemplate', 'applyTemplate', readOutput],
            // delete temp folder
            cleanup: ['writeTemplate', 'readOutput', cleanup]
        }, function(error, result) {
            utils.checkError(error && error.error, error && error.code, cb, () => {
                let backupData = { render: result.renderTemplate.render, templateOutput: result.applyTemplate };
                helper.backup((options.params.templateState) ? 'modify' : 'create', options, backupData, function() {
                    if(result.applyTemplate && result.applyTemplate.error) {
                        let terraformErrors = {};
                        terraformErrors.applyError = helper.parseErrorOutput({ output: result.applyTemplate.error });
                        // not options.params.templateState -> this is a create operation
                        // result.readOutput.stateFileData -> not a terraform syntax error, deployment started but failed
                        if(!options.params.templateState && result.readOutput && result.readOutput.stateFileData) {
                            return rollback(options, result, terraformErrors, cb);
                        }
                        else {
                            // this is a modify operation, do not destroy | return error
                            return utils.checkError(error && error.error, error && error.code, cb, cb);
                        }
                    }

                    return cb(null, {
                        applied: true,
                        stateFileData: result.readOutput.stateFileData,
                        output: result.applyTemplate.output,
                        render: result.renderTemplate.render
                    });
                });
            });
        });
    }
575698
def urls_from_file_tree(template_dir):
    """"""
    urls = []  # keep a list of of all the files/paths

    # Should be somethign like:
    # /path/to/myproject/templates/staticflatpages
    root_dir = join(template_dir, 'staticflatpages')

    for root, dirs, files in walk(template_dir):
        # Only do this for the ``staticflatpages`` directory or sub-directories
        if "staticflatpages" in root:
            root = root.replace(root_dir, '')
            for f in files:
                path = join(root, f)
                path = _format_as_url(path)
                urls.append(path)
    return urls
949934
def copy_templates(entry_point, py_entry_point, auto_write, output_dir):
    '''
    '''

    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    scripts = []
    for f in os.listdir(bin_path()):
        ext = os.path.splitext(f)[-1]
        newline = NEWLINE_MAP.get(ext, '\n')
        template = bin_path(f)
        destination = output_dir + '/' + entry_point + ext
        scripts.append(destination)

        with io.open(template, 'r') as f:
            code = f.read()
        code = code.replace('{{entry_point}}', entry_point)
        code = code.replace('{{py_entry_point}}', py_entry_point)
        code = code.replace('{{auto_write}}', str(int(auto_write)))

        with io.open(destination, 'w', newline=newline) as f:
            f.write(code)

    return scripts
772443
def load_template(path_or_buffer):
    '''
    '''

    from itertools import groupby
    from operator import itemgetter

    path_or_buffer = _stringify_path(path_or_buffer)

    if is_file_like(path_or_buffer):
        templates = json.load(path_or_buffer)
    else:
        with open(path_or_buffer, 'r') as f:
            templates = json.load(f)

    options = []

    grouper = itemgetter('page', 'extraction_method')

    for key, grp in groupby(sorted(templates, key=grouper), grouper):
        tmp_options = [_convert_template_option(e) for e in grp]

        if len(tmp_options) == 1:
            options.append(tmp_options[0])
            continue

        option = tmp_options[0]
        areas = [e.get('area') for e in tmp_options]
        option['area'] = areas
        option['multiple_tables'] = True
        options.append(option)

    return options
364523
def GetConfigFromTemplate(self, template_path):
    """"""
    with zipfile.ZipFile(template_path) as template_zip:
      build_yaml = None
      for name in template_zip.namelist():
        if name.endswith("build.yaml"):
          build_yaml = name
          break
      if not build_yaml:
        raise RuntimeError("Couldn't find build.yaml in %s" % template_path)
      with template_zip.open(build_yaml) as buildfile:
        repack_config = config.CONFIG.CopyConfig()
        utf8_buildfile = io.TextIOWrapper(buildfile, encoding="utf-8")
        parser = config_lib.YamlParser(fd=utf8_buildfile)
        config_data = parser.RawData()
        self.Validate(config_data, template_path)
        repack_config.MergeData(config_data)
    return repack_config