<commit_before># Contains the BasicParser class.
# For its subclasses, see html_log_parser.rb and text_log_parser.rb.
# The subclasses parse the file passed into it and return a LogFile object.
# The BasicParser class just provides some common functionality.
#
# Please use Pidgin2Adium.parse or Pidgin2Adium.parse_and_generate instead of
# using these classes directly.

require 'pidgin2adium/log_file'
require 'pidgin2adium/messages/all'

module Pidgin2Adium
  # BasicParser is a base class. Its subclasses are TextLogParser and
  # HtmlLogParser.
  class BasicParser
    # "2007-04-17 12:33:13" => %w(2007 04 17)
    TIME_REGEX = /^(\d{4})-(\d{2})-(\d{2}) \d{2}:\d{2}:\d{2}$/

    def initialize(src_path, user_aliases)
      @src_path = src_path
      # Whitespace is removed for easy matching later on.
      @user_aliases = user_aliases.split(',').map{|x| x.downcase.gsub(/\s+/,'') }.uniq

      # @user_alias is set each time get_sender_by_alias is called. It is a non-normalized
      # alias.
      # Set an initial value just in case the first message doesn't give
      # us an alias.
      @user_alias = user_aliases.split(',')[0]
    end

    # This method returns a LogFile instance, or false if an error occurred.
    def parse
      if pre_parse
        cleaned_file_content = cleanup(@file_content).split("\n")

        messages = cleaned_file_content.map do |line|
          # "next" returns nil which is removed by compact
          next if line =~ /^\s+$/
          if line =~ @line_regex
            create_message($~.captures)
          elsif line =~ @line_regex_status
            message = create_status_or_event_message($~.captures)
            if message == false
              # Error occurred while parsing
              return false
            end
          end
        end.compact
        LogFile.new(messages)
      end
    end

    # Extract required data from the file. Run by parse. Sets these
    # variables:
    # * @sender_screen_name
    # * @receiver_screen_name
    # * @basic_time_info
    # Returns true if none of these variables are false or nil.
    def pre_parse
      read_source_file
      metadata = Metadata.new(MetadataParser.new(@first_line).parse)
      if metadata.valid?
        @sender_screen_name = metadata.sender_screen_name
        @receiver_screen_name = metadata.receiver_screen_name
        start_time = metadata.start_time
        @basic_time_info = {:year => start_time.year,
                            :month => start_time.mon,
                            :day => start_time.mday}
      end
    end

    def get_sender_by_alias(alias_name)
      no_action = alias_name.sub(/^\*{3}/, '')
      if @user_aliases.include?(no_action.downcase.gsub(/\s+/, ''))
        # Set the current alias being used of the ones in @user_aliases
        @user_alias = no_action
        @sender_screen_name
      else
        @receiver_screen_name
      end
    end

    #--
    # create_message takes an array of captures from matching against
    # @line_regex and returns a Message object or one of its subclasses.
    # It can be used for TextLogParser and HtmlLogParser because both of
    # they return data in the same indexes in the matches array.
    #++
    def create_message(matches)
      # Either a regular message line or an auto-reply/away message.
      time = create_adium_time(matches[0])
      return nil if time.nil?
      buddy_alias = matches[1]
      sender = get_sender_by_alias(buddy_alias)
      body = matches[3]
      if matches[2] # auto-reply
        AutoReplyMessage.new(sender, time, buddy_alias, body)
      else
        # normal message
        XMLMessage.new(sender, time, buddy_alias, body)
      end
    end

    #--<commit_msg>Clearer control flow. Remove unused error checking.<commit_after># Contains the BasicParser class.
# For its subclasses, see html_log_parser.rb and text_log_parser.rb.
# The subclasses parse the file passed into it and return a LogFile object.
# The BasicParser class just provides some common functionality.
#
# Please use Pidgin2Adium.parse or Pidgin2Adium.parse_and_generate instead of
# using these classes directly.

require 'pidgin2adium/log_file'
require 'pidgin2adium/messages/all'

module Pidgin2Adium
  # BasicParser is a base class. Its subclasses are TextLogParser and
  # HtmlLogParser.
  class BasicParser
    # "2007-04-17 12:33:13" => %w(2007 04 17)
    TIME_REGEX = /^(\d{4})-(\d{2})-(\d{2}) \d{2}:\d{2}:\d{2}$/

    def initialize(src_path, user_aliases)
      @src_path = src_path
      # Whitespace is removed for easy matching later on.
      @user_aliases = user_aliases.split(',').map{|x| x.downcase.gsub(/\s+/,'') }.uniq

      # @user_alias is set each time get_sender_by_alias is called. It is a non-normalized
      # alias.
      # Set an initial value just in case the first message doesn't give
      # us an alias.
      @user_alias = user_aliases.split(',')[0]
    end

    # This method returns a LogFile instance, or false if an error occurred.
    def parse
      if pre_parse
        cleaned_file_content = cleanup(@file_content).split("\n")
        cleaned_file_content.reject! { |line| line.strip.empty? }

        messages = cleaned_file_content.map do |line|
          if line =~ @line_regex
            create_message($~.captures)
          elsif line =~ @line_regex_status
            message = create_status_or_event_message($~.captures)
          end
        end
        LogFile.new(messages)
      end
    end

    # Extract required data from the file. Run by parse. Sets these
    # variables:
    # * @sender_screen_name
    # * @receiver_screen_name
    # * @basic_time_info
    # Returns true if none of these variables are false or nil.
    def pre_parse
      read_source_file
      metadata = Metadata.new(MetadataParser.new(@first_line).parse)
      if metadata.valid?
        @sender_screen_name = metadata.sender_screen_name
        @receiver_screen_name = metadata.receiver_screen_name
        start_time = metadata.start_time
        @basic_time_info = {:year => start_time.year,
                            :month => start_time.mon,
                            :day => start_time.mday}
      end
    end

    def get_sender_by_alias(alias_name)
      no_action = alias_name.sub(/^\*{3}/, '')
      if @user_aliases.include?(no_action.downcase.gsub(/\s+/, ''))
        # Set the current alias being used of the ones in @user_aliases
        @user_alias = no_action
        @sender_screen_name
      else
        @receiver_screen_name
      end
    end

    #--
    # create_message takes an array of captures from matching against
    # @line_regex and returns a Message object or one of its subclasses.
    # It can be used for TextLogParser and HtmlLogParser because both of
    # they return data in the same indexes in the matches array.
    #++
    def create_message(matches)
      # Either a regular message line or an auto-reply/away message.
      time = create_adium_time(matches[0])
      if time
        buddy_alias = matches[1]
        sender = get_sender_by_alias(buddy_alias)
        body = matches[3]
        if matches[2] # auto-reply
          AutoReplyMessage.new(sender, time, buddy_alias, body)
        else
          # normal message
          XMLMessage.new(sender, time, buddy_alias, body)
        end
      end
    end

    #--
{'fraction_non_alphanumeric': 0.08819112627986347, 'fraction_numerical': 0.010648464163822525, 'mean_word_length': 2.530602409638554, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '3092818', 'n_tokens_mistral': 2276, 'n_tokens_neox': 2199, 'n_words': 646}