<commit_before>require 'yaml'
require 'openssl'

module Keystorage
  class << self
    def list(group=nil,file=nil)
      Manager.new(file).list(group)
    end
    
    def set(group,key,value,file=nil)
      Manager.new(file).set(group,key,value)
    end
    
    def get(group,name,file=nil)
      Manager.new(file).get(group,name)
    end

    def delete(group,name=nil,file=nil)
      Manager.new(file).delete(group,name)
    end

  end

  class Manager

    def initialize(file=nil)
      @file = DEFAULT_FILE
      @file = file if file
    end

    def list(group=nil)
      data = Hash.new
      if File::exist?(@file)
        File.open(@file,'r') do |f|
          data = YAML.load(f)
          return data.keys unless group
          return data[group].keys if data[group]
        end
      end
      Hash.new
    end

    def get(group,name)
      raise "missing group" unless group
      raise "missing name" unless name

      begin
        File.open(@file,'r') do |f|
          data=YAML.load(f)
          raise "missing keystorage" unless data
          raise "missing group "+group unless data.has_key?(group)
          raise "missing group "+group+" name "+name unless data[group].has_key?(name)

          return decode(data[group][name])
        end
      rescue =>e
      end
      false
    end

    def all
      return YAML.load_file(@file) if File.exist?(@file)
      Hash.new
    end

    def set(group,key,value)
      data=all
      data = Hash.new unless data
      data[group] = Hash.new unless data.has_key?(group)
      data[group][key] = encode(value)
      write(data)
    end

    def write(data)
      File.open(@file,'w',0600) do |f|
        YAML.dump(data,f)
      end
    end<commit_msg>fix: unsafe class<commit_after>require 'yaml'
require 'openssl'

module Keystorage
  class << self
    def list(group=nil,file=nil)
      Manager.new(file).list(group)
    end
    
    def set(group,key,value,file=nil)
      Manager.new(file).set(group,key,value)
    end
    
    def get(group,name,file=nil)
      Manager.new(file).get(group,name)
    end

    def delete(group,name=nil,file=nil)
      Manager.new(file).delete(group,name)
    end

  end

  class Manager

    def initialize(file=nil)
      @file = DEFAULT_FILE
      @file = file if file
    end

    def list(group=nil)
      data = Hash.new
      if File::exist?(@file)
        File.open(@file,'r') do |f|
          data = YAML.load(f)
          return data.keys unless group
          return data[group].keys if data[group]
        end
      end
      Hash.new
    end

    def get(group,name)
      raise "missing group" unless group
      raise "missing name" unless name

      begin
        File.open(@file,'r') do |f|
          data=YAML.load(f)
          raise "missing keystorage" unless data
          raise "missing group "+group unless data.has_key?(group)
          raise "missing group "+group+" name "+name unless data[group].has_key?(name)

          return decode(data[group][name])
        end
      rescue =>e
      end
      false
    end

    def all
      return YAML.load_file(@file) if File.exist?(@file)
      Hash.new
    end

    def set(group,key,value)
      data=all
      data = Hash.new unless data
      data[group] = Hash.new unless data.has_key?(group)
      data[group][key.to_s] = encode(value.to_s)
      write(data)
    end

    def write(data)
      File.open(@file,'w',0600) do |f|
        YAML.dump(data,f)
      end
    end
{'fraction_non_alphanumeric': 0.11473988439306358, 'fraction_numerical': 0.0023121387283236996, 'mean_word_length': 2.160730593607306, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '2195423', 'n_tokens_mistral': 1148, 'n_tokens_neox': 1115, 'n_words': 269}