class PhTools::PhFile

phtools file name operations

Constants

NICKNAME_MAX_SIZE
NICKNAME_MIN_SIZE

filename constants

NICKNAME_SIZE
ZERO_DATE

Attributes

author[R]
basename[R]
basename_clean[R]
basename_part[R]
date_time[R]
dirname[R]
extname[R]
filename[R]
type[R]

Public Class Methods

get_date_time(date_string) click to toggle source
# File lib/phtools/ph_file.rb, line 53
def self.get_date_time(date_string)
  /^(?<date>\d{8})-(?<time>\d{6})/ =~ date_string
  DateTime.parse("#{Regexp.last_match(:date)}T#{Regexp.last_match(:time)}")
rescue ArgumentError
  PhFile::ZERO_DATE
end
new(filename) click to toggle source
# File lib/phtools/ph_file.rb, line 63
def initialize(filename)
  set_state(filename)
end
validate_author(author) click to toggle source
# File lib/phtools/ph_file.rb, line 39
def self.validate_author(author)
  case
  when author.size != NICKNAME_SIZE
    return [false, "'#{author}' wrong author size, should be #{NICKNAME_SIZE} chars long"]
  when /[-_\s]/.match(author)
    return [false, "'#{author}' author should not contain spaces [_- ]"]
  when /[\d]/.match(author)
    return [false, "'#{author}' author should not contain digits"]
  when /[\W]/.match(author)
    return [false, "'#{author}' author should contain only ASCII chars"]
  end
  [true, '']
end
validate_file!(filename, file_type) click to toggle source
# File lib/phtools/ph_file.rb, line 29
def self.validate_file!(filename, file_type)
  fail PhTools::Error, 'does not exist' unless
    filename && File.exist?(filename)
  fail PhTools::Error, 'not a file' if File.directory?(filename)
  fail PhTools::Error, 'no permission to write' unless
    File.writable_real?(filename)
  fail PhTools::Error, 'unsupported type' unless
    file_type.include?(File.extname(filename).slice(1..-1).downcase)
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/phtools/ph_file.rb, line 71
def <=>(other)
  @filename <=> other.filename
end
audio?() click to toggle source
# File lib/phtools/ph_file.rb, line 101
def audio?
  FILE_TYPE_AUDIO.include?(@type)
end
basename_is_standard?() click to toggle source
# File lib/phtools/ph_file.rb, line 81
def basename_is_standard?
  @basename == basename_standard
end
basename_standard(basename_clean: @basename_clean, date_time: @date_time, author: @author) click to toggle source
# File lib/phtools/ph_file.rb, line 75
def basename_standard(basename_clean: @basename_clean,
                      date_time: @date_time,
                      author: @author)
  %(#{date_time.strftime('%Y%m%d-%H%M%S')}_#{(author.upcase + 'XXXXXX')[0, NICKNAME_SIZE]} #{basename_clean})
end
cleanse(dirname: @dirname, basename_clean: @basename_clean, extname: @extname) click to toggle source
# File lib/phtools/ph_file.rb, line 125
def cleanse(dirname: @dirname, basename_clean: @basename_clean,
            extname: @extname)
  File.join(dirname, basename_clean + extname)
end
cleanse!(dirname: @dirname, basename_clean: @basename_clean, extname: @extname) click to toggle source
# File lib/phtools/ph_file.rb, line 130
def cleanse!(dirname: @dirname, basename_clean: @basename_clean,
             extname: @extname)
  filename = cleanse(dirname: dirname, basename_clean: basename_clean,
                     extname: extname)
  set_state(filename)
  filename
end
date_time_ok?() click to toggle source
# File lib/phtools/ph_file.rb, line 143
def date_time_ok?
  @date_time != ZERO_DATE
end
date_time_to_time() click to toggle source
# File lib/phtools/ph_file.rb, line 147
def date_time_to_time
  Time.new(@date_time.year, @date_time.month, @date_time.day,
           @date_time.hour, @date_time.min, @date_time.sec)
  # no use of @date_time.zone - assuming file's timezone always
  # equals to photografer's computer timezone
end
dirname=(dirname = @dirname) click to toggle source
# File lib/phtools/ph_file.rb, line 138
def dirname=(dirname = @dirname)
  @dirname = dirname
  @filename = File.join(@dirname, @basename + @extname)
end
image?() click to toggle source
# File lib/phtools/ph_file.rb, line 85
def image?
  FILE_TYPE_IMAGE.include?(@type)
end
image_normal?() click to toggle source
# File lib/phtools/ph_file.rb, line 89
def image_normal?
  FILE_TYPE_IMAGE_NORMAL.include?(@type)
end
image_raw?() click to toggle source
# File lib/phtools/ph_file.rb, line 93
def image_raw?
  FILE_TYPE_IMAGE_RAW.include?(@type)
end
standardize(dirname: @dirname, basename_clean: @basename_clean, extname: @extname, date_time: @date_time, author: @author) click to toggle source
# File lib/phtools/ph_file.rb, line 105
def standardize(dirname: @dirname, basename_clean: @basename_clean,
                extname: @extname, date_time: @date_time,
                author: @author)
  File.join(dirname,
            basename_standard(basename_clean: basename_clean,
                              date_time: date_time,
                              author: author) + extname)
end
standardize!(dirname: @dirname, basename_clean: @basename_clean, extname: @extname, date_time: @date_time, author: @author) click to toggle source
# File lib/phtools/ph_file.rb, line 114
def standardize!(dirname: @dirname, basename_clean: @basename_clean,
                 extname: @extname, date_time: @date_time,
                 author: @author)

  filename = standardize(dirname: dirname, basename_clean: basename_clean,
                         extname: extname, date_time: date_time,
                         author: author)
  set_state(filename)
  filename
end
to_s() click to toggle source
# File lib/phtools/ph_file.rb, line 67
def to_s
  @filename.to_s
end
video?() click to toggle source
# File lib/phtools/ph_file.rb, line 97
def video?
  FILE_TYPE_VIDEO.include?(@type)
end

Private Instance Methods

parse_basename() click to toggle source
# File lib/phtools/ph_file.rb, line 196
def parse_basename
  default = { prefix: '', clean: '', date: '', time: '', author: '', id: '', flags: '' }

  case @basename
  # check YYYYmmdd-HHMMSS_AUT[ID]{FLAGS}cleanname
  when /^(?<prefix>(?<date>\d{8})-(?<time>\d{6})_(?<author>\w{#{NICKNAME_MIN_SIZE},#{NICKNAME_MAX_SIZE}})\[(?<id>.*)\]\{(?<flags>.*)\})(?<clean>.*)/
    @basename_part = default.merge(prefix: Regexp.last_match(:prefix),
                                   clean: Regexp.last_match(:clean),
                                   date: Regexp.last_match(:date),
                                   time: Regexp.last_match(:time),
                                   author: Regexp.last_match(:author),
                                   id: Regexp.last_match(:id),
                                   flags: Regexp.last_match(:flags))

  # check YYYYmmdd-HHMMSS_AUT[ID]cleanname
  when /^(?<prefix>(?<date>\d{8})-(?<time>\d{6})_(?<author>\w{#{NICKNAME_MIN_SIZE},#{NICKNAME_MAX_SIZE}})\[(?<id>.*)\])(?<clean>.*)/
    @basename_part = default.merge(prefix: Regexp.last_match(:prefix),
                                   clean: Regexp.last_match(:clean),
                                   date: Regexp.last_match(:date),
                                   time: Regexp.last_match(:time),
                                   author: Regexp.last_match(:author),
                                   id: Regexp.last_match(:id))
  # STANDARD template
  # check YYYYmmdd-HHMMSS_AUT cleanname
  when /^(?<prefix>(?<date>\d{8})-(?<time>\d{6})_(?<author>\w{#{NICKNAME_MIN_SIZE},#{NICKNAME_MAX_SIZE}})[-\s_])(?<clean>.*)/
    @basename_part = default.merge(prefix: Regexp.last_match(:prefix),
                                   clean: Regexp.last_match(:clean),
                                   date: Regexp.last_match(:date),
                                   time: Regexp.last_match(:time),
                                   author: Regexp.last_match(:author))
  # check if name = YYYYmmdd-HHMM_AAA_name
  when /^(?<prefix>(?<date>\d{8})-(?<time>\d{4})[-\s_](?<author>\w{#{NICKNAME_MIN_SIZE},#{NICKNAME_MAX_SIZE}})[-\s_])(?<clean>.*)/
    @basename_part = default.merge(prefix: Regexp.last_match(:prefix),
                                   clean: Regexp.last_match(:clean),
                                   date: Regexp.last_match(:date),
                                   time: Regexp.last_match(:time),
                                   author: Regexp.last_match(:author))
  # check if name = YYYYmmdd-HHMM_name
  when /^(?<prefix>(?<date>\d{8})-(?<time>\d{4})[-\s_])(?<clean>.*)/
    @basename_part = default.merge(prefix: Regexp.last_match(:prefix),
                                   clean: Regexp.last_match(:clean),
                                   date: Regexp.last_match(:date),
                                   time: Regexp.last_match(:time))
  # check if name = YYYYmmdd_name
  when /^(?<prefix>(?<date>\d{8})[-\s_])(?<clean>.*)/
    @basename_part = default.merge(prefix: Regexp.last_match(:prefix),
                                   clean: Regexp.last_match(:clean),
                                   date: Regexp.last_match(:date))
  # check if name = YYYY_name
  when /^(?<prefix>(?<date>\d{4})[-\s_])(?<clean>.*)/
    @basename_part = default.merge(prefix: Regexp.last_match(:prefix),
                                   clean: Regexp.last_match(:clean),
                                   date: Regexp.last_match(:date))
  else
    @basename_part = default.merge(clean: @basename)
  end
end
reveal_date_time() click to toggle source
# File lib/phtools/ph_file.rb, line 168
def reveal_date_time
  date = @basename_part[:date]
  time = @basename_part[:time]
  strptime_template = ''
  strptime_string = ''
  case date.size
  when 4 # expecting Year e.g.2001
    strptime_template += '%Y'
    strptime_string += date
  when 8 # expecting YYmmdd e.g.20010101
    strptime_template += '%Y%m%d'
    strptime_string += date
  end
  case time.size
  when 4 # expecting HHMM e.g. 1025
    strptime_template += '%H%M'
    strptime_string += time
  when 6 # expecting YHHMMSS e.g.102559
    strptime_template += '%H%M%S'
    strptime_string += time
  end

  return ZERO_DATE if strptime_string.empty?
  DateTime.strptime(strptime_string, strptime_template)
rescue ArgumentError
  return ZERO_DATE
end
set_state(filename) click to toggle source
# File lib/phtools/ph_file.rb, line 156
def set_state(filename)
  @dirname = File.dirname(filename)
  @extname = File.extname(filename)
  @basename = File.basename(filename, @extname)
  @filename = File.join(@dirname, @basename + @extname)
  @type = @extname.empty? ? '' : @extname.slice(1..-1).downcase
  parse_basename
  @basename_clean = @basename_part[:clean]
  @date_time = reveal_date_time
  @author = @basename_part[:author] || ''
end