Extract and save extras in PyRequirement

This commit is contained in:
Semyon Proshev
2016-06-20 19:36:17 +03:00
parent ca3d199fb0
commit 14ebb1f037
2 changed files with 53 additions and 8 deletions
@@ -151,10 +151,16 @@ public class PyRequirement {
@NotNull
private static final String REQUIREMENT_NAME_REGEXP = "(?<" + NAME_GROUP + ">" + IDENTIFIER_REGEXP + ")";
@NotNull
private static final String REQUIREMENT_EXTRAS_GROUP = "extras";
@NotNull
private static final String REQUIREMENT_EXTRAS_REGEXP =
"(\\[" + IDENTIFIER_REGEXP +
"(" + LINE_WS_REGEXP + "*," + LINE_WS_REGEXP + "*" + IDENTIFIER_REGEXP + ")*\\])?";
"(?<" + REQUIREMENT_EXTRAS_GROUP + ">" +
"\\[" +
IDENTIFIER_REGEXP + "(" + LINE_WS_REGEXP + "*," + LINE_WS_REGEXP + "*" + IDENTIFIER_REGEXP + ")*" +
"\\]" +
")?";
@NotNull
private static final String REQUIREMENT_VERSIONS_SPECS_GROUP = "versionspecs";
@@ -192,11 +198,14 @@ public class PyRequirement {
@NotNull
private final String myName;
@NotNull
private final List<PyRequirementVersionSpec> myVersionSpecs;
@NotNull
private final List<String> myInstallOptions;
@NotNull
private final List<PyRequirementVersionSpec> myVersionSpecs;
private final String myExtras;
public PyRequirement(@NotNull String name) {
this(name, Collections.emptyList());
@@ -210,9 +219,14 @@ public class PyRequirement {
this(name, Collections.singletonList(calculateVersionSpec(version, PyRequirementRelation.EQ)), installOptions);
}
public PyRequirement(@NotNull String name, @NotNull String version, @NotNull List<String> installOptions, @NotNull String extras) {
this(name, Collections.singletonList(calculateVersionSpec(version, PyRequirementRelation.EQ)), installOptions, extras);
}
public PyRequirement(@NotNull String name, @NotNull List<PyRequirementVersionSpec> versionSpecs) {
myName = name;
myVersionSpecs = versionSpecs;
myExtras = "";
myInstallOptions = Collections.singletonList(toString());
}
@@ -220,6 +234,17 @@ public class PyRequirement {
myName = name;
myVersionSpecs = versionSpecs;
myInstallOptions = Collections.unmodifiableList(installOptions);
myExtras = "";
}
public PyRequirement(@NotNull String name,
@NotNull List<PyRequirementVersionSpec> versionSpecs,
@NotNull List<String> installOptions,
@NotNull String extras) {
myName = name;
myVersionSpecs = versionSpecs;
myInstallOptions = Collections.unmodifiableList(installOptions);
myExtras = extras;
}
@NotNull
@@ -234,7 +259,7 @@ public class PyRequirement {
@Override
public String toString() {
return myName + StringUtil.join(myVersionSpecs, ",");
return myName + myExtras + StringUtil.join(myVersionSpecs, ",");
}
@Override
@@ -247,6 +272,7 @@ public class PyRequirement {
if (!myName.equals(that.myName)) return false;
if (!myVersionSpecs.equals(that.myVersionSpecs)) return false;
if (!myInstallOptions.equals(that.myInstallOptions)) return false;
if (!myExtras.equals(that.myExtras)) return false;
return true;
}
@@ -256,6 +282,7 @@ public class PyRequirement {
int result = myName.hashCode();
result = 31 * result + myVersionSpecs.hashCode();
result = 31 * result + myInstallOptions.hashCode();
result = 31 * result + myExtras.hashCode();
return result;
}
@@ -363,8 +390,17 @@ public class PyRequirement {
private static PyRequirement parseRequirement(@NotNull String line) {
final Matcher matcher = REQUIREMENT.matcher(line);
if (matcher.matches()) {
final String name = matcher.group(NAME_GROUP);
final List<PyRequirementVersionSpec> versionSpecs = parseVersionSpecs(matcher.group(REQUIREMENT_VERSIONS_SPECS_GROUP));
return new PyRequirement(matcher.group(NAME_GROUP), versionSpecs, calculateRequirementInstallOptions(matcher));
final List<String> installOptions = calculateRequirementInstallOptions(matcher);
final String extras = matcher.group(REQUIREMENT_EXTRAS_GROUP);
if (extras == null) {
return new PyRequirement(name, versionSpecs, installOptions);
}
else {
return new PyRequirement(name, versionSpecs, installOptions, extras);
}
}
return null;
@@ -16,6 +16,7 @@
package com.jetbrains.python.packaging;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.containers.ContainerUtil;
import com.jetbrains.python.fixtures.PyTestCase;
@@ -2277,7 +2278,9 @@ public class PyRequirementTest extends PyTestCase {
doCommentAtTheEndTest(name + " # comment");
doCommentAtTheEndTest(name, version, name + "==" + version + " # comment");
doCommentAtTheEndTest(name + "[PDF] # comment");
assertEquals(new PyRequirement(name, Collections.emptyList(), Collections.singletonList(name + "[PDF]"), "[PDF]"),
PyRequirement.fromLine(name + "[PDF] # comment"));
final PyRequirement requirement = new PyRequirement(name, Collections.emptyList(), Arrays.asList(name, "--install-option", "option"));
@@ -2350,8 +2353,14 @@ public class PyRequirementTest extends PyTestCase {
sb.setLength(sb.length() - 1);
}
final String line = sb.toString();
final String options = sb.toString();
assertEquals(new PyRequirement(name, expectedVersionSpecs, Collections.singletonList(line)), PyRequirement.fromLine(line));
if (extras == null) {
assertEquals(new PyRequirement(name, expectedVersionSpecs, Collections.singletonList(options)), PyRequirement.fromLine(options));
}
else {
assertEquals(new PyRequirement(name, expectedVersionSpecs, Collections.singletonList(options), StringUtil.trimLeading(extras)),
PyRequirement.fromLine(options));
}
}
}