Skip to content

Commit

Permalink
✨ feature: Python input transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
queer committed Mar 12, 2022
1 parent 9bcfebc commit df2f68c
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 1 deletion.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@
<version>22.0.0.2</version>
</dependency>

<dependency>
<groupId>org.python</groupId>
<artifactId>jython-standalone</artifactId>
<version>2.7.2</version>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/gg/amy/utt/UTT.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ InputFormat.YAML, new YamlTransformer(),
InputFormat.PLAIN, new PlainTransformer(),
InputFormat.PROPERTIES, new PropertiesTransformer(),
InputFormat.TOML, new TomlTransformer(),
InputFormat.BASE64, new Base64Transformer()
InputFormat.BASE64, new Base64Transformer(),
InputFormat.PYTHON, new PythonTransformer()
);
private static final Map<OutputFormat, Transformer> OUTPUT_TRANSFORMERS = Map.of(
OutputFormat.CSV, new CsvTransformer(),
Expand Down
1 change: 1 addition & 0 deletions src/main/java/gg/amy/utt/data/InputFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ public enum InputFormat {
TOML,
PLAIN,
BASE64,
PYTHON,
}
37 changes: 37 additions & 0 deletions src/main/java/gg/amy/utt/transform/impl/PythonTransformer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package gg.amy.utt.transform.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import gg.amy.utt.transform.TransformationContext;
import gg.amy.utt.transform.Transformer;
import org.python.util.PythonInterpreter;

import javax.annotation.Nonnull;

/**
* @author amy
* @since 3/11/22.
*/
public class PythonTransformer implements Transformer {
private static final ObjectMapper MAPPER = new ObjectMapper();

@Nonnull
@Override
public Object transformInput(@Nonnull final TransformationContext ctx, @Nonnull final String input) {
final var interpreter = new PythonInterpreter();
interpreter.set("input", input);
interpreter.exec("import ast, json; output = json.dumps(ast.literal_eval(input))");
final var output = interpreter.get("output");
try {
return MAPPER.readValue(output.asString(), Object.class);
} catch(@Nonnull final JsonProcessingException e) {
throw new IllegalStateException(e);
}
}

@Nonnull
@Override
public String transformOutput(@Nonnull final TransformationContext ctx, @Nonnull final Object input) {
throw new UnsupportedOperationException("Python objects are not a valid output format!");
}
}
24 changes: 24 additions & 0 deletions src/test/java/gg/amy/utt/transform/impl/PythonTransformerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package gg.amy.utt.transform.impl;

import gg.amy.utt.data.InputFormat;
import gg.amy.utt.data.OutputFormat;
import gg.amy.utt.transform.TransformationContext;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* @author amy
* @since 3/11/22.
*/
public class PythonTransformerTest {
@Test
public void testPythonToJsonTransformation() {
final var input = """
{'key': 'value', 'list': [1, 2, 3]}
""";
final var ctx = new TransformationContext(InputFormat.PYTHON, OutputFormat.JSON, null, null, false, false);
final var out = new JsonTransformer().transformOutput(ctx, new PythonTransformer().transformInput(ctx, input));
assertEquals("{\"list\":[1,2,3],\"key\":\"value\"}", out);
}
}

0 comments on commit df2f68c

Please sign in to comment.