Class: Imagesorter::FileBatchProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/imagesorter/file_batch_processor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source:, processor:, categorizer: nil, progress_proc: nil, threads: 1, extensions: nil, recursive: false) ⇒ FileBatchProcessor

Returns a new instance of FileBatchProcessor.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/imagesorter/file_batch_processor.rb', line 7

def initialize(source:,
               processor:,
               categorizer: nil,
               progress_proc: nil,
               threads: 1,
               extensions: nil,
               recursive: false)
  @dir           = source
  @categorizer   = categorizer || Categorizers::ChainedCategorizer.new(Categorizers::FileExifCategorizer.new,
                                                                       Categorizers::FileStatCategorizer.new(:ctime))
  @processor     = processor
  @progress_proc = progress_proc
  @queue         = Queue.new
  @files         = []
  @threads       = threads
  @recursive     = recursive
  @extensions    = Array(extensions).map(&:upcase)
  @total_steps   = nil

  @skipped_files = 0
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



5
6
7
# File 'lib/imagesorter/file_batch_processor.rb', line 5

def files
  @files
end

Instance Method Details

#collect!Object



43
44
45
46
47
# File 'lib/imagesorter/file_batch_processor.rb', line 43

def collect!
  @files = []

  collect_files_from_dir(@dir)
end

#collect_file_from_dir(dir, file) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/imagesorter/file_batch_processor.rb', line 55

def collect_file_from_dir(dir, file)
  return if file =~ /^\.\.?$/
  full_path = File.join(dir, file)

  if File.directory?(full_path)
    collect_files_from_dir(full_path) if @recursive
    return
  end

  if include_file?(full_path)
    @files << SortableFile.new(full_path)
  else
    @skipped_files += 1
  end

  increment('Collecting files')
end

#collect_files_from_dir(dir) ⇒ Object



49
50
51
52
53
# File 'lib/imagesorter/file_batch_processor.rb', line 49

def collect_files_from_dir(dir)
  Dir.foreach(dir) do |file|
    collect_file_from_dir(dir, file)
  end
end

#execute!Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/imagesorter/file_batch_processor.rb', line 29

def execute!
  collect!

  @total_steps = @skipped_files + @files.size * 3

  process!

  if @threads > 1
    start_queue_workers
  else
    work_on_queue
  end
end

#include_file?(file) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
# File 'lib/imagesorter/file_batch_processor.rb', line 73

def include_file?(file)
  File.file?(file) &&
    (@extensions.empty? || @extensions.include?(File.extname(file).delete('.').upcase))
end

#increment(step) ⇒ Object



103
104
105
106
107
# File 'lib/imagesorter/file_batch_processor.rb', line 103

def increment(step)
  return if @progress_proc.nil?
  @progress_proc.call(step: step.ljust(14, ' '),
                      total_steps: @total_steps)
end

#process!Object



78
79
80
81
82
# File 'lib/imagesorter/file_batch_processor.rb', line 78

def process!
  @files.each do |file|
    queue_categorizing(file)
  end
end

#queue_categorizing(file) ⇒ Object



84
85
86
87
88
89
90
91
92
93
# File 'lib/imagesorter/file_batch_processor.rb', line 84

def queue_categorizing(file)
  queue_job do
    file.process!(@categorizer)

    Imagesorter.logger.debug "#{file.file.path} metadata: #{JSON.pretty_generate(file.to_h)}"

    increment(@categorizer.step_name)
    queue_proceesing(file)
  end
end

#queue_job(&block) ⇒ Object



109
110
111
# File 'lib/imagesorter/file_batch_processor.rb', line 109

def queue_job(&block)
  @queue.push(block)
end

#queue_proceesing(file) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/imagesorter/file_batch_processor.rb', line 95

def queue_proceesing(file)
  return if @processor.nil?
  queue_job do
    file.process!(@processor)
    increment(@processor.step_name)
  end
end

#start_queue_workersObject



120
121
122
123
124
125
126
127
128
# File 'lib/imagesorter/file_batch_processor.rb', line 120

def start_queue_workers
  @threads = Array.new(@threads) do
    Thread.new do
      work_on_queue
    end
  end

  @threads.each(&:join)
end

#work_on_queueObject



113
114
115
116
117
118
# File 'lib/imagesorter/file_batch_processor.rb', line 113

def work_on_queue
  until @queue.empty?
    job = @queue.shift
    job.call
  end
end