<commit_before>package com.jetbrains.python.console;

import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.collect.Collections2;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.util.IconLoader;
import com.jetbrains.python.PyBundle;
import com.jetbrains.python.buildout.BuildoutFacet;
import com.jetbrains.python.run.PythonCommandLineState;
import com.jetbrains.python.sdk.PythonSdkType;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;

/**
 * @author oleg
 */
public class RunPythonConsoleAction extends AnAction implements DumbAware {

  public RunPythonConsoleAction() {
    super();
    getTemplatePresentation().setIcon(IconLoader.getIcon("/com/jetbrains/python/icons/python.png"));
  }

  @Override
  public void update(final AnActionEvent e) {
    e.getPresentation().setVisible(false);
    e.getPresentation().setEnabled(false);
    final Project project = e.getData(LangDataKeys.PROJECT);
    if (project != null) {
      for (Module module : ModuleManager.getInstance(project).getModules()) {
        e.getPresentation().setVisible(true);
        if (PythonSdkType.findPythonSdk(module) != null) {
          e.getPresentation().setEnabled(true);
          break;
        }
      }
    }
  }

  public void actionPerformed(final AnActionEvent e) {
    final Project project = e.getData(LangDataKeys.PROJECT);
    runPythonConsole(project);
  }

  @Nullable
  public static PydevConsoleRunner runPythonConsole(Project project) {
    assert project != null : "Project is null";
    Sdk sdk = null;
    Module module = null;
    for (Module m : ModuleManager.getInstance(project).getModules()) {
      module = m;
      sdk = PythonSdkType.findPythonSdk(module);
      if (sdk != null) {
        break;
      }
    }
    assert module != null : "Module is null";
    assert sdk != null : "Sdk is null";

    String[] setup_fragment;

    Collection<String> pythonPath = PythonCommandLineState.collectPythonPath(module);

    final String path = Joiner.on(", ").join(Collections2.transform(pythonPath, new Function<String, String>() {
      @Override
      public String apply(String input) {
        return "'" + input + "'";
      }
    }));
    String workingDir = ModuleRootManager.getInstance(module).getContentRoots()[0].getPath();
    final String self_path_append = "sys.path.extend([" + path + "])";
    BuildoutFacet facet = BuildoutFacet.getInstance(module);
    if (facet != null) {
      setup_fragment = new String[]{facet.getPathPrependStatement(), self_path_append};
    }
    else {
      setup_fragment = new String[]{self_path_append};
    }

    return PydevConsoleRunner.run(project, sdk, PyBundle.message("python.console"), workingDir, setup_fragment);
  }
}<commit_msg>Correct python path in django console.<commit_after>package com.jetbrains.python.console;

import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.collect.Collections2;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.util.IconLoader;
import com.jetbrains.python.PyBundle;
import com.jetbrains.python.buildout.BuildoutFacet;
import com.jetbrains.python.run.PythonCommandLineState;
import com.jetbrains.python.sdk.PythonSdkType;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;

/**
 * @author oleg
 */
public class RunPythonConsoleAction extends AnAction implements DumbAware {

  public RunPythonConsoleAction() {
    super();
    getTemplatePresentation().setIcon(IconLoader.getIcon("/com/jetbrains/python/icons/python.png"));
  }

  @Override
  public void update(final AnActionEvent e) {
    e.getPresentation().setVisible(false);
    e.getPresentation().setEnabled(false);
    final Project project = e.getData(LangDataKeys.PROJECT);
    if (project != null) {
      for (Module module : ModuleManager.getInstance(project).getModules()) {
        e.getPresentation().setVisible(true);
        if (PythonSdkType.findPythonSdk(module) != null) {
          e.getPresentation().setEnabled(true);
          break;
        }
      }
    }
  }

  public void actionPerformed(final AnActionEvent e) {
    final Project project = e.getData(LangDataKeys.PROJECT);
    runPythonConsole(project);
  }

  @Nullable
  public static PydevConsoleRunner runPythonConsole(Project project) {
    assert project != null : "Project is null";
    Sdk sdk = null;
    Module module = null;
    for (Module m : ModuleManager.getInstance(project).getModules()) {
      module = m;
      sdk = PythonSdkType.findPythonSdk(module);
      if (sdk != null) {
        break;
      }
    }
    assert module != null : "Module is null";
    assert sdk != null : "Sdk is null";

    String[] setup_fragment;

    Collection<String> pythonPath = PythonCommandLineState.collectPythonPath(module);

    final String self_path_append = constructPythonPathCommand(pythonPath);

    String workingDir = ModuleRootManager.getInstance(module).getContentRoots()[0].getPath();
    BuildoutFacet facet = BuildoutFacet.getInstance(module);
    if (facet != null) {
      setup_fragment = new String[]{facet.getPathPrependStatement(), self_path_append};
    }
    else {
      setup_fragment = new String[]{self_path_append};
    }

    return PydevConsoleRunner.run(project, sdk, PyBundle.message("python.console"), workingDir, setup_fragment);
  }

  public static String constructPythonPathCommand(Collection<String> pythonPath) {
    final String path = Joiner.on(", ").join(Collections2.transform(pythonPath, new Function<String, String>() {
      @Override
      public String apply(String input) {
        return "'" + input + "'";
      }
    }));

    return "sys.path.extend([" + path + "])";
  }
}
{'fraction_non_alphanumeric': 0.11676829268292684, 'fraction_numerical': 0.0009146341463414635, 'mean_word_length': 4.113795791114575, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5452096', 'n_tokens_mistral': 2004, 'n_tokens_neox': 1952, 'n_words': 368}