<commit_before>
module Coral
module Util
class Disk
  
  #-----------------------------------------------------------------------------
  # Properties
 
  @@files = {}
  
  @@separator   = false
  @@description = ''
  
  #-----------------------------------------------------------------------------
  # Utilities
  
  def self.open(file_name, options = {}, reset = false)
    mode          = options[:mode].to_s
    
    @@separator   = ( options[:separator] ? options[:separator] : false )
    @@description = ( options[:description] ? options[:description] : '' )
    
    if @@files.has_key?(file_name) && ! reset
      reset = true if ! mode.empty? && mode != @@files[file_name][:mode]
    end
    
    if ! @@files.has_key?(file_name) || ! @@files[file_name][:file] || reset
      @@files[file_name][:file].close if @@files[file_name] && @@files[file_name][:file]
      unless mode.empty? || ( mode == 'r' && ! File.exists?(file_name) )
        @@files[file_name] = {
          :file => File.open(file_name, mode),
          :mode => mode,
        }
      end
    end
    return nil unless @@files[file_name]
    return @@files[file_name][:file]
  end
  
  #---
  
  def self.read(file_name, options = {})
    options[:mode] = ( options[:mode] ? options[:mode] : 'r' )
    file           = open(file_name, options)
    
    if file
      return file.read
    end
    return nil
  end
  
  #---
  
  def self.write(file_name, data, options = {})
    options[:mode] = ( options[:mode] ? options[:mode] : 'w' )
    file           = open(file_name, options)
    
    if file<commit_msg>Backporting disk exists and filename methods and a bug fix to the disk library from version 0.3.<commit_after>
module Coral
module Util
class Disk
  
  #-----------------------------------------------------------------------------
  # Properties
 
  @@files = {}
  
  @@separator   = false
  @@description = ''
  
  #-----------------------------------------------------------------------------
  # Utilities
  
  def self.exists?(file)
    return File.exists?(File.expand_path(file))  
  end
  
  #---
  
  def self.filename(file_name)
    return ( file_name.is_a?(Array) ? file_name.join(File::SEPARATOR) : file_name.to_s )
  end
  
  #---
  
  def self.open(file_name, options = {}, reset = false)
    mode          = options[:mode].to_s
    
    @@separator   = ( options[:separator] ? options[:separator] : false )
    @@description = ( options[:description] ? options[:description] : '' )
    
    if @@files.has_key?(file_name) && ! reset
      reset = true if ! mode.empty? && mode != @@files[file_name][:mode]
    end
    
    if ! @@files.has_key?(file_name) || ! @@files[file_name][:file] || reset
      @@files[file_name][:file].close if @@files[file_name] && @@files[file_name][:file]
      unless mode.empty? || ( mode == 'r' && ! File.exists?(file_name) )
        @@files[file_name] = {
          :file => File.open(file_name, mode),
          :mode => mode,
        }
      end
    end
    return nil unless @@files[file_name]
    return @@files[file_name][:file]
  end
  
  #---
  
  def self.read(file_name, options = {})
    options[:mode] = ( options[:mode] ? options[:mode] : 'r' )
    file           = open(file_name, options)
    
    if file
      file.pos = 0 if options[:mode] == 'r'
      return file.read
    end
    return nil
  end
  
  #---
  
  def self.write(file_name, data, options = {})
    options[:mode] = ( options[:mode] ? options[:mode] : 'w' )
    file           = open(file_name, options)
    
    if file
{'fraction_non_alphanumeric': 0.23444570135746606, 'fraction_numerical': 0.0008484162895927602, 'mean_word_length': 1.8250798722044728, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '745574', 'n_tokens_mistral': 1135, 'n_tokens_neox': 1101, 'n_words': 224}