Skip to content

Commit

Permalink
Refactor formatting process to its own class FormatterRunner [#74]
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdstaaij committed Mar 5, 2017
1 parent 56af297 commit da0567f
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 45 deletions.
67 changes: 67 additions & 0 deletions lib/formatter_runner.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
require 'json'
require_relative '../formatters/lib/formatter_base'
require_relative 'util'

Dir[File.dirname(__FILE__) + '/../formatters/*.rb'].each do |file|
require File.expand_path(file)
end

class FormatterRunner

def initialize(dumper, progress)
# We could potentially support more dumper types in the future, but for now,
# this is the only possible option, so we can just assert on it
unless dumper.get_output_type == 'json_lines'
raise "FormatterRunner: unsupported dumper #{dumper.get_output_type}"
end

@progress = progress
end

def format(dialogs)
return if dialogs.empty?
formatters = self.class.load_formatters
formatters.reject! do |formatter|
formatter.start_backup(dialogs) == false
end
dialogs.each do |dialog|
dialog_progress = @progress[dialog['id'].to_s]
next unless dialog_progress
formatters.reject! do |formatter|
formatter.start_dialog(dialog, dialog_progress) == false
end
dumper_outfile = dialog_progress.dumper_state['outfile']
json_file = File.join(get_backup_dir, dumper_outfile)
File.open(json_file, 'r:UTF-8').each do |line|
formatters.reject! do |formatter|
msg = JSON.parse(line)
formatter.format_message(dialog, dialog_progress, msg) == false
end
end
formatters.reject! do |formatter|
formatter.end_dialog(dialog, dialog_progress) == false
end
end
formatters.each do |formatter|
formatter.end_backup(dialogs)
end
end

private

def self.load_formatters
formatter_classes = {}
FormatterBase.descendants.each do |formatter_class|
unless formatter_class::NAME.empty?
formatter_classes[formatter_class::NAME] = formatter_class
end
end
($config['formatters'] || {}).map do |name,options|
unless formatter_classes.key?(name)
raise 'Formatter "%s" is enabled but does not exist' % [name]
end
formatter_classes[name].new(options || {})
end
end

end
48 changes: 3 additions & 45 deletions telegram-history-dump.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,14 @@
require 'time'
require 'timeout'
require 'yaml'
require_relative 'formatters/lib/formatter_base'
require_relative 'lib/cli_parser'
require_relative 'lib/json_lines_dumper'
require_relative 'lib/formatter_runner'
require_relative 'lib/dump_progress'
require_relative 'lib/util'
require_relative 'lib/tg_def'
require_relative 'lib/msg_id'

Dir[File.dirname(__FILE__) + '/formatters/*.rb'].each do |file|
require File.expand_path(file)
end

cli_opts = CliParser.parse(ARGV)

def connect_socket
Expand Down Expand Up @@ -296,20 +292,6 @@ def save_progress
end
end

formatter_classes = {}
enabled_formatters = []
FormatterBase.descendants.each do |formatter_class|
unless formatter_class::NAME.empty?
formatter_classes[formatter_class::NAME] = formatter_class
end
end
($config['formatters'] || {}).each do |name,options|
unless formatter_classes.key?(name)
raise 'Formatter "%s" is enabled but does not exist' % [name]
end
enabled_formatters.push(formatter_classes[name].new(options || {}))
end

connect_socket

dialogs = exec_tg_command('dialog_list', $config['maximum_dialogs'])
Expand Down Expand Up @@ -366,32 +348,8 @@ def save_progress
end
$dumper.end_backup

unless enabled_formatters.empty?
$log.info('Formatting messages')
enabled_formatters.reject! do |formatter|
formatter.start_backup(backup_list) == false
end
backup_list.each do |dialog|
progress = $progress[dialog['id'].to_s]
next unless progress
enabled_formatters.reject! do |formatter|
formatter.start_dialog(dialog, progress) == false
end
dumper_outfile = progress.dumper_state['outfile']
json_file = File.join(get_backup_dir, dumper_outfile)
File.open(json_file, 'r:UTF-8').each do |line|
enabled_formatters.reject! do |formatter|
formatter.format_message(dialog, progress, JSON.parse(line)) == false
end
end
enabled_formatters.reject! do |formatter|
formatter.end_dialog(dialog, progress) == false
end
end
enabled_formatters.each do |formatter|
formatter.end_backup(backup_list)
end
end
$log.info('Formatting messages')
FormatterRunner.new($dumper, $progress).format(backup_list)

if cli_opts.kill_tg
connect_socket
Expand Down

0 comments on commit da0567f

Please sign in to comment.