<commit_before>require File.join(File.dirname(__FILE__), 'helpers', 'ruby')

require 'rubygems' if RUBY_VERSION < '1.9.0'
require 'bundler'
require 'bundler/setup'

gem 'eventmachine', '~> 1.0.0.beta.4'

require 'optparse'
require 'json'
require 'hashie'
require 'amqp'
require 'cabin'
require 'cabin/outputs/em-stdlib-logger'

module Sensu
  class Config
    attr_accessor :settings, :logger

    def initialize(options={})
      @options = options
      read_config
      validate_config
    end

    def open_log
      @logger = Cabin::Channel.new
      log_file = @options[:log_file] || '/tmp/sensu.log'
      if File.writable?(log_file) || !File.exist?(log_file) && File.writable?(File.dirname(log_file))
        ruby_logger = Logger.new(log_file)
      else
        invalid_config('log file is not writable: ' + log_file)
      end
      @logger.subscribe(Cabin::Outputs::EmStdlibLogger.new(ruby_logger))
      @logger.level = @options[:verbose] ? :debug : :info
      Signal.trap('USR1') do
        @logger.level = @logger.level == :info ? :debug : :info
      end
      @logger
    end

    def read_config
      config_file = @options[:config_file] || '/etc/sensu/config.json'
      config_dir = @options[:config_dir] || '/etc/sensu/conf.d'
      if File.readable?(config_file)
        begin
          @settings = Hashie::Mash.new(JSON.parse(File.open(config_file, 'r').read))
        rescue JSON::ParserError => error
          invalid_config('configuration file (' + config_file + ') must be valid JSON: ' + error)
        end
      else
        invalid_config('configuration file does not exist or is not readable: ' + config_file)
      end
      if File.exists?(config_dir)
        Dir[config_dir + '/**/*.json'].each do |snippet_file|
          begin
            snippet_hash = JSON.parse(File.open(snippet_file, 'r').read)
          rescue JSON::ParserError => error
            invalid_config('configuration snippet file (' + snippet_file + ') must be valid JSON: ' + error)
          end<commit_msg>Use a hash of default options, make validation optional<commit_after>require File.join(File.dirname(__FILE__), 'helpers', 'ruby')

require 'rubygems' if RUBY_VERSION < '1.9.0'
require 'bundler'
require 'bundler/setup'

gem 'eventmachine', '~> 1.0.0.beta.4'

require 'optparse'
require 'json'
require 'hashie'
require 'amqp'
require 'cabin'
require 'cabin/outputs/em-stdlib-logger'

module Sensu
  class Config
    attr_accessor :settings, :logger

    DEFAULT_OPTIONS = {
      :log_file => '/tmp/sensu.log',
      :config_file => '/etc/sensu/config.json',
      :config_dir => '/etc/sensu/conf.d',
      :validate => true,
    }

    def initialize(options={})
      @options = DEFAULT_OPTIONS.merge(options)
      read_config
      validate_config if @options[:validate]
    end

    def open_log
      @logger = Cabin::Channel.new
      if File.writable?(@options[:log_file]) || !File.exist?(@options[:log_file]) && File.writable?(File.dirname(@options[:log_file]))
        ruby_logger = Logger.new(@options[:log_file])
      else
        invalid_config('log file is not writable: ' + @options[:log_file])
      end
      @logger.subscribe(Cabin::Outputs::EmStdlibLogger.new(ruby_logger))
      @logger.level = @options[:verbose] ? :debug : :info
      Signal.trap('USR1') do
        @logger.level = @logger.level == :info ? :debug : :info
      end
      @logger
    end

    def read_config
      if File.readable?(@options[:config_file])
        begin
          @settings = Hashie::Mash.new(JSON.parse(File.open(@options[:config_file], 'r').read))
        rescue JSON::ParserError => error
          invalid_config('configuration file (' + @options[:config_file] + ') must be valid JSON: ' + error)
        end
      else
        invalid_config('configuration file does not exist or is not readable: ' + @options[:config_file])
      end
      if File.exists?(@options[:config_dir])
        Dir[@options[:config_dir] + '/**/*.json'].each do |snippet_file|
          begin
            snippet_hash = JSON.parse(File.open(snippet_file, 'r').read)
          rescue JSON::ParserError => error
            invalid_config('configuration snippet file (' + snippet_file + ') must be valid JSON: ' + error)
          end
{'fraction_non_alphanumeric': 0.136986301369863, 'fraction_numerical': 0.003778932451582428, 'mean_word_length': 3.0180265654648957, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, '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': '5174244', 'n_tokens_mistral': 1355, 'n_tokens_neox': 1325, 'n_words': 287}