<commit_before>/**
* Copyright IBM Corporation 2016
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import Foundation
import CouchDB
import SwiftyJSON
import CFEnvironment

public struct Configuration {

  public enum Error: ErrorProtocol {
    case IO(String)
  }

  // Instance constants
  let configurationFile = "config.json"
  let appEnv: AppEnv

  init() throws {
    // Generate file path for config.json
    let filePath = #file
    let components = filePath.characters.split(separator: "/").map(String.init)
    let notLastThree = components[0..<components.count - 3]
    let finalPath = "/" + notLastThree.joined(separator: "/") + "/\(configurationFile)"

    if let configData = NSData(contentsOfFile: finalPath) {
      let configJson = JSON(data: configData)
      appEnv = try CFEnvironment.getAppEnv(configJson)
      return
    }
    throw Error.IO("Failed to read/parse the contents of the '\(configurationFile)' configuration file.")
  }

  func getDatabase(dbName: String) throws -> Database {
    if let couchDBCredentials = appEnv.getService("Cloudant NoSQL DB-fz")?.credentials {
      if let host = couchDBCredentials["host"].string,
        user = couchDBCredentials["username"].string,
        password = couchDBCredentials["password"].string {
        //port = couchDBCredentials["port"].int {
        //TODO Update port value - Kitura-CouchDB needs to be tagged
          let port = 80
          let connProperties = ConnectionProperties(host: host, port: Int16(port), secured: false, username: user, password: password)
          let dbClient = CouchDBClient(connectionProperties: connProperties)
        return dbClient.database(dbName)
      }
    }
    throw Error.IO("Failed to obtain database service and/or credentials.")
  }
}<commit_msg>Several changes in order to compile with latest Kitura code.<commit_after>/**
* Copyright IBM Corporation 2016
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import Foundation
import CouchDB
import SwiftyJSON
import CFEnvironment

public struct Configuration {

  public enum Error: ErrorProtocol {
    case IO(String)
  }

  // Instance constants
  let configurationFile = "config.json"
  let appEnv: AppEnv

  init() throws {
    // Generate file path for config.json
    let filePath = #file
    let components = filePath.characters.split(separator: "/").map(String.init)
    let notLastThree = components[0..<components.count - 3]
    let finalPath = "/" + notLastThree.joined(separator: "/") + "/\(configurationFile)"

    if let configData = NSData(contentsOfFile: finalPath) {
      let configJson = JSON(data: configData)
      appEnv = try CFEnvironment.getAppEnv(configJson)
      return
    }
    throw Error.IO("Failed to read/parse the contents of the '\(configurationFile)' configuration file.")
  }

  func getDatabase(dbName: String) throws -> Database {
    if let couchDBCredentials = appEnv.getService("Cloudant NoSQL DB-fz")?.credentials {
      if let host = couchDBCredentials["host"].string,
        user = couchDBCredentials["username"].string,
        password = couchDBCredentials["password"].string,
        port = couchDBCredentials["port"].int {
          let connProperties = ConnectionProperties(host: host, port: Int16(port), secured: true, username: user, password: password)
          let dbClient = CouchDBClient(connectionProperties: connProperties)
        return dbClient.database(dbName)
      }
    }
    throw Error.IO("Failed to obtain database service and/or credentials.")
  }
}
{'fraction_non_alphanumeric': 0.09483526268922529, 'fraction_numerical': 0.00578806767586821, 'mean_word_length': 3.6607883817427385, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '928102', 'n_tokens_mistral': 1204, 'n_tokens_neox': 1191, 'n_words': 423}