Search in sources :

Example 6 with MisconfigurationException

use of org.python.pydev.core.MisconfigurationException in project Pydev by fabioz.

the class AdditionalSystemInterpreterInfo method getPythonPathFolders.

@Override
protected Set<String> getPythonPathFolders() {
    Set<String> ret = new HashSet<>();
    try {
        IInterpreterInfo interpreterInfo = this.manager.getInterpreterInfo(additionalInfoInterpreter, new NullProgressMonitor());
        ret.addAll(interpreterInfo.getPythonPath());
    } catch (MisconfigurationException e) {
        Log.log(e);
    }
    return ret;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IInterpreterInfo(org.python.pydev.core.IInterpreterInfo) MisconfigurationException(org.python.pydev.core.MisconfigurationException) HashSet(java.util.HashSet)

Example 7 with MisconfigurationException

use of org.python.pydev.core.MisconfigurationException in project Pydev by fabioz.

the class PyGoToDefinition method findDefinitionsAndOpen.

public ItemPointer[] findDefinitionsAndOpen(boolean doOpenDefinition) {
    request = null;
    ps = PySelectionFromEditor.createPySelectionFromEditor(getTextEditor());
    final PyEdit pyEdit = getPyEdit();
    RefactoringRequest refactoringRequest;
    try {
        refactoringRequest = getRefactoringRequest();
    } catch (MisconfigurationException e1) {
        Log.log(e1);
        return new ItemPointer[0];
    }
    final Shell shell = EditorUtils.getShell();
    try {
        if (areRefactorPreconditionsOK(refactoringRequest)) {
            boolean acceptTypeshed = false;
            boolean findInAdditionalInfo = false;
            ItemPointer[] defs = findDefinition(pyEdit, acceptTypeshed, findInAdditionalInfo);
            boolean retry = defs == null || defs.length == 0;
            if (!retry) {
                if (defs.length == 1) {
                    if (defs[0].definition == null) {
                        retry = true;
                    } else if (defs[0].definition.module == null) {
                        retry = true;
                    } else if (defs[0].definition.module.getFile() == null) {
                        retry = true;
                    }
                }
            }
            if (retry) {
                acceptTypeshed = true;
                findInAdditionalInfo = true;
                defs = findDefinition(pyEdit, acceptTypeshed, findInAdditionalInfo);
            }
            if (doOpenDefinition) {
                openDefinition(defs, pyEdit, shell);
            }
            return defs;
        }
    } catch (Exception e) {
        Log.log(e);
        String msg = e.getMessage();
        if (msg == null) {
            msg = "Unable to get error msg (null)";
        }
        ErrorDialog.openError(shell, "Error", "Unable to do requested action", new Status(Status.ERROR, PydevPlugin.getPluginID(), 0, msg, e));
    }
    return null;
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) Shell(org.eclipse.swt.widgets.Shell) RefactoringRequest(org.python.pydev.ast.refactoring.RefactoringRequest) MisconfigurationException(org.python.pydev.core.MisconfigurationException) PyEdit(org.python.pydev.editor.PyEdit) TooManyMatchesException(org.python.pydev.ast.refactoring.TooManyMatchesException) BadLocationException(org.eclipse.jface.text.BadLocationException) MisconfigurationException(org.python.pydev.core.MisconfigurationException) ItemPointer(org.python.pydev.ast.item_pointer.ItemPointer)

Example 8 with MisconfigurationException

use of org.python.pydev.core.MisconfigurationException in project Pydev by fabioz.

the class PyRenameInFileAction method fillWithOccurrences.

/**
 * Puts the found positions referente to the occurrences in the group
 *
 * @param document the document that will contain this positions
 * @param group the group that will contain this positions
 * @param ps the selection used
 * @return
 *
 * @throws BadLocationException
 * @throws OperationCanceledException
 * @throws CoreException
 * @throws MisconfigurationException
 */
private boolean fillWithOccurrences(IDocument document, LinkedPositionGroup group, IProgressMonitor monitor, PySelection ps) throws BadLocationException, OperationCanceledException, CoreException, MisconfigurationException {
    RefactoringRequest req = MarkOccurrencesJob.getRefactoringRequest(pyEdit, MarkOccurrencesJob.getRefactorAction(pyEdit), ps);
    if (monitor.isCanceled()) {
        return false;
    }
    PyRenameEntryPoint processor = new PyRenameEntryPoint(req);
    // process it to get what we need
    processor.checkInitialConditions(monitor);
    processor.checkFinalConditions(monitor, null);
    HashSet<ASTEntry> occurrences = processor.getOccurrences();
    if (monitor.isCanceled()) {
        return false;
    }
    // used so that we don't add duplicates
    Set<Tuple<Integer, Integer>> found = new HashSet<Tuple<Integer, Integer>>();
    List<ProposalPosition> groupPositions = new ArrayList<ProposalPosition>();
    if (occurrences != null) {
        // first, just sort by position (line, col)
        ArrayList<ASTEntry> sortedOccurrences = new ArrayList<ASTEntry>(occurrences);
        Collections.sort(sortedOccurrences, new Comparator<ASTEntry>() {

            @Override
            public int compare(ASTEntry o1, ASTEntry o2) {
                int thisVal = o1.node.beginLine;
                int anotherVal = o2.node.beginLine;
                int ret;
                if (thisVal == anotherVal) {
                    // if it's in the same line, let's sort by column
                    thisVal = o1.node.beginColumn;
                    anotherVal = o2.node.beginColumn;
                    ret = (thisVal < anotherVal ? -1 : (thisVal == anotherVal ? 0 : 1));
                } else {
                    ret = (thisVal < anotherVal ? -1 : 1);
                }
                return ret;
            }
        });
        // now, gather positions to add to the group
        int i = 0;
        int firstPosition = -1;
        int absoluteCursorOffset = ps.getAbsoluteCursorOffset();
        for (ASTEntry entry : sortedOccurrences) {
            try {
                IRegion lineInformation = document.getLineInformation(entry.node.beginLine - 1);
                int colDef = NodeUtils.getClassOrFuncColDefinition(entry.node) - 1;
                int offset = lineInformation.getOffset() + colDef;
                int len = req.qualifier.length();
                Tuple<Integer, Integer> foundAt = new Tuple<Integer, Integer>(offset, len);
                if (!found.contains(foundAt)) {
                    i++;
                    ProposalPosition proposalPosition = new ProposalPosition(document, offset, len, i, new ICompletionProposal[0]);
                    found.add(foundAt);
                    groupPositions.add(proposalPosition);
                    if (offset <= absoluteCursorOffset && absoluteCursorOffset < offset + len) {
                        firstPosition = i;
                    }
                }
            } catch (Exception e) {
                Log.log(e);
                return false;
            }
        }
        if (firstPosition != -1) {
            ArrayList<ProposalPosition> newGroupPositions = new ArrayList<ProposalPosition>();
            // add from current to end
            for (i = firstPosition - 1; i < groupPositions.size(); i++) {
                newGroupPositions.add(groupPositions.get(i));
            }
            // and now from the start up to the current
            for (i = 0; i < firstPosition - 1; i++) {
                newGroupPositions.add(groupPositions.get(i));
            }
            groupPositions = newGroupPositions;
        }
        for (ProposalPosition proposalPosition : groupPositions) {
            group.addPosition(proposalPosition);
        }
    }
    return groupPositions.size() > 0;
}
Also used : RefactoringRequest(org.python.pydev.ast.refactoring.RefactoringRequest) ArrayList(java.util.ArrayList) PyRenameEntryPoint(com.python.pydev.analysis.refactoring.wizards.rename.PyRenameEntryPoint) IRegion(org.eclipse.jface.text.IRegion) CoreException(org.eclipse.core.runtime.CoreException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) BadLocationException(org.eclipse.jface.text.BadLocationException) MisconfigurationException(org.python.pydev.core.MisconfigurationException) ProposalPosition(org.eclipse.jface.text.link.ProposalPosition) PyRenameEntryPoint(com.python.pydev.analysis.refactoring.wizards.rename.PyRenameEntryPoint) ASTEntry(org.python.pydev.parser.visitors.scope.ASTEntry) Tuple(org.python.pydev.shared_core.structure.Tuple) HashSet(java.util.HashSet)

Example 9 with MisconfigurationException

use of org.python.pydev.core.MisconfigurationException in project Pydev by fabioz.

the class AbstractInterpreterManager method getBuiltinMod.

@Override
public IModule getBuiltinMod(String projectInterpreterName, IModuleRequestState moduleRequest) {
    // Cache with the internal name.
    projectInterpreterName = getInternalName(projectInterpreterName);
    if (projectInterpreterName == null) {
        return null;
    }
    String cacheName = projectInterpreterName + "_" + moduleRequest.getAcceptTypeshed();
    IModule mod = builtinMod.get(cacheName);
    if (mod != null) {
        return mod;
    }
    try {
        InterpreterInfo interpreterInfo = this.getInterpreterInfo(projectInterpreterName, null);
        ISystemModulesManager modulesManager = interpreterInfo.getModulesManager();
        mod = modulesManager.getBuiltinModule("__builtin__", false, moduleRequest);
        if (mod == null) {
            // Python 3.0 has builtins and not __builtin__
            mod = modulesManager.getBuiltinModule("builtins", false, moduleRequest);
        }
        if (mod != null) {
            builtinMod.put(cacheName, mod);
        }
    } catch (MisconfigurationException e) {
        Log.log(e);
    }
    return builtinMod.get(cacheName);
}
Also used : IModule(org.python.pydev.core.IModule) ISystemModulesManager(org.python.pydev.core.ISystemModulesManager) MisconfigurationException(org.python.pydev.core.MisconfigurationException) IInterpreterInfo(org.python.pydev.core.IInterpreterInfo)

Example 10 with MisconfigurationException

use of org.python.pydev.core.MisconfigurationException in project Pydev by fabioz.

the class PythonNature method getVersionAndError.

@Override
public Tuple<String, String> getVersionAndError(boolean translateIfInterpreter) throws CoreException {
    if (project != null) {
        if (versionPropertyCache == null) {
            String storeVersion = getStore().getPropertyFromXml(getPythonProjectVersionQualifiedName());
            if (storeVersion == null) {
                // there is no such property set (let's set it to the default)
                // will set the versionPropertyCache too
                setVersion(getDefaultVersion(false), null);
            } else {
                // now, before returning and setting in the cache, let's make sure it's a valid version.
                if (!IPythonNature.Versions.ALL_VERSIONS_ANY_FLAVOR.contains(storeVersion)) {
                    Log.log("The stored version is invalid (" + storeVersion + "). Setting default.");
                    // will set the versionPropertyCache too
                    setVersion(getDefaultVersion(false), null);
                } else {
                    // Ok, it's correct.
                    versionPropertyCache = storeVersion;
                }
            }
        }
    } else {
        String msg = "Trying to get version without project set. Returning default.";
        Log.log(msg);
        return new Tuple<String, String>(getDefaultVersion(translateIfInterpreter), msg);
    }
    if (versionPropertyCache == null) {
        String msg = "The cached version is null. Returning default.";
        Log.log(msg);
        return new Tuple<String, String>(getDefaultVersion(translateIfInterpreter), msg);
    } else if (!IPythonNature.Versions.ALL_VERSIONS_ANY_FLAVOR.contains(versionPropertyCache)) {
        String msg = "The cached version (" + versionPropertyCache + ") is invalid. Returning default.";
        Log.log(msg);
        return new Tuple<String, String>(getDefaultVersion(translateIfInterpreter), msg);
    }
    if (translateIfInterpreter && versionPropertyCache.endsWith(IPythonNature.Versions.INTERPRETER_VERSION)) {
        Tuple<String, String> split = StringUtils.splitOnFirst(versionPropertyCache, ' ');
        String errorMessage;
        try {
            IInterpreterInfo info = this.getProjectInterpreter();
            String version = info.getVersion();
            if (version != null) {
                return new Tuple<String, String>(IPythonNature.Versions.convertToInternalVersion(new FastStringBuffer(split.o1, 6).append(' '), version), null);
            } else {
                errorMessage = "Unable to get version from interpreter info: " + info.getNameForUI() + " - " + info.getExecutableOrJar();
                Log.log(errorMessage);
            }
        } catch (MisconfigurationException | PythonNatureWithoutProjectException e) {
            Log.log(e);
            errorMessage = e.getMessage();
        }
        return new Tuple<String, String>(split.o1 + " " + "2.7", errorMessage + " (in project: " + getProject() + ")");
    }
    return new Tuple<String, String>(versionPropertyCache, null);
}
Also used : FastStringBuffer(org.python.pydev.shared_core.string.FastStringBuffer) IInterpreterInfo(org.python.pydev.core.IInterpreterInfo) MisconfigurationException(org.python.pydev.core.MisconfigurationException) PythonNatureWithoutProjectException(org.python.pydev.core.PythonNatureWithoutProjectException) Tuple(org.python.pydev.shared_core.structure.Tuple)

Aggregations

MisconfigurationException (org.python.pydev.core.MisconfigurationException)99 IPythonNature (org.python.pydev.core.IPythonNature)28 ArrayList (java.util.ArrayList)23 File (java.io.File)21 CoreException (org.eclipse.core.runtime.CoreException)18 IOException (java.io.IOException)14 SourceModule (org.python.pydev.ast.codecompletion.revisited.modules.SourceModule)14 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)13 IDocument (org.eclipse.jface.text.IDocument)13 IInterpreterInfo (org.python.pydev.core.IInterpreterInfo)13 IInterpreterManager (org.python.pydev.core.IInterpreterManager)12 PythonNatureWithoutProjectException (org.python.pydev.core.PythonNatureWithoutProjectException)12 HashSet (java.util.HashSet)11 IModule (org.python.pydev.core.IModule)11 BadLocationException (org.eclipse.jface.text.BadLocationException)10 SimpleNode (org.python.pydev.parser.jython.SimpleNode)10 AbstractAdditionalDependencyInfo (com.python.pydev.analysis.additionalinfo.AbstractAdditionalDependencyInfo)9 IFile (org.eclipse.core.resources.IFile)9 IGrammarVersionProvider (org.python.pydev.core.IGrammarVersionProvider)9 TokensList (org.python.pydev.core.TokensList)9