<commit_before>package com.thoughtworks.verify.pact

import java.io.File

import play.api.libs.json.Json

import scala.io.Source

/**
  * Created by xfwu on 12/07/2017.
  */
object PactFile {
  implicit val pactRequestFormat = Json.format[PactRequest]
  implicit val pactResponseFormat = Json.format[PactResponse]
  implicit val interactionFormat = Json.format[Interaction]
  implicit val providerFormat = Json.format[Provider]
  implicit val consumerFormat = Json.format[Consumer]
  implicit val pactFormat = Json.format[Pact]
  implicit val pactsFormat = Json.format[Pacts]

  def loadPacts(dir: File): List[Pacts] = {
    val (subDirs, files) = listFiles(dir).partition(_.isDirectory)
    val pacts: Seq[Pact] = parsePacts(files)
    val subPacts: List[Pacts] = loadParsePacts(subDirs)
    if (pacts.isEmpty) {
      subPacts
    } else {
      subPacts :+ Pacts(dir.getName, pacts)
    }
  }

  private def loadParsePacts(subDirs: Seq[File]): List[Pacts] = {
    if(subDirs != null && !subDirs.isEmpty) {
      subDirs.flatMap(subDir => loadPacts(subDir)).toList
    } else {
      Nil
    }
  }

  private def parsePacts(files: Seq[File]): Seq[Pact] = {
    if(files != null && !files.isEmpty) {
      files.filter(_.getName.endsWith(".json")).map(f => Source.fromFile(f).getLines().mkString("\n")).map(parsePact)
    } else {
      Nil
    }
  }

  private def listFiles(dir: File): Seq[File] = {<commit_msg>ignore special pact files<commit_after>package com.thoughtworks.verify.pact

import java.io.File

import play.api.libs.json.Json

import scala.io.Source

/**
  * Created by xfwu on 12/07/2017.
  */
object PactFile {
  implicit val pactRequestFormat = Json.format[PactRequest]
  implicit val pactResponseFormat = Json.format[PactResponse]
  implicit val interactionFormat = Json.format[Interaction]
  implicit val providerFormat = Json.format[Provider]
  implicit val consumerFormat = Json.format[Consumer]
  implicit val pactFormat = Json.format[Pact]
  implicit val pactsFormat = Json.format[Pacts]

  def loadPacts(dir: File): List[Pacts] = {
    val (subDirs, files) = listFiles(dir).partition(_.isDirectory)
    val pacts: Seq[Pact] = parsePacts(files)
    val subPacts: List[Pacts] = loadParsePacts(subDirs)
    if (pacts.isEmpty) {
      subPacts
    } else {
      subPacts :+ Pacts(dir.getName, pacts)
    }
  }

  private def loadParsePacts(subDirs: Seq[File]): List[Pacts] = {
    if(subDirs != null && !subDirs.isEmpty) {
      subDirs.flatMap(subDir => loadPacts(subDir)).toList
    } else {
      Nil
    }
  }

  private def parsePacts(files: Seq[File]): Seq[Pact] = {
    if(files != null && !files.isEmpty) {
      files.filter(_.getName.endsWith(".json"))
        .filterNot(_.getName.startsWith("_"))
        .map(f => Source.fromFile(f).getLines().mkString("\n"))
        .map(parsePact)
    } else {
      Nil
    }
  }

  private def listFiles(dir: File): Seq[File] = {
{'fraction_non_alphanumeric': 0.13257575757575757, 'fraction_numerical': 0.005509641873278237, 'mean_word_length': 3.5390625, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2979078', 'n_tokens_mistral': 1035, 'n_tokens_neox': 1010, 'n_words': 196}