<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

// TODO: Determine feasibility of returning structs/classes instead of
// dictionaries for the methods and instance variables exposed in this class.
public class AppEnv {

  let isLocal: Bool
  let app: [String:AnyObject]
  let services: [String:AnyObject]
  let port: Int
  let name: String?
  let bind: String
  let urls: [String]

  // Constructor
  public init(options: [String:AnyObject]) throws {
    // NSProcessInfo.processInfo().environment returns [String : String]
    let environmentVars = NSProcessInfo.processInfo().environment
    let vcapApplication = environmentVars["VCAP_APPLICATION"]
    isLocal = (vcapApplication == nil)

    // Get App
    if isLocal {
      if let appDictionary = options["vcap"]?["application"] as? [String:AnyObject] {
        app = appDictionary
      } else {
        app = [:]
      }
    } else {
       if let appDictionary = Utils.convertStringToDictionary(vcapApplication) {
         app = appDictionary
       } else {
         app = [:]
       }
    }

    // Get services
    let vcapServices = environmentVars["VCAP_SERVICES"]
    if isLocal {
      if let servicesDictionary = options["vcap"]?["services"] as? [String:AnyObject] {
        services = servicesDictionary
      } else {
        services = [:]
      }
    } else {
      if let servicesDictionary = Utils.convertStringToDictionary(vcapServices) {
        services = servicesDictionary
      } else {
         services = [:]
       }
    }

    // Get port
    var portString: String? = environmentVars["PORT"] ?? environmentVars["CF_INSTANCE_PORT"] ??
      environmentVars["VCAP_APP_PORT"] ?? nil

    if app["name"] == nil && portString == nil {
      portString = "8090"
    }

    var number: Int?
    if portString != nil {
      number = Int(portString!)
    }
    if number == nil {
        throw CFEnvironmentError.VariableNotFound
    }
    port = number!

    // Get name
    if let nameString = options["vcap"]?["name"] as? String {
      name = nameString
    } else if let nameString = app["name"] as? String {
      name = nameString
    } else {
      name = nil
    }

    // TODO: Add logic for parsing Package.swft and manifest.yml
    //https://github.com/behrang/YamlSwift

    // Get bind
    bind = (app["host"] != nil) ? app["host"] as! String : "localhost"

    // Get urls
    var uris: [String] = (app["uris"] != nil) ? app["uris"] as! [String] : []
    if isLocal {<commit_msg>initial commit<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

// TODO: Determine feasibility of returning structs/classes instead of
// dictionaries for the methods and instance variables exposed in this class.
public class AppEnv {

  let isLocal: Bool
  let app: [String:AnyObject]
  let services: [String:AnyObject]
  let port: Int
  let name: String?
  let bind: String
  let urls: [String]

  /**
  * This vcap option property is ignored if not running locally.
  */
  public init(options: [String:AnyObject]) throws {
    // NSProcessInfo.processInfo().environment returns [String : String]
    let environmentVars = NSProcessInfo.processInfo().environment
    let vcapApplication = environmentVars["VCAP_APPLICATION"]
    isLocal = (vcapApplication == nil)

    // Get App
    if isLocal {
      if let appDictionary = options["vcap"]?["application"] as? [String:AnyObject] {
        app = appDictionary
      } else {
        app = [:]
      }
    } else {
       if let appDictionary = Utils.convertStringToDictionary(vcapApplication) {
         app = appDictionary
       } else {
         app = [:]
       }
    }

    // Get services
    let vcapServices = environmentVars["VCAP_SERVICES"]
    if isLocal {
      if let servicesDictionary = options["vcap"]?["services"] as? [String:AnyObject] {
        services = servicesDictionary
      } else {
        services = [:]
      }
    } else {
      if let servicesDictionary = Utils.convertStringToDictionary(vcapServices) {
        services = servicesDictionary
      } else {
         services = [:]
       }
    }

    // Get port
    var portString: String? = environmentVars["PORT"] ?? environmentVars["CF_INSTANCE_PORT"] ??
      environmentVars["VCAP_APP_PORT"] ?? nil

    if app["name"] == nil && portString == nil {
      portString = "8090"
    }

    var number: Int?
    if portString != nil {
      number = Int(portString!)
    }
    if number == nil {
        throw CFEnvironmentError.VariableNotFound
    }
    port = number!

    // Get name
    if let nameString = options["name"] as? String {
      name = nameString
    } else if let nameString = app["name"] as? String {
      name = nameString
    } else {
      name = nil
    }

    // TODO: Add logic for parsing manifest.yml
    //https://github.com/behrang/YamlSwift

    // Get bind
    bind = (app["host"] != nil) ? app["host"] as! String : "localhost"

    // Get urls
    var uris: [String] = (app["uris"] != nil) ? app["uris"] as! [String] : []
    if isLocal {
{'fraction_non_alphanumeric': 0.1085965483555845, 'fraction_numerical': 0.0039075219798111365, 'mean_word_length': 2.7343465045592703, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '3932458', 'n_tokens_mistral': 1758, 'n_tokens_neox': 1709, 'n_words': 564}