Search in sources :

Example 1 with JsonValue

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;
}
Also used : JsonArray(org.python.pydev.json.eclipsesource.JsonArray) ParseOutput(org.python.pydev.shared_core.parsing.BaseParser.ParseOutput) JsonValue(org.python.pydev.json.eclipsesource.JsonValue) JsonObject(org.python.pydev.json.eclipsesource.JsonObject) ParseException(org.python.pydev.parser.jython.ParseException) ISimpleNode(org.python.pydev.shared_core.model.ISimpleNode) ParseException(org.python.pydev.parser.jython.ParseException)

Example 2 with JsonValue

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;
}
Also used : JsonArray(org.python.pydev.json.eclipsesource.JsonArray) SimpleRunner(org.python.pydev.ast.runners.SimpleRunner) ArrayList(java.util.ArrayList) JsonValue(org.python.pydev.json.eclipsesource.JsonValue) JsonObject(org.python.pydev.json.eclipsesource.JsonObject) File(java.io.File)

Example 3 with JsonValue

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();
}
Also used : JsonArray(org.python.pydev.json.eclipsesource.JsonArray) JsonValue(org.python.pydev.json.eclipsesource.JsonValue) JsonObject(org.python.pydev.json.eclipsesource.JsonObject)

Example 4 with JsonValue

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;
}
Also used : JsonArray(org.python.pydev.json.eclipsesource.JsonArray) SimpleRunner(org.python.pydev.ast.runners.SimpleRunner) UnableToFindExecutableException(org.python.pydev.core.IInterpreterInfo.UnableToFindExecutableException) ArrayList(java.util.ArrayList) JsonValue(org.python.pydev.json.eclipsesource.JsonValue) JsonObject(org.python.pydev.json.eclipsesource.JsonObject) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) UnableToFindExecutableException(org.python.pydev.core.IInterpreterInfo.UnableToFindExecutableException)

Example 5 with JsonValue

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());
}
Also used : JsonValue(org.python.pydev.json.eclipsesource.JsonValue) GenCythonAstImpl(org.python.pydev.ast.cython.GenCythonAstImpl) ParserInfo(org.python.pydev.parser.PyParser.ParserInfo) Document(org.eclipse.jface.text.Document)

Aggregations

JsonValue (org.python.pydev.json.eclipsesource.JsonValue)6 JsonObject (org.python.pydev.json.eclipsesource.JsonObject)5 JsonArray (org.python.pydev.json.eclipsesource.JsonArray)4 File (java.io.File)2 ArrayList (java.util.ArrayList)2 SimpleRunner (org.python.pydev.ast.runners.SimpleRunner)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 Matcher (java.util.regex.Matcher)1 Document (org.eclipse.jface.text.Document)1 GenCythonAstImpl (org.python.pydev.ast.cython.GenCythonAstImpl)1 UnableToFindExecutableException (org.python.pydev.core.IInterpreterInfo.UnableToFindExecutableException)1 ParserInfo (org.python.pydev.parser.PyParser.ParserInfo)1 ParseException (org.python.pydev.parser.jython.ParseException)1 ISimpleNode (org.python.pydev.shared_core.model.ISimpleNode)1 ParseOutput (org.python.pydev.shared_core.parsing.BaseParser.ParseOutput)1 Tuple (org.python.pydev.shared_core.structure.Tuple)1