Class: Pidgin2Adium::LogConverter

Inherits:
Object
  • Object
show all
Includes:
Pidgin2Adium
Defined in:
lib/pidgin2adium/log_converter.rb

Overview

An easy way to batch-process a directory. Used by the pidgin2adium command-line script.

Constant Summary

Constants included from Pidgin2Adium

ADIUM_LOG_DIR, BAD_DIRS, FILE_EXISTS, VERSION

Instance Method Summary collapse

Methods included from Pidgin2Adium

balance_tags_c, delete_search_indexes, error, log_msg, oops, parse, parse_and_generate

Constructor Details

#initialize(pidgin_log_dir, aliases, opts = {}) ⇒ LogConverter

You can add options using the opts hash, which can have the following keys, all of which are optional:

  • overwrite: If true, then overwrite even if log is found. Defaults to false.

  • output_dir: The top-level dir to put the logs in. Logs under output_dir are still each in their own folders, etc. Defaults to Pidgin2Adium::ADIUM_LOG_DIR



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/pidgin2adium/log_converter.rb', line 15

def initialize(pidgin_log_dir, aliases, opts = {})
  # parse_and_generate will process it for us
  @opts = opts

  @pidgin_log_dir = File.expand_path(pidgin_log_dir)
  @my_aliases = aliases

  unless File.directory?(@pidgin_log_dir)
    msg = "Source directory #{@pidgin_log_dir} does not exist or is not a directory."
    error(msg)

    # ENOENT automatically prepends "No such file or directory - " to
    # its initializer's arguments
    raise Errno::ENOENT.new("source directory #{@pidgin_log_dir}")
  end
end

Instance Method Details

#get_all_chat_filesObject



66
67
68
69
# File 'lib/pidgin2adium/log_converter.rb', line 66

def get_all_chat_files
  # recurse into each subdir
  Dir.glob("#{@pidgin_log_dir}/**/*.{htm,html,txt}") - BAD_DIRS
end

#startObject

Runs Pidgin2Adium::parse_and_generate on every log file in directory provided in new, then deletes Adium’s search indexes to force it to rescan logs on startup.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/pidgin2adium/log_converter.rb', line 35

def start
  log_msg "Begin converting."
  begin
    files_path = get_all_chat_files()
  rescue Errno::EACCES => bang
    error("Sorry, permission denied for getting Pidgin chat files from #{@pidgin_log_dir}.")
    error("Details: #{bang.message}")
    raise bang
  end

  total_files = files_path.size
  total_successes = 0
  log_msg("#{total_files} files to convert.")
  files_path.each_with_index do |fname, i|
    log_msg(
      sprintf("[%d/%d] Converting %s...",
              (i+1), total_files, fname)
    )
    result = parse_and_generate(fname, @my_aliases, @opts)
    total_successes += 1 if result == true
  end

  delete_search_indexes()

  Pidgin2Adium.log_msg "Finished converting! Converted #{total_successes} files of #{total_files} total."
  puts "Minor error messages:"
  puts @@oops_messages.join("\n")
  puts "Major error messages:"
  puts @@error_messages.join("\n")
end