@NotNull
  private static List<RatedResolveResult> resolveInDirectory(
      @NotNull final String referencedName,
      @Nullable final PsiFile containingFile,
      final PsiDirectory dir,
      boolean isFileOnly,
      boolean checkForPackage) {
    final PsiDirectory subdir = dir.findSubdirectory(referencedName);
    if (subdir != null && (!checkForPackage || PyUtil.isPackage(subdir, containingFile))) {
      return ResolveResultList.to(subdir);
    }

    final PsiFile module = findPyFileInDir(dir, referencedName);
    if (module != null) {
      return ResolveResultList.to(module);
    }

    if (!isFileOnly) {
      // not a subdir, not a file; could be a name in parent/__init__.py
      final PsiFile initPy = dir.findFile(PyNames.INIT_DOT_PY);
      if (initPy == containingFile) {
        return Collections.emptyList(); // don't dive into the file we're in
      }
      if (initPy instanceof PyFile) {
        return ((PyFile) initPy).multiResolveName(referencedName);
      }
    }
    return Collections.emptyList();
  }
 @NotNull
 private static List<RatedResolveResult> resolveInPackageDirectory(
     @Nullable PsiElement parent,
     @NotNull String referencedName,
     @Nullable PsiFile containingFile,
     boolean fileOnly,
     boolean checkForPackage) {
   final PsiElement parentDir = PyUtil.turnInitIntoDir(parent);
   if (parentDir instanceof PsiDirectory) {
     final List<RatedResolveResult> resolved =
         resolveInDirectory(
             referencedName, containingFile, (PsiDirectory) parentDir, fileOnly, checkForPackage);
     if (!resolved.isEmpty()) {
       for (RatedResolveResult result : resolved) {
         if (result.getRate() > RatedResolveResult.RATE_LOW) {
           return resolved;
         }
       }
     }
     if (parent instanceof PsiFile) {
       final PsiElement foreign = resolveForeignImports((PsiFile) parent, referencedName);
       if (foreign != null) {
         final ResolveResultList results = new ResolveResultList();
         results.addAll(resolved);
         results.poke(foreign, RatedResolveResult.RATE_NORMAL);
         return results;
       }
     }
     return resolved;
   }
   return Collections.emptyList();
 }
 public static ResolveResultList rateResults(List<? extends PsiElement> targets) {
   ResolveResultList ret = new ResolveResultList();
   for (PsiElement target : targets) {
     if (target instanceof PsiDirectory) {
       target = PyUtil.getPackageElement((PsiDirectory) target, null);
     }
     if (target != null) {
       int rate = RatedResolveResult.RATE_HIGH;
       if (target instanceof PyFile) {
         for (PyResolveResultRater rater :
             Extensions.getExtensions(PyResolveResultRater.EP_NAME)) {
           rate += rater.getImportElementRate(target);
         }
       } else if (isDunderAll(target)) {
         rate = RatedResolveResult.RATE_NORMAL;
       }
       ret.poke(target, rate);
     }
   }
   return ret;
 }