Exemple #1
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);
      }
    }
  }
Exemple #2
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);
    }
  }