-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
28f9d40
commit fa65b3e
Showing
8 changed files
with
230 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,5 +12,6 @@ | |
<module>infra</module> | ||
<module>core</module> | ||
<module>client</module> | ||
<module>py</module> | ||
</modules> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
9 changes: 9 additions & 0 deletions
9
common/py/src/main/java/zingg/common/py/annotations/PythonClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
9 changes: 9 additions & 0 deletions
9
common/py/src/main/java/zingg/common/py/annotations/PythonMethod.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
91 changes: 91 additions & 0 deletions
91
common/py/src/main/java/zingg/common/py/processors/PythonClassProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
76 changes: 76 additions & 0 deletions
76
common/py/src/main/java/zingg/common/py/processors/PythonMethodProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} |