Ejemplo n.º 1
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;
 }
Ejemplo n.º 2
0
 public static ModuleAdapter createModuleAdapter(
     PythonModuleManager pythonModuleManager, File file, IDocument doc, IPythonNature nature)
     throws Throwable {
   if (file != null && file.exists()) {
     if (pythonModuleManager != null) {
       IModulesManager modulesManager = pythonModuleManager.getIModuleManager();
       if (modulesManager != null) {
         String modName = modulesManager.resolveModule(REF.getFileAbsolutePath(file));
         if (modName != null) {
           IModule module = modulesManager.getModule(modName, nature, true);
           if (module instanceof ISourceModule) {
             SourceModule iSourceModule = (SourceModule) module;
             if (iSourceModule.parseError != null) {
               throw iSourceModule.parseError;
             }
             return new ModuleAdapter(pythonModuleManager, ((ISourceModule) module), nature, doc);
           }
         }
       }
     }
   }
   return new ModuleAdapter(pythonModuleManager, file, doc, getRootNode(doc), nature);
 }
Ejemplo n.º 3
0
  /** @param version IPythonNature.PYTHON_VERSION_XXX */
  protected ModuleAdapter createModuleAdapterFromDataSource(String version) throws Throwable {
    codeCompletionTestsBase.restorePythonPath(
        REF.getFileAbsolutePath(data.file.getParentFile()), true);
    PythonModuleManager pythonModuleManager =
        new PythonModuleManager(CodeCompletionTestsBase.nature);
    if (version != null) {
      // As the files will be found in the system, we need to set the system modules manager info.
      IModulesManager modulesManager = pythonModuleManager.getIModuleManager();
      SystemModulesManager systemModulesManager =
          (SystemModulesManager) modulesManager.getSystemModulesManager();
      systemModulesManager.setInfo(new InterpreterInfo(version, "", new ArrayList<String>()));

      CodeCompletionTestsBase.nature.setVersion(version, null);
    }
    ModuleAdapter module =
        VisitorFactory.createModuleAdapter(
            pythonModuleManager,
            data.file,
            new Document(data.source),
            CodeCompletionTestsBase.nature,
            CodeCompletionTestsBase.nature);
    return module;
  }
Ejemplo n.º 4
0
  /** @param module - module from where to get completions. */
  @SuppressWarnings("unchecked")
  private CompiledModule(String name, int tokenTypes, IModulesManager manager) {
    super(name);
    isPythonBuiltin = ("__builtin__".equals(name) || "builtins".equals(name));
    if (COMPILED_MODULES_ENABLED) {
      try {
        setTokens(name, manager);
      } catch (Exception e) {
        // ok, something went wrong... let's give it another shot...
        synchronized (this) {
          try {
            wait(10);
          } catch (InterruptedException e1) {
            // empty block
          } // just wait a little before a retry...
        }

        try {
          AbstractShell shell =
              AbstractShell.getServerShell(manager.getNature(), AbstractShell.COMPLETION_SHELL);
          synchronized (shell) {
            shell.clearSocket();
          }
          setTokens(name, manager);
        } catch (Exception e2) {
          tokens = new HashMap<String, IToken>();
          Log.log(e2);
        }
      }
    } else {
      // not used if not enabled.
      tokens = new HashMap<String, IToken>();
    }
    List<IModulesObserver> participants =
        ExtensionHelper.getParticipants(ExtensionHelper.PYDEV_MODULES_OBSERVER);
    if (participants != null) {
      for (IModulesObserver observer : participants) {
        observer.notifyCompiledModuleCreated(this, manager);
      }
    }
  }
  /**
   * Restores the info for a module manager
   *
   * @param monitor a monitor to keep track of the progress
   * @param m the module manager
   * @param nature the associated nature (may be null if there is no associated nature -- as is the
   *     case when restoring system info).
   * @return the info generated from the module manager
   */
  public static AbstractAdditionalTokensInfo restoreInfoForModuleManager(
      IProgressMonitor monitor,
      IModulesManager m,
      String additionalFeedback,
      AbstractAdditionalTokensInfo info,
      IPythonNature nature,
      int grammarVersion) {
    if (monitor == null) {
      monitor = new NullProgressMonitor();
    }
    // TODO: Check if keeping a zip file open makes things faster...
    // Timer timer = new Timer();
    ModulesKey[] allModules = m.getOnlyDirectModules();
    int i = 0;

    FastStringBuffer msgBuffer = new FastStringBuffer();

    for (ModulesKey key : allModules) {
      if (monitor.isCanceled()) {
        return null;
      }
      i++;

      if (PythonPathHelper.canAddAstInfoFor(
          key)) { // otherwise it should be treated as a compiled module (no ast generation)

        if (i % 17 == 0) {
          msgBuffer.clear();
          msgBuffer.append("Creating ");
          msgBuffer.append(additionalFeedback);
          msgBuffer.append(" additional info (");
          msgBuffer.append(i);
          msgBuffer.append(" of ");
          msgBuffer.append(allModules.length);
          msgBuffer.append(") for ");
          msgBuffer.append(key.file.getName());
          monitor.setTaskName(msgBuffer.toString());
          monitor.worked(1);
        }

        try {
          if (info.addAstInfo(key, false) == null) {
            String str = "Unable to generate ast -- using %s.\nError:%s";
            ErrorDescription errorDesc = null;
            throw new RuntimeException(
                com.aptana.shared_core.string.StringUtils.format(
                    str,
                    PyParser.getGrammarVersionStr(grammarVersion),
                    (errorDesc != null && errorDesc.message != null)
                        ? errorDesc.message
                        : "unable to determine"));
          }

        } catch (Throwable e) {
          Log.log(IStatus.ERROR, "Problem parsing the file :" + key.file + ".", e);
        }
      }
    }
    // timer.printDiff("Time to restore additional info");
    return info;
  }
Ejemplo n.º 6
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);
    }
  }