<commit_before>package edu.pdx.cs410J.grader;

import edu.pdx.cs410J.ParserException;

import java.io.*;

public class FetchAndProcessGraderEmail {

  public static void main(String[] args) {
    String passwordFile = null;
    String directoryName = null;
    String gradeBookFile = null;
    String whatToFetch = null;

    for (String arg : args) {
      if (passwordFile == null) {
        passwordFile = arg;

      } else if (directoryName == null) {
        directoryName = arg;

      } else if (gradeBookFile == null) {
        gradeBookFile = arg;

      } else if (whatToFetch == null) {
        whatToFetch = arg;

      } else {
        usage("Extraneous argument: " + arg);
      }
    }

    if (passwordFile == null) {
      usage("Missing password file");
    }

    if (directoryName == null) {
      usage("Missing directory name");
    }

    if (gradeBookFile == null) {
      usage("Missing grade book file");
    }

    if (whatToFetch == null) {
      usage("Missing kind of email to fetch");
    }

    String password = readGraderEmailPasswordFromFile(passwordFile);
    File directory = getDirectory(directoryName);
    GradeBook gradeBook = getGradeBook(gradeBookFile);

    GraderEmailAccount account = new GraderEmailAccount(password);
    ProjectSubmissionsProcessor processor = new ProjectSubmissionsProcessor(directory, gradeBook);
    account.fetchAttachmentsFromUnreadMessagesInFolder(processor.getEmailFolder(), processor);
  }

  private static GradeBook getGradeBook(String gradeBookFile) {
    try {
      XmlGradeBookParser parser = new XmlGradeBookParser(gradeBookFile);
      return parser.parse();

    } catch (IOException ex) {
      return usage("Couldn't read grade book file \"" + gradeBookFile + "\"");

    } catch (ParserException ex) {
      return usage("Couldn't parse grade book file \"" + gradeBookFile + "\"");
    }
  }

  private static File getDirectory(String directoryName) {
    File directory = new File(directoryName);
    if (!directory.exists()) {
      return usage("Download directory \"" + directoryName + "\" does not exist");
    }

    if (!directory.isDirectory()) {
      return usage("Download directory \"" + directoryName + "\" is not a directory");
    }<commit_msg>Validate whatToFetch.<commit_after>package edu.pdx.cs410J.grader;

import edu.pdx.cs410J.ParserException;

import java.io.*;

public class FetchAndProcessGraderEmail {

  public static void main(String[] args) {
    String passwordFile = null;
    String directoryName = null;
    String gradeBookFile = null;
    String whatToFetch = null;

    for (String arg : args) {
      if (passwordFile == null) {
        passwordFile = arg;

      } else if (directoryName == null) {
        directoryName = arg;

      } else if (gradeBookFile == null) {
        gradeBookFile = arg;

      } else if (whatToFetch == null) {
        whatToFetch = arg;

      } else {
        usage("Extraneous argument: " + arg);
      }
    }

    if (passwordFile == null) {
      usage("Missing password file");
    }

    if (directoryName == null) {
      usage("Missing directory name");
    }

    if (gradeBookFile == null) {
      usage("Missing grade book file");
    }

    if (whatToFetch == null) {
      usage("Missing kind of email to fetch");
    }

    String password = readGraderEmailPasswordFromFile(passwordFile);
    File directory = getDirectory(directoryName);
    GradeBook gradeBook = getGradeBook(gradeBookFile);

    GraderEmailAccount account = new GraderEmailAccount(password);
    ProjectSubmissionsProcessor processor = getStudentEmailAttachmentProcessor(whatToFetch, directory, gradeBook);
    account.fetchAttachmentsFromUnreadMessagesInFolder(processor.getEmailFolder(), processor);
  }

  private static ProjectSubmissionsProcessor getStudentEmailAttachmentProcessor(String whatToFetch, File directory, GradeBook gradeBook) {
    if (whatToFetch.equalsIgnoreCase("projects")) {
      return new ProjectSubmissionsProcessor(directory, gradeBook);

    } else if (whatToFetch.equalsIgnoreCase("surveys")) {
      throw new UnsupportedOperationException("Not implemented yet");

    } else {
      return usage("Cannot fetch \"" + whatToFetch + "\"");
    }
  }

  private static GradeBook getGradeBook(String gradeBookFile) {
    try {
      XmlGradeBookParser parser = new XmlGradeBookParser(gradeBookFile);
      return parser.parse();

    } catch (IOException ex) {
      return usage("Couldn't read grade book file \"" + gradeBookFile + "\"");

    } catch (ParserException ex) {
      return usage("Couldn't parse grade book file \"" + gradeBookFile + "\"");
    }
  }

  private static File getDirectory(String directoryName) {
    File directory = new File(directoryName);
    if (!directory.exists()) {
      return usage("Download directory \"" + directoryName + "\" does not exist");
    }

    if (!directory.isDirectory()) {
      return usage("Download directory \"" + directoryName + "\" is not a directory");
    }
{'fraction_non_alphanumeric': 0.10948466011630238, 'fraction_numerical': 0.00240625626629236, 'mean_word_length': 3.234295415959253, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1696441', 'n_tokens_mistral': 1411, 'n_tokens_neox': 1346, 'n_words': 359}