<commit_before>require 'yaml'

module Duse
  module CLIConfig
    extend self

    def uri=(uri)
      fail ArgumentError, 'Not an uri' unless uri =~ URI.regexp
      set 'uri', uri
    end

    def uri
      get 'uri'
    end

    def token=(token)
      fail ArgumentError, 'Token must be a string'  unless token.is_a? String
      fail ArgumentError, 'Token must not be empty' if token.empty?
      set 'token', token
    end

    def token
      get 'token'
    end

    def set(key, value)
      config = load
      config[key] = value
      save(config)
    end

    def get(key)
      load[key]
    end

    def config
      load
    end

    def config_file
      File.join(Dir.home, '.duse.yml')
    end
    
    private

    def load
      config = YAML.load load_config_file
      return {} unless config.is_a? Hash
      config
    end

    def save(config)
      File.open(config_file, 'w') do |file|
        file.write config.to_yaml
        file.chmod 0600
      end
    end

    def load_config_file
      # pretend like it's an empty file if config does not exists
      return "--- {}\n" unless File.exist? config_file<commit_msg>change config file location<commit_after>require 'yaml'

module Duse
  module CLIConfig
    extend self

    def uri=(uri)
      fail ArgumentError, 'Not an uri' unless uri =~ URI.regexp
      set 'uri', uri
    end

    def uri
      get 'uri'
    end

    def token=(token)
      fail ArgumentError, 'Token must be a string'  unless token.is_a? String
      fail ArgumentError, 'Token must not be empty' if token.empty?
      set 'token', token
    end

    def token
      get 'token'
    end

    def set(key, value)
      config = load
      config[key] = value
      save(config)
    end

    def get(key)
      load[key]
    end

    def config
      load
    end

    def config_file
      File.join config_dir, 'config.yml'
    end
    
    private

    def config_dir
      File.join Dir.home, '.config', 'duse'
    end

    def load
      config = YAML.load load_config_file
      return {} unless config.is_a? Hash
      config
    end

    def save(config)
      FileUtils.mkdir_p config_dir
      File.open(config_file, 'w') do |file|
        file.write config.to_yaml
        file.chmod 0600
      end
    end

    def load_config_file
      # pretend like it's an empty file if config does not exists
      return "--- {}\n" unless File.exist? config_file
{'fraction_non_alphanumeric': 0.07689127738735015, 'fraction_numerical': 0.0033071517155849523, 'mean_word_length': 1.9404617253948968, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2102764', 'n_tokens_mistral': 786, 'n_tokens_neox': 747, 'n_words': 254}