<commit_before>module Chatterbot

  #
  # routines for storing config information for the bot
  module Config  
    attr_accessor :config
    
    #
    # the entire config for the bot, loaded from YAML files and the DB if applicable
    def config
      @config ||= load_config
    end   

    #
    # has the config been loaded yet?
    def has_config?
      ! @config.nil?
    end   

    #
    # should we log tweets to the database?
    def log_tweets?
      config.has_key?(:db_uri)
    end

    #
    # Check to see if Sequel was loaded successfully.  If not, we won't make any DB calls
    def has_sequel?
      ! defined?(Sequel).nil?
    end
    
    #
    # do we have a DB connection string?
    def has_db?
      has_sequel? && config.has_key?(:db_uri)
    end

    #
    # are we in debug mode?
    def debug_mode?
      config[:debug_mode] || false
    end

    #
    # Should we run any config updates?
    def update_config?
      config[:dry_run] || true
    end

    #
    # should we write to a log file?
    def logging?
      has_config? && config.has_key?(:log_dest)
    end

    #
    # destination for log entries
    def log_dest
      config[:log_dest]
    end

    #
    # store since_id to a different key so that it doesn't actually
    # get updated until the bot is done running
    def since_id=(x)
      config[:tmp_since_id] = x
    end

    #
    # return the ID of the most recent tweet pulled up in searches
    def since_id
      config[:since_id] || 1
    end   

    #
    # write out our config file
    def update_config
      return if ! update_config?

      # don't update flat file if we can store to the DB instead
      if has_db?
        store_database_config
      else
        store_local_config
      end
    end

    #
    # update the since_id with either the highest ID of the specified
    # tweets, unless it is lower than what we have already
    def update_since_id(search)
      unless search.nil?
        tmp_id = case
                   # incoming tweets
                 when search.has_key?(:id) then search[:id]
                   
                   # incoming searches
                 when search.has_key?("max_id") then search["max_id"]

                   # other?
                 else 1
                 end.to_i
        config[:tmp_since_id] = [config[:tmp_since_id].to_i, tmp_id].max        
      end
    end

    #
    # return a hash of the params we need to connect to the Twitter API
    def client_params
      { 
        :consumer_key => config[:consumer_key],
        :consumer_secret => config[:consumer_secret],
        :token => config[:token].nil? ? nil : config[:token],
        :secret => config[:secret].nil? ? nil : config[:secret]
      }
    end

    #
    # do we have an API key specified?
    def needs_api_key?
      config[:consumer_key].nil? || config[:consumer_secret].nil?
    end

    #
    # has this script validated with Twitter OAuth?
    def needs_auth_token?
      config[:token].nil?
    end

    #
    # figure out what config file to load based on the name of the bot
    def config_file
      "#{botname}.yml"
    end

    #
    # load in a config file
    def slurp_file(f)
      f = File.expand_path(f)

      tmp = {}

      if File.exist?(f)
        File.open( f ) { |yf| 
          tmp = YAML::load( yf ) 
        }
      end
      tmp.symbolize_keys! unless tmp == false<commit_msg>use the path of the calling script to figure out the path to the config file<commit_after>module Chatterbot

  #
  # routines for storing config information for the bot
  module Config  
    attr_accessor :config
    
    #
    # the entire config for the bot, loaded from YAML files and the DB if applicable
    def config
      @config ||= load_config
    end   

    #
    # has the config been loaded yet?
    def has_config?
      ! @config.nil?
    end   

    #
    # should we log tweets to the database?
    def log_tweets?
      config.has_key?(:db_uri)
    end

    #
    # Check to see if Sequel was loaded successfully.  If not, we won't make any DB calls
    def has_sequel?
      ! defined?(Sequel).nil?
    end
    
    #
    # do we have a DB connection string?
    def has_db?
      has_sequel? && config.has_key?(:db_uri)
    end

    #
    # are we in debug mode?
    def debug_mode?
      config[:debug_mode] || false
    end

    #
    # Should we run any config updates?
    def update_config?
      config[:dry_run] || true
    end

    #
    # should we write to a log file?
    def logging?
      has_config? && config.has_key?(:log_dest)
    end

    #
    # destination for log entries
    def log_dest
      config[:log_dest]
    end

    #
    # store since_id to a different key so that it doesn't actually
    # get updated until the bot is done running
    def since_id=(x)
      config[:tmp_since_id] = x
    end

    #
    # return the ID of the most recent tweet pulled up in searches
    def since_id
      config[:since_id] || 1
    end   

    #
    # write out our config file
    def update_config
      return if ! update_config?

      # don't update flat file if we can store to the DB instead
      if has_db?
        store_database_config
      else
        store_local_config
      end
    end

    #
    # update the since_id with either the highest ID of the specified
    # tweets, unless it is lower than what we have already
    def update_since_id(search)
      unless search.nil?
        tmp_id = case
                   # incoming tweets
                 when search.has_key?(:id) then search[:id]
                   
                   # incoming searches
                 when search.has_key?("max_id") then search["max_id"]

                   # other?
                 else 1
                 end.to_i
        config[:tmp_since_id] = [config[:tmp_since_id].to_i, tmp_id].max        
      end
    end

    #
    # return a hash of the params we need to connect to the Twitter API
    def client_params
      { 
        :consumer_key => config[:consumer_key],
        :consumer_secret => config[:consumer_secret],
        :token => config[:token].nil? ? nil : config[:token],
        :secret => config[:secret].nil? ? nil : config[:secret]
      }
    end

    #
    # do we have an API key specified?
    def needs_api_key?
      config[:consumer_key].nil? || config[:consumer_secret].nil?
    end

    #
    # has this script validated with Twitter OAuth?
    def needs_auth_token?
      config[:token].nil?
    end

    #
    # figure out what config file to load based on the name of the bot
    def config_file
      File.join(File.expand_path(File.dirname($0)), "#{botname}.yml")
    end

    #
    # load in a config file
    def slurp_file(f)
      f = File.expand_path(f)

      tmp = {}

      if File.exist?(f)
        File.open( f ) { |yf| 
          tmp = YAML::load( yf ) 
        }
      end
      tmp.symbolize_keys! unless tmp == false
{'fraction_non_alphanumeric': 0.0788712011577424, 'fraction_numerical': 0.000723589001447178, 'mean_word_length': 1.8676348547717843, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '2939105', 'n_tokens_mistral': 2154, 'n_tokens_neox': 2054, 'n_words': 693}