Arguments provided for tox
authorIlya.Kazakevich <Ilya.Kazakevich@jetbrains.com>
Tue, 8 Dec 2015 18:05:39 +0000 (21:05 +0300)
committerIlya.Kazakevich <Ilya.Kazakevich@jetbrains.com>
Tue, 8 Dec 2015 18:05:39 +0000 (21:05 +0300)
python/src/com/jetbrains/python/run/AbstractPythonRunConfiguration.java
python/src/com/jetbrains/python/testing/tox/PyToxCommandLineState.java
python/src/com/jetbrains/python/testing/tox/PyToxConfiguration.java
python/src/com/jetbrains/python/testing/tox/PyToxConfigurationSettings.form [new file with mode: 0644]
python/src/com/jetbrains/python/testing/tox/PyToxConfigurationSettings.java
python/src/com/jetbrains/serialization/AnnotationSerializationFilter.java [new file with mode: 0644]
python/src/com/jetbrains/serialization/CompoundFilter.java [new file with mode: 0644]

index f52d6c8fad509a7ee286b91e8c5d9beba29df43e..753c5616dcdce4ff827b7556c30468bdf31e5c3a 100644 (file)
@@ -73,6 +73,11 @@ public abstract class AbstractPythonRunConfiguration<T extends AbstractRunConfig
   private boolean myAddSourceRoots = true;
 
   protected PathMappingSettings myMappingSettings;
+  /**
+   * To prevent "double module saving" child may enable this flag
+   * and no module info would be saved
+   */
+  protected boolean mySkipModuleSerialization;
 
   public AbstractPythonRunConfiguration(Project project, final ConfigurationFactory factory) {
     super(project, factory);
@@ -235,7 +240,9 @@ public abstract class AbstractPythonRunConfiguration<T extends AbstractRunConfig
     myAddContentRoots = addContentRoots == null || Boolean.parseBoolean(addContentRoots);
     final String addSourceRoots = JDOMExternalizerUtil.readField(element, "ADD_SOURCE_ROOTS");
     myAddSourceRoots = addSourceRoots == null || Boolean.parseBoolean(addSourceRoots);
-    getConfigurationModule().readExternal(element);
+    if ( !mySkipModuleSerialization) {
+      getConfigurationModule().readExternal(element);
+    }
 
     setMappingSettings(PathMappingSettings.readExternal(element));
     // extension settings:
@@ -259,7 +266,9 @@ public abstract class AbstractPythonRunConfiguration<T extends AbstractRunConfig
     JDOMExternalizerUtil.writeField(element, "IS_MODULE_SDK", Boolean.toString(myUseModuleSdk));
     JDOMExternalizerUtil.writeField(element, "ADD_CONTENT_ROOTS", Boolean.toString(myAddContentRoots));
     JDOMExternalizerUtil.writeField(element, "ADD_SOURCE_ROOTS", Boolean.toString(myAddSourceRoots));
-    getConfigurationModule().writeExternal(element);
+    if ( !mySkipModuleSerialization) {
+      getConfigurationModule().writeExternal(element);
+    }
 
     // extension settings:
     PythonRunConfigurationExtensionsManager.getInstance().writeExternal(this, element);
index 5025ce19fe1100215b305e80841d054d0fe7905f..81ee29677e417e9d45814097c2d1748ab0671bb7 100644 (file)
@@ -15,6 +15,8 @@
  */
 package com.jetbrains.python.testing.tox;
 
+import com.intellij.execution.configurations.GeneralCommandLine;
+import com.intellij.execution.configurations.ParamsGroup;
 import com.intellij.execution.runners.ExecutionEnvironment;
 import com.jetbrains.python.HelperPackage;
 import com.jetbrains.python.PythonHelper;
@@ -29,9 +31,15 @@ import java.util.List;
  */
 class PyToxCommandLineState extends PythonTestCommandLineStateBase {
 
-  public PyToxCommandLineState(PyToxConfiguration configuration,
-                               ExecutionEnvironment environment) {
+  @NotNull
+  private final String[] myArguments;
+
+
+  PyToxCommandLineState(@NotNull final PyToxConfiguration configuration,
+                                @NotNull final ExecutionEnvironment environment,
+                                @NotNull final String... arguments) {
     super(configuration, environment);
+    myArguments = arguments;
   }
 
   @Override
@@ -39,6 +47,16 @@ class PyToxCommandLineState extends PythonTestCommandLineStateBase {
     return PythonHelper.TOX;
   }
 
+
+  @Override
+  public GeneralCommandLine generateCommandLine() {
+    final GeneralCommandLine line = super.generateCommandLine();
+    final ParamsGroup group = line.getParametersList().getParamsGroup(GROUP_SCRIPT);
+    assert group != null: "No group " + GROUP_SCRIPT;
+    group.addParameters(myArguments);
+    return line;
+  }
+
   @NotNull
   @Override
   protected List<String> getTestSpecs() {
index 04568c2069d8ac3febd4e6c75dedc6530b666be7..0d0a07976c3a59b2626b42f2de8287abc4a4def5 100644 (file)
@@ -20,7 +20,15 @@ import com.intellij.execution.configurations.RunProfileState;
 import com.intellij.execution.runners.ExecutionEnvironment;
 import com.intellij.openapi.options.SettingsEditor;
 import com.intellij.openapi.project.Project;
+import com.intellij.openapi.util.InvalidDataException;
+import com.intellij.openapi.util.WriteExternalException;
+import com.intellij.util.xmlb.SkipEmptySerializationFilter;
+import com.intellij.util.xmlb.XmlSerializer;
+import com.intellij.util.xmlb.annotations.Tag;
+import com.jetbrains.serialization.CompoundFilter;
+import com.jetbrains.serialization.AnnotationSerializationFilter;
 import com.jetbrains.python.run.AbstractPythonRunConfiguration;
+import org.jdom.Element;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 
@@ -29,14 +37,44 @@ import org.jetbrains.annotations.Nullable;
  */
 final class PyToxConfiguration extends AbstractPythonRunConfiguration<PyToxConfiguration> {
 
+  private static final String[] EMPTY_STRINGS = new String[0];
   @NotNull
   private final Project myProject;
 
+  @Tag
+  private String[] myArguments;
+
   PyToxConfiguration(@NotNull final PyToxConfigurationFactory factory, @NotNull final Project project) {
     super(project, factory);
     myProject = project;
+    // Module will be stored with XmlSerializer
+    //noinspection AssignmentToSuperclassField
+    mySkipModuleSerialization = true;
+  }
+
+  @NotNull
+  String[] getArguments() {
+    return (myArguments == null ? EMPTY_STRINGS : myArguments.clone());
+  }
+
+  void setArguments(@NotNull final String... arguments) {
+    myArguments = arguments.clone();
   }
 
+  @Override
+  public void readExternal(Element element) throws InvalidDataException {
+    super.readExternal(element);
+    XmlSerializer.deserializeInto(this, element);
+  }
+
+  @Override
+  public void writeExternal(final Element element) throws WriteExternalException {
+    super.writeExternal(element);
+    XmlSerializer.serializeInto(this, element, new CompoundFilter(
+      new SkipEmptySerializationFilter(),
+      new AnnotationSerializationFilter()
+    ));
+  }
 
   @Override
   protected SettingsEditor<PyToxConfiguration> createConfigurationEditor() {
@@ -46,7 +84,6 @@ final class PyToxConfiguration extends AbstractPythonRunConfiguration<PyToxConfi
   @Nullable
   @Override
   public RunProfileState getState(@NotNull final Executor executor, @NotNull final ExecutionEnvironment environment) {
-    getWorkingDirectory();
-    return new PyToxCommandLineState(this, environment);
+    return  new PyToxCommandLineState(this, environment, myArguments);
   }
 }
diff --git a/python/src/com/jetbrains/python/testing/tox/PyToxConfigurationSettings.form b/python/src/com/jetbrains/python/testing/tox/PyToxConfigurationSettings.form
new file mode 100644 (file)
index 0000000..9f5beb9
--- /dev/null
@@ -0,0 +1,52 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.jetbrains.python.testing.tox.PyToxConfigurationSettings">
+  <grid id="27dc6" binding="myPanel" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
+    <margin top="0" left="0" bottom="0" right="0"/>
+    <constraints>
+      <xy x="20" y="20" width="500" height="400"/>
+    </constraints>
+    <properties/>
+    <border type="none"/>
+    <children>
+      <component id="e408a" class="javax.swing.JLabel">
+        <constraints>
+          <grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
+        </constraints>
+        <properties>
+          <text value="Tox"/>
+        </properties>
+      </component>
+      <grid id="f677e" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
+        <margin top="0" left="0" bottom="0" right="0"/>
+        <constraints>
+          <grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
+        </constraints>
+        <properties/>
+        <border type="none"/>
+        <children>
+          <vspacer id="a4965">
+            <constraints>
+              <grid row="1" column="1" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
+            </constraints>
+          </vspacer>
+          <component id="43a1d" class="javax.swing.JTextField" binding="myArgumentsField">
+            <constraints>
+              <grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
+                <preferred-size width="150" height="-1"/>
+              </grid>
+            </constraints>
+            <properties/>
+          </component>
+          <component id="8d4c" class="com.intellij.ui.components.JBLabel">
+            <constraints>
+              <grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
+            </constraints>
+            <properties>
+              <text value="Arguments"/>
+            </properties>
+          </component>
+        </children>
+      </grid>
+    </children>
+  </grid>
+</form>
index 1b5691470144f898cb2fbcdc3fb2f3d7b4d24e8a..1c97aad51f5d9ac611b9d38438fc86535d00536a 100644 (file)
@@ -16,9 +16,9 @@
 package com.jetbrains.python.testing.tox;
 
 import com.intellij.openapi.module.Module;
-import com.intellij.openapi.options.ConfigurationException;
 import com.intellij.openapi.options.SettingsEditor;
 import com.intellij.openapi.project.Project;
+import com.intellij.openapi.util.text.StringUtil;
 import com.jetbrains.python.run.AbstractPyCommonOptionsForm;
 import com.jetbrains.python.run.AbstractPythonRunConfiguration;
 import com.jetbrains.python.run.PyCommonOptionsFormData;
@@ -28,27 +28,33 @@ import org.jetbrains.annotations.NotNull;
 import javax.swing.*;
 import java.awt.*;
 import java.util.List;
+import java.util.regex.Pattern;
 
 /**
  * @author Ilya.Kazakevich
  */
 final class PyToxConfigurationSettings extends SettingsEditor<PyToxConfiguration> {
+  private static final Pattern ARG_SEPARATOR = Pattern.compile("\\s+");
   @NotNull
   private final Project myProject;
   private AbstractPyCommonOptionsForm myForm;
+  private JPanel myPanel;
+  private JTextField myArgumentsField;
 
   PyToxConfigurationSettings(@NotNull final Project project) {
     myProject = project;
   }
 
   @Override
-  protected void applyEditorTo(final PyToxConfiguration s) throws ConfigurationException {
+  protected void applyEditorTo(final PyToxConfiguration s) {
     AbstractPythonRunConfiguration.copyParams(myForm, s);
+    s.setArguments(ARG_SEPARATOR.split(myArgumentsField.getText()));
   }
 
   @Override
   protected void resetEditorFrom(final PyToxConfiguration s) {
     AbstractPythonRunConfiguration.copyParams(s, myForm);
+    myArgumentsField.setText(StringUtil.join(s.getArguments(), " "));
   }
 
   @NotNull
@@ -56,7 +62,7 @@ final class PyToxConfigurationSettings extends SettingsEditor<PyToxConfiguration
   protected JComponent createEditor() {
 
     final JPanel panel = new JPanel(new BorderLayout());
-    panel.add(new JLabel("Tox"), BorderLayout.PAGE_START);
+    panel.add(myPanel, BorderLayout.PAGE_START);
 
     myForm = createEnvPanel();
     final JComponent envPanel = myForm.getMainPanel();
diff --git a/python/src/com/jetbrains/serialization/AnnotationSerializationFilter.java b/python/src/com/jetbrains/serialization/AnnotationSerializationFilter.java
new file mode 100644 (file)
index 0000000..3eaea6c
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ * Copyright 2000-2015 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.jetbrains.serialization;
+
+import com.intellij.util.xmlb.Accessor;
+import com.intellij.util.xmlb.SerializationFilterBase;
+import com.intellij.util.xmlb.annotations.Attribute;
+import com.intellij.util.xmlb.annotations.Property;
+import com.intellij.util.xmlb.annotations.Tag;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Only accepts accessors with xmlb tags
+ * @author Ilya.Kazakevich
+ */
+public final class AnnotationSerializationFilter extends SerializationFilterBase {
+  @Override
+  protected boolean accepts(@NotNull final Accessor accessor, @NotNull final Object bean, @Nullable final Object beanValue) {
+    return accessor.getAnnotation(Property.class) != null
+           || accessor.getAnnotation(Tag.class) != null
+           || accessor.getAnnotation(Attribute.class) != null;
+  }
+}
diff --git a/python/src/com/jetbrains/serialization/CompoundFilter.java b/python/src/com/jetbrains/serialization/CompoundFilter.java
new file mode 100644 (file)
index 0000000..58f513f
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2000-2015 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.jetbrains.serialization;
+
+import com.intellij.util.xmlb.Accessor;
+import com.intellij.util.xmlb.SerializationFilter;
+import com.intellij.util.xmlb.SerializationFilterBase;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Returns true only if all filters return true
+ * @author Ilya.Kazakevich
+ */
+public final class CompoundFilter extends SerializationFilterBase {
+  @NotNull
+  private final SerializationFilter[] myFilters;
+
+  public CompoundFilter(@NotNull final SerializationFilter... filters) {
+    myFilters = filters.clone();
+  }
+
+  @Override
+  protected boolean accepts(@NotNull final Accessor accessor, @NotNull final Object bean, @Nullable final Object beanValue) {
+    for (final SerializationFilter filter : myFilters) {
+      if (!filter.accepts(accessor, bean)) {
+        return false;
+      }
+    }
+    return true;
+  }
+}