<commit_before>module ODFReport

class Chart
  include Nested

  attr_accessor :name, :collection, :series, :title, :type, :legend, :labels, :file, :id, :target, :excel

  @@id_target = {}
  @@name_id = {}

  def initialize(opts)
    @name       = opts[:name]
    @collection = opts[:collection]

    @series     = opts[:series] || nil
    @type       = opts[:type]   || nil
    @colors     = opts[:colors] || nil

    @title      = opts[:title]  || :default
    @legend     = opts[:legend] || :default
    @labels     = opts[:labels] || :default
    @x_axis     = opts[:x_axis] || :default
    @y_axis     = opts[:y_axis] || :default
    @f_size     = opts[:f_size] || :default
    @g_line     = opts[:g_line] || :default

    @file       = opts[:file]   || nil
    @id         = ''
    @target     = ''
    @excel      = ''
  end

  def replace!(doc, filename, row = nil)

    if /_rels\/document/ === filename # Look into _rels/document.xml.rels

      doc.xpath("//xmlns:Relationship").each do |rel|
        @@id_target[rel.attr('Id')] = rel.attr('Target')
      end

    elsif /_rels\/chart/ === filename # Look into _rels/chart.xml.rels

      chart_name = @name
      chart_id = @@name_id[chart_name]
      chart_target = @@id_target[chart_id]
      return if chart_target.nil?
      chart_target = chart_target.split("\/").last

      if /#{Regexp.quote(chart_target)}/ === filename

        target = doc.xpath("//xmlns:Relationship").first['Target']
        current_path = "word" + target[2..-1]

        excel_file_data = @file.read_file(current_path)
        tmp_filename = "temp_#{current_path.split('/').last}"
        ::File.open(tmp_filename, "wb") {|f| f.write(excel_file_data) } # TODO fix the temporary file to actually reference a temporary file path in the tmp folder


        report = ODFReport::Report.new(tmp_filename) do |r|
           r.add_spreadsheet(@name, @collection, :series => @series, :title => @title, :type => @type)
        end

        replacement_path = current_path.split('/').last

        report.generate(replacement_path)

      end

    elsif /word\/document/ === filename # Look into word/document.xml

      id = doc.xpath("//w:drawing//wp:docPr[@title='#{@name}']/following-sibling::*").xpath(".//c:chart", {'c' => "http://schemas.openxmlformats.org/drawingml/2006/chart"})
      @@name_id[@name] = id.attr('id').value unless id.empty?

    elsif /charts\/chart/ === filename # Look through chart.xml

      return if @@id_target[@@name_id[@name]].nil?
      if filename.include? @@id_target[@@name_id[@name]]

        # function - work in progress
        # fix_invalid_values

        determine_type(doc)

        case @type

        when 'pie', 'doughnut' # For Pie/Doughnut Charts

          @series = [@series] unless @series.is_a? Array
          @collection.each { |k, v| @collection[k] = [v] } unless @collection.values.first.is_a? Array

          add_series(doc, @type)

          add_color(doc)

          add_data(doc)
<commit_msg>delete chart with no data<commit_after>module ODFReport

class Chart
  include Nested

  attr_accessor :name, :collection, :series, :title, :type, :legend, :labels, :file, :id, :target, :excel

  @@id_target = {}
  @@name_id = {}

  def initialize(opts)
    @name       = opts[:name]
    @collection = opts[:collection]

    @series     = opts[:series] || nil
    @type       = opts[:type]   || nil
    @colors     = opts[:colors] || nil

    @title      = opts[:title]  || :default
    @legend     = opts[:legend] || :default
    @labels     = opts[:labels] || :default
    @x_axis     = opts[:x_axis] || :default
    @y_axis     = opts[:y_axis] || :default
    @f_size     = opts[:f_size] || :default
    @g_line     = opts[:g_line] || :default

    @file       = opts[:file]   || nil
    @id         = ''
    @target     = ''
    @excel      = ''
  end

  def replace!(doc, filename, row = nil)

    if /_rels\/document/ === filename # Look into _rels/document.xml.rels

      doc.xpath("//xmlns:Relationship").each do |rel|
        @@id_target[rel.attr('Id')] = rel.attr('Target')
      end

    elsif /_rels\/chart/ === filename # Look into _rels/chart.xml.rels

      chart_name = @name
      chart_id = @@name_id[chart_name]
      chart_target = @@id_target[chart_id]
      return if chart_target.nil?
      chart_target = chart_target.split("\/").last

      if /#{Regexp.quote(chart_target)}/ === filename

        target = doc.xpath("//xmlns:Relationship").first['Target']
        current_path = "word" + target[2..-1]

        excel_file_data = @file.read_file(current_path)
        tmp_filename = "temp_#{current_path.split('/').last}"
        ::File.open(tmp_filename, "wb") {|f| f.write(excel_file_data) } # TODO fix the temporary file to actually reference a temporary file path in the tmp folder


        report = ODFReport::Report.new(tmp_filename) do |r|
           r.add_spreadsheet(@name, @collection, :series => @series, :title => @title, :type => @type)
        end

        replacement_path = current_path.split('/').last

        report.generate(replacement_path)

      end

    elsif /word\/document/ === filename # Look into word/document.xml

      id = doc.xpath("//w:drawing//wp:docPr[@title='#{@name}']/following-sibling::*").xpath(".//c:chart", {'c' => "http://schemas.openxmlformats.org/drawingml/2006/chart"})
      @@name_id[@name] = id.attr('id').value unless id.empty?
      doc.xpath("//w:drawing//wp:docPr[@title='#{@name}']/ancestor::*[w:drawing]").remove if @collection.values.flatten.map {|v| v.abs}.sum.zero?

    elsif /charts\/chart/ === filename # Look through chart.xml

      return if @@id_target[@@name_id[@name]].nil?
      if filename.include? @@id_target[@@name_id[@name]]

        # function - work in progress
        # fix_invalid_values

        determine_type(doc)

        case @type

        when 'pie', 'doughnut' # For Pie/Doughnut Charts

          @series = [@series] unless @series.is_a? Array
          @collection.each { |k, v| @collection[k] = [v] } unless @collection.values.first.is_a? Array

          add_series(doc, @type)

          add_color(doc)

          add_data(doc)

{'fraction_non_alphanumeric': 0.16373411534701857, 'fraction_numerical': 0.0019550342130987292, 'mean_word_length': 2.6563430613460395, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 14}, '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': '2096353', 'n_tokens_mistral': 2110, 'n_tokens_neox': 2010, 'n_words': 415}