Search in sources :

Example 26 with FastStringBuffer

use of org.python.pydev.shared_core.string.FastStringBuffer in project Pydev by fabioz.

the class InterpreterInfo method restorePythonpath.

/**
 * Restores the path with the discovered libs
 * @param path
 */
public void restorePythonpath(IProgressMonitor monitor) {
    FastStringBuffer buffer = new FastStringBuffer();
    for (Iterator<String> iter = libs.iterator(); iter.hasNext(); ) {
        String folder = iter.next();
        buffer.append(folder);
        buffer.append("|");
    }
    restorePythonpath(buffer.toString(), monitor);
}
Also used : FastStringBuffer(org.python.pydev.shared_core.string.FastStringBuffer)

Example 27 with FastStringBuffer

use of org.python.pydev.shared_core.string.FastStringBuffer in project Pydev by fabioz.

the class FileUtilsFileBuffer method getCustomReturnFromFile.

/**
 * @param f the file from where we want to get the contents
 * @param returnType the class that specifies the return type of this method.
 * If null, it'll return in the fastest possible way available.
 * Valid options are:
 *      String.class
 *      IDocument.class
 *      FastStringBuffer.class
 *
 * @return an object with the contents from the file, having the return type
 * of the object specified by the parameter returnType.
 */
public static Object getCustomReturnFromFile(java.io.File f, boolean loadIfNotInWorkspace, Class<? extends Object> returnType) throws IOException {
    IPath path = Path.fromOSString(FileUtils.getFileAbsolutePath(f));
    IDocument doc = getDocFromPath(path);
    if (doc != null) {
        if (returnType == null || returnType == IDocument.class) {
            return doc;
        } else if (returnType == String.class) {
            return doc.get();
        } else if (returnType == FastStringBuffer.class) {
            return new FastStringBuffer(doc.get(), 16);
        } else {
            throw new RuntimeException("Don't know how to treat requested return type: " + returnType);
        }
    }
    if (doc == null && loadIfNotInWorkspace) {
        FileInputStream stream = new FileInputStream(f);
        try {
            String encoding;
            try {
                encoding = FileUtils.getPythonFileEncoding(f);
            } catch (PyUnsupportedEncodingException e) {
                // The encoding specified in the file is unsupported
                encoding = null;
            }
            return FileUtils.getStreamContents(stream, encoding, null, returnType);
        } finally {
            try {
                if (stream != null) {
                    stream.close();
                }
            } catch (Exception e) {
                Log.log(e);
            }
        }
    }
    return doc;
}
Also used : IPath(org.eclipse.core.runtime.IPath) FastStringBuffer(org.python.pydev.shared_core.string.FastStringBuffer) PyUnsupportedEncodingException(org.python.pydev.shared_core.io.PyUnsupportedEncodingException) IDocument(org.eclipse.jface.text.IDocument) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) PyUnsupportedEncodingException(org.python.pydev.shared_core.io.PyUnsupportedEncodingException)

Example 28 with FastStringBuffer

use of org.python.pydev.shared_core.string.FastStringBuffer in project Pydev by fabioz.

the class SimpleRunner method run.

/**
 * @return a tuple with the process created and a string representation of the cmdarray.
 */
public Tuple<Process, String> run(String[] cmdarray, File workingDir, IPythonNature nature, IProgressMonitor monitor, ICallback<String[], String[]> updateEnv) {
    if (monitor == null) {
        monitor = new NullProgressMonitor();
    }
    String executionString = getArgumentsAsStr(cmdarray);
    monitor.setTaskName("Executing: " + executionString);
    monitor.worked(5);
    Process process = null;
    try {
        monitor.setTaskName("Making pythonpath environment..." + executionString);
        String[] envp = null;
        if (nature != null) {
            // Don't remove as it *should* be updated based on the nature)
            envp = getEnvironment(nature, nature.getProjectInterpreter(), nature.getRelatedInterpreterManager());
        } else {
            // work in Windows by default because the Library/bin and DLLs folder is not in the PATH).
            if (PlatformUtils.isWindowsPlatform()) {
                envp = getDefaultSystemEnvAsArray(null);
                String executable = cmdarray[0];
                File f = new File(executable);
                File parentFile = f.getParentFile();
                for (int i = 0; i < envp.length; i++) {
                    String string = envp[i];
                    if (string.startsWith("PATH=")) {
                        boolean changed = false;
                        FastStringBuffer buf = new FastStringBuffer(string, 50);
                        for (File check : new File[] { new File(parentFile, "DLLs"), new File(new File(parentFile, "Library"), "bin") }) {
                            try {
                                if (check.exists()) {
                                    changed = true;
                                    if (!buf.endsWith(File.pathSeparator)) {
                                        buf.append(File.pathSeparator);
                                    }
                                    buf.appendObject(check);
                                }
                            } catch (Exception e) {
                            // ignore
                            }
                        }
                        if (changed) {
                            envp[i] = buf.toString();
                        }
                        break;
                    }
                }
            }
        }
        // Otherwise, use default (used when configuring the interpreter for instance).
        monitor.setTaskName("Making exec..." + executionString);
        if (workingDir != null) {
            if (!workingDir.isDirectory()) {
                throw new RuntimeException(StringUtils.format("Working dir must be an existing directory (received: %s)", workingDir));
            }
        }
        if (updateEnv != null) {
            envp = updateEnv.call(envp);
        }
        process = createProcess(cmdarray, envp, workingDir);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return new Tuple<Process, String>(process, executionString);
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) FastStringBuffer(org.python.pydev.shared_core.string.FastStringBuffer) File(java.io.File) CoreException(org.eclipse.core.runtime.CoreException) IOException(java.io.IOException) MisconfigurationException(org.python.pydev.core.MisconfigurationException) Tuple(org.python.pydev.shared_core.structure.Tuple)

Example 29 with FastStringBuffer

use of org.python.pydev.shared_core.string.FastStringBuffer in project Pydev by fabioz.

the class ModulesKey method toString.

@Override
public String toString() {
    FastStringBuffer ret = new FastStringBuffer(this.getClass().getSimpleName(), 40 + name.length());
    ret.append('[');
    ret.append(name);
    if (file != null) {
        ret.append(" - ");
        ret.appendObject(file);
    }
    ret.append(']');
    return ret.toString();
}
Also used : FastStringBuffer(org.python.pydev.shared_core.string.FastStringBuffer)

Example 30 with FastStringBuffer

use of org.python.pydev.shared_core.string.FastStringBuffer in project Pydev by fabioz.

the class ModulesKeyForZip method toString.

@Override
public String toString() {
    FastStringBuffer ret = new FastStringBuffer(name, 40);
    if (file != null) {
        ret.append(" - ");
        ret.appendObject(file);
    }
    if (zipModulePath != null) {
        ret.append(" - zip path:");
        ret.append(zipModulePath);
    }
    return ret.toString();
}
Also used : FastStringBuffer(org.python.pydev.shared_core.string.FastStringBuffer)

Aggregations

FastStringBuffer (org.python.pydev.shared_core.string.FastStringBuffer)300 ArrayList (java.util.ArrayList)32 Tuple (org.python.pydev.shared_core.structure.Tuple)26 File (java.io.File)25 IOException (java.io.IOException)20 CoreException (org.eclipse.core.runtime.CoreException)19 MisconfigurationException (org.python.pydev.core.MisconfigurationException)18 IDocument (org.eclipse.jface.text.IDocument)16 SimpleNode (org.python.pydev.parser.jython.SimpleNode)15 Document (org.eclipse.jface.text.Document)14 HashSet (java.util.HashSet)13 IFile (org.eclipse.core.resources.IFile)13 BadLocationException (org.eclipse.jface.text.BadLocationException)12 HashMap (java.util.HashMap)11 List (java.util.List)10 IRegion (org.eclipse.jface.text.IRegion)10 ModulesKey (org.python.pydev.core.ModulesKey)10 ParseException (org.python.pydev.parser.jython.ParseException)10 Entry (java.util.Map.Entry)9 IPythonNature (org.python.pydev.core.IPythonNature)9