<commit_before># This is currently a bit of a mess.
module Tracksperanto::Pipeline
class Base
  attr_accessor :converted_points
  attr_accessor :converted_keyframes
  attr_accessor :progress_block
  
  def run(from_input_file_path, pix_w, pix_h, parser_class)
    # Read the input file
    read_data = File.read(from_input_file_path)
    
    # Assign the parser
    parser = create_parser(parser_class, pix_w, pix_h)
    
    # Setup a multiplexer
    mux = setup_outputs_for(from_input_file_path)
    
    # Setup middlewares - skip for now
    scaler = Tracksperanto::Middleware::Scaler.new(mux)
    slipper = Tracksperanto::Middleware::Slipper.new(scaler)
    golden = Tracksperanto::Middleware::Golden.new(slipper)
    reformat = Tracksperanto::Middleware::Reformat.new(golden)
    
    # Yield middlewares to the block
    yield(scaler, slipper, golden, reformat) if block_given?
    
    @converted_points, @converted_keyframes = run_export(read_data, parser, reformat) do | p, m |
      @progress_block.call(p, m) if @progress_block
    end
  end
  
  # Runs the export and returns the number of points and keyframes processed.
  # If a block is passed, the block will receive the percent complete and the last
  # status message that you can pass back to the UI
  # # :yields: percent_complete, status_message
  def run_export(tracker_data_blob, parser, processor)
    points, keyframes, percent_complete = 0, 0, 0.0
    
    yield(percent_complete, "Starting the parse routine") if block_given?
    parser.progress_block = lambda do | message |
      yield(percent_complete, message) if block_given?
    end
    
    trackers = parser.parse(tracker_data_blob)
    
    yield(percent_complete = 20.0, "Starting export for #{trackers.length} trackers") if block_given?
    
    percent_per_tracker = 80.0 / trackers.length
    
    # Use the width and height provided by the parser itself
    processor.start_export(parser.width, parser.height)
    
    yield(percent_complete, "Starting export") if block_given?
    
    trackers.each do | t |
      kf_weight = percent_per_tracker / t.keyframes.length
      points += 1
      processor.start_tracker_segment(t.name)
      t.keyframes.each do | kf |
        keyframes += 1
        processor.export_point(kf.frame, kf.abs_x, kf.abs_y, kf.residual)
        yield(percent_complete += kf_weight, "Writing keyframe") if block_given?
      end
      processor.end_tracker_segment
    end
    processor.end_export
    
    yield(100.0, "Wrote #{points} points and #{keyframes} keyframes") if block_given?
    
    [points, keyframes]
  end
  
  def create_parser(parser_class, w, h)
    p = parser_class.new
    p.width, p.height = w, h
    p
  end
  
  def setup_outputs_for(input_file_path)
    file_name = File.basename(input_file_path).gsub(/\.([^\.]+)$/, '')
    Tracksperanto::Export::Mux.new(
      Tracksperanto.exporters.map do | exporter_class |
        export_name = "%s_%s" % [file_name, exporter_class.desc_and_extension]
        export_path = File.dirname(input_file_path) + '/' + export_name
        exporter = exporter_class.new(File.open(export_path, 'w'))
        Tracksperanto::Middleware::Close.new(exporter)
      end
    )
  end
end
end<commit_msg>Make the Pipeline raise when no trackers are recovered since there is no sense to summon the rest of the machinery in that case<commit_after># This is currently a bit of a mess.
module Tracksperanto::Pipeline
class Base
  attr_accessor :converted_points
  attr_accessor :converted_keyframes
  attr_accessor :progress_block
  
  def run(from_input_file_path, pix_w, pix_h, parser_class)
    # Read the input file
    read_data = File.read(from_input_file_path)
    
    # Assign the parser
    parser = create_parser(parser_class, pix_w, pix_h)
    
    # Setup a multiplexer
    mux = setup_outputs_for(from_input_file_path)
    
    # Setup middlewares - skip for now
    scaler = Tracksperanto::Middleware::Scaler.new(mux)
    slipper = Tracksperanto::Middleware::Slipper.new(scaler)
    golden = Tracksperanto::Middleware::Golden.new(slipper)
    reformat = Tracksperanto::Middleware::Reformat.new(golden)
    
    # Yield middlewares to the block
    yield(scaler, slipper, golden, reformat) if block_given?
    
    @converted_points, @converted_keyframes = run_export(read_data, parser, reformat) do | p, m |
      @progress_block.call(p, m) if @progress_block
    end
  end
  
  # Runs the export and returns the number of points and keyframes processed.
  # If a block is passed, the block will receive the percent complete and the last
  # status message that you can pass back to the UI
  # # :yields: percent_complete, status_message
  def run_export(tracker_data_blob, parser, processor)
    points, keyframes, percent_complete = 0, 0, 0.0
    
    yield(percent_complete, "Starting the parse routine") if block_given?
    parser.progress_block = lambda do | message |
      yield(percent_complete, message) if block_given?
    end
    
    trackers = parser.parse(tracker_data_blob)
    
    raise "Could not recover any trackers from this file. Wrong import format maybe?" if trackers.empty?
    
    yield(percent_complete = 20.0, "Starting export for #{trackers.length} trackers") if block_given?
    
    percent_per_tracker = 80.0 / trackers.length
    
    # Use the width and height provided by the parser itself
    processor.start_export(parser.width, parser.height)
    
    yield(percent_complete, "Starting export") if block_given?
    
    trackers.each do | t |
      kf_weight = percent_per_tracker / t.keyframes.length
      points += 1
      processor.start_tracker_segment(t.name)
      t.keyframes.each do | kf |
        keyframes += 1
        processor.export_point(kf.frame, kf.abs_x, kf.abs_y, kf.residual)
        yield(percent_complete += kf_weight, "Writing keyframe") if block_given?
      end
      processor.end_tracker_segment
    end
    processor.end_export
    
    yield(100.0, "Wrote #{points} points and #{keyframes} keyframes") if block_given?
    
    [points, keyframes]
  end
  
  def create_parser(parser_class, w, h)
    p = parser_class.new
    p.width, p.height = w, h
    p
  end
  
  def setup_outputs_for(input_file_path)
    file_name = File.basename(input_file_path).gsub(/\.([^\.]+)$/, '')
    Tracksperanto::Export::Mux.new(
      Tracksperanto.exporters.map do | exporter_class |
        export_name = "%s_%s" % [file_name, exporter_class.desc_and_extension]
        export_path = File.dirname(input_file_path) + '/' + export_name
        exporter = exporter_class.new(File.open(export_path, 'w'))
        Tracksperanto::Middleware::Close.new(exporter)
      end
    )
  end
end
end
{'fraction_non_alphanumeric': 0.08878224355128975, 'fraction_numerical': 0.004799040191961607, 'mean_word_length': 3.4048877146631438, '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': '311834', 'n_tokens_mistral': 2212, 'n_tokens_neox': 2166, 'n_words': 541}