use of org.python.pydev.json.eclipsesource.JsonValue in project Pydev by fabioz.
the class GenCythonAstImpl method jsonToParseOutput.
private ParseOutput jsonToParseOutput(ParserInfo p, String cythonJson, long modifiedTime) {
JsonValue json = JsonValue.readFrom(cythonJson);
JsonObject asObject = json.asObject();
JsonValue errors = asObject.get("errors");
ParseException exc = null;
for (JsonValue v : errors.asArray()) {
JsonObject objError = v.asObject();
JsonValue lineValue = objError.get("line");
JsonValue colValue = objError.get("col");
JsonValue messageValue = objError.get("message_only");
exc = new ParseException(messageValue.asString(), lineValue.asInt(), colValue.asInt());
}
JsonValue ast = asObject.get("ast");
if (ast == null || !ast.isObject()) {
ParseOutput parseOutput = new ParseOutput(null, exc, modifiedTime);
parseOutput.isCython = true;
return parseOutput;
} else {
JsonValue body = ast.asObject().get("stats");
if (body != null && body.isArray()) {
// System.out.println(body.toPrettyString());
JsonToNodesBuilder builder = new JsonToNodesBuilder(p);
JsonArray asArray = body.asArray();
Iterator<JsonValue> iterator = asArray.iterator();
while (iterator.hasNext()) {
JsonValue node = iterator.next();
try {
builder.addStatement(node);
} catch (Exception e) {
log("Error converting cython json to ast: " + node, e);
}
}
ISimpleNode mod = builder.createModule();
ParseOutput parseOutput = new ParseOutput(mod, null, modifiedTime);
parseOutput.isCython = true;
return parseOutput;
}
}
return null;
}
use of org.python.pydev.json.eclipsesource.JsonValue in project Pydev by fabioz.
the class CondaPackageManager method listCondaEnvironments.
public static List<File> listCondaEnvironments(File condaExecutable) {
List<File> lst = new ArrayList<>();
String encoding = "utf-8";
Tuple<String, String> output = new SimpleRunner().runAndGetOutput(new String[] { condaExecutable.toString(), "env", "list", "--json" }, null, null, null, encoding);
Log.logInfo(output.o1);
JsonObject jsonOutput = JsonValue.readFrom(output.o1).asObject();
JsonArray envs = jsonOutput.get("envs").asArray();
for (JsonValue env : envs.values()) {
lst.add(new File(env.asString()));
}
return lst;
}
use of org.python.pydev.json.eclipsesource.JsonValue in project Pydev by fabioz.
the class PathMappingsPrefsPage method checkPathMappingsFormat.
private Optional<String> checkPathMappingsFormat(JsonValue json) {
if (!json.isArray()) {
return Optional.of("Path Mappings JSON must be an array.");
}
JsonArray array = json.asArray();
int i = 0;
for (JsonValue value : array) {
if (!value.isObject()) {
return Optional.of("Only objects with \"localRoot\" and \"remoteRoot\" are accepted as path mapping entries.");
}
i++;
String objectId = "Path mapping entry: " + i;
JsonObject object = value.asObject();
if (object.size() != 2) {
return Optional.of(objectId + " is expected to contain \"localRoot\" and \"remoteRoot\" keys.");
}
List<String> objectNames = object.names();
if (!objectNames.contains("localRoot")) {
return Optional.of(objectId + " does not contain \"localRoot\" key.");
}
if (!objectNames.contains("remoteRoot")) {
return Optional.of(objectId + " does not contain \"remoteRoot\" key.");
}
if (!object.get("localRoot").isString()) {
return Optional.of(objectId + " \"localRoot\" value is not a string.");
}
if (!object.get("remoteRoot").isString()) {
return Optional.of(objectId + " \"remoteRoot\" value is not a string.");
}
}
// no errors
return Optional.empty();
}
use of org.python.pydev.json.eclipsesource.JsonValue in project Pydev by fabioz.
the class CondaPackageManager method list.
@Override
public List<String[]> list() {
List<String[]> listed = new ArrayList<String[]>();
File condaExecutable;
try {
condaExecutable = PyDevCondaPreferences.findCondaExecutable(interpreterInfo);
} catch (UnableToFindExecutableException e) {
return errorToList(listed, e);
}
// use system encoding
String encoding = null;
Tuple<String, String> output = new SimpleRunner().runAndGetOutput(new String[] { condaExecutable.toString(), "list", "-p", prefix.toString(), "--json" }, null, null, null, encoding);
try {
JsonValue readFrom = JsonValue.readFrom(output.o1);
JsonArray asArray = readFrom.asArray();
for (JsonValue value : asArray) {
JsonObject asObject = value.asObject();
JsonValue name = asObject.get("name");
JsonValue version = asObject.get("version");
JsonValue channel = asObject.get("channel");
JsonValue buildString = asObject.get("build_string");
listed.add(new String[] { name.asString(), version.asString(), StringUtils.join("", buildString.asString(), " (", channel.asString(), ")") });
}
} catch (Exception e) {
// Older version of Conda had a different json format and "list --json" wouldn't show pip info, so,
// fallback to an implementation which just did a conda list without --json and parse its output
output = new SimpleRunner().runAndGetOutput(new String[] { condaExecutable.toString(), "list", "-p", prefix.toString() }, null, null, null, encoding);
List<String> splitInLines = StringUtils.splitInLines(output.o1, false);
for (String line : splitInLines) {
line = line.trim();
if (line.startsWith("#")) {
continue;
}
List<String> split = StringUtils.split(line, ' ');
if (split.size() >= 3) {
listed.add(new String[] { split.get(0), split.get(1), StringUtils.join(" - ", split.subList(2, split.size() - 1)) });
} else if (split.size() == 2) {
listed.add(new String[] { split.get(0), split.get(1), "" });
}
}
}
return listed;
}
use of org.python.pydev.json.eclipsesource.JsonValue in project Pydev by fabioz.
the class GenCythonAstTest method testGenCythonAstBasic.
public void testGenCythonAstBasic() throws Exception {
ParserInfo parserInfo = new ParserInfo(new Document("a = 10"), grammarVersionProvider);
String output = new GenCythonAstImpl(parserInfo).genCythonJson();
JsonValue value = JsonValue.readFrom(output);
JsonValue body = value.asObject().get("ast").asObject().get("stats");
assertEquals(body, JsonValue.readFrom("[\n" + " {\n" + " \"__node__\": \"SingleAssignment\",\n" + " \"line\": 1,\n" + " \"col\": 4,\n" + " \"lhs\": {\n" + " \"__node__\": \"Name\",\n" + " \"line\": 1,\n" + " \"col\": 0,\n" + " \"name\": \"a\"\n" + " },\n" + " \"rhs\": {\n" + " \"__node__\": \"Int\",\n" + " \"line\": 1,\n" + " \"col\": 4,\n" + " \"is_c_literal\": \"None\",\n" + " \"value\": \"10\",\n" + " \"unsigned\": \"\",\n" + " \"longness\": \"\",\n" + " \"constant_result\": \"10\",\n" + " \"type\": \"long\"\n" + " }\n" + " }\n" + " ]\n" + "\n"));
assertEquals("Module[body=[Assign[targets=[Name[id=a, ctx=Store, reserved=false]], value=Num[n=10, type=Int, num=10], type=null]]]", new GenCythonAstImpl(parserInfo).genCythonAst().ast.toString());
}
Aggregations