Exemple #1
0
  public void run(IAction action) {
    IPythonNature pythonNature;
    try {
      pythonNature = getPyEdit().getPythonNature();
    } catch (MisconfigurationException e1) {
      handle(e1);
      return;
    }
    PySelection ps = new PySelection(this.getPyEdit());
    String selectedText = ps.getSelectedText();
    if (selectedText == null || selectedText.length() == 0) {
      try {
        selectedText = ps.getCurrToken().o1;
      } catch (BadLocationException e) {
        // ignore
      }
    }

    if (pythonNature != null) {
      IInterpreterManager manager = pythonNature.getRelatedInterpreterManager();
      getFromManagerAndRelatedNatures(selectedText, manager);
    } else {
      getFromSystemManager(selectedText);
    }
  }
Exemple #2
0
 /** @return a tuple with the process created and a string representation of the cmdarray. */
 public Tuple<Process, String> run(
     String[] cmdarray, File workingDir, IPythonNature nature, IProgressMonitor monitor) {
   if (monitor == null) {
     monitor = new NullProgressMonitor();
   }
   String executionString = getArgumentsAsStr(cmdarray);
   monitor.setTaskName("Executing: " + executionString);
   monitor.worked(5);
   Process process = null;
   try {
     monitor.setTaskName("Making pythonpath environment..." + executionString);
     String[] envp = null;
     if (nature != null) {
       envp =
           getEnvironment(
               nature,
               nature.getProjectInterpreter(),
               nature.getRelatedInterpreterManager()); // Don't remove as it *should* be updated
       // based on the nature)
     }
     // Otherwise, use default (used when configuring the interpreter for instance).
     monitor.setTaskName("Making exec..." + executionString);
     if (workingDir != null) {
       if (!workingDir.isDirectory()) {
         throw new RuntimeException(
             org.python.pydev.shared_core.string.StringUtils.format(
                 "Working dir must be an existing directory (received: %s)", workingDir));
       }
     }
     process = createProcess(cmdarray, envp, workingDir);
   } catch (Exception e) {
     throw new RuntimeException(e);
   }
   return new Tuple<Process, String>(process, executionString);
 }
  public void run(IAction action) {
    FastStringBuffer buf = new FastStringBuffer();
    try {
      PyEdit pyEdit = getPyEdit();

      PySelection pySelection = new PySelection(pyEdit);

      IPythonNature nature = pyEdit.getPythonNature();
      File editorFile = pyEdit.getEditorFile();

      if (editorFile != null) {
        if (nature != null) {
          String mod = nature.resolveModule(editorFile);
          if (mod != null) {
            buf.append(mod);

          } else {
            // Support for external files (not in PYTHONPATH).
            buf.append(FullRepIterable.getFirstPart(editorFile.getName()));
          }
        } else {
          buf.append(FullRepIterable.getFirstPart(editorFile.getName()));
        }
      }

      List<stmtType> path =
          FastParser.parseToKnowGloballyAccessiblePath(
              pySelection.getDoc(), pySelection.getStartLineIndex());
      for (stmtType stmtType : path) {
        if (buf.length() > 0) {
          buf.append('.');
        }
        buf.append(NodeUtils.getRepresentationString(stmtType));
      }

    } catch (MisconfigurationException e1) {
      Log.log(e1);
      return;
    }

    Transfer[] dataTypes = new Transfer[] {TextTransfer.getInstance()};
    Object[] data = new Object[] {buf.toString()};

    Clipboard clipboard = new Clipboard(EditorUtils.getShell().getDisplay());
    try {
      clipboard.setContents(data, dataTypes);
    } catch (SWTError e) {
      if (e.code != DND.ERROR_CANNOT_SET_CLIPBOARD) {
        throw e;
      }
      MessageDialog.openError(
          EditorUtils.getShell(), "Error copying to clipboard.", e.getMessage());
    } finally {
      clipboard.dispose();
    }
  }
  /**
   * Returns the location of the selected interpreter in the launch configuration
   *
   * @param conf
   * @return the string location of the selected interpreter in the launch configuration
   * @throws CoreException if unable to retrieve the launch configuration attribute or if unable to
   *     resolve the default interpreter.
   * @throws MisconfigurationException
   */
  public static IInterpreterInfo getInterpreterLocation(
      ILaunchConfiguration conf, IPythonNature nature, IInterpreterManager interpreterManager)
      throws InvalidRunException, CoreException, MisconfigurationException {
    String location =
        conf.getAttribute(Constants.ATTR_INTERPRETER, Constants.ATTR_INTERPRETER_DEFAULT);

    if (location != null && location.equals(Constants.ATTR_INTERPRETER_DEFAULT)) {
      if (nature != null
          && nature.getInterpreterType() == interpreterManager.getInterpreterType()) {

        // When both, the interpreter for the launch and the nature have the same type, let's get
        // the
        // launch location from the project
        try {
          return nature.getProjectInterpreter();
        } catch (PythonNatureWithoutProjectException e) {
          throw new RuntimeException(e);
        }

      } else {

        // When it doesn't have the same type it means that we're trying to run as jython a python
        // project (or vice-versa), so, we must get the interpreter from the interpreter manager!
        return interpreterManager.getDefaultInterpreterInfo(true);
      }

    } else {
      IInterpreterInfo interpreterInfo = interpreterManager.getInterpreterInfo(location, null);
      if (interpreterInfo != null) {
        return interpreterInfo;
      } else {
        File file = new File(location);
        if (!file.exists()) {
          throw new InvalidRunException("Error. The interprer: " + location + " does not exist");

        } else {
          // it does not have information on the given interpreter!!
          if (nature == null) {
            throw new InvalidRunException(
                "Error. The interpreter: >>"
                    + location
                    + "<< is not configured in the pydev preferences as a valid interpreter (null nature).");
          } else {
            throw new InvalidRunException(
                "Error. The interpreter: >>"
                    + location
                    + "<< is not configured in the pydev preferences as a valid '"
                    + nature.getVersion()
                    + "' interpreter.");
          }
        }
      }
    }
  }
Exemple #5
0
  /**
   * Generates the cached tokens in the needed structure for a 'fast' search given a token
   * representation (creates a map with the name of the token --> token).
   */
  private Map<String, IToken> internalGenerateCachedTokens(
      IPythonNature nature,
      ICompletionCache completionCache,
      String activationToken,
      boolean searchSameLevelMods)
      throws CompletionRecursionException {

    Map<String, IToken> cachedTokens = new HashMap<String, IToken>();

    // if still not found, we have to get all the tokens, including regular and wild imports
    ICompletionState state =
        CompletionStateFactory.getEmptyCompletionState(nature, completionCache);
    ICodeCompletionASTManager astManager = nature.getAstManager();
    state.setActivationToken(activationToken);

    // we don't want to gather builtins in this case.
    state.setBuiltinsGotten(true);
    IToken[] globalTokens = astManager.getCompletionsForModule(this, state, searchSameLevelMods);
    for (IToken token : globalTokens) {
      String rep = token.getRepresentation();
      IToken t = cachedTokens.get(rep);
      if (t != null) {
        // only override tokens if it's a getattr that's not defined in the builtin module
        if (rep.equals("__getattribute__") || rep.equals("__getattr__")) {
          if (!isTokenFromBuiltins(token)) {
            cachedTokens.put(rep, token);
          }
        }
      } else {
        cachedTokens.put(rep, token);
      }
    }
    return cachedTokens;
  }
 /** @return the module name or null if it is not possible to determine the module name */
 public String resolveModule() {
   if (moduleName == null) {
     if (file != null && nature != null) {
       try {
         moduleName = nature.resolveModule(file);
       } catch (MisconfigurationException e) {
         throw new RuntimeException(e);
       }
     }
   }
   return moduleName;
 }
Exemple #7
0
 /**
  * This function creates a module and resolves the module name (use this function if only the file
  * is available).
  *
  * @throws MisconfigurationException
  */
 public static IModule createModuleFromDoc(File file, IDocument doc, IPythonNature pythonNature)
     throws MisconfigurationException {
   IModulesManager projModulesManager = pythonNature.getAstManager().getModulesManager();
   String moduleName = null;
   if (file != null) {
     moduleName = projModulesManager.resolveModule(FileUtils.getFileAbsolutePath(file));
   }
   if (moduleName == null) {
     moduleName = MODULE_NAME_WHEN_FILE_IS_UNDEFINED;
   }
   IModule module = createModuleFromDoc(moduleName, file, doc, pythonNature, false);
   return module;
 }
Exemple #8
0
  /**
   * @param pythonNatures the natures from were we can get info
   * @param additionalInfo the additional informations
   * @param selectedText the text that should be initially set as a filter
   */
  public static void doSelect(
      List<IPythonNature> pythonNatures,
      List<AbstractAdditionalTokensInfo> additionalInfo,
      String selectedText) {

    SelectionDialog dialog =
        GlobalsDialogFactory.create(getShell(), additionalInfo, selectedText, pythonNatures);

    dialog.open();
    Object[] result = dialog.getResult();
    if (result != null && result.length > 0) {
      for (Object obj : result) {
        IInfo entry;
        if (obj instanceof AdditionalInfoAndIInfo) {
          entry = ((AdditionalInfoAndIInfo) obj).info;
        } else {
          entry = (IInfo) obj;
        }
        List<ItemPointer> pointers = new ArrayList<ItemPointer>();

        CompletionCache completionCache = new CompletionCache();
        for (IPythonNature pythonNature : pythonNatures) {
          // try to find in one of the natures...
          ICodeCompletionASTManager astManager = pythonNature.getAstManager();
          if (astManager == null) {
            continue;
          }
          AnalysisPlugin.getDefinitionFromIInfo(
              pointers, astManager, pythonNature, entry, completionCache);
          if (pointers.size() > 0) {
            new PyOpenAction().run(pointers.get(0));
            break; // don't check the other natures
          }
        }
      }
    }
  }
Exemple #9
0
  /**
   * Creates a string that can be passed as the PYTHONPATH
   *
   * @param project the project we want to get the settings from. If it is null, the system
   *     pythonpath is returned
   * @param interpreter this is the interpreter to be used to create the env.
   * @return a string that can be used as the PYTHONPATH env variable
   */
  public static String makePythonPathEnvString(
      IPythonNature pythonNature, IInterpreterInfo interpreter, IInterpreterManager manager) {
    if (pythonNature == null) {
      if (interpreter == null) {
        return makePythonPathEnvFromPaths(
            new ArrayList<
                String>()); // no pythonpath can be gotten (set to empty, so that the default is
                            // gotten)
      } else {
        List<String> pythonPath = interpreter.getPythonPath();
        return makePythonPathEnvFromPaths(pythonPath);
      }
    }

    List<String> paths;

    // if we have a project, get its complete pythonpath
    IPythonPathNature pythonPathNature = pythonNature.getPythonPathNature();
    if (pythonPathNature == null) {
      IProject project = pythonNature.getProject();
      String projectName;
      if (project == null) {
        projectName = "null?";
      } else {
        projectName = project.getName();
      }
      throw new RuntimeException(
          "The project "
              + projectName
              + " does not have the pythonpath configured, \n"
              + "please configure it correcly (please check the pydev getting started guide at \n"
              + "http://pydev.org/manual_101_root.html for better information on how to do it).");
    }
    paths = pythonPathNature.getCompleteProjectPythonPath(interpreter, manager);

    return makePythonPathEnvFromPaths(paths);
  }
Exemple #10
0
  /**
   * This method creates a source module from a file.
   *
   * @return
   * @throws IOException
   * @throws MisconfigurationException
   */
  public static AbstractModule createModule(
      String name, File f, IPythonNature nature, boolean checkForPath)
      throws IOException, MisconfigurationException {
    if (PythonPathHelper.isValidFileMod(f.getName())) {
      if (PythonPathHelper.isValidSourceFile(f.getName())) {
        return createModuleFromDoc(
            name, f, FileUtilsFileBuffer.getDocFromFile(f), nature, checkForPath);

      } else { // this should be a compiled extension... we have to get completions from the python
        // shell.
        return new CompiledModule(name, nature.getAstManager().getModulesManager(), nature);
      }
    }

    // if we are here, return null...
    return null;
  }
Exemple #11
0
  @Override
  public boolean isInGlobalTokens(
      String tok, IPythonNature nature, ICompletionCache completionCache) {
    // we have to override because there is no way to check if it is in some import from some other
    // place if it has dots on the tok...

    if (tok.indexOf('.') == -1) {
      return isInDirectGlobalTokens(tok, completionCache);
    } else {
      ICompletionState state =
          CompletionStateFactory.getEmptyCompletionState(nature, completionCache);
      String[] headAndTail = FullRepIterable.headAndTail(tok);
      state.setActivationToken(headAndTail[0]);
      String head = headAndTail[1];
      IToken[] globalTokens = getGlobalTokens(state, nature.getAstManager());
      for (IToken token : globalTokens) {
        if (token.getRepresentation().equals(head)) {
          return true;
        }
      }
    }
    return false;
  }
 /**
  * Expands and returns the working directory attribute of the given launch configuration. Returns
  * <code>null</code> if a working directory is not specified. If specified, the working is
  * verified to point to an existing directory in the local file system.
  *
  * @param configuration launch configuration
  * @return an absolute path to a directory in the local file system, or <code>null</code> if
  *     unspecified
  * @throws CoreException if unable to retrieve the associated launch configuration attribute, if
  *     unable to resolve any variables, or if the resolved location does not point to an existing
  *     directory in the local file system
  */
 public static IPath getWorkingDirectory(ILaunchConfiguration configuration, IPythonNature nature)
     throws CoreException {
   IProject project = nature.getProject();
   String location =
       configuration.getAttribute(
           Constants.ATTR_WORKING_DIRECTORY, "${project_loc:/" + project.getName() + "}");
   if (location != null) {
     String expandedLocation = getStringSubstitution(nature).performStringSubstitution(location);
     if (expandedLocation.length() > 0) {
       File path = new File(expandedLocation);
       if (path.isDirectory()) {
         return new Path(expandedLocation);
       }
       throw new CoreException(
           PydevDebugPlugin.makeStatus(
               IStatus.ERROR,
               "Unable to get working location for the run \n(the location: '"
                   + expandedLocation
                   + "' is not a valid directory).",
               null));
     }
   }
   return null;
 }
Exemple #13
0
  /* (non-Javadoc)
   * @see org.python.pydev.editor.codecompletion.IPyCodeCompletion#getCodeCompletionProposals(org.eclipse.jface.text.ITextViewer, org.python.pydev.editor.codecompletion.CompletionRequest)
   */
  @SuppressWarnings("unchecked")
  public List getCodeCompletionProposals(ITextViewer viewer, CompletionRequest request)
      throws CoreException, BadLocationException, IOException, MisconfigurationException,
          PythonNatureWithoutProjectException {
    if (request.getPySelection().getCursorLineContents().trim().startsWith("#")) {
      // this may happen if the context is still not correctly computed in python
      return new PyStringCodeCompletion().getCodeCompletionProposals(viewer, request);
    }
    if (DebugSettings.DEBUG_CODE_COMPLETION) {
      Log.toLogFile(this, "Starting getCodeCompletionProposals");
      Log.addLogLevel();
      Log.toLogFile(this, "Request:" + request);
    }

    ArrayList<ICompletionProposal> ret = new ArrayList<ICompletionProposal>();

    // let's see if we should do a code-completion in the current scope...
    if (!isValidCompletionContext(request)) {
      request.showTemplates = false;
      return ret;
    }

    try {
      IPythonNature pythonNature = request.nature;
      checkPythonNature(pythonNature);

      ICodeCompletionASTManager astManager = pythonNature.getAstManager();
      if (astManager == null) {
        // we're probably still loading it.
        return ret;
      }

      // list of Object[], IToken or ICompletionProposal
      List<Object> tokensList = new ArrayList<Object>();
      lazyStartShell(request);
      String trimmed = request.activationToken.replace('.', ' ').trim();

      ImportInfo importsTipper = getImportsTipperStr(request);

      int line = request.doc.getLineOfOffset(request.documentOffset);
      IRegion region = request.doc.getLineInformation(line);

      ICompletionState state =
          new CompletionState(
              line,
              request.documentOffset - region.getOffset(),
              null,
              request.nature,
              request.qualifier);
      state.setIsInCalltip(request.isInCalltip);

      boolean importsTip = false;

      if (importsTipper.importsTipperStr.length() != 0) {
        // code completion in imports
        request.isInCalltip = false; // if found after (, but in an import, it is not a calltip!
        importsTip = doImportCompletion(request, astManager, tokensList, importsTipper);

      } else if (trimmed.length() > 0 && request.activationToken.indexOf('.') != -1) {
        // code completion for a token
        doTokenCompletion(request, astManager, tokensList, trimmed, state);

      } else {
        // go to globals
        doGlobalsCompletion(request, astManager, tokensList, state);
      }

      Map<String, IToken> alreadyChecked = new HashMap<String, IToken>();

      String lowerCaseQual = request.qualifier.toLowerCase();
      if (lowerCaseQual.length()
          >= PyCodeCompletionPreferencesPage.getArgumentsDeepAnalysisNChars()) {
        // this can take some time on the analysis, so, let's let the user choose on how many chars
        // does he
        // want to do the analysis...
        state.pushFindResolveImportMemoryCtx();
        try {
          for (Iterator<Object> it = tokensList.listIterator(); it.hasNext(); ) {
            Object o = it.next();
            if (o instanceof IToken) {
              it
                  .remove(); // always remove the tokens from the list (they'll be re-added later
                             // once they are filtered)

              IToken initialToken = (IToken) o;

              IToken token = initialToken;
              String strRep = token.getRepresentation();
              IToken prev = alreadyChecked.get(strRep);

              if (prev != null) {
                if (prev.getArgs().length() != 0) {
                  continue; // we already have a version with args... just keep going
                }
              }

              if (!strRep.toLowerCase().startsWith(lowerCaseQual)) {
                // just re-add it if we're going to actually use it (depending on the qualifier)
                continue;
              }

              while (token.isImportFrom()) {
                // we'll only add it here if it is an import from (so, set the flag to false for the
                // outer add)

                if (token.getArgs().length() > 0) {
                  // if we already have the args, there's also no reason to do it (that's what we'll
                  // do here)
                  break;
                }
                ICompletionState s =
                    state.getCopyForResolveImportWithActTok(token.getRepresentation());
                s.checkFindResolveImportMemory(token);

                IToken token2 = astManager.resolveImport(s, token);
                if (token2 != null && initialToken != token2) {
                  String args = token2.getArgs();
                  if (args.length() > 0) {
                    // put it into the map (may override previous if it didn't have args)
                    initialToken.setArgs(args);
                    initialToken.setDocStr(token2.getDocStr());
                    if (initialToken instanceof SourceToken && token2 instanceof SourceToken) {
                      SourceToken initialSourceToken = (SourceToken) initialToken;
                      SourceToken token2SourceToken = (SourceToken) token2;
                      initialSourceToken.setAst(token2SourceToken.getAst());
                    }
                    break;
                  }
                  if (token2 == null
                      || (token2.equals(token)
                          && token2.getArgs().equals(token.getArgs())
                          && token2.getParentPackage().equals(token.getParentPackage()))) {
                    break;
                  }
                  token = token2;
                } else {
                  break;
                }
              }

              alreadyChecked.put(strRep, initialToken);
            }
          }

        } finally {
          state.popFindResolveImportMemoryCtx();
        }
      }

      tokensList.addAll(alreadyChecked.values());
      changeItokenToCompletionPropostal(viewer, request, ret, tokensList, importsTip, state);
    } catch (CompletionRecursionException e) {
      if (onCompletionRecursionException != null) {
        onCompletionRecursionException.call(e);
      }
      if (DebugSettings.DEBUG_CODE_COMPLETION) {
        Log.toLogFile(e);
      }
      // PydevPlugin.log(e);
      // ret.add(new CompletionProposal("",request.documentOffset,0,0,null,e.getMessage(),
      // null,null));
    }

    if (DebugSettings.DEBUG_CODE_COMPLETION) {
      Log.remLogLevel();
      Log.toLogFile(this, "Finished completion. Returned:" + ret.size() + " completions.\r\n");
    }

    return ret;
  }
Exemple #14
0
  private void fillNatureCompletionsForConsole(
      IScriptConsoleViewer viewer,
      int requestOffset,
      List<ICompletionProposal> completions,
      String qual,
      boolean addAutoImport,
      int qlen,
      String lowerQual,
      IPythonNature nature,
      boolean getSystem) {
    AbstractAdditionalInterpreterInfo additionalInfoForProject;

    if (getSystem) {
      try {
        additionalInfoForProject =
            AdditionalSystemInterpreterInfo.getAdditionalSystemInfo(
                PydevPlugin.getInterpreterManager(nature),
                nature.getProjectInterpreter().getExecutableOrJar());
      } catch (Exception e) {
        PydevPlugin.log(e);
        return;
      }
    } else {
      try {
        additionalInfoForProject =
            AdditionalProjectInterpreterInfo.getAdditionalInfoForProject(nature);
      } catch (Exception e) {
        PydevPlugin.log(e);
        return;
      }
    }

    List<IInfo> tokensStartingWith =
        additionalInfoForProject.getTokensStartingWith(
            qual, AbstractAdditionalInterpreterInfo.TOP_LEVEL);

    FastStringBuffer realImportRep = new FastStringBuffer();
    FastStringBuffer displayString = new FastStringBuffer();
    FastStringBuffer tempBuf = new FastStringBuffer();
    for (IInfo info : tokensStartingWith) {
      // there always must be a declaringModuleName
      String declaringModuleName = info.getDeclaringModuleName();
      boolean hasInit = false;
      if (declaringModuleName.endsWith(".__init__")) {
        declaringModuleName =
            declaringModuleName.substring(
                0, declaringModuleName.length() - 9); // remove the .__init__
        hasInit = true;
      }

      String rep = info.getName();
      String lowerRep = rep.toLowerCase();
      if (!lowerRep.startsWith(lowerQual)) {
        continue;
      }

      if (addAutoImport) {
        realImportRep.clear();
        realImportRep.append("from ");
        realImportRep.append(
            AutoImportsPreferencesPage.removeImportsStartingWithUnderIfNeeded(
                declaringModuleName, tempBuf));
        realImportRep.append(" import ");
        realImportRep.append(rep);
      }

      displayString.clear();
      displayString.append(rep);
      displayString.append(" - ");
      displayString.append(declaringModuleName);
      if (hasInit) {
        displayString.append(".__init__");
      }

      PyConsoleCompletion proposal =
          new PyConsoleCompletion(
              rep,
              requestOffset - qlen,
              qlen,
              realImportRep.length(),
              AnalysisPlugin.getImageForAutoImportTypeInfo(info),
              displayString.toString(),
              (IContextInformation) null,
              "",
              lowerRep.equals(lowerQual)
                  ? IPyCompletionProposal.PRIORITY_LOCALS_1
                  : IPyCompletionProposal.PRIORITY_GLOBALS,
              realImportRep.toString(),
              viewer);

      completions.add(proposal);
    }
  }
  /**
   * Sets defaults.
   *
   * @throws InvalidRunException
   * @throws MisconfigurationException
   */
  @SuppressWarnings("unchecked")
  public PythonRunnerConfig(
      ILaunchConfiguration conf, String mode, String run, boolean makeArgumentsVariableSubstitution)
      throws CoreException, InvalidRunException, MisconfigurationException {
    // 1st thing, see if this is a valid run.
    project = getProjectFromConfiguration(conf);

    if (project == null) { // Ok, we could not find it out
      throw Log.log("Could not get project for configuration: " + conf);
    }

    // We need the project to find out the default interpreter from the InterpreterManager.
    IPythonNature pythonNature = PythonNature.getPythonNature(project);
    if (pythonNature == null) {
      CoreException e = Log.log("No python nature for project: " + project.getName());
      throw e;
    }

    // now, go on configuring other things
    this.configuration = conf;
    this.run = run;
    isDebug = mode.equals(ILaunchManager.DEBUG_MODE);
    isInteractive = mode.equals("interactive");

    resource = getLocation(conf, pythonNature);
    arguments = getArguments(conf, makeArgumentsVariableSubstitution);
    IPath workingPath = getWorkingDirectory(conf, pythonNature);
    workingDirectory = workingPath == null ? null : workingPath.toFile();
    acceptTimeout = PydevPrefs.getPreferences().getInt(PydevEditorPrefs.CONNECT_TIMEOUT);

    interpreterLocation =
        getInterpreterLocation(conf, pythonNature, this.getRelatedInterpreterManager());
    interpreter = getInterpreter(interpreterLocation, conf, pythonNature);

    // make the environment
    ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager();
    envp = launchManager.getEnvironment(conf);
    IInterpreterManager manager;
    if (isJython()) {
      manager = PydevPlugin.getJythonInterpreterManager();
    } else if (isIronpython()) {
      manager = PydevPlugin.getIronpythonInterpreterManager();
    } else {
      manager = PydevPlugin.getPythonInterpreterManager();
    }

    boolean win32 = PlatformUtils.isWindowsPlatform();

    if (envp == null) {
      // ok, the user has done nothing to the environment, just get all the default environment
      // which has the pythonpath in it
      envp = SimpleRunner.getEnvironment(pythonNature, interpreterLocation, manager);

    } else {
      // ok, the user has done something to configure it, so, just add the pythonpath to the
      // current env (if he still didn't do so)
      Map envMap = conf.getAttribute(ILaunchManager.ATTR_ENVIRONMENT_VARIABLES, (Map) null);

      String pythonpath =
          SimpleRunner.makePythonPathEnvString(pythonNature, interpreterLocation, manager);
      updateVar(pythonNature, manager, win32, envMap, "PYTHONPATH", pythonpath);
      if (isJython()) {
        // Also update the classpath env variable.
        updateVar(pythonNature, manager, win32, envMap, "CLASSPATH", pythonpath);
        // And the jythonpath env variable
        updateVar(pythonNature, manager, win32, envMap, "JYTHONPATH", pythonpath);

      } else if (isIronpython()) {
        // Also update the ironpythonpath env variable.
        updateVar(pythonNature, manager, win32, envMap, "IRONPYTHONPATH", pythonpath);
      }

      // And we also must get the environment variables specified in the interpreter manager.
      envp = interpreterLocation.updateEnv(envp, envMap.keySet());
    }

    boolean hasDjangoNature = project.hasNature(PythonNature.DJANGO_NATURE_ID);

    String settingsModule = null;
    Map<String, String> variableSubstitution = null;
    final String djangoSettingsKey = "DJANGO_SETTINGS_MODULE";
    String djangoSettingsEnvEntry = null;
    try {
      variableSubstitution = pythonNature.getPythonPathNature().getVariableSubstitution();
      settingsModule = variableSubstitution.get(djangoSettingsKey);
      if (settingsModule != null) {
        if (settingsModule.trim().length() > 0) {
          djangoSettingsEnvEntry = djangoSettingsKey + "=" + settingsModule.trim();
        }
      }
    } catch (Exception e1) {
      Log.log(e1);
    }
    if (djangoSettingsEnvEntry == null && hasDjangoNature) {
      // Default if not specified (only add it if the nature is there).
      djangoSettingsEnvEntry = djangoSettingsKey + "=" + project.getName() + ".settings";
    }

    // Note: set flag even if not debugging as the user may use remote-debugging later on.
    boolean geventSupport =
        DebugPrefsPage.getGeventDebugging()
            && pythonNature.getInterpreterType() == IPythonNature.INTERPRETER_TYPE_PYTHON;

    // Now, set the pythonpathUsed according to what's in the environment.
    String p = "";
    for (int i = 0; i < envp.length; i++) {
      String s = envp[i];
      Tuple<String, String> tup = StringUtils.splitOnFirst(s, '=');
      String var = tup.o1;
      if (win32) {
        // On windows it doesn't matter, always consider uppercase.
        var = var.toUpperCase();
      }

      if (var.equals("PYTHONPATH")) {
        p = tup.o2;

      } else if (var.equals(djangoSettingsKey)) {
        // Update it.
        if (djangoSettingsEnvEntry != null) {
          envp[i] = djangoSettingsEnvEntry;
          djangoSettingsEnvEntry = null;
        }
      }

      if (geventSupport) {
        if (var.equals("GEVENT_SUPPORT")) {
          // Flag already set in the environment
          geventSupport = false;
        }
      }
    }

    // Still not added, let's do that now.
    if (djangoSettingsEnvEntry != null) {
      envp = StringUtils.addString(envp, djangoSettingsEnvEntry);
    }
    if (geventSupport) {
      envp = StringUtils.addString(envp, "GEVENT_SUPPORT=True");
    }
    this.pythonpathUsed = p;
  }
Exemple #16
0
  /**
   * @param findInfo
   * @see
   *     org.python.pydev.editor.codecompletion.revisited.modules.AbstractModule#findDefinition(java.lang.String,
   *     int, int)
   */
  public Definition[] findDefinition(
      ICompletionState state, int line, int col, IPythonNature nature) throws Exception {
    String token = state.getActivationToken();

    if (TRACE_COMPILED_MODULES) {
      System.out.println("CompiledModule.findDefinition:" + token);
    }
    Definition[] found = this.definitionsFoundCache.getObj(token);
    if (found != null) {
      if (TRACE_COMPILED_MODULES) {
        System.out.println("CompiledModule.findDefinition: found in cache.");
      }
      return found;
    }

    AbstractShell shell = AbstractShell.getServerShell(nature, AbstractShell.COMPLETION_SHELL);
    synchronized (shell) {
      Tuple<String[], int[]> def =
          shell.getLineCol(
              this.name,
              token,
              nature
                  .getAstManager()
                  .getModulesManager()
                  .getCompletePythonPath(
                      nature.getProjectInterpreter(),
                      nature.getRelatedInterpreterManager())); // default
      if (def == null) {
        if (TRACE_COMPILED_MODULES) {
          System.out.println("CompiledModule.findDefinition:" + token + " = empty");
        }
        this.definitionsFoundCache.add(token, EMPTY_DEFINITION);
        return EMPTY_DEFINITION;
      }
      String fPath = def.o1[0];
      if (fPath.equals("None")) {
        if (TRACE_COMPILED_MODULES) {
          System.out.println("CompiledModule.findDefinition:" + token + " = None");
        }
        Definition[] definition =
            new Definition[] {new Definition(def.o2[0], def.o2[1], token, null, null, this)};
        this.definitionsFoundCache.add(token, definition);
        return definition;
      }
      File f = new File(fPath);
      String foundModName = nature.resolveModule(f);
      String foundAs = def.o1[1];

      IModule mod;
      if (foundModName == null) {
        // this can happen in a case where we have a definition that's found from a compiled file
        // which actually
        // maps to a file that's outside of the pythonpath known by Pydev.
        String n = FullRepIterable.getFirstPart(f.getName());
        mod = AbstractModule.createModule(n, f, nature, true);
      } else {
        mod = nature.getAstManager().getModule(foundModName, nature, true);
      }

      if (TRACE_COMPILED_MODULES) {
        System.out.println("CompiledModule.findDefinition: found at:" + mod.getName());
      }
      int foundLine = def.o2[0];
      if (foundLine == 0
          && foundAs != null
          && foundAs.length() > 0
          && mod != null
          && state.canStillCheckFindSourceFromCompiled(mod, foundAs)) {
        // TODO: The nature (and so the grammar to be used) must be defined by the file we'll parse
        // (so, we need to know the system modules manager that actually created it to know the
        // actual nature)
        IModule sourceMod =
            AbstractModule.createModuleFromDoc(
                mod.getName(), f, new Document(REF.getFileContents(f)), nature, true);
        if (sourceMod instanceof SourceModule) {
          Definition[] definitions =
              (Definition[])
                  sourceMod.findDefinition(state.getCopyWithActTok(foundAs), -1, -1, nature);
          if (definitions.length > 0) {
            this.definitionsFoundCache.add(token, definitions);
            return definitions;
          }
        }
      }
      if (mod == null) {
        mod = this;
      }
      int foundCol = def.o2[1];
      if (foundCol < 0) {
        foundCol = 0;
      }
      if (TRACE_COMPILED_MODULES) {
        System.out.println("CompiledModule.findDefinition: found compiled at:" + mod.getName());
      }
      Definition[] definitions =
          new Definition[] {new Definition(foundLine + 1, foundCol + 1, token, null, null, mod)};
      this.definitionsFoundCache.add(token, definitions);
      return definitions;
    }
  }
Exemple #17
0
  /**
   * @param name determines the name of the method to visit (added removed or changed)
   * @param resource the resource to visit
   * @param document the document from the resource
   * @param monitor
   */
  private void visitWith(
      int visitType,
      final IResource resource,
      ICallback0<IDocument> document,
      IProgressMonitor monitor) {
    if (monitor.isCanceled()) {
      return; // it's already cancelled
    }
    IPythonNature nature = PythonNature.getPythonNature(resource);
    if (nature == null) {
      return;
    }
    if (!nature.startRequests()) {
      return;
    }

    try {

      try {
        // we visit external because we must index them
        if (!isResourceInPythonpathProjectSources(resource, nature, true)) {
          return; // we only analyze resources that are in the pythonpath
        }
      } catch (Exception e1) {
        Log.log(e1);
        return; // we only analyze resources that are in the pythonpath
      }

      VisitorMemo copyMemo = new VisitorMemo(this.memo);
      FastStringBuffer bufferToCommunicateProgress = new FastStringBuffer();

      for (PyDevBuilderVisitor visitor : visitors) {
        // some visitors cannot visit too many elements because they do a lot of processing
        if (visitor.maxResourcesToVisit() == PyDevBuilderVisitor.MAX_TO_VISIT_INFINITE
            || visitor.maxResourcesToVisit() >= totalResources) {
          visitor.memo = copyMemo; // setting the memo must be the first thing.
          try {
            // communicate progress for each visitor
            PyDevBuilder.communicateProgress(
                monitor,
                totalResources,
                currentResourcesVisited,
                resource,
                visitor,
                bufferToCommunicateProgress);
            switch (visitType) {
              case VISIT_ADD:
                visitor.visitAddedResource(resource, document, monitor);
                break;

              case VISIT_CHANGE:
                visitor.visitChangedResource(resource, document, monitor);
                break;

              case VISIT_REMOVE:
                visitor.visitRemovedResource(resource, document, monitor);
                break;

              default:
                throw new RuntimeException("Error: visit type not properly given!"); // $NON-NLS-1$
            }
          } catch (Exception e) {
            Log.log(e);
          }
        }
      }

    } finally {
      nature.endRequests();
    }
  }
  @Override
  public void addProps(
      MarkerAnnotationAndPosition markerAnnotation,
      IAnalysisPreferences analysisPreferences,
      String line,
      PySelection ps,
      int offset,
      IPythonNature nature,
      PyEdit edit,
      List<ICompletionProposal> props)
      throws BadLocationException, CoreException {
    if (nature == null) {
      return;
    }

    ICodeCompletionASTManager astManager = nature.getAstManager();
    if (astManager == null) {
      return;
    }

    if (markerAnnotation.position == null) {
      return;
    }
    IMarker marker = markerAnnotation.markerAnnotation.getMarker();
    Integer id = (Integer) marker.getAttribute(AnalysisRunner.PYDEV_ANALYSIS_TYPE);
    int start = markerAnnotation.position.offset;
    int end = start + markerAnnotation.position.length;
    ps.setSelection(start, end);
    String markerContents;
    try {
      markerContents = ps.getSelectedText();
    } catch (Exception e1) {
      return; // Selection may be wrong.
    }

    IDocument doc = ps.getDoc();
    List<String> parametersAfterCall = ps.getParametersAfterCall(end);

    switch (id) {
      case IAnalysisPreferences.TYPE_UNDEFINED_VARIABLE:
        addCreateClassOption(ps, edit, props, markerContents, parametersAfterCall);

        addCreateMethodOption(ps, edit, props, markerContents, parametersAfterCall);
        break;

      case IAnalysisPreferences.TYPE_UNDEFINED_IMPORT_VARIABLE:
        // Say we had something as:
        // import sys
        // sys.Bar
        // in which case 'Bar' is undefined
        // in this situation, the activationTokenAndQual would be "sys." and "Bar"
        // and we want to get the definition for "sys"
        String[] activationTokenAndQual = ps.getActivationTokenAndQual(true);

        if (activationTokenAndQual[0].endsWith(".")) {
          ArrayList<IDefinition> selected = findDefinitions(nature, edit, start - 2, doc);

          for (IDefinition iDefinition : selected) {

            IModule module = iDefinition.getModule();
            if (module.getFile() != null) {
              Definition definition = (Definition) iDefinition;
              File file = module.getFile();
              if (definition.ast == null) {
                // if we have no ast in the definition, it means the module itself was found (global
                // scope)

                // Add option to create class at the given module!
                addCreateClassOption(ps, edit, props, markerContents, parametersAfterCall, file);

                addCreateMethodOption(ps, edit, props, markerContents, parametersAfterCall, file);
              } else if (definition.ast instanceof ClassDef) {
                ClassDef classDef = (ClassDef) definition.ast;
                // Ok, we should create a field or method in this case (accessing a classmethod or
                // staticmethod)
                PyCreateMethodOrField pyCreateMethod = new PyCreateMethodOrField();
                String className = NodeUtils.getNameFromNameTok(classDef.name);
                pyCreateMethod.setCreateInClass(className);
                pyCreateMethod.setCreateAs(PyCreateMethodOrField.CLASSMETHOD);
                addCreateClassmethodOption(
                    ps,
                    edit,
                    props,
                    markerContents,
                    parametersAfterCall,
                    pyCreateMethod,
                    file,
                    className);
              }
            }
          }
        }
        break;

      case IAnalysisPreferences.TYPE_UNRESOLVED_IMPORT:
        // This case is the following: from other_module4 import Foo
        // with 'Foo' being undefined.
        // So, we have to suggest creating a Foo class/method in other_module4
        PyImportsHandling importsHandling = new PyImportsHandling(ps.getDoc(), false);
        int offsetLine = ps.getLineOfOffset(start);
        String selectedText = ps.getSelectedText();

        Tuple<IModule, String> found = null;
        String foundFromImportStr = null;
        boolean isImportFrom = false;
        OUT:
        for (ImportHandle handle : importsHandling) {
          if (handle.startFoundLine == offsetLine
              || handle.endFoundLine == offsetLine
              || (handle.startFoundLine < offsetLine && handle.endFoundLine > offsetLine)) {
            List<ImportHandleInfo> importInfo = handle.getImportInfo();
            for (ImportHandleInfo importHandleInfo : importInfo) {
              String fromImportStr = importHandleInfo.getFromImportStr();
              List<String> importedStr = importHandleInfo.getImportedStr();
              for (String imported : importedStr) {
                if (selectedText.equals(imported)) {
                  if (fromImportStr != null) {
                    foundFromImportStr = fromImportStr + "." + imported;
                    isImportFrom = true;
                  } else {
                    // if fromImportStr == null, it's not a from xxx import yyy (i.e.: simple
                    // import)
                    foundFromImportStr = imported;
                  }
                  try {
                    String currentModule = nature.resolveModule(edit.getEditorFile());
                    ICompletionState state =
                        CompletionStateFactory.getEmptyCompletionState(
                            nature, new CompletionCache());

                    found =
                        nature
                            .getAstManager()
                            .findModule(
                                foundFromImportStr,
                                currentModule,
                                state,
                                new SourceModule(
                                    currentModule, edit.getEditorFile(), edit.getAST(), null));
                  } catch (Exception e) {
                    Log.log(e);
                  }
                  break OUT;
                }
              }
            }
            break OUT;
          }
        }

        boolean addOptionToCreateClassOrMethod = isImportFrom;

        if (found != null && found.o1 != null) {
          // Ok, we found a module, now, it may be that we still have to create some intermediary
          // modules
          // or just create a class or method at the end.
          if (found.o1 instanceof SourceModule) {

            // if all was found, there's nothing left to create.
            if (found.o2 != null && found.o2.length() > 0) {
              SourceModule sourceModule = (SourceModule) found.o1;
              File file = sourceModule.getFile();

              if (found.o2.indexOf('.') != -1) {
                // We have to create some intermediary structure.
                if (!addOptionToCreateClassOrMethod) {

                  // Cannot create class or method from the info (only the module structure).
                  if (sourceModule.getName().endsWith(".__init__")) {
                    File f = getFileStructure(file.getParentFile(), found.o2);
                    addCreateModuleOption(ps, edit, props, markerContents, f);
                  }

                } else {
                  // Ok, the leaf may be a class or method.
                  if (sourceModule.getName().endsWith(".__init__")) {
                    String moduleName = FullRepIterable.getWithoutLastPart(sourceModule.getName());
                    String withoutLastPart = FullRepIterable.getWithoutLastPart(found.o2);
                    moduleName += "." + withoutLastPart;

                    String classOrMethodName = FullRepIterable.getLastPart(found.o2);

                    File f = getFileStructure(file.getParentFile(), withoutLastPart);
                    addCreateClassInNewModuleOption(
                        ps, edit, props, classOrMethodName, moduleName, parametersAfterCall, f);
                    addCreateMethodInNewModuleOption(
                        ps, edit, props, classOrMethodName, moduleName, parametersAfterCall, f);
                  }
                }

              } else {
                // Ok, it's all there, we just have to create the leaf.
                if (!addOptionToCreateClassOrMethod
                    || sourceModule.getName().endsWith(".__init__")) {
                  // Cannot create class or method from the info (only the module structure).
                  if (sourceModule.getName().endsWith(".__init__")) {
                    File f =
                        new File(
                            file.getParent(),
                            found.o2 + FileTypesPreferencesPage.getDefaultDottedPythonExtension());
                    addCreateModuleOption(ps, edit, props, markerContents, f);
                  }
                } else {
                  // Ok, the leaf may be a class or method.
                  addCreateClassOption(ps, edit, props, markerContents, parametersAfterCall, file);
                  addCreateMethodOption(ps, edit, props, markerContents, parametersAfterCall, file);
                }
              }
            }
          }

        } else if (foundFromImportStr != null) {
          // We couldn't find anything there, so, we have to create the modules structure as needed
          // and
          // maybe create a class or module at the end (but only if it's an import from).
          // Ok, it's all there, we just have to create the leaf.

          // Discover the source folder where we should create the structure.

          File editorFile = edit.getEditorFile();
          String onlyProjectPythonPathStr =
              nature.getPythonPathNature().getOnlyProjectPythonPathStr(false);
          List<String> split =
              StringUtils.splitAndRemoveEmptyTrimmed(onlyProjectPythonPathStr, '|');
          for (int i = 0; i < split.size(); i++) {
            String fullPath = FileUtils.getFileAbsolutePath(split.get(i));
            fullPath = PythonPathHelper.getDefaultPathStr(fullPath);
            split.set(i, fullPath);
          }
          HashSet<String> projectSourcePath = new HashSet<String>(split);
          if (projectSourcePath.size() == 0) {
            return; // No source folder for editor... this shouldn't happen (code analysis wouldn't
            // even run on it).
          }
          String fullPath = FileUtils.getFileAbsolutePath(editorFile);
          fullPath = PythonPathHelper.getDefaultPathStr(fullPath);
          String foundSourceFolderFullPath = null;
          if (projectSourcePath.size() == 1) {
            foundSourceFolderFullPath = projectSourcePath.iterator().next();
          } else {
            for (String string : projectSourcePath) {
              if (fullPath.startsWith(string)) {
                // Use this as the source folder
                foundSourceFolderFullPath = string;
                break;
              }
            }
          }
          if (foundSourceFolderFullPath != null) {

            if (!addOptionToCreateClassOrMethod) {
              // Cannot create class or method from the info (only the module structure).

              File f = getFileStructure(new File(foundSourceFolderFullPath), foundFromImportStr);
              addCreateModuleOption(ps, edit, props, foundFromImportStr, f);

            } else {
              // Ok, the leaf may be a class or method.
              String moduleName = FullRepIterable.getWithoutLastPart(foundFromImportStr);
              File file = getFileStructure(new File(foundSourceFolderFullPath), moduleName);
              String lastPart = FullRepIterable.getLastPart(foundFromImportStr);
              addCreateClassInNewModuleOption(
                  ps, edit, props, lastPart, moduleName, parametersAfterCall, file);
              addCreateMethodInNewModuleOption(
                  ps, edit, props, lastPart, moduleName, parametersAfterCall, file);
            }
          }
        }
        break;
    }
  }
Exemple #19
0
  /**
   * @see
   *     org.python.pydev.editor.codecompletion.revisited.modules.AbstractModule#getGlobalTokens(java.lang.String)
   */
  public IToken[] getGlobalTokens(ICompletionState state, ICodeCompletionASTManager manager) {
    String activationToken = state.getActivationToken();
    if (activationToken.length() == 0) {
      return getGlobalTokens();
    }

    Map<String, IToken> v = cache.get(activationToken);
    if (v != null) {
      Collection<IToken> values = v.values();
      return values.toArray(new IToken[values.size()]);
    }

    IToken[] toks = new IToken[0];

    if (COMPILED_MODULES_ENABLED) {
      try {
        final IPythonNature nature = manager.getNature();

        final AbstractShell shell;
        try {
          shell = AbstractShell.getServerShell(nature, AbstractShell.COMPLETION_SHELL);
        } catch (Exception e) {
          throw new RuntimeException("Unable to create shell for CompiledModule: " + this.name, e);
        }
        synchronized (shell) {
          String act = name + '.' + activationToken;
          String tokenToCompletion = act;
          if (isPythonBuiltin) {
            String replacement = BUILTIN_REPLACEMENTS.get(activationToken);
            if (replacement != null) {
              tokenToCompletion = name + '.' + replacement;
            }
          }

          List<String[]> completions =
              shell.getImportCompletions(
                      tokenToCompletion,
                      manager
                          .getModulesManager()
                          .getCompletePythonPath(
                              nature.getProjectInterpreter(),
                              nature.getRelatedInterpreterManager()))
                  .o2;

          ArrayList<IToken> array = new ArrayList<IToken>();

          for (Iterator<String[]> iter = completions.iterator(); iter.hasNext(); ) {
            String[] element = iter.next();
            if (element.length >= 4) { // it might be a server error
              IToken t =
                  new CompiledToken(
                      element[0], element[1], element[2], act, Integer.parseInt(element[3]));
              array.add(t);
            }
          }
          toks = (CompiledToken[]) array.toArray(new CompiledToken[0]);
          HashMap<String, IToken> map = new HashMap<String, IToken>();
          for (IToken token : toks) {
            map.put(token.getRepresentation(), token);
          }
          cache.put(activationToken, map);
        }
      } catch (Exception e) {
        Log.log(
            "Error while getting info for module:"
                + this.name
                + ". Project: "
                + manager.getNature().getProject(),
            e);
      }
    }
    return toks;
  }
 /**
  * @return the module name where the completion request took place (may be null if there is no
  *     editor file associated)
  */
 public String resolveModule() {
   if (initialModule == null && editorFile != null) {
     initialModule = nature.resolveModule(editorFile);
   }
   return initialModule;
 }
  /**
   * @return the function name for this breakpoint.
   *     <p>A return of "None" signals that we couldn't discover the function name (so, we should
   *     try to match things in the whole file, and not only in the given context, as we don't know
   *     which context it is)
   */
  public String getFunctionName() {
    String fileStr = getFile();
    File file = fileStr != null ? new File(fileStr) : null;
    if (file == null || !file.exists()) {
      return "None";
    }

    if (file.lastModified() == lastModifiedTimeCached) {
      return functionName;
    }

    try {
      IPythonNature nature = getPythonNature();
      if (nature == null) {
        lastModifiedTimeCached = 0;
        return "None";
      }
      ICodeCompletionASTManager astManager = nature.getAstManager();
      if (astManager == null) {
        lastModifiedTimeCached = 0;
        return "None";
      }
      // Only mark it as found if we were able to get the python nature (otherwise, this could
      // change later
      // if requesting during a setup)
      if (nature.startRequests()) { // start requests, as we'll ask for resolve and get module.
        SourceModule sourceModule = null;
        try {
          String modName = nature.resolveModule(fileStr);
          if (modName != null) {
            // when all is set up, this is the most likely path we're going to use
            // so, we shouldn't have delays when the module is changed, as it's already
            // ok for use.
            IModule module = astManager.getModule(modName, nature, true);
            if (module instanceof SourceModule) {
              sourceModule = (SourceModule) module;
            }
          }
        } finally {
          nature.endRequests();
        }
        lastModifiedTimeCached = file.lastModified();

        if (sourceModule == null) {
          // the text for the breakpoint requires the function name, and it may be requested before
          // the ast manager is actually restored (so, modName is None, and we have little
          // alternative
          // but making a parse to get the function name)
          IDocument doc = getDocument();
          sourceModule = AbstractModule.createModuleFromDoc("", null, doc, nature, true);
        }

        int lineToUse = getLineNumber() - 1;

        if (sourceModule == null || sourceModule.getAst() == null || lineToUse < 0) {
          functionName = "None";
          return functionName;
        }

        SimpleNode ast = sourceModule.getAst();

        functionName = NodeUtils.getContextName(lineToUse, ast);
        if (functionName == null) {
          functionName = ""; // global context
        }
        return functionName;
      }
      // If it was found, it would've already returned. So, match anything as we couldn't determine
      // it.
      functionName = "None";

    } catch (Exception e) {
      // Some error happened determining it. Match anything.
      Log.log(
          "Error determining breakpoint context. Breakpoint at: "
              + file
              + " will match any context.",
          e);
      functionName = "None";
    }
    return functionName;
  }
  /**
   * This method returns the module that corresponds to the path passed as a parameter.
   *
   * @param name the name of the module we're looking for (e.g.: mod1.mod2)
   * @param dontSearchInit is used in a negative form because initially it was isLookingForRelative,
   *     but it actually defines if we should look in __init__ modules too, so, the name matches the
   *     old signature.
   *     <p>NOTE: isLookingForRelative description was: when looking for relative imports, we don't
   *     check for __init__
   * @return the module represented by this name
   */
  protected IModule getModule(
      boolean acceptCompiledModule, String name, IPythonNature nature, boolean dontSearchInit) {
    if (temporaryModules != null) {
      synchronized (lockTemporaryModules) {
        if (temporaryModules != null) {
          // who knows, maybe it became null at that point.
          FastStack<IModule> stack = temporaryModules.get(name);
          if (stack != null && stack.size() > 0) {
            if (DEBUG_TEMPORARY_MODULES) {
              System.out.println("Returning temporary module: " + name);
            }
            return stack.peek();
          }
        }
      }
    }
    AbstractModule n = null;
    ModulesKey keyForCacheAccess = new ModulesKey(null, null);

    if (!dontSearchInit) {
      if (n == null) {
        keyForCacheAccess.name = new StringBuffer(name).append(".__init__").toString();
        n = cache.getObj(keyForCacheAccess, this);
        if (n != null) {
          name += ".__init__";
        }
      }
    }
    if (n == null) {
      keyForCacheAccess.name = name;
      n = cache.getObj(keyForCacheAccess, this);
    }

    if (n instanceof SourceModule) {
      // ok, module exists, let's check if it is synched with the filesystem version...
      SourceModule s = (SourceModule) n;
      if (!s.isSynched()) {
        // change it for an empty and proceed as usual.
        n = (AbstractModule) addModule(createModulesKey(s.getName(), s.getFile()));
      }
    }

    if (n instanceof EmptyModule) {
      EmptyModule e = (EmptyModule) n;

      boolean found = false;

      if (!found && e.f != null) {

        if (!e.f.exists()) {
          // if the file does not exist anymore, just remove it.
          keyForCacheAccess.name = name;
          keyForCacheAccess.file = e.f;
          doRemoveSingleModule(keyForCacheAccess);
          n = null;

        } else {
          // file exists
          n = checkOverride(name, nature, n);

          if (n instanceof EmptyModule) {
            // ok, handle case where the file is actually from a zip file...
            if (e instanceof EmptyModuleForZip) {
              EmptyModuleForZip emptyModuleForZip = (EmptyModuleForZip) e;

              if (emptyModuleForZip.pathInZip.endsWith(".class") || !emptyModuleForZip.isFile) {
                // handle java class... (if it's a class or a folder in a jar)
                n = JythonModulesManagerUtils.createModuleFromJar(emptyModuleForZip);
                n = decorateModule(n, nature);

              } else if (FileTypesPreferencesPage.isValidDll(emptyModuleForZip.pathInZip)) {
                // .pyd
                n = new CompiledModule(name, IToken.TYPE_BUILTIN, nature.getAstManager());
                n = decorateModule(n, nature);

              } else if (PythonPathHelper.isValidSourceFile(emptyModuleForZip.pathInZip)) {
                // handle python file from zip... we have to create it getting the contents from the
                // zip file
                try {
                  IDocument doc =
                      REF.getDocFromZip(emptyModuleForZip.f, emptyModuleForZip.pathInZip);
                  // NOTE: The nature (and so the grammar to be used) must be defined by this
                  // modules
                  // manager (and not by the initial caller)!!
                  n =
                      AbstractModule.createModuleFromDoc(
                          name, emptyModuleForZip.f, doc, this.getNature(), -1, false);
                  SourceModule zipModule = (SourceModule) n;
                  zipModule.zipFilePath = emptyModuleForZip.pathInZip;
                  n = decorateModule(n, nature);
                } catch (Exception exc1) {
                  PydevPlugin.log(exc1);
                  n = null;
                }
              }

            } else {
              // regular case... just go on and create it.
              try {
                // NOTE: The nature (and so the grammar to be used) must be defined by this modules
                // manager (and not by the initial caller)!!
                n = AbstractModule.createModule(name, e.f, this.getNature(), -1);
                n = decorateModule(n, nature);
              } catch (IOException exc) {
                keyForCacheAccess.name = name;
                keyForCacheAccess.file = e.f;
                doRemoveSingleModule(keyForCacheAccess);
                n = null;
              } catch (MisconfigurationException exc) {
                PydevPlugin.log(exc);
                n = null;
              }
            }
          }
        }

      } else { // ok, it does not have a file associated, so, we treat it as a builtin (this can
               // happen in java jars)
        n = checkOverride(name, nature, n);
        if (n instanceof EmptyModule) {
          if (acceptCompiledModule) {
            n = new CompiledModule(name, IToken.TYPE_BUILTIN, nature.getAstManager());
            n = decorateModule(n, nature);
          } else {
            return null;
          }
        }
      }

      if (n != null) {
        doAddSingleModule(createModulesKey(name, e.f), n);
      } else {
        System.err.println("The module " + name + " could not be found nor created!");
      }
    }

    if (n instanceof EmptyModule) {
      throw new RuntimeException("Should not be an empty module anymore!");
    }
    if (n instanceof SourceModule) {
      SourceModule sourceModule = (SourceModule) n;
      // now, here's a catch... it may be a bootstrap module...
      if (sourceModule.isBootstrapModule()) {
        // if it's a bootstrap module, we must replace it for the related compiled module.
        n = new CompiledModule(name, IToken.TYPE_BUILTIN, nature.getAstManager());
        n = decorateModule(n, nature);
      }
    }

    return n;
  }
Exemple #23
0
 /**
  * This method creates the python server process and starts the sockets, so that we can talk with
  * the server.
  *
  * @throws IOException
  * @throws CoreException
  * @throws MisconfigurationException
  * @throws PythonNatureWithoutProjectException
  */
 /*package*/ void startIt(IPythonNature nature)
     throws IOException, JDTNotAvailableException, CoreException, MisconfigurationException,
         PythonNatureWithoutProjectException {
   this.startIt(nature.getProjectInterpreter());
 }
Exemple #24
0
  private void setTokens(String name, IModulesManager manager)
      throws IOException, Exception, CoreException {
    if (TRACE_COMPILED_MODULES) {
      Log.log(IStatus.INFO, ("Compiled modules: getting info for:" + name), null);
    }
    final IPythonNature nature = manager.getNature();
    AbstractShell shell = AbstractShell.getServerShell(nature, AbstractShell.COMPLETION_SHELL);
    synchronized (shell) {
      Tuple<String, List<String[]>> completions =
          shell.getImportCompletions(
              name,
              manager.getCompletePythonPath(
                  nature.getProjectInterpreter(),
                  nature.getRelatedInterpreterManager())); // default

      if (TRACE_COMPILED_MODULES) {
        Log.log(
            IStatus.INFO,
            ("Compiled modules: "
                + name
                + " file: "
                + completions.o1
                + " found: "
                + completions.o2.size()
                + " completions."),
            null);
      }
      String fPath = completions.o1;
      if (fPath != null) {
        if (!fPath.equals("None")) {
          this.file = new File(fPath);
        }

        String f = fPath;
        if (f.toLowerCase().endsWith(".pyc")) {
          f = f.substring(0, f.length() - 1); // remove the c from pyc
          File f2 = new File(f);
          if (f2.exists()) {
            this.file = f2;
          }
        }
      }
      ArrayList<IToken> array = new ArrayList<IToken>();

      for (String[] element : completions.o2) {
        // let's make this less error-prone.
        try {
          String o1 = element[0]; // this one is really, really needed
          String o2 = "";
          String o3 = "";

          if (element.length > 0) {
            o2 = element[1];
          }

          if (element.length > 0) {
            o3 = element[2];
          }

          IToken t;
          if (element.length > 0) {
            t = new CompiledToken(o1, o2, o3, name, Integer.parseInt(element[3]));
          } else {
            t = new CompiledToken(o1, o2, o3, name, IToken.TYPE_BUILTIN);
          }

          array.add(t);
        } catch (Exception e) {
          String received = "";
          for (int i = 0; i < element.length; i++) {
            received += element[i];
            received += "  ";
          }

          Log.log(
              IStatus.ERROR,
              ("Error getting completions for compiled module "
                  + name
                  + " received = '"
                  + received
                  + "'"),
              e);
        }
      }

      // as we will use it for code completion on sources that map to modules, the __file__ should
      // also
      // be added...
      if (array.size() > 0 && (name.equals("__builtin__") || name.equals("builtins"))) {
        array.add(new CompiledToken("__file__", "", "", name, IToken.TYPE_BUILTIN));
        array.add(new CompiledToken("__name__", "", "", name, IToken.TYPE_BUILTIN));
        array.add(new CompiledToken("__builtins__", "", "", name, IToken.TYPE_BUILTIN));
        array.add(new CompiledToken("__dict__", "", "", name, IToken.TYPE_BUILTIN));
      }

      addTokens(array);
    }
  }