<commit_before>module RailsUploads
  module Magick
    class Image

      def initialize(source, output=nil)
        @source = source
        @output = output
      end

      def convert(args)
        tokens = [convert? ? 'convert' : 'mogrify']
        file_to_tokens tokens
        args_to_tokens args, tokens        
        tokens << @output if convert?
        success, output = run(tokens)
        if block_given? 
          yield success, output
        else
          success
        end
      end

      def identify(args)
        tokens = ['identify']
        args_to_tokens args, tokens
        file_to_tokens tokens
        success, output = run(tokens)
        if block_given?
          yield success, output
        else
          success
        end
      end

      def width
        dimensions[0]
      end

      def height
        dimensions[1]
      end

      def resize_to_fill(max_width, max_height)
        width, height = dimensions
        scale = [max_width/width.to_f, max_height/height.to_f].max
        convert :resize => "#{scale*width}x#{scale*height}", :gravity => 'center', :crop => "#{max_width}x#{max_height}" 
      end

      def resize_to_fit(max_width, max_height)
        width, height = dimensions
        ratios = [max_width/width.to_f, max_height/height.to_f]
        if max_width < width or max_height < height
          scale = ratios.max
        else
          scale = ratios.min
        end
        convert :resize => "#{scale*width}x#{scale*height}", :gravity => 'center'
      end

      protected

      def convert?
        not (@output.nil? and ::File.exists?(@output))
      end

      def dimensions
        identify(:format => '%wx%h') do |success, output|
          success ? output.chomp.split('x').map(&:to_i) : 0
        end
      end
  
      def file_to_tokens(tokens)
        tokens << (convert? ? @source : @output)
      end
    
      def args_to_tokens(args, tokens)
        args.each { |k, v| tokens << "-#{k} #{v}" }
      end

      def run(tokens)
        cmd = tokens.join(' ')
        puts cmd.inspect
        output = IO.popen(cmd) { |o| o.read }
        success = $?.exitstatus == 0
        [success, output]
      end 

    end
  end
end
<commit_msg>Remove puts<commit_after>module RailsUploads
  module Magick
    class Image

      def initialize(source, output=nil)
        @source = source
        @output = output
      end

      def convert(args)
        tokens = [convert? ? 'convert' : 'mogrify']
        file_to_tokens tokens
        args_to_tokens args, tokens        
        tokens << @output if convert?
        success, output = run(tokens)
        if block_given? 
          yield success, output
        else
          success
        end
      end

      def identify(args)
        tokens = ['identify']
        args_to_tokens args, tokens
        file_to_tokens tokens
        success, output = run(tokens)
        if block_given?
          yield success, output
        else
          success
        end
      end

      def width
        dimensions[0]
      end

      def height
        dimensions[1]
      end

      def resize_to_fill(max_width, max_height)
        width, height = dimensions
        scale = [max_width/width.to_f, max_height/height.to_f].max
        convert :resize => "#{scale*width}x#{scale*height}", :gravity => 'center', :crop => "#{max_width}x#{max_height}" 
      end

      def resize_to_fit(max_width, max_height)
        width, height = dimensions
        ratios = [max_width/width.to_f, max_height/height.to_f]
        if max_width < width or max_height < height
          scale = ratios.max
        else
          scale = ratios.min
        end
        convert :resize => "#{scale*width}x#{scale*height}", :gravity => 'center'
      end

      protected

      def convert?
        not (@output.nil? and ::File.exists?(@output))
      end

      def dimensions
        identify(:format => '%wx%h') do |success, output|
          success ? output.chomp.split('x').map(&:to_i) : 0
        end
      end
  
      def file_to_tokens(tokens)
        tokens << (convert? ? @source : @output)
      end
    
      def args_to_tokens(args, tokens)
        args.each { |k, v| tokens << "-#{k} #{v}" }
      end

      def run(tokens)
        cmd = tokens.join(' ')
        output = IO.popen(cmd) { |o| o.read }
        success = $?.exitstatus == 0
        [success, output]
      end 

    end
  end
end

{'fraction_non_alphanumeric': 0.10418079096045198, 'fraction_numerical': 0.001807909604519774, 'mean_word_length': 1.7889098928796472, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 15, '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': '7466070', 'n_tokens_mistral': 1347, 'n_tokens_neox': 1307, 'n_words': 339}