Class: UncleKryon::Main

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/unclekryon.rb

Constant Summary collapse

OPT_HELP_UPDATED_ON =
'Change all "updated_*on" datetimes to <datetime>' \
" (e.g., #{Util.format_datetime(DateTime.now)})"

Instance Method Summary collapse

Methods included from Logging

#init_log, #log

Constructor Details

#initialize(args) ⇒ Main

Returns a new instance of Main.



60
61
62
63
64
65
66
# File 'lib/unclekryon.rb', line 60

def initialize(args)
  @args = args
  @cmd = nil
  @did_cmd = false
  @options = {}
  @parsers = []
end

Instance Method Details

#add_parser(parser) ⇒ Object



138
139
140
141
# File 'lib/unclekryon.rb', line 138

def add_parser(parser)
  @parsers.push(parser)
  parser.order!(@args,into: @options)
end

#cmd?(cmd) ⇒ Boolean

Returns:

  • (Boolean)


151
152
153
154
155
156
157
158
# File 'lib/unclekryon.rb', line 151

def cmd?(cmd)
  return false if @did_cmd || @cmd.nil?

  user_cmd = @cmd.gsub(/[[:space:]]+/,'').downcase
  cmd = cmd.gsub(/[[:space:]]+/,'').downcase

  return cmd.start_with?(user_cmd)
end

#do_cmd?Boolean

Returns:

  • (Boolean)


160
161
162
# File 'lib/unclekryon.rb', line 160

def do_cmd?
  return !@options[:help] && !@options[:version]
end

#gsub_updated_on(dirname, updated_on) ⇒ Object



353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'lib/unclekryon.rb', line 353

def gsub_updated_on(dirname,updated_on)
  updated_on = Util.parse_datetime_s(updated_on) # Raise errors on bad format
  updated_on = Util.format_datetime(updated_on)

  Dir.glob(File.join(dirname,'*.yaml')) do |filepath|
    lines = IO.readlines(filepath)
    update_count = 0

    lines.each_with_index do |line,i|
      if line =~ /\A\s*updated\_.*on\:.*\Z/i
        line = line.split(':')[0] << ": '#{updated_on}'"
        lines[i] = line
        update_count += 1
      end
    end

    if !@options[:no_clobber]
      File.open(filepath,'w') do |file|
        file.puts lines
      end
    end

    puts %Q("#{filepath}" updated_on: #{update_count})
  end
end

#log_optsObject



147
148
149
# File 'lib/unclekryon.rb', line 147

def log_opts
  log.info("Using options#{@options}")
end

#parse_hax_cmdObject



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
194
195
196
197
198
# File 'lib/unclekryon.rb', line 164

def parse_hax_cmd
  return if !cmd?('hax')

  parser = OptionParser.new do |op|
    op.banner = '<hax> options:'

    op.on('-d','--dir <dir>','Directory to save the hax data to' \
          " (default: #{Hacker::HAX_DIRNAME})") do |dir|
      @options[:hax_dirname] = dir
    end
    op.on('-t','--train','Train the data using machine learning')
    op.on('-i','--train-dir <dir>','Directory to save the training data to' \
          " (default: #{Hacker::TRAIN_DIRNAME})") do |dir|
      @options[:train_dirname] = dir
    end
    op.on('-u','--updated-on <datetime>',OPT_HELP_UPDATED_ON) do |datetime|
      @options[:updated_on] = datetime
    end
  end

  add_parser(parser)

  if do_cmd?
    if @options[:updated_on]
      hax_dirname = @options[:hax_dirname] || Hacker::HAX_DIRNAME
      gsub_updated_on(hax_dirname,@options[:updated_on])

      @did_cmd = true
    end
  end

  if shift_args
    parse_hax_kryon_cmd
  end
end

#parse_hax_kryon_aum_cmdObject



223
224
225
226
227
228
229
# File 'lib/unclekryon.rb', line 223

def parse_hax_kryon_aum_cmd
  return if !cmd?('aum')

  if shift_args
    parse_hax_kryon_aum_year_cmd
  end
end

#parse_hax_kryon_aum_year_cmdObject



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/unclekryon.rb', line 231

def parse_hax_kryon_aum_year_cmd
  return if !cmd?('year')

  parser = OptionParser.new do |op|
    op.banner = '<year> options:'

    op.on('-t','--title <title>','Title of year release to hack (e.g., 2017)')
    op.on('-a','--album <album>','Album to hack (e.g., 2017.12.25, 1.10, 6.4:2)')
    op.on('-s','--albums','Hack all albums')
    op.on('-b','--begin-album <album>','Hack all albums starting from <album>') do |begin_album|
      @options[:albums] = true
      @options[:begin_album] = begin_album
    end
  end

  add_parser(parser)

  if do_cmd?
    log_opts

    hacker = Hacker.new(**@options)

    if @options[:train]
      if @options[:album]
        hacker.train_kryon_aum_year_album(@options[:album],@options[:title])
        @did_cmd = true
      elsif @options[:title]
        if @options[:albums]
          hacker.train_kryon_aum_year_albums(@options[:title],@options[:begin_album])
        else
          hacker.train_kryon_aum_year(@options[:title])
        end

        @did_cmd = true
      end
    else
      if @options[:album]
        hacker.parse_kryon_aum_year_album(@options[:album],@options[:title])
        @did_cmd = true
      elsif @options[:title]
        if @options[:albums]
          hacker.parse_kryon_aum_year_albums(@options[:title],@options[:begin_album])
        else
          hacker.parse_kryon_aum_year(@options[:title])
        end

        @did_cmd = true
      end
    end
  end
end

#parse_hax_kryon_cmdObject



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/unclekryon.rb', line 200

def parse_hax_kryon_cmd
  return if !cmd?('kryon')

  parser = OptionParser.new do |op|
    op.banner = '<kryon> options:'

    op.on('-f','--file <file>','File to save the hax data to' \
          " (default: #{Hacker::HAX_KRYON_FILENAME})") do |file|
      @options[:hax_kryon_filename] = file
    end
    op.on('-t','--train-file <file>','File to save the training data to' \
          " (default: #{Hacker::TRAIN_KRYON_FILENAME})") do |file|
      @options[:train_kryon_filename] = file
    end
  end

  add_parser(parser)

  if shift_args
    parse_hax_kryon_aum_cmd
  end
end

#parse_iso_cmdObject



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/unclekryon.rb', line 283

def parse_iso_cmd
  return if !cmd?('iso')

  parser = OptionParser.new do |op|
    op.banner = '<iso> options:'

    op.on('-d','--dir <dir>','Directory to read/write ISO data' \
          " (default: #{BaseIsos::DEFAULT_DIR})") do |dir|
      @options[:iso_dirname] = dir
    end
    op.on('-u','--updated-on <datetime>',OPT_HELP_UPDATED_ON) do |datetime|
      @options[:updated_on] = datetime
    end
  end

  add_parser(parser)

  if do_cmd?
    if @options[:updated_on]
      iso_dirname = @options[:iso_dirname] || BaseIsos::DEFAULT_DIR
      gsub_updated_on(iso_dirname,@options[:updated_on])

      @did_cmd = true
    end
  end

  if shift_args
    parse_iso_list_cmd
  end
end

#parse_iso_list_cmdObject



314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/unclekryon.rb', line 314

def parse_iso_list_cmd
  return if !cmd?('list')

  parser = OptionParser.new do |op|
    op.banner = '<list> options:'

    op.on('-c','--canada','List Canadian provinces and territories')
    op.on('-u','--usa','List USA states')
    op.on('-o','--country','List countries')
    op.on('-l','--language','List languages')
    op.on('-r','--region','List regions (i.e., continents, etc.)')
    op.on('-s','--subregion','List subregions')
  end

  add_parser(parser)

  if do_cmd?
    if @options[:canada]
      puts Iso.can_provs_terrs
      @did_cmd = true
    elsif @options[:usa]
      puts Iso.usa_states
      @did_cmd = true
    elsif @options[:country]
      puts Iso.countries
      @did_cmd = true
    elsif @options[:language]
      puts Iso.languages
      @did_cmd = true
    elsif @options[:region]
      puts Iso.regions
      @did_cmd = true
    elsif @options[:subregion]
      puts Iso.subregions
      @did_cmd = true
    end
  end
end

#runObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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
# File 'lib/unclekryon.rb', line 68

def run
  parser = OptionParser.new do |op|
    op.program_name = 'unclekryon'
    op.version = VERSION

    op.banner = <<~BANNER
      Usage:    #{op.program_name} [options] <sub_cmd> [options] <sub_cmd>...

      Sub Commands:
          hax kryon aum year
          iso list

      Options:
    BANNER

    op.on('-d','--dev','Raise errors on missing data, etc.') do
      DevOpts.instance.dev = true
    end
    op.on('-n','--no-clobber','No clobbering of files, dry run; prints to console') do
      @options[:no_clobber] = true
    end
    op.on('-h','--help','Print help to console')
    op.on('-t','--test','Fill in training data with random values, etc. for fast testing') do
      DevOpts.instance.test = true
    end
    op.on('-v','--version','Print version to console') do
      puts "#{op.program_name} v#{op.version}"
      exit
    end
  end

  add_parser(parser)

  if shift_args
    parse_hax_cmd
    parse_iso_cmd
  end

  return if @did_cmd

  @parsers.each do |p|
    puts p
    puts
  end

  puts(<<~EXAMPLES)
    Examples:
        # To view all of the options for the sub commands:
        $ #{parser.program_name} hax kryon aum year
        $ #{parser.program_name} iso list

        <hax>:
        # Train the data 1st before haxing (if there is no training data)
        $ #{parser.program_name} -d hax -t kryon aum year -t 2017 -s
        $ #{parser.program_name} -d hax -t kryon aum year -t 2017 -a 2.2

        # Hax the data (even though --title is not required, it is recommended)
        $ #{parser.program_name} -d hax kryon aum year -t 2017 -s
        $ #{parser.program_name} -d hax kryon aum year -t 2017 -a 10.9
        $ #{parser.program_name} -d hax kryon aum year -a 2017.9.29

        # Hax the 2nd "6.4" album (if there are 2)
        $ #{parser.program_name} -d hax kryon aum year -t 2017 -a 6.4:2

        <iso>:
        $ #{parser.program_name} -n iso -u '2019-04-16 11:11:00'
        $ #{parser.program_name} iso list -r
  EXAMPLES
end

#shift_argsObject



143
144
145
# File 'lib/unclekryon.rb', line 143

def shift_args
  return !@args.nil? && !(@cmd = @args.shift).nil?
end