<commit_before>module DarwinCore
  class Archive

    def self.generate(opts = {})
      new(opts).generate
    end

    def initialize(opts = {})
      @opts = opts
      @opts[:path] ||= "dwca.zip"
      @opts[:core] ||= "occurrence"
      @opts[:metadata] ||= File.join(Rails.root, "app", "views", "observations", "gbif.eml.erb")
      @opts[:descriptor] ||= File.join(Rails.root, "app", "views", "observations", "gbif.descriptor.builder")
      @opts[:quality] ||= @opts[:quality_grade] || "research"
      @opts[:photo_licenses] ||= ["CC0", "CC-BY", "CC-BY-NC", "CC-BY-SA", "CC-BY-ND", "CC-BY-NC-SA", "CC-BY-NC-ND"]
      @opts[:licenses] ||= ["any"]
      @opts[:licenses] = @opts[:licenses].first if @opts[:licenses].size == 1
      @opts[:private_coordinates] ||= false
      @logger = @opts[:logger] || Rails.logger
      @logger.level = Logger::DEBUG if @opts[:debug]

      # Make a unique dir to put our files
      @work_path = Dir.mktmpdir
      FileUtils.mkdir_p @work_path, :mode => 0755

      @place = Place.find_by_id(@opts[:place].to_i) || Place.find_by_name(@opts[:place])
      logger.debug "Found place: #{@place}"
      @taxon = if @opts[:taxon].is_a?(::Taxon)
        @opts[:taxon]
      else
        ::Taxon.find_by_id(@opts[:taxon].to_i) || ::Taxon.active.find_by_name(@opts[:taxon])
      end
      logger.debug "Found taxon: #{@taxon}"
      @project = if @opts[:project].is_a?(::Project)
        @opts[:project]
      else
        ::Project.find( @opts[:project] ) rescue nil
      end
      logger.debug "Found project: #{@project}"
      logger.debug "Photo licenses: #{@opts[:photo_licenses].inspect}"
    end

    def logger
      @logger || Rails.logger
    end

    def generate
      @generate_started_at = Time.now
      logger.debug "[DEBUG] Generating archive, options: #{@opts}"
      unless @opts[:metadata].to_s.downcase == "skip"
        metadata_path = make_metadata
        logger.debug "Metadata: #{metadata_path}"
      end
      descriptor_path = make_descriptor
      logger.debug "Descriptor: #{descriptor_path}"
      data_paths = make_data
      logger.debug "Data: #{data_paths.inspect}"
      paths = [metadata_path, descriptor_path, data_paths].flatten.compact
      archive_path = make_archive(*paths)
      logger.debug "Archive: #{archive_path}"
      FileUtils.mv(archive_path, @opts[:path])
      logger.info "Archive generated: #{@opts[:path]}"
      if @benchmarks
        logger.info %w(BENCHMARK TOTAL AVG).map{|h| h.ljust( 30 )}.join( " " )
        @benchmarks.each do |key, times|
          logger.info [key, times.sum.round(5), (times.sum.to_f / times.size).round(5)].map{|h| h.to_s.ljust( 30 )}.join( " " )
        end
      end
      if @opts[:additional_with_taxa_path]
        logger.info "Making taxa extension..."
        paths << make_api_all_taxon_data
        archive_path = make_archive(*paths)
        logger.debug "Moving #{archive_path} to #{@opts[:additional_with_taxa_path]}"
        FileUtils.mv(archive_path, @opts[:additional_with_taxa_path])

        # Make a POST request to an endpoint indicating the archive with taxon data is updated
        if @opts[:post_taxon_archive_to_url] && @opts[:post_taxon_archive_as_url]
          options = {
           body: {
              name: "iNaturalist",
              url: @opts[:post_taxon_archive_as_url]
           }.to_json,
           headers: {
             "Content-Type" => "application/json"
           }
          }
          logger.debug "Posting #{req.body} to #{@opts[:post_taxon_archive_to_url]}"
          response = HTTParty.post( @opts[:post_taxon_archive_to_url], options )
        end
      end
      @opts[:path]
    end

    def make_metadata
      m = DarwinCore::Metadata.new(@opts.merge(uri: FakeView.observations_url(observations_params)))
      tmp_path = File.join(@work_path, "metadata.eml.xml")
      open(tmp_path, 'w') do |f|
        f << m.render(:file => @opts[:metadata])
      end
      tmp_path
    end

    def make_descriptor
      extensions = []
      if @opts[:extensions]
        @opts[:extensions].each do |e|
          case e<commit_msg>fixing debug message in archive.rb<commit_after>module DarwinCore
  class Archive

    def self.generate(opts = {})
      new(opts).generate
    end

    def initialize(opts = {})
      @opts = opts
      @opts[:path] ||= "dwca.zip"
      @opts[:core] ||= "occurrence"
      @opts[:metadata] ||= File.join(Rails.root, "app", "views", "observations", "gbif.eml.erb")
      @opts[:descriptor] ||= File.join(Rails.root, "app", "views", "observations", "gbif.descriptor.builder")
      @opts[:quality] ||= @opts[:quality_grade] || "research"
      @opts[:photo_licenses] ||= ["CC0", "CC-BY", "CC-BY-NC", "CC-BY-SA", "CC-BY-ND", "CC-BY-NC-SA", "CC-BY-NC-ND"]
      @opts[:licenses] ||= ["any"]
      @opts[:licenses] = @opts[:licenses].first if @opts[:licenses].size == 1
      @opts[:private_coordinates] ||= false
      @logger = @opts[:logger] || Rails.logger
      @logger.level = Logger::DEBUG if @opts[:debug]

      # Make a unique dir to put our files
      @work_path = Dir.mktmpdir
      FileUtils.mkdir_p @work_path, :mode => 0755

      @place = Place.find_by_id(@opts[:place].to_i) || Place.find_by_name(@opts[:place])
      logger.debug "Found place: #{@place}"
      @taxon = if @opts[:taxon].is_a?(::Taxon)
        @opts[:taxon]
      else
        ::Taxon.find_by_id(@opts[:taxon].to_i) || ::Taxon.active.find_by_name(@opts[:taxon])
      end
      logger.debug "Found taxon: #{@taxon}"
      @project = if @opts[:project].is_a?(::Project)
        @opts[:project]
      else
        ::Project.find( @opts[:project] ) rescue nil
      end
      logger.debug "Found project: #{@project}"
      logger.debug "Photo licenses: #{@opts[:photo_licenses].inspect}"
    end

    def logger
      @logger || Rails.logger
    end

    def generate
      @generate_started_at = Time.now
      logger.debug "[DEBUG] Generating archive, options: #{@opts}"
      unless @opts[:metadata].to_s.downcase == "skip"
        metadata_path = make_metadata
        logger.debug "Metadata: #{metadata_path}"
      end
      descriptor_path = make_descriptor
      logger.debug "Descriptor: #{descriptor_path}"
      data_paths = make_data
      logger.debug "Data: #{data_paths.inspect}"
      paths = [metadata_path, descriptor_path, data_paths].flatten.compact
      archive_path = make_archive(*paths)
      logger.debug "Archive: #{archive_path}"
      FileUtils.mv(archive_path, @opts[:path])
      logger.info "Archive generated: #{@opts[:path]}"
      if @benchmarks
        logger.info %w(BENCHMARK TOTAL AVG).map{|h| h.ljust( 30 )}.join( " " )
        @benchmarks.each do |key, times|
          logger.info [key, times.sum.round(5), (times.sum.to_f / times.size).round(5)].map{|h| h.to_s.ljust( 30 )}.join( " " )
        end
      end
      if @opts[:additional_with_taxa_path]
        logger.info "Making taxa extension..."
        paths << make_api_all_taxon_data
        archive_path = make_archive(*paths)
        logger.debug "Moving #{archive_path} to #{@opts[:additional_with_taxa_path]}"
        FileUtils.mv(archive_path, @opts[:additional_with_taxa_path])

        # Make a POST request to an endpoint indicating the archive with taxon data is updated
        if @opts[:post_taxon_archive_to_url] && @opts[:post_taxon_archive_as_url]
          options = {
           body: {
              name: "iNaturalist",
              url: @opts[:post_taxon_archive_as_url]
           }.to_json,
           headers: {
             "Content-Type" => "application/json"
           }
          }
          logger.debug "Posting #{options.body} to #{@opts[:post_taxon_archive_to_url]}"
          response = HTTParty.post( @opts[:post_taxon_archive_to_url], options )
        end
      end
      @opts[:path]
    end

    def make_metadata
      m = DarwinCore::Metadata.new(@opts.merge(uri: FakeView.observations_url(observations_params)))
      tmp_path = File.join(@work_path, "metadata.eml.xml")
      open(tmp_path, 'w') do |f|
        f << m.render(:file => @opts[:metadata])
      end
      tmp_path
    end

    def make_descriptor
      extensions = []
      if @opts[:extensions]
        @opts[:extensions].each do |e|
          case e
{'fraction_non_alphanumeric': 0.1531837477258945, 'fraction_numerical': 0.0029108550636749546, 'mean_word_length': 2.765296803652968, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '7360498', 'n_tokens_mistral': 2740, 'n_tokens_neox': 2665, 'n_words': 533}