From 14ebb1f037732656f96299fe0997da578a71d6a8 Mon Sep 17 00:00:00 2001 From: Semyon Proshev Date: Mon, 20 Jun 2016 19:36:17 +0300 Subject: [PATCH] Extract and save extras in PyRequirement --- .../python/packaging/PyRequirement.java | 46 +++++++++++++++++-- .../python/packaging/PyRequirementTest.java | 15 ++++-- 2 files changed, 53 insertions(+), 8 deletions(-) diff --git a/python/openapi/src/com/jetbrains/python/packaging/PyRequirement.java b/python/openapi/src/com/jetbrains/python/packaging/PyRequirement.java index 384f24224371..e293e680c28a 100644 --- a/python/openapi/src/com/jetbrains/python/packaging/PyRequirement.java +++ b/python/openapi/src/com/jetbrains/python/packaging/PyRequirement.java @@ -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 myVersionSpecs; + @NotNull private final List myInstallOptions; @NotNull - private final List 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 installOptions, @NotNull String extras) { + this(name, Collections.singletonList(calculateVersionSpec(version, PyRequirementRelation.EQ)), installOptions, extras); + } + public PyRequirement(@NotNull String name, @NotNull List 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 versionSpecs, + @NotNull List 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 versionSpecs = parseVersionSpecs(matcher.group(REQUIREMENT_VERSIONS_SPECS_GROUP)); - return new PyRequirement(matcher.group(NAME_GROUP), versionSpecs, calculateRequirementInstallOptions(matcher)); + final List 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; diff --git a/python/testSrc/com/jetbrains/python/packaging/PyRequirementTest.java b/python/testSrc/com/jetbrains/python/packaging/PyRequirementTest.java index 48f85184db48..bac7467bb314 100644 --- a/python/testSrc/com/jetbrains/python/packaging/PyRequirementTest.java +++ b/python/testSrc/com/jetbrains/python/packaging/PyRequirementTest.java @@ -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)); + } } }