Added quick fix installation for mangled packages like "import sklearn" -> quickfix install scikit-learn

This commit is contained in:
Ekaterina Tuzova
2015-05-07 20:16:15 +03:00
parent 33604ed23f
commit 85a4479279
3 changed files with 4800 additions and 3 deletions
File diff suppressed because it is too large Load Diff
@@ -593,9 +593,13 @@ public class PyUnresolvedReferencesInspection extends PyInspection {
final Sdk sdk = PythonSdkType.findPythonSdk(module);
if (module != null && sdk != null) {
if (PyPIPackageUtil.INSTANCE.isInPyPI(packageName)) {
final List<PyRequirement> requirements = Collections.singletonList(new PyRequirement(packageName));
final String name = "Install package " + packageName;
actions.add(new PyPackageRequirementsInspection.PyInstallRequirementsFix(name, module, sdk, requirements));
addInstallPackageAction(actions, packageName, module, sdk);
}
else {
if (PyPIPackageUtil.PACKAGES_TOPLEVEL.containsKey(packageName)) {
final String suggestedPackage = PyPIPackageUtil.PACKAGES_TOPLEVEL.get(packageName);
addInstallPackageAction(actions, suggestedPackage, module, sdk);
}
}
}
}
@@ -603,6 +607,12 @@ public class PyUnresolvedReferencesInspection extends PyInspection {
registerProblem(node, description, hl_type, null, rangeInElement, actions.toArray(new LocalQuickFix[actions.size()]));
}
private static void addInstallPackageAction(List<LocalQuickFix> actions, String packageName, Module module, Sdk sdk) {
final List<PyRequirement> requirements = Collections.singletonList(new PyRequirement(packageName));
final String name = "Install package " + packageName;
actions.add(new PyPackageRequirementsInspection.PyInstallRequirementsFix(name, module, sdk, requirements));
}
/**
* Checks if type is custom type and has custom member with certain name
* @param refName name to check
@@ -17,9 +17,12 @@ package com.jetbrains.python.packaging;
import com.google.common.collect.Lists;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.io.HttpRequests;
import com.intellij.util.net.HttpConfigurable;
import com.intellij.webcore.packaging.RepoPackage;
import com.jetbrains.python.PythonHelpersLocator;
import org.apache.xmlrpc.*;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
@@ -54,6 +57,8 @@ public class PyPIPackageUtil {
@NonNls public static final String PYPI_URL = "https://pypi.python.org/pypi";
@NonNls public static final String PYPI_LIST_URL = "https://pypi.python.org/pypi?%3Aaction=index";
public static Map<String, String> PACKAGES_TOPLEVEL = new HashMap<String, String>();
public static final PyPIPackageUtil INSTANCE = new PyPIPackageUtil();
private XmlRpcClient myXmlRpcClient;
@@ -63,6 +68,31 @@ public class PyPIPackageUtil {
private Set<RepoPackage> myAdditionalPackageNames;
@Nullable private volatile Set<String> myPackageNames = null;
static {
try {
fillPackages();
}
catch (IOException e) {
LOG.error("Cannot find \"packages\". " + e.getMessage());
}
}
private static void fillPackages() throws IOException {
FileReader reader = new FileReader(PythonHelpersLocator.getHelperPath("/tools/packages"));
try {
final String text = FileUtil.loadTextAndClose(reader);
final List<String> lines = StringUtil.split(text, "\n");
for (String line : lines) {
final List<String> split = StringUtil.split(line, " ");
PACKAGES_TOPLEVEL.put(split.get(0), split.get(1));
}
}
finally {
reader.close();
}
}
public static Set<String> getPackageNames(final String url) throws IOException {
final TreeSet<String> names = new TreeSet<String>();
final HTMLEditorKit.ParserCallback callback =