Class: MmTool::ApplicationMain

Inherits:
Object
  • Object
show all
Defined in:
lib/mm_tool/application_main.rb

Overview

The main application.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApplicationMain


Initialize




39
40
41
42
# File 'lib/mm_tool/application_main.rb', line 39

def initialize
  @tempfile = nil
  @defaults = MmUserDefaults.shared_user_defaults
end

Instance Attribute Details

#tempfileObject


Attributes




34
35
36
# File 'lib/mm_tool/application_main.rb', line 34

def tempfile
  @tempfile
end

Class Method Details

.shared_applicationObject


Singleton accessor




47
48
49
50
51
52
# File 'lib/mm_tool/application_main.rb', line 47

def self.shared_application
  unless @self
    @self = self.new
  end
  @self
end

Instance Method Details

#information_headerObject


Return the report header.


noinspection RubyResolve



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/mm_tool/application_main.rb', line 89

def information_header
  info_table_src = TTY::Table.new
  @defaults.label_value_pairs.each {|pair| info_table_src << pair}
  info_table_src << ["Disposition Columns:", MmMovie.dispositions.join(', ')]
  info_table_src << ["Transcode File Location:", self.tempfile ? tempfile&.path : 'n/a']

  info_table = C.bold("Looking for file(s) and processing them with the following options:\n").dup
  info_table << info_table_src.render(:basic) do |renderer|
    a = @defaults.label_value_pairs.map {|p| p[0].length }.max + 1
    b = OutputHelper.console_width - a - 2
    renderer.alignments    = [:right, :left]
    renderer.multiline     = true
    renderer.column_widths = [a,b]
  end << "\n\n"
end

#output(message, command = false) ⇒ Object


Output a message to the screen, and if applicable, to the temporary file for later opening.




58
59
60
61
62
63
64
65
66
67
68
# File 'lib/mm_tool/application_main.rb', line 58

def output(message, command = false)
  puts message
  if @tempfile
    if command
      message = message + "\n"
    else
      message = message.lines.collect {|line| "# #{line}"}.join + "\n"
    end
    @tempfile&.write(message)
  end
end

#run(file_or_dir) ⇒ Object


Run the application with the given file/directory.




148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/mm_tool/application_main.rb', line 148

def run(file_or_dir)

  @file_count = { :processed => 0, :displayed => 0 }

  if @defaults[:transcode]
    @tempfile = Tempfile.create(['mm_tool-', '.sh'])
    @tempfile&.write(transcode_script_header)
    # @tempfile.flush
  end

  if @defaults[:info_header]
    output information_header
  end

  if File.file?(file_or_dir)

    original_scan_type = @defaults[:scan_type]
    @defaults[:scan_type] = :force
    run_loop(file_or_dir)
    @defaults[:scan_type] = original_scan_type

  elsif File.directory?(file_or_dir)

    extensions = @defaults[:container_files]&.join(',')
    Dir.chdir(file_or_dir) do
      Dir.glob("**/*.{#{extensions}}").map {|path| File.expand_path(path) }.sort(&NaturalSort).each do |file|
        run_loop(file)
      end
    end

  else
    output "Error: Execution should never have reached this point."
    exit 1
  end

  unless @defaults[:shell_commands]
    output("Transcode File Location: #{tempfile&.path}") if @tempfile
    output("#{File.basename($0)} processed #{@file_count[:processed]} files and displayed data for #{@file_count[:displayed]} of them.")
  end

ensure
  if @tempfile
    @tempfile&.close
    # @tempfile.unlink
  end
end

#run_loop(file_name) ⇒ Object


The main run loop, to be run for each file.




108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/mm_tool/application_main.rb', line 108

def run_loop(file_name)

  if @defaults[:ignore_files]
    MmMovieIgnoreList.shared_ignore_list.add(path: file_name)
    output("Note: added #{file_name} to the list of files to be ignored.")
  elsif @defaults[:unignore_files]
    MmMovieIgnoreList.shared_ignore_list.remove(path: file_name)
    output("Note: removed #{file_name} to the list of files to be ignored.")
  else
    @file_count[:processed] = @file_count[:processed] + 1
    movie = MmMovie.new(with_file: file_name)
    a = movie.interesting? # already interesting if low-quality, but separate quality check made for logic, below.
    b = MmMovieIgnoreList.shared_ignore_list.include?(file_name)
    c = movie.meets_minimum_quality?
    s = @defaults[:scan_type]&.to_sym

    if (s == :normal && a && !b && c) ||
        (s == :all && !b) ||
        (s == :flagged && b) ||
        (s == :quality && !c ) ||
        (s == :force)

      @file_count[:displayed] = @file_count[:displayed] + 1
      output(file_name)
      output(movie.format_table)
      output(movie.stream_table)
      if @defaults[:shell_commands]
        output("echo \"#{File.basename(file_name)}\" ; \\", true)
        output("#{movie.command_rename} ; \\", true)
        output("#{movie.command_transcode} ; \\", true)
        output(movie.command_review_post, true)
      end
      output("\n\n", true)
    end
  end # if
end

#transcode_script_headerObject


Return the transcode file header.




73
74
75
76
77
78
79
80
81
82
83
# File 'lib/mm_tool/application_main.rb', line 73

def transcode_script_header
  <<~HEREDOC
    #!/bin/sh

    # Check this file, make any changes, and save it. Execute it directly,
    # or execute it with the sh command if it's not executable.

    set -e

  HEREDOC
end