<commit_before>module PSQLCM
  class << self
    def verbose(message)
      $stdout.puts message if (config.verbose || config.debug)
    end

    def debug(message)
      $stdout.puts message if config.debug
    end

    def halt!(message)
      $stderr.puts message
      exit 1
    end

    def config
      @config ||= OpenStruct.new
    end

    def databases
      @databases = db.
        exec("SELECT datname as name FROM pg_database WHERE datname !~ 'template*|postgres';").
        map {|row| row['name']}

      if config.databases.to_a.empty?
        halt! 'A list of databases must be given:\n  --databases={database_one}[,{database_two}[,...]]'
      else # filter out databases not specified.
        @databases.select!{ |name| config.databases.include?(name) }
      end
      debug "databases> #{@databases}"
      @databases
    end

    def schemas(name = 'postgres')
      @schemas = db(name).
        exec("SELECT nspname AS name FROM pg_namespace WHERE nspname !~ '^pg_.*|information_schema';").
        map{|row| row['name']}

      # Filter out schemas not specified, if specified.
      @schemas.select!{ |name| config.schemas.include?(name) } if config.schemas
      debug "schemas> #{@schemas}"
      @schemas
    end

    def tree
      return @tree if @tree
      @tree = {}
      databases.each do |dbname|
        @tree[dbname] = schemas(dbname)
      end
      debug "tree> #{@tree}"
      @tree
    end

    def run!(action = config.action)
      case action
      when 'console'
        require 'psql-cm/cli'
        ::PSQLCM::Console.run!
      when 'dump'
        dump!
      when 'restore'
        restore!
      when 'setup'
        setup!
      when 'submit'
        submit!
      else
        halt! "An action must be given! {setup, dump, restore}" if action.nil?
        halt! "Action '#{action}' is not handled."
      end
    end

    def sh(command)
      debug "$ #{command}"
      %x[#{command} 2>&1 | awk '! /NOTICE/']
    end

    def uri
      return config.uri unless config.uri.to_s.empty?
      $stdout.puts "NOTICE: uri is not set, defaulting to postgres://127.0.0.1:5432 (format: postgres://{user}:{password}@{host}:{port}/{database}\nwhere user, password, port and database are optional)"
      config.uri = "postgres://127.0.0.1:5432"
    end
<commit_msg>Cleaned up NOTICE formatting.<commit_after>module PSQLCM
  class << self
    def verbose(message)
      $stdout.puts message if (config.verbose || config.debug)
    end

    def debug(message)
      $stdout.puts message if config.debug
    end

    def halt!(message)
      $stderr.puts message
      exit 1
    end

    def config
      @config ||= OpenStruct.new
    end

    def databases
      @databases = db.
        exec("SELECT datname as name FROM pg_database WHERE datname !~ 'template*|postgres';").
        map {|row| row['name']}

      if config.databases.to_a.empty?
        halt! 'A list of databases must be given:\n  --databases={database_one}[,{database_two}[,...]]'
      else # filter out databases not specified.
        @databases.select!{ |name| config.databases.include?(name) }
      end
      debug "databases> #{@databases}"
      @databases
    end

    def schemas(name = 'postgres')
      @schemas = db(name).
        exec("SELECT nspname AS name FROM pg_namespace WHERE nspname !~ '^pg_.*|information_schema';").
        map{|row| row['name']}

      # Filter out schemas not specified, if specified.
      @schemas.select!{ |name| config.schemas.include?(name) } if config.schemas
      debug "schemas> #{@schemas}"
      @schemas
    end

    def tree
      return @tree if @tree
      @tree = {}
      databases.each do |dbname|
        @tree[dbname] = schemas(dbname)
      end
      debug "tree> #{@tree}"
      @tree
    end

    def run!(action = config.action)
      case action
      when 'console'
        require 'psql-cm/cli'
        ::PSQLCM::Console.run!
      when 'dump'
        dump!
      when 'restore'
        restore!
      when 'setup'
        setup!
      when 'submit'
        submit!
      else
        halt! "An action must be given! {setup, dump, restore}" if action.nil?
        halt! "Action '#{action}' is not handled."
      end
    end

    def sh(command)
      debug "$ #{command}"
      %x[#{command} 2>&1 | awk '! /NOTICE/']
    end

    def uri
      return config.uri unless config.uri.to_s.empty?
      $stdout.puts "NOTICE: uri is not set, defaulting to postgres://127.0.0.1:5432 (format: postgres://{user}:{password}@{host}:{port}/{database} (where user, password, port and database are optional)"
      config.uri = "postgres://127.0.0.1:5432"
    end

{'fraction_non_alphanumeric': 0.1300690250215703, 'fraction_numerical': 0.00992234685073339, 'mean_word_length': 2.2770318021201414, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '2534452', 'n_tokens_mistral': 1504, 'n_tokens_neox': 1438, 'n_words': 408}