<commit_before>module PoParser
  class Header
    attr_reader :entry, :original_configs
    attr_accessor :comments, :pot_creation_date, :po_revision_date, :project_id,
            :report_to, :last_translator, :team, :language, :charset,
            :encoding, :plural_forms

    def initialize(entry)
      @entry            = entry
      @comments         = entry.translator_comment.to_s
      @original_configs = convert_msgstr_to_hash(entry.msgstr)

      HEADER_LABELS.each do |k, v|
        instance_variable_set "@#{k.to_s}".to_sym, @original_configs[v]
      end
    end

    def configs
      hash = {}
      HEADER_LABELS.each do |k, v|
        hash[v] = instance_variable_get "@#{k}".to_sym
      end
      @original_configs.merge(hash)
    end

    def to_h
      @entry.to_h
    end

    def to_s
      string = []
      @comments.each do |comment|
        string << "# #{comment}".strip
      end
      string << "msgid \"\"\nmsgstr \"\""
      configs.each do |k, v|
        string << "#{k}: #{v}\n".dump
      end
      string.join("\n")
    end

  private
    def convert_msgstr_to_hash(msgstr)
      options_array = msgstr.to_s.map do |options|
        options.split(':', 2).each do |k|
          k.strip!
          k.chomp!
          k.gsub!(/\\+n$/, '')
        end
      end
      merge_to_previous_string(options_array).to_h<commit_msg>uses Hash instead of to_h for compatibility with ruby 1.9<commit_after>module PoParser
  class Header
    attr_reader :entry, :original_configs
    attr_accessor :comments, :pot_creation_date, :po_revision_date, :project_id,
            :report_to, :last_translator, :team, :language, :charset,
            :encoding, :plural_forms

    def initialize(entry)
      @entry            = entry
      @comments         = entry.translator_comment.to_s
      @original_configs = convert_msgstr_to_hash(entry.msgstr)

      HEADER_LABELS.each do |k, v|
        instance_variable_set "@#{k.to_s}".to_sym, @original_configs[v]
      end
    end

    def configs
      hash = {}
      HEADER_LABELS.each do |k, v|
        hash[v] = instance_variable_get "@#{k}".to_sym
      end
      @original_configs.merge(hash)
    end

    def to_h
      @entry.to_h
    end

    def to_s
      string = []
      @comments.each do |comment|
        string << "# #{comment}".strip
      end
      string << "msgid \"\"\nmsgstr \"\""
      configs.each do |k, v|
        string << "#{k}: #{v}\n".dump
      end
      string.join("\n")
    end

  private
    def convert_msgstr_to_hash(msgstr)
      options_array = msgstr.to_s.map do |options|
        options.split(':', 2).each do |k|
          k.strip!
          k.chomp!
          k.gsub!(/\\+n$/, '')
        end
      end
      Hash[merge_to_previous_string(options_array)]
{'fraction_non_alphanumeric': 0.12233079985522982, 'fraction_numerical': 0.0014477017734346724, 'mean_word_length': 2.140909090909091, 'pattern_counts': {'":': 0, '<': 15, '<?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': '476405', 'n_tokens_mistral': 955, 'n_tokens_neox': 926, 'n_words': 192}