Class: AstroSubframeOrganizer::Organizer

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

Overview

Main class responsible for organizing astrophotography subframes based on metadata and user input.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logger

Constructor Details

#initialize(type:, path: Dir.pwd, prompt: AstroSubframeOrganizer.prompt, equipment_selector: nil) ⇒ Organizer

Returns a new instance of Organizer.



11
12
13
14
15
16
17
# File 'lib/astro_subframe_organizer/organizer.rb', line 11

def initialize(type:, path: Dir.pwd, prompt: AstroSubframeOrganizer.prompt, equipment_selector: nil)
  @prompt = prompt
  @path = path
  @type = type
  @file_sets = file_sets_for(type)
  @equipment_selector = equipment_selector || EquipmentSelector.new(prompt)
end

Instance Attribute Details

#equipment_selectorObject (readonly)

Returns the value of attribute equipment_selector.



8
9
10
# File 'lib/astro_subframe_organizer/organizer.rb', line 8

def equipment_selector
  @equipment_selector
end

#file_setsObject (readonly)

Returns the value of attribute file_sets.



8
9
10
# File 'lib/astro_subframe_organizer/organizer.rb', line 8

def file_sets
  @file_sets
end

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/astro_subframe_organizer/organizer.rb', line 8

def path
  @path
end

#typeObject (readonly)

Returns the value of attribute type.



8
9
10
# File 'lib/astro_subframe_organizer/organizer.rb', line 8

def type
  @type
end

Instance Method Details

#fits_filesObject

rubocop:disable Metrics/AbcSize



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/astro_subframe_organizer/organizer.rb', line 19

def fits_files # rubocop:disable Metrics/AbcSize
  exts = (Config.fits_extensions + Config.raw_extensions).flat_map { |e| [e.downcase, e.upcase] }
  all = Dir.glob(exts.map { |e| "**/*#{e}" }, base: path)
  processable = all.filter { |f| !File.basename(f).match?(Utils::ExifRenamer::RAW_NAME_PATTERN) }
                   .uniq
                   .map { |relative| File.join(path, relative) }

  if processable.size != all.size
    unprocessable_raw_files_warning = 'Unprocessed raw images detected, but will be ignored. " +
    "Raw images must be renamed before organizing. Run `astro-subframe-organizer raw rename_from_exif`, " +
    "then try again.'
    logger.warn(unprocessable_raw_files_warning)
  end

  processable.map { |f| Astrophoto.new(f) }
end

#organize(dry_run: false) ⇒ Object

rubocop:disable Metrics/AbcSize,Metrics/MethodLength



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
65
66
67
68
69
70
71
72
73
74
# File 'lib/astro_subframe_organizer/organizer.rb', line 36

def organize(dry_run: false) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
  logger.info "Preparing to move #{file_sets.sum { |set| set.files.size }} #{type} files " \
              "from #{file_sets.size} groups..."
  @file_sets.each do |fileset|
    next if fileset.already_moved?

    if fileset.all_unmoved?
      move = ENV['ASTRO_SUBFRAME_SKIP_CONFIRM'] == 'true' ||
             prompt.yes?(
               <<~MSG,
                 Preparing to move #{fileset.size} #{fileset.type} file(s)
                    FROM #{relative_to_pwd(fileset.current_dir)}
                    TO   #{relative_to_pwd(fileset.files.first.target_dir)}
                 Continue?
               MSG
               default: 'y',
             )
      next unless move
    end

    logger.info "For #{type} set #{fileset.files.first.filename}..#{fileset.files.last.filename}:"

    check_telescope(fileset) if [Astrophoto::FLAT, Astrophoto::LIGHT].include?(type)
    check_filter(fileset) if [Astrophoto::FLAT, Astrophoto::LIGHT].include?(type)
    check_camera(fileset)

    require 'tty-progressbar'

    # Initialize the bar with the size of the fileset
    bar = TTY::ProgressBar.new('Moving files [:bar] :current/:total (:percent) :eta', total: fileset.size)

    fileset.each do |file|
      file.move(dry_run, bar)
      bar.advance(1) # Move the bar forward by 1 for each file
    end

    logger.info 'Done'
  end
end