Search in sources :

Example 16 with STGroupFile

use of org.stringtemplate.v4.STGroupFile in project bookish by parrt.

the class Tool method executeCodeSnippets.

/**
 * generate python files to execute \pyfig, \pyeval blocks
 */
public void executeCodeSnippets(Book book, String buildDir, List<List<ExecutableCodeDef>> codeBlocks) {
    String snippetsDir = buildDir + "/snippets";
    // combine list of code snippets for each label into file
    STGroup pycodeTemplates = new STGroupFile("templates/pyeval.stg");
    for (int i = 0; i < book.filenames.size(); i++) {
        // for each chapter
        List<ExecutableCodeDef> codeDefs = codeBlocks.get(i);
        if (codeDefs.size() == 0) {
            continue;
        }
        String basename = stripFileExtension(codeDefs.get(0).inputFilename);
        // prepare directories
        String chapterSnippetsDir = snippetsDir + "/" + basename;
        ParrtIO.mkdir(chapterSnippetsDir);
        ParrtIO.mkdir(outputDir + "/images/" + basename);
        // every chapter snippets dir gets a data link to book data directory
        if (!new File(chapterSnippetsDir + "/data").exists()) {
            execCommandLine("ln -s " + dataDir + " " + chapterSnippetsDir + "/data");
        }
        // get mapping from label (or index if no label) to list of snippets
        MultiMap<String, ExecutableCodeDef> labelToDefs = new MultiMap<>();
        List<String> labels = new ArrayList<>();
        for (ExecutableCodeDef codeDef : codeDefs) {
            // for each code blob
            String label = codeDef.label != null ? codeDef.label : String.valueOf(codeDef.index);
            if (!labels.contains(label))
                labels.add(label);
            labelToDefs.map(label, codeDef);
        }
        // track snippet label order
        for (String label : labels) {
            // for each group of code with same label
            List<ExecutableCodeDef> defs = labelToDefs.get(label);
            String snippetFilename = basename + "_" + label + ".py";
            List<ST> snippets = new ArrayList<>();
            for (ExecutableCodeDef def : defs) {
                String tname = def.isOutputVisible ? "pyeval" : "pyfig";
                ST snippet = pycodeTemplates.getInstanceOf(tname);
                snippet.add("def", def);
                // Don't allow "plt.show()" to execute, strip it
                String code = null;
                if (def.code != null) {
                    code = def.code.replace("plt.show()", "");
                }
                if (code != null && code.trim().length() == 0) {
                    code = null;
                }
                snippet.add("code", code);
                snippets.add(snippet);
            }
            ST file = pycodeTemplates.getInstanceOf("pyfile");
            file.add("snippets", snippets);
            file.add("buildDir", buildDir);
            file.add("outputDir", outputDir);
            // file.add("imagesDir", inputDir+"/images");
            file.add("basename", basename);
            file.add("label", label);
            String pycode = file.render();
            String snippetHashFilename = chapterSnippetsDir + "/" + basename + "_" + label + "-" + md5hash(pycode) + ".hash";
            if (!Files.exists(Paths.get(snippetHashFilename))) {
                System.err.println("BUILDING " + snippetFilename);
                // save empty hash marker file
                ParrtIO.save(snippetHashFilename, "");
                ParrtIO.save(chapterSnippetsDir + "/" + snippetFilename, pycode);
                // EXEC!
                String[] result = ParrtSys.execInDir(chapterSnippetsDir, "pythonw", snippetFilename);
                if (result[1] != null && result[1].length() > 0) {
                    // errors during python compilation not running
                    System.err.println(result[1]);
                }
            }
            for (ExecutableCodeDef def : defs) {
                String stderr = ParrtIO.load(chapterSnippetsDir + "/" + basename + "_" + label + "_" + def.index + ".err");
                if (def instanceof PyFigDef) {
                    ((PyFigDef) def).generatedFilenameNoSuffix = outputDir + "/images/" + basename + "/" + basename + "_" + label + "_" + def.index;
                }
                if (stderr.trim().length() > 0) {
                    System.err.println(stderr);
                }
                if (def.isOutputVisible) {
                    if (def.tree instanceof BookishParser.PyevalContext) {
                        BookishParser.PyevalContext tree = (BookishParser.PyevalContext) def.tree;
                        tree.stdout = ParrtIO.load(chapterSnippetsDir + "/" + basename + "_" + label + "_" + def.index + ".out");
                        tree.stderr = stderr.trim();
                        if (tree.stdout.length() == 0)
                            tree.stdout = null;
                        if (tree.stderr.length() == 0)
                            tree.stderr = null;
                        // System.out.println("stderr: "+stderr);
                        if (def.displayExpr != null) {
                            String dataFilename = basename + "_" + label + "_" + def.index + ".csv";
                            tree.displayData = ParrtIO.load(chapterSnippetsDir + "/" + dataFilename);
                        // System.out.println("data: "+tree.displayData);
                        }
                    } else {
                        BookishParser.Inline_pyevalContext tree = (BookishParser.Inline_pyevalContext) def.tree;
                        tree.stdout = ParrtIO.load(chapterSnippetsDir + "/" + basename + "_" + label + "_" + def.index + ".out");
                        tree.stderr = stderr.trim();
                        if (tree.stdout.length() == 0)
                            tree.stdout = null;
                        if (tree.stderr.length() == 0)
                            tree.stderr = null;
                        String dataFilename = basename + "_" + label + "_" + def.index + ".csv";
                        tree.displayData = ParrtIO.load(chapterSnippetsDir + "/" + dataFilename);
                    }
                }
            }
        }
    }
}
Also used : ST(org.stringtemplate.v4.ST) STGroup(org.stringtemplate.v4.STGroup) ArrayList(java.util.ArrayList) BookishParser(us.parr.bookish.parse.BookishParser) PyFigDef(us.parr.bookish.model.entity.PyFigDef) MultiMap(org.antlr.v4.runtime.misc.MultiMap) ExecutableCodeDef(us.parr.bookish.model.entity.ExecutableCodeDef) STGroupFile(org.stringtemplate.v4.STGroupFile) File(java.io.File) STGroupFile(org.stringtemplate.v4.STGroupFile)

Example 17 with STGroupFile

use of org.stringtemplate.v4.STGroupFile in project eclipse-collections by eclipse.

the class EclipseCollectionsCodeGenerator method generateFiles.

/**
 * Generates code and only write contents to disk which differ from the current file contents.
 *
 * @return The number of files written.
 */
public int generateFiles() {
    List<URL> allTemplateFilesFromClassPath = FileUtils.getAllTemplateFilesFromClasspath(this.templateDirectory, this.classPathURLs);
    for (URL url : allTemplateFilesFromClassPath) {
        this.url = url;
        this.templateFile = new STGroupFile(this.url, "UTF-8", '<', '>');
        this.templateFile.setListener(this.stErrorListener);
        this.templateFile.registerRenderer(String.class, new IntegerOrStringRenderer());
        if (this.templateFile.isDefined("fileName")) {
            this.setTest();
            File targetPath = this.constructTargetPath();
            FileUtils.createDirectory(targetPath);
            boolean hasTwoPrimitives = this.templateFile.isDefined("hasTwoPrimitives") && Boolean.valueOf(this.templateFile.getInstanceOf("hasTwoPrimitives").render());
            boolean skipBoolean = this.templateFile.isDefined("skipBoolean") && Boolean.valueOf(this.templateFile.getInstanceOf("skipBoolean").render());
            boolean skipBooleanKeys = this.templateFile.isDefined("skipBooleanKeys") && Boolean.valueOf(this.templateFile.getInstanceOf("skipBooleanKeys").render());
            if (hasTwoPrimitives) {
                for (Primitive primitive1 : Primitive.values()) {
                    if (primitive1 == Primitive.BOOLEAN && (skipBoolean || skipBooleanKeys)) {
                        continue;
                    }
                    for (Primitive primitive2 : Primitive.values()) {
                        if (primitive2 == Primitive.BOOLEAN && skipBoolean) {
                            continue;
                        }
                        String sourceFileName = this.executeTemplate(primitive1, primitive2, "fileName");
                        File outputFile = new File(targetPath, sourceFileName + ".java");
                        if (!EclipseCollectionsCodeGenerator.sourceFileExists(outputFile)) {
                            String classContents = this.executeTemplate(primitive1, primitive2, "class");
                            this.checkSumClassContentsAndWrite(classContents, targetPath, sourceFileName);
                        }
                    }
                }
            } else {
                for (Primitive primitive : Primitive.values()) {
                    if (primitive == Primitive.BOOLEAN && skipBoolean) {
                        continue;
                    }
                    String sourceFileName = this.executeTemplate(primitive, "fileName");
                    File outputFile = new File(targetPath, sourceFileName + ".java");
                    if (!EclipseCollectionsCodeGenerator.sourceFileExists(outputFile)) {
                        String classContents = this.executeTemplate(primitive, "class");
                        this.checkSumClassContentsAndWrite(classContents, targetPath, sourceFileName);
                    }
                }
            }
        }
    }
    return this.numFileWritten;
}
Also used : Primitive(org.eclipse.collections.codegenerator.model.Primitive) IntegerOrStringRenderer(org.eclipse.collections.codegenerator.tools.IntegerOrStringRenderer) STGroupFile(org.stringtemplate.v4.STGroupFile) File(java.io.File) STGroupFile(org.stringtemplate.v4.STGroupFile) URL(java.net.URL)

Example 18 with STGroupFile

use of org.stringtemplate.v4.STGroupFile in project antlr4 by tunnelvisionlabs.

the class ErrorManager method setFormat.

/**
 * The format gets reset either from the Tool if the user supplied a command line option to that effect
 *  Otherwise we just use the default "antlr".
 */
public void setFormat(String formatName) {
    this.formatName = formatName;
    String fileName = FORMATS_DIR + formatName + STGroup.GROUP_FILE_EXTENSION;
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    URL url = cl.getResource(fileName);
    if (url == null) {
        cl = ErrorManager.class.getClassLoader();
        url = cl.getResource(fileName);
    }
    if (url == null && formatName.equals("antlr")) {
        rawError("ANTLR installation corrupted; cannot find ANTLR messages format file " + fileName);
        panic();
    } else if (url == null) {
        rawError("no such message format file " + fileName + " retrying with default ANTLR format");
        // recurse on this rule, trying the default message format
        setFormat("antlr");
        return;
    }
    format = new STGroupFile(fileName, "UTF-8");
    format.load();
    if (!initSTListener.errors.isEmpty()) {
        rawError("ANTLR installation corrupted; can't load messages format file:\n" + initSTListener.toString());
        panic();
    }
    boolean formatOK = verifyFormat();
    if (!formatOK && formatName.equals("antlr")) {
        rawError("ANTLR installation corrupted; ANTLR messages format file " + formatName + ".stg incomplete");
        panic();
    } else if (!formatOK) {
        // recurse on this rule, trying the default message format
        setFormat("antlr");
    }
}
Also used : STGroupFile(org.stringtemplate.v4.STGroupFile) URL(java.net.URL)

Example 19 with STGroupFile

use of org.stringtemplate.v4.STGroupFile in project antlr4 by tunnelvisionlabs.

the class BuildDependencyGenerator method loadDependencyTemplates.

public void loadDependencyTemplates() {
    if (templates != null)
        return;
    String fileName = "org/antlr/v4/tool/templates/depend.stg";
    templates = new STGroupFile(fileName, "UTF-8");
}
Also used : STGroupFile(org.stringtemplate.v4.STGroupFile)

Example 20 with STGroupFile

use of org.stringtemplate.v4.STGroupFile in project antlr4 by tunnelvisionlabs.

the class TestGenerator method generateCodeForFoldersInIndex.

protected void generateCodeForFoldersInIndex(STGroup targetGroup, String rootFolder) {
    STGroup index = new STGroupFile(rootFolder + "/Index.stg");
    // make sure the index group is loaded since we call rawGetDictionary
    index.load();
    Map<String, Object> folders = index.rawGetDictionary("TestFolders");
    if (folders != null) {
        for (String key : folders.keySet()) {
            final String testdir = rootFolder + "/" + key;
            STGroup testIndex = new STGroupFile(testdir + "/Index.stg");
            testIndex.load();
            Map<String, Object> templateNames = testIndex.rawGetDictionary("TestTemplates");
            if (templateNames != null && !templateNames.isEmpty()) {
                final ArrayList<String> sortedTemplateNames = new ArrayList<String>(templateNames.keySet());
                Collections.sort(sortedTemplateNames);
                generateTestFile(testIndex, targetGroup, testdir, sortedTemplateNames);
            }
        }
    }
}
Also used : STGroup(org.stringtemplate.v4.STGroup) ArrayList(java.util.ArrayList) STGroupFile(org.stringtemplate.v4.STGroupFile)

Aggregations

STGroupFile (org.stringtemplate.v4.STGroupFile)26 STGroup (org.stringtemplate.v4.STGroup)12 StringRenderer (org.stringtemplate.v4.StringRenderer)7 URL (java.net.URL)6 Before (org.junit.Before)6 ST (org.stringtemplate.v4.ST)5 File (java.io.File)4 ArrayList (java.util.ArrayList)3 NumberRenderer (org.stringtemplate.v4.NumberRenderer)3 STErrorListener (org.stringtemplate.v4.STErrorListener)3 STMessage (org.stringtemplate.v4.misc.STMessage)3 IOException (java.io.IOException)2 CodeGenerator (org.antlr.v4.codegen.CodeGenerator)2 Pair (org.antlr.v4.runtime.misc.Pair)2 BodyExtractorFilter (se.inera.intyg.webcert.web.integration.integrationtest.BodyExtractorFilter)2 FileOutputStream (java.io.FileOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 MultiMap (org.antlr.v4.runtime.misc.MultiMap)1 NotNull (org.antlr.v4.runtime.misc.NotNull)1 Primitive (org.eclipse.collections.codegenerator.model.Primitive)1