<commit_before>isdefined(Base, :__precompile__) && __precompile__()

module AWS
using Requests
using JSON
using LightXML

const EP_US_EAST_NORTHERN_VIRGINIA     = "ec2.us-east-1.amazonaws.com"
const EP_US_WEST_OREGON                = "ec2.us-west-2.amazonaws.com"
const EP_US_WEST_NORTHERN_CALIFORNIA   = "ec2.us-west-1.amazonaws.com"
const EP_EU_IRELAND                    = "ec2.eu-west-1.amazonaws.com"
const EP_AP_SINGAPORE                  = "ec2.ap-southeast-1.amazonaws.com"
const EP_AP_SYDNEY                     = "ec2.ap-southeast-2.amazonaws.com"
const EP_AP_TOKYO                      = "ec2.ap-northeast-1.amazonaws.com"
const EP_SA_SAO_PAULO                  = "ec2.sa-east-1.amazonaws.com"

export EP_US_EAST_NORTHERN_VIRGINIA, EP_US_WEST_OREGON, EP_US_WEST_NORTHERN_CALIFORNIA
export EP_EU_IRELAND, EP_AP_SINGAPORE, EP_AP_SYDNEY, EP_AP_TOKYO, EP_SA_SAO_PAULO

export AWSEnv

const AP_NORTHEAST_1 = "ap-northeast-1" # Asia Pacific (Tokyo) Region
const AP_SOUTHEAST_1 = "ap-southeast-1" # Asia Pacific (Singapore) Region
const AP_SOUTHEAST_2 = "ap-southeast-2" # Asia Pacific (Sydney) Region
const EU_WEST_1 = "eu-west-1" # EU (Ireland) Region
const SA_EAST_1 = "sa-east-1" # South America (Sao Paulo) Region
const US_EAST_1 = "us-east-1" # US East (Northern Virginia) Region
const US_WEST_1 = "us-west-1" # US West (Northern California) Region
const US_WEST_2 = "us-west-2" # US West (Oregon) Region

function __init__()
    config_file_base = Sys.KERNEL == :Windows ? ENV["APPDATA"] : homedir()

    # Search for default AWS_ID and AWS_SECKEY
    global AWS_ID = ""
    global AWS_SECKEY = ""
    global AWS_REGION = US_EAST_1
    global AWS_TOKEN = ""
    if haskey(ENV, "AWS_ID") && haskey(ENV, "AWS_SECKEY")
        AWS_ID = ENV["AWS_ID"]
        AWS_SECKEY = ENV["AWS_SECKEY"]
    elseif haskey(ENV, "AWS_ACCESS_KEY_ID") && haskey(ENV, "AWS_SECRET_ACCESS_KEY")
        AWS_ID = ENV["AWS_ACCESS_KEY_ID"]
        AWS_SECKEY = ENV["AWS_SECRET_ACCESS_KEY"]
    elseif isfile(joinpath(homedir(), ".aws/config")) || isfile(joinpath(homedir(), ".aws/credentials"))
        if isfile(joinpath(homedir(), ".aws/config"))
            credentialFileName = joinpath(homedir(), ".aws/config")
        else
            credentialFileName = joinpath(homedir(), ".aws/credentials")
        end
        for line in readlines( credentialFileName )
            line = replace(line, " ", "")
            line = replace(line, "\n", "")
            segs = split(line, "=")
            if ismatch(r"^aws_secret_access_key", segs[1])
                AWS_SECKEY = segs[2]
            elseif ismatch(r"^aws_access_key_id", segs[1])
                AWS_ID = segs[2]
            elseif ismatch(r"^region", segs[1])
                AWS_REGION = segs[2]
            end
        end
    else
        secret_path = joinpath(config_file_base, ".awssecret")
        if isfile(secret_path)
            AWS_ID, AWS_SECKEY = split(readchomp(secret_path))
        end
    end

    # Search for default AWS_REGION
    if haskey(ENV, "AWS_REGION")
        AWS_REGION = ENV["AWS_REGION"]
    elseif haskey(ENV, "AWS_DEFAULT_REGION")
        AWS_REGION = ENV["AWS_DEFAULT_REGION"]
    elseif isfile( joinpath(config_file_base, ".awsregion") )
        region_path = joinpath(config_file_base, ".awsregion")
        AWS_REGION = readchomp(region_path)
    else
        println("use default region: $(AWS_REGION)")
    end
end

type AWSEnv
    aws_id::String         # AWS Access Key id<commit_msg>read configuration and credentials in right order<commit_after>isdefined(Base, :__precompile__) && __precompile__()

module AWS
using Requests
using JSON
using LightXML

const EP_US_EAST_NORTHERN_VIRGINIA     = "ec2.us-east-1.amazonaws.com"
const EP_US_WEST_OREGON                = "ec2.us-west-2.amazonaws.com"
const EP_US_WEST_NORTHERN_CALIFORNIA   = "ec2.us-west-1.amazonaws.com"
const EP_EU_IRELAND                    = "ec2.eu-west-1.amazonaws.com"
const EP_AP_SINGAPORE                  = "ec2.ap-southeast-1.amazonaws.com"
const EP_AP_SYDNEY                     = "ec2.ap-southeast-2.amazonaws.com"
const EP_AP_TOKYO                      = "ec2.ap-northeast-1.amazonaws.com"
const EP_SA_SAO_PAULO                  = "ec2.sa-east-1.amazonaws.com"

export EP_US_EAST_NORTHERN_VIRGINIA, EP_US_WEST_OREGON, EP_US_WEST_NORTHERN_CALIFORNIA
export EP_EU_IRELAND, EP_AP_SINGAPORE, EP_AP_SYDNEY, EP_AP_TOKYO, EP_SA_SAO_PAULO

export AWSEnv

const AP_NORTHEAST_1 = "ap-northeast-1" # Asia Pacific (Tokyo) Region
const AP_SOUTHEAST_1 = "ap-southeast-1" # Asia Pacific (Singapore) Region
const AP_SOUTHEAST_2 = "ap-southeast-2" # Asia Pacific (Sydney) Region
const EU_WEST_1 = "eu-west-1" # EU (Ireland) Region
const SA_EAST_1 = "sa-east-1" # South America (Sao Paulo) Region
const US_EAST_1 = "us-east-1" # US East (Northern Virginia) Region
const US_WEST_1 = "us-west-1" # US West (Northern California) Region
const US_WEST_2 = "us-west-2" # US West (Oregon) Region

function read_aws_config(configFileName::String = joinpath(homedir(), ".aws/config"))
    global AWS_ID
    global AWS_SECKEY
    global AWS_REGION

    isfile(configFileName) || return
    for line in readlines(configFileName)
        line = replace(line, " ", "")
        line = replace(line, "\n", "")
        segs = split(line, "=")
        if ismatch(r"^aws_secret_access_key", segs[1])
            AWS_SECKEY = segs[2]
        elseif ismatch(r"^aws_access_key_id", segs[1])
            AWS_ID = segs[2]
        elseif ismatch(r"^region", segs[1])
            AWS_REGION = segs[2]
        end
    end
end

function __init__()
    config_file_base = Sys.KERNEL == :Windows ? ENV["APPDATA"] : homedir()

    # Search for default AWS_ID and AWS_SECKEY
    global AWS_ID = ""
    global AWS_SECKEY = ""
    global AWS_REGION = US_EAST_1
    global AWS_TOKEN = ""

    # read the default configuration file first
    read_aws_config()

    # override specific options in the same precedence as specified in
    # http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html#config-settings-and-precedence
    credentials_path = joinpath(homedir(), ".aws/credentials")
    if haskey(ENV, "AWS_ID") && haskey(ENV, "AWS_SECKEY")
        AWS_ID = ENV["AWS_ID"]
        AWS_SECKEY = ENV["AWS_SECKEY"]
    elseif haskey(ENV, "AWS_ACCESS_KEY_ID") && haskey(ENV, "AWS_SECRET_ACCESS_KEY")
        AWS_ID = ENV["AWS_ACCESS_KEY_ID"]
        AWS_SECKEY = ENV["AWS_SECRET_ACCESS_KEY"]
    elseif isfile(credentials_path)
        read_aws_config(credentials_path)
    else
        secret_path = joinpath(config_file_base, ".awssecret")
        if isfile(secret_path)
            AWS_ID, AWS_SECKEY = split(readchomp(secret_path))
        end
    end

    # Search for default AWS_REGION
    if haskey(ENV, "AWS_REGION")
        AWS_REGION = ENV["AWS_REGION"]
    elseif haskey(ENV, "AWS_DEFAULT_REGION")
        AWS_REGION = ENV["AWS_DEFAULT_REGION"]
    elseif isfile( joinpath(config_file_base, ".awsregion") )
        region_path = joinpath(config_file_base, ".awsregion")
        AWS_REGION = readchomp(region_path)
    else
        println("use default region: $(AWS_REGION)")
    end
end

type AWSEnv
    aws_id::String         # AWS Access Key id
{'fraction_non_alphanumeric': 0.11010704851939386, 'fraction_numerical': 0.010843875990546365, 'mean_word_length': 3.0944792259533296, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4770440', 'n_tokens_mistral': 2672, 'n_tokens_neox': 2649, 'n_words': 501}