<commit_before>require "aws/profile_parser/version"

module AWS
  class ProfileParser

    def initialize
      @file = ENV['AWS_CONFIG_FILE']
      @credentials = nil
    end

    # returns hash of AWS credential
    def get(profile='default')
      raise 'Config File does not exist' unless File.exists?(@file)

      @credentials = parse if @credentials.nil?
      raise 'The profile is not specified in the config file' unless @credentials.has_key?(profile)

      @credentials[profile]
    end

    def parse
      section = { "" => {} }
      current = {}
      s = StringScanner.new(File.read(@file))

      while !s.eos?
        case
        when s.scan(/\s+/)
          # do nothing
        when s.scan(/\[(profile\s+)?(.+?)\]/)
          # strip 'profile' from each profile key and set to section key
          section[s[2]] = current = {}
        when s.scan(/(\w+)\s*=\s*([^\n]+)/)
          # strip 'aws_' from each key
          current[s[1].sub(/^aws_/, '').to_sym] = s[2]
        else
          raise "Parse Error. I guess config file syntax is something wrong."
        end<commit_msg>bugfix when AWS_CONFIG_FILE is not set<commit_after>require "aws/profile_parser/version"

module AWS
  class ProfileParser

    def initialize
      @file = ENV['AWS_CONFIG_FILE'] || ENV['HOME'] + "/.aws/config"
      @credentials = nil
    end

    # returns hash of AWS credential
    def get(profile='default')
      raise 'Config File does not exist' unless File.exists?(@file)

      @credentials = parse if @credentials.nil?
      raise 'The profile is not specified in the config file' unless @credentials.has_key?(profile)

      @credentials[profile]
    end

    def parse
      section = {}
      current = {}
      s = StringScanner.new(File.read(@file))

      while !s.eos?
        case
        when s.scan(/\s+/)
          # do nothing
        when s.scan(/\[(profile\s+)?(.+?)\]/)
          # strip 'profile' from each profile key and set to section key
          section[s[2]] = current = {}
        when s.scan(/(\w+)\s*=\s*([^\n]+)/)
          # strip 'aws_' from each key
          current[s[1].sub(/^aws_/, '').to_sym] = s[2]
        else
          raise "Parse Error. I guess config file syntax is something wrong."
        end
{'fraction_non_alphanumeric': 0.13505108840515326, 'fraction_numerical': 0.0026654820079964462, 'mean_word_length': 2.185289957567185, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '630914', 'n_tokens_mistral': 698, 'n_tokens_neox': 683, 'n_words': 200}