Commit accc6eaa authored by PONCELET's avatar PONCELET

Handle dynamic group naming

parent f1759f9c
......@@ -38,7 +38,7 @@ public class PythonSourceCodeGenerator {
Files.createDirectories(outputDir.toPath());
Path outputFolder=outputDir.toPath();
Files.write(outputFolder.resolve(Paths.get("XSFConstants.py")),
SCGroupAdapter.getPythonSourceCode(group,"",""));
SCGroupAdapter.getPythonSourceCode(group,"","",false));
}
}
......@@ -276,7 +276,7 @@ public class SCGroupAdapter {
}
}
public static List<String> getPythonSourceCode(Group group, String ident, String path) {
public static List<String> getPythonSourceCode(Group group, String identation, String pathGetter,boolean is_sensor_associated) {
// Build file contents
List<String> sourceCode = new ArrayList<>();
......@@ -285,17 +285,11 @@ public class SCGroupAdapter {
// group.subGroups.isEmpty())
// return sourceCode;
String sensorGroupName = group.name;
if (group.groupCategory == GroupCategory.eSensor) {
sensorGroupName = "000";
// This is a sensor, there can be found several categories, we replace them by
// an example with id 000
}
path = path + sensorGroupName + "/";
String sensorGroupName = null;
// This group declaration
if (ident.isEmpty()) {
if (pathGetter.isEmpty()) {
sourceCode.add(String.format(pythonCommentMarker + "Class generated for XSF see %s\n" + pythonCommentMarker,
"https://gitlab.ifremer.fr/fleet/XSF"));
sourceCode.add("\nimport netCDF4 as nc");
......@@ -309,14 +303,50 @@ public class SCGroupAdapter {
sourceCode.add("types = dict()");
}
String className=getClassName(group, "");
if (group.groupCategory == GroupCategory.eSensor) {
is_sensor_associated=true;
// This is a sensor, there can be found several categories, we replace them by
// an example with id 000
}
//sourceCode.add(String.format("\n" + identation + "# noinspection PyUnresolvedReferences"));
sourceCode.add(String.format(identation + "class %s:",className));
sourceCode.add(String.format("\n" + ident + "class %s:", getClassName(group, "")));
String localIdent = ident + pythonIndent;
sourceCode.add(String.format("%sGROUP_PATH = \"%s\"", localIdent, path));
sourceCode.add(String.format("\n%sdef create_group(self, group: nc.Dataset):", localIdent));
sourceCode.add(String.format("%s%sg = group.createGroup(self.GROUP_PATH)", localIdent, pythonIndent));
String localIdent = identation + pythonIndent;
sourceCode.add(String.format("%s@staticmethod",localIdent));
if(pathGetter.isEmpty())
{
sourceCode.add(String.format("%sdef get_group_path():\n%s%sreturn \"%s\"", localIdent,localIdent,pythonIndent, "/"));
}
else
{
if (group.groupCategory == GroupCategory.eSensor)
{
sourceCode.add(String.format("%sdef get_group_path(ident=None):\n%s%sreturn %s", localIdent,localIdent,pythonIndent, String.format("%s+ident+\"/\"", pathGetter)));
} else if(is_sensor_associated)
{
sourceCode.add(String.format("%sdef get_group_path(ident=None):\n%s%sreturn %s", localIdent,localIdent,pythonIndent, String.format("%s+\"%s/\"", pathGetter,group.name)));
}
else
{
sourceCode.add(String.format("%sdef get_group_path():\n%s%sreturn %s", localIdent,localIdent,pythonIndent, String.format("%s+\"%s/\"", pathGetter,group.name)));
}
}
if(is_sensor_associated)
{
sourceCode.add(String.format("\n%sdef create_group(self, group: nc.Dataset, ident=None):", localIdent));
sourceCode.add(String.format("%s%sg = group.createGroup(self.get_group_path(ident))", localIdent, pythonIndent));
}
else
{
sourceCode.add(String.format("\n%sdef create_group(self, group: nc.Dataset):", localIdent));
sourceCode.add(String.format("%s%sg = group.createGroup(self.get_group_path())", localIdent, pythonIndent));
}
for(Type type:group.getTypes())
{
sourceCode.add(String.format("%s%s#type %s %s %s", localIdent,pythonIndent,type.getName(),type.getComment(),type.getDescription()));
......@@ -395,13 +425,13 @@ public class SCGroupAdapter {
+ "group.createDimension(dimension_name, values[dimension_name])\n");
}
boolean l_sensor_associated = is_sensor_associated;
// Coordinate variables Accessors
if (group.coordinatevariables != null && !group.coordinatevariables.isEmpty()) {
sourceCode.add(localIdent + "# Coordinate Variables names");
String localPath = path;
group.coordinatevariables.forEach(v -> {
try {
writeVariable(sourceCode, localIdent, localPath, v);
writeVariable(sourceCode, localIdent, v,l_sensor_associated,className);
} catch (Exception e) {
throw new RuntimeException(e);
}
......@@ -410,24 +440,34 @@ public class SCGroupAdapter {
// Variables declaration
if (!group.variables.isEmpty()) {
sourceCode.add(localIdent + "# Variables names");
String localPath = path;
group.variables.forEach(v -> {
writeVariable(sourceCode, localIdent, localPath, v);
writeVariable(sourceCode, localIdent, v,l_sensor_associated,className);
});
}
// Sub groups
if (!group.subGroups.isEmpty()) {
sourceCode.add("\n" + localIdent + "# Sub groups");
for (Group sg : group.subGroups) {
sourceCode.addAll(getPythonSourceCode(sg, localIdent, path));
if(is_sensor_associated)
{
sourceCode.addAll(getPythonSourceCode(sg, identation, String.format("%s.get_group_path(ident)", className),is_sensor_associated));
} else
{
sourceCode.addAll(getPythonSourceCode(sg, identation, String.format("%s.get_group_path()", className),is_sensor_associated));
}
}
}
return sourceCode;
}
private static void writeVariable(List<String> sourceCode, String localIdent, String localPath, Variable v) {
sourceCode.add(String.format(localIdent + "%s = \"%s%s\"", v.name.toUpperCase(), localPath, v.name));
private static void writeVariable(List<String> sourceCode, String localIdent, Variable v,boolean is_sensor_associated, String className) {
sourceCode.add(String.format(localIdent +"@staticmethod"));
if(is_sensor_associated)
sourceCode.add(String.format(localIdent + "def %s(ident=None):\n"+localIdent+pythonIndent+"return %s%s+\"%s\"", v.name.toUpperCase(), className,".get_group_path(ident)", v.name));
else
sourceCode.add(String.format(localIdent + "def %s():\n"+localIdent+pythonIndent+"return %s%s+\"%s\"", v.name.toUpperCase(), className,".get_group_path()", v.name));
String datatype="";
String comment="";
boolean has_fill_value=true;
......
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