<commit_before>require 'micro_magick/version'
require 'micro_magick/identify_parser'
require 'micro_magick/image'
require 'open3'

module MicroMagick

  Error = Class.new(StandardError)
  NoSuchFile = Class.new(Error)
  MissingBinaries = Class.new(Error)
  ArgumentError = Class.new(Error)
  CorruptImageError = Class.new(Error)

  def self.use_imagemagick
    @cmd_prefix = ''
  end

  def self.use_graphicsmagick
    @cmd_prefix = 'gm '
  end

  def self.reset!
    @cmd_prefix = nil
  end

  def self.cmd_prefix
    @cmd_prefix ||= begin
      if system('hash gm 2>&-')
        'gm '
      elsif system('hash convert 2>&-')
        ''
      else
        raise MissingBinaries, 'Please install either GraphicsMagick or ImageMagick'
      end
    end
  end

  def self.exec(cmds, return_stdout = false)
    final_cmd = cmd_prefix + cmds.join(' ')
    stdout = Open3.popen3(final_cmd) do |stdin, stdout, stderr|
      err = stderr.read.strip
      if err.size > 0
        error = if err =~ /unrecognized option/i
          ArgumentError
        elsif err =~ /corrupt/i
          CorruptImageError
        elsif err =~ /no such file or directory/i
          NoSuchFile
        else
          Error
        end
        raise error, "#{final_cmd} failed: #{err}"
      end
      stdout.read.strip
    end
    return_stdout ? stdout : final_cmd
  rescue Errno::ENOENT => e
    raise NoSuchFile, e.message
  end
end

<commit_msg>unable to open is NoSuchFile<commit_after>require 'micro_magick/version'
require 'micro_magick/identify_parser'
require 'micro_magick/image'
require 'open3'

module MicroMagick

  Error = Class.new(StandardError)
  NoSuchFile = Class.new(Error)
  MissingBinaries = Class.new(Error)
  ArgumentError = Class.new(Error)
  CorruptImageError = Class.new(Error)

  def self.use_imagemagick
    @cmd_prefix = ''
  end

  def self.use_graphicsmagick
    @cmd_prefix = 'gm '
  end

  def self.reset!
    @cmd_prefix = nil
  end

  def self.cmd_prefix
    @cmd_prefix ||= begin
      if system('hash gm 2>&-')
        'gm '
      elsif system('hash convert 2>&-')
        ''
      else
        raise MissingBinaries, 'Please install either GraphicsMagick or ImageMagick'
      end
    end
  end

  def self.exec(cmds, return_stdout = false)
    final_cmd = cmd_prefix + cmds.join(' ')
    stdout = Open3.popen3(final_cmd) do |stdin, stdout, stderr|
      err = stderr.read.strip
      if err.size > 0
        error = if err =~ /unrecognized option/i
          ArgumentError
        elsif err =~ /corrupt/i
          CorruptImageError
        elsif err =~ /no such file or directory|unable to open/i
          NoSuchFile
        else
          Error
        end
        raise error, "#{final_cmd} failed: #{err}"
      end
      stdout.read.strip
    end
    return_stdout ? stdout : final_cmd
  rescue Errno::ENOENT => e
    raise NoSuchFile, e.message
  end
end


{'fraction_non_alphanumeric': 0.0920778318276581, 'fraction_numerical': 0.004169562195969423, 'mean_word_length': 2.456182472989196, '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': '7348770', 'n_tokens_mistral': 979, 'n_tokens_neox': 939, 'n_words': 240}