Skip to content

Commit

Permalink
Annotations to Generate py codes
Browse files Browse the repository at this point in the history
  • Loading branch information
gnanaprakash-ravi committed Jan 24, 2024
1 parent 28f9d40 commit fa65b3e
Show file tree
Hide file tree
Showing 8 changed files with 230 additions and 3 deletions.
28 changes: 28 additions & 0 deletions common/client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,38 @@
<artifactId>zingg-common-client</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>zingg</groupId>
<artifactId>zingg-common-py</artifactId>
<version>${zingg.version}</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.source}</target>
<showWarnings>true</showWarnings>

<annotationProcessors>
<annotationProcessor>
zingg.common.py.processors.PythonClassProcessor
</annotationProcessor>
<annotationProcessor>
zingg.common.py.processors.PythonMethodProcessor
</annotationProcessor>
</annotationProcessors>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@
import zingg.common.client.ZFrame;
import zingg.common.client.util.StringRedactor;

import zingg.common.py.annotations.PythonClass;
import zingg.common.py.annotations.PythonMethod;


/**Actual pipe def in the args. One pipe can be used at multiple places with different tables, locations, queries etc
*
* @author sgoyal
*
*/

@PythonClass
@JsonInclude(Include.NON_NULL)
public class Pipe<D,R,C> implements Serializable{ // St:StructType, Sv:SaveMode

Expand Down Expand Up @@ -57,12 +60,12 @@ public void setSchema(String schema) {
this.schema = schema;
}


@PythonMethod
public String getName() {
return name;
}


@PythonMethod
@JsonValue
public void setName(String name) {
this.name = name;
Expand Down
1 change: 1 addition & 0 deletions common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
<module>infra</module>
<module>core</module>
<module>client</module>
<module>py</module>
</modules>
</project>
10 changes: 10 additions & 0 deletions common/py/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>zingg</groupId>
<artifactId>zingg-common</artifactId>
<version>${zingg.version}</version>
</parent>
<artifactId>zingg-common-py</artifactId>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package zingg.common.py.annotations;

import javax.annotation.processing.*;

import java.lang.annotation.Target;
import java.lang.annotation.ElementType;

@Target({ElementType.TYPE})
public @interface PythonClass {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package zingg.common.py.annotations;

import javax.annotation.processing.*;

import java.lang.annotation.Target;
import java.lang.annotation.ElementType;

@Target({ElementType.METHOD})
public @interface PythonMethod {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package zingg.common.py.processors;

import java.util.List;
import javax.annotation.processing.*;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.type.TypeKind;
import java.util.Set;
import javax.lang.model.element.*;
import javax.lang.model.util.ElementFilter;

import zingg.common.py.annotations.*;

@SupportedAnnotationTypes("zingg.common.py.annotations.PythonClass")
public class PythonClassProcessor extends AbstractProcessor {

private boolean importsAndDeclarationsGenerated = false;

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {

// Imports and global declarations
if (!importsAndDeclarationsGenerated) {
generateImportsAndDeclarations();
importsAndDeclarationsGenerated = true;
}


// process Services annotation
for (Element element : roundEnv.getElementsAnnotatedWith(PythonClass.class)) {
if (element.getKind() == ElementKind.CLASS) {
TypeElement classElement = (TypeElement) element;
PackageElement packageElement =
(PackageElement) classElement.getEnclosingElement();
System.out.println("class " + element.getSimpleName() + ":");

// __init__ method
System.out.println(" def __init__(self" +
generateConstructorParameters(classElement) + "):");
if (element.getSimpleName().contentEquals("EPipe")) {
generateClassInitializationCode(classElement);
}
for (VariableElement field : ElementFilter.fieldsIn(classElement.getEnclosedElements())) {
if (!field.getSimpleName().contentEquals("serialVersionUID")) {
generateFieldInitializationCode(field);
}
}
}
System.out.println();
// rest of generated class contents
}

return false;

}

private void generateImportsAndDeclarations() {
System.out.println("import logging");
System.out.println("from zingg.client import *");
System.out.println("LOG = logging.getLogger(\"zingg.pipes\")");
System.out.println();
System.out.println("JPipe = getJVM().zingg.spark.client.pipe.SparkPipe");
System.out.println("FilePipe = getJVM().zingg.common.client.pipe.FilePipe");
System.out.println("JStructType = getJVM().org.apache.spark.sql.types.StructType");
System.out.println();
}

private void generateClassInitializationCode(TypeElement classElement) {
System.out.println(" self.EPipe = getJVM().zingg.spark.client.pipe.SparkPipe()");
}

// private void generateFieldInitializationCode(VariableElement field, ExecutableElement methodElement, TypeElement classElement) {
private void generateFieldInitializationCode(VariableElement field) {
System.out.println(" self.EPipe." + field.getSimpleName() + " = " + field.getSimpleName());
// String fieldName = field.getSimpleName().toString();
// String methodName = methodElement.getSimpleName().toString();
// System.out.println(" self." + fieldName + " = " + "getJVM()." +
// classElement.getQualifiedName().toString() + "." + methodName + "(" + fieldName + ")");
}

private String generateConstructorParameters(TypeElement classElement) {
StringBuilder parameters = new StringBuilder();
for (VariableElement field : ElementFilter.fieldsIn(classElement.getEnclosedElements())) {
if (!field.getSimpleName().contentEquals("serialVersionUID")) {
parameters.append(", ");
parameters.append(field.getSimpleName());
}
}
return parameters.toString();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package zingg.common.py.processors;

import java.util.List;
import javax.annotation.processing.*;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.type.TypeKind;
import java.util.Set;
import javax.lang.model.element.*;
import javax.lang.model.util.ElementFilter;

import zingg.common.py.annotations.*;

@SupportedAnnotationTypes("zingg.common.py.annotations.PythonMethod")
public class PythonMethodProcessor extends AbstractProcessor {

private boolean importsAndDeclarationsGenerated = false;

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {

// process Services annotation
for (Element element : roundEnv.getElementsAnnotatedWith(PythonMethod.class)) {

if (element.getKind() == ElementKind.METHOD) {
ExecutableElement methodElement = (ExecutableElement) element;
System.out.println(" def " + methodElement.getSimpleName() + "(self" +
generateMethodSignature(methodElement) + "):\n " + generateMethodReturn(methodElement));
generateFieldAssignment(methodElement);
}
System.out.println();

// rest of generated class contents
}
return false;
}

private String generateMethodSignature(ExecutableElement methodElement) {
StringBuilder signature = new StringBuilder();
signature.append(generateMethodParameters(methodElement));
return signature.toString();
}

private String generateMethodParameters(ExecutableElement methodElement) {
StringBuilder parameters = new StringBuilder();
for (VariableElement parameter : methodElement.getParameters()) {
parameters.append(", ");
parameters.append(parameter.getSimpleName());
}
return parameters.toString();
}

private String generateMethodReturn(ExecutableElement methodElement) {
TypeMirror returnType = methodElement.getReturnType();
if (returnType.getKind() == TypeKind.VOID) {
return "";
} else {
String returnTypeString = resolveType(returnType);
String variableName = methodElement.getSimpleName().toString();
return "return " + variableName;
}
}

private String resolveType(TypeMirror typeMirror) {
return typeMirror.toString();
}

private void generateFieldAssignment(ExecutableElement methodElement) {
List<? extends VariableElement> parameters = methodElement.getParameters();
if (!parameters.isEmpty()) {
VariableElement parameter = parameters.get(0);
String variableName = parameter.getSimpleName().toString();
System.out.println(" self." + variableName + " = " + variableName);
}
}

}

0 comments on commit fa65b3e

Please sign in to comment.