<commit_before>require 'yaml'

class WhiskeyDisk
  class Config
    class << self
      def environment_name
        (ENV['to'] && ENV['to'] != '') ? ENV['to'] : false
      end
      
      def path
        (ENV['path'] && ENV['path'] != '') ? ENV['path'] : false
      end
      
      def contains_rakefile?(path)
        File.exists?(File.expand_path(File.join(path, 'Rakefile')))
      end
      
      def base_path
        return path if path
        while (!contains_rakefile?(Dir.pwd))
          raise "Could not find Rakefile in the current directory tree!" if Dir.pwd == '/'
          Dir.chdir('..')
        end
        File.join(Dir.pwd, 'config')
      end
      
      def configuration_file
        return path if path and File.file?(path)
        per_environment_file = File.join(base_path, 'deploy', "#{environment_name}.yml")
        return per_environment_file if File.exists?(per_environment_file)
        per_environment_file = File.join(base_path, "#{environment_name}.yml")
        return per_environment_file if File.exists?(per_environment_file)
        raise "Could not locate configuration file in path [#{base_path}]" unless File.exists?(File.join(base_path, 'deploy.yml'))
        File.expand_path(File.join(base_path, 'deploy.yml'))
      end
      
      def configuration_data
        raise "Configuration file [#{configuration_file}] not found!" unless File.exists?(configuration_file)
        File.read(configuration_file)
      end
      
      def project_name(config)
        return '' unless config['repository'] and config['repository'] != ''
        config['repository'].sub(%r{^.*[/:]}, '').sub(%r{\.git$}, '')
      end
      
      def load_data
        YAML.load(configuration_data)
      rescue Exception => e
        raise %Q{Error reading configuration file [#{configuration_file}]: "#{e}"}
      end
      
      def fetch
        raise "Cannot determine current environment -- try rake ... to=staging, for example." unless environment_name
        data = load_data
        raise "No configuration file defined data for environment [#{environment_name}]" unless data[environment_name]
        config = (data[environment_name] || {}).merge({'environment' => environment_name})
        { 'project' => project_name(config) }.merge(config)
      end
    end
  end
end<commit_msg>Refactoring WhiskeyDisk::Config<commit_after>require 'yaml'

class WhiskeyDisk
  class Config
    class << self
      def environment_name
        (ENV['to'] && ENV['to'] != '') ? ENV['to'] : false
      end
      
      def path
        (ENV['path'] && ENV['path'] != '') ? ENV['path'] : false
      end
      
      def contains_rakefile?(path)
        File.exists?(File.expand_path(File.join(path, 'Rakefile')))
      end
      
      def find_rakefile_from_current_path
        while (!contains_rakefile?(Dir.pwd))
          raise "Could not find Rakefile in the current directory tree!" if Dir.pwd == '/'
          Dir.chdir('..')
        end
        File.join(Dir.pwd, 'config')
      end
      
      def base_path
        return path if path
        find_rakefile_from_current_path
      end
      
      def configuration_file
        return path if path and File.file?(path)
        
        [ 
          File.join(base_path, 'deploy', "#{environment_name}.yml"),
          File.join(base_path, "#{environment_name}.yml"), 
          File.join(base_path, 'deploy.yml') 
        ].each { |file|  return file if File.exists?(file) }
        
        raise "Could not locate configuration file in path [#{base_path}]"
      end
      
      def configuration_data
        raise "Configuration file [#{configuration_file}] not found!" unless File.exists?(configuration_file)
        File.read(configuration_file)
      end
      
      def project_name(config)
        return '' unless config['repository'] and config['repository'] != ''
        config['repository'].sub(%r{^.*[/:]}, '').sub(%r{\.git$}, '')
      end
      
      def load_data
        YAML.load(configuration_data)
      rescue Exception => e
        raise %Q{Error reading configuration file [#{configuration_file}]: "#{e}"}
      end
      
      def fetch
        raise "Cannot determine current environment -- try rake ... to=staging, for example." unless environment_name
        data = load_data
        raise "No configuration file defined data for environment [#{environment_name}]" unless data[environment_name]
        config = (data[environment_name] || {}).merge({'environment' => environment_name})
        { 'project' => project_name(config) }.merge(config)
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.12551664128779638, 'fraction_numerical': 0.0, 'mean_word_length': 2.480696442089326, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '6058528', 'n_tokens_mistral': 1310, 'n_tokens_neox': 1306, 'n_words': 327}