public List<IInfo> addAstInfo(ModulesKey key, boolean generateDelta) throws Exception {
    boolean isZipModule = key instanceof ModulesKeyForZip;
    ModulesKeyForZip modulesKeyForZip = null;
    if (isZipModule) {
      modulesKeyForZip = (ModulesKeyForZip) key;
    }

    Object doc;
    if (isZipModule) {
      doc =
          FileUtilsFileBuffer.getCustomReturnFromZip(
              modulesKeyForZip.file, modulesKeyForZip.zipModulePath, null);

    } else {
      doc = FileUtilsFileBuffer.getCustomReturnFromFile(key.file, true, null);
    }

    char[] charArray;
    int len;
    if (doc instanceof IDocument) {
      IDocument document = (IDocument) doc;
      charArray = document.get().toCharArray();
      len = charArray.length;

    } else if (doc instanceof FastStringBuffer) {
      FastStringBuffer fastStringBuffer = (FastStringBuffer) doc;
      // In this case, we can actually get the internal array without doing any copies (and just
      // specifying the len).
      charArray = fastStringBuffer.getInternalCharsArray();
      len = fastStringBuffer.length();

    } else if (doc instanceof String) {
      String str = (String) doc;
      charArray = str.toCharArray();
      len = charArray.length;

    } else if (doc instanceof char[]) {
      charArray = (char[]) doc;
      len = charArray.length;

    } else {
      throw new RuntimeException("Don't know how to handle: " + doc + " -- " + doc.getClass());
    }

    SimpleNode node = FastDefinitionsParser.parse(charArray, key.file.getName(), len);
    if (node == null) {
      return null;
    }

    return addAstInfo(node, key, generateDelta);
  }
 private IDocument getDocument() {
   IMarker marker = getMarker();
   IResource r = marker.getResource();
   if (r instanceof IFile) {
     return FileUtilsFileBuffer.getDocFromResource(r);
   } else {
     // it's an external file...
     try {
       return FileUtilsFileBuffer.getDocFromFile(
           new File((String) marker.getAttribute(PyBreakpoint.PY_BREAK_EXTERNAL_PATH_ID)));
     } catch (Exception e) {
       throw new RuntimeException(e);
     }
   }
 }
  public void finishDelayedVisits() {
    for (IResourceDelta delta : delayedVisits) {
      try {
        IResource resource = delta.getResource();

        boolean isAddRemove = false;
        switch (delta.getKind()) {
          case IResourceDelta.ADDED:
            memo.put(PyDevBuilderVisitor.DOCUMENT_TIME, System.currentTimeMillis());
            visitAddedResource(
                resource, FileUtilsFileBuffer.getDocOnCallbackFromResource(resource), monitor);
            isAddRemove = true;
            break;

          case IResourceDelta.CHANGED:
            memo.put(PyDevBuilderVisitor.DOCUMENT_TIME, System.currentTimeMillis());
            visitChangedResource(
                resource, FileUtilsFileBuffer.getDocOnCallbackFromResource(resource), monitor);
            isAddRemove = true;
            break;
        }

        if (isAddRemove) {
          // communicate the progress
          currentResourcesVisited++;
          FastStringBuffer bufferToCreateString = new FastStringBuffer();
          PyDevBuilder.communicateProgress(
              monitor,
              totalResources,
              currentResourcesVisited,
              resource,
              this,
              bufferToCreateString);
        }
      } catch (Exception e) {
        Log.log(e);
      }
    }
  }
Exemple #4
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 #5
0
  /**
   * 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) {
    synchronized (lockTemporaryModules) {
      SortedMap<Integer, IModule> map = temporaryModules.get(name);
      if (map != null && map.size() > 0) {
        if (DEBUG_TEMPORARY_MODULES) {
          System.out.println("Returning temporary module: " + name);
        }
        return map.get(map.lastKey());
      }
    }
    AbstractModule n = null;
    ModulesKey keyForCacheAccess = new ModulesKey(null, null);

    if (!dontSearchInit) {
      if (n == null) {
        keyForCacheAccess.name =
            (String) StringUtils.join(".", new String[] {name, "__init__"}, null);
        n = cache.getObj(keyForCacheAccess, this);
        if (n != null) {
          name = keyForCacheAccess.name;
        }
      }
    }
    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;

      if (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, this);
                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 =
                      FileUtilsFileBuffer.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(), false);
                  SourceModule zipModule = (SourceModule) n;
                  zipModule.zipFilePath = emptyModuleForZip.pathInZip;
                  n = decorateModule(n, nature);
                } catch (Exception exc1) {
                  Log.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(), true);
                n = decorateModule(n, nature);
              } catch (IOException exc) {
                keyForCacheAccess.name = name;
                keyForCacheAccess.file = e.f;
                doRemoveSingleModule(keyForCacheAccess);
                n = null;
              } catch (MisconfigurationException exc) {
                Log.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, this);
            n = decorateModule(n, nature);
          } else {
            return null;
          }
        }
      }

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

    if (n instanceof EmptyModule) {
      throw new RuntimeException("Should not be an empty module anymore: " + n);
    }
    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, this);
        n = decorateModule(n, nature);
      }
    }

    return n;
  }