Commit ac7ea6f1 authored by PONCELET's avatar PONCELET

Add python source code generator

parent b8c5a105
......@@ -71,7 +71,8 @@ public class CodeGenerator {
Group root=gson.fromJson(reader, Group.class);
new ExcelGenerator().run(root,Paths.get(outputDir.getAbsolutePath(), "XSF_model.xlsx").toFile());
new AsciiDoctorGenerator().run(root, Paths.get(outputDir.getAbsolutePath(), "Groups.adoc").toFile());
new SourceCodeGenerator().run(root, Paths.get(outputDir.getAbsolutePath(), "code").toFile());
new JavaSourceCodeGenerator().run(root, Paths.get(outputDir.getAbsolutePath(), "code").toFile());
new PythonSourceCodeGenerator().run(root, Paths.get(outputDir.getAbsolutePath(), "code").toFile());
}
}
......
......@@ -12,7 +12,7 @@ import java.util.List;
import fr.ifremer.fr.xsf.generator.model.Group;
import fr.ifremer.fr.xsf.generator.model.SCGroupAdapter;
public class SourceCodeGenerator {
public class JavaSourceCodeGenerator {
private static String OUTPUT_PACKAGE = "fr.ifremer.globe.api.xsf.converter.common.xsf.groups";
private static String OUTPUT_FOLDER_PREDEFINED_LAYER = "predefinedLayer";
......
package fr.ifremer.fr.xsf.generator;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import fr.ifremer.fr.xsf.generator.model.Group;
import fr.ifremer.fr.xsf.generator.model.SCGroupAdapter;
public class PythonSourceCodeGenerator {
private static String OUTPUT_FOLDER_API = "python";
/**
* generate documentation in excel file format
* @throws Exception
**/
public void run(Group root, File outputDir) throws Exception
{
generateSourceFile(Paths.get(outputDir.getAbsolutePath(),OUTPUT_FOLDER_API).toFile(), root);
}
/**
* Generates source files
*
* @throws URISyntaxException
*/
private void generateSourceFile(File outputDir, Group group)
throws IOException {
if (!outputDir.exists())
Files.createDirectories(outputDir.toPath());
Path outputFolder=outputDir.toPath();
Files.write(outputFolder.resolve(Paths.get("XSFConstants.py")),
SCGroupAdapter.getPythonSourceCode(group,"","/"));
}
}
......@@ -139,5 +139,54 @@ public class SCGroupAdapter {
return sourceCode;
}
private static String pythonIndent=" ";
private static String pythonCommentMarker="\"\"\"\n";
public static List<String> getPythonSourceCode(Group group, String ident, String path) {
// Build file contents
List<String> sourceCode = new ArrayList<>();
path=path+group.name+"/";
//reject if empty group
// if(group.dims.isEmpty() && group.variables.isEmpty() && group.subGroups.isEmpty())
// return sourceCode;
// This group declaration
if(ident.isEmpty())
{
sourceCode.add(String.format(pythonCommentMarker+"Class generated for XSF see %s\n"+pythonCommentMarker, "https://gitlab.ifremer.fr/fleet/XSF"));
}
sourceCode.add(String.format("\n"+ident+"class %s:", getClassName(group)));
String localIdent=ident+pythonIndent;
sourceCode.add(String.format("%sGROUP_PATH = \"%s\"", localIdent,path));
// Dimension name code generation
if(!group.dims.isEmpty())
{
sourceCode.add(localIdent+"# dimensions names declaration");
group.dims.forEach(d->
sourceCode.add(localIdent+String.format("%s = \"%s\"", SCDimensionAdapter.getDimNameDeclaration(d),SCDimensionAdapter.getDimVariableName(d))));
}
// Variables declaration
if (!group.variables.isEmpty()) {
sourceCode.add(localIdent+"# Variables names");
String localPath=path;
group.variables.forEach(v -> {
sourceCode.add(String.format(localIdent+"%s = \"%s%s\"", v.name.toUpperCase(),localPath,v.name));
});
}
// Sub groups
if (!group.subGroups.isEmpty()) {
sourceCode.add("\n"+localIdent+"# Sub groups");
for(Group sg:group.subGroups)
{
sourceCode.addAll(getPythonSourceCode(sg,localIdent,path));
}
}
return sourceCode;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment