class ImageOptim::Config

Read, merge and parse configuration

Constants

GLOBAL_PATH

Global config path at ‘$XDG_CONFIG_HOME/image_optim.yml` (by default `~/.config/image_optim.yml`)

LOCAL_PATH

Local config path at ‘./.image_optim.yml`

Public Class Methods

load_yaml_file(path) click to toggle source
# File lib/image_optim/config.rb, line 47
def load_yaml_file(path)
  if YAML.respond_to?(:safe_load_file)
    YAML.safe_load_file(path, permitted_classes: [Range])
  else
    YAML.load_file(path)
  end
end
new(options) click to toggle source

Merge config from files with passed options Config files are checked at ‘GLOBAL_PATH` and `LOCAL_PATH` unless overriden using `:config_paths`

# File lib/image_optim/config.rb, line 59
def initialize(options)
  config_paths = options.delete(:config_paths) || [GLOBAL_PATH, LOCAL_PATH]
  config_paths = Array(config_paths)

  to_merge = config_paths.map{ |path| self.class.read_options(path) }
  to_merge << HashHelpers.deep_symbolise_keys(options)

  @options = to_merge.reduce do |memo, hash|
    HashHelpers.deep_merge(memo, hash)
  end
  @used = Set.new
end
read_options(path) click to toggle source

Read options at path: expand path (warn on failure), return {} if file does not exist or is empty, read yaml, check if it is a Hash, deep symbolise keys

# File lib/image_optim/config.rb, line 27
def read_options(path)
  begin
    full_path = File.expand_path(path)
  rescue ArgumentError => e
    warn "Can't expand path #{path}: #{e}"
    return {}
  end
  return {} unless File.size?(full_path)

  config = load_yaml_file(full_path)
  unless config.is_a?(Hash)
    fail "expected hash, got #{config.inspect}"
  end

  HashHelpers.deep_symbolise_keys(config)
rescue => e
  warn "exception when reading #{full_path}: #{e}"
  {}
end

Public Instance Methods

allow_lossy() click to toggle source

Allow lossy workers and optimizations, converted to boolean

# File lib/image_optim/config.rb, line 167
def allow_lossy
  !!get!(:allow_lossy)
end
assert_no_unused_options!() click to toggle source

Fail unless all options were marked as used (directly or indirectly accessed using ‘get!`)

# File lib/image_optim/config.rb, line 87
def assert_no_unused_options!
  unknown_options = @options.reject{ |key, _value| @used.include?(key) }
  return if unknown_options.empty?

  fail ConfigurationError, "unknown options #{unknown_options.inspect}"
end
cache_dir() click to toggle source
# File lib/image_optim/config.rb, line 171
def cache_dir
  dir = get!(:cache_dir)
  dir unless dir.nil? || dir.empty?
end
cache_worker_digests() click to toggle source
# File lib/image_optim/config.rb, line 176
def cache_worker_digests
  !!get!(:cache_worker_digests)
end
for_worker(klass) click to toggle source

Options for worker class by its ‘bin_sym`:

  • ‘Hash` passed as is

  • ‘{}` for `true` or `nil`

  • ‘false` for `false`

  • otherwise fail with ‘ConfigurationError`

# File lib/image_optim/config.rb, line 185
def for_worker(klass)
  worker_options = get!(klass.bin_sym)

  case worker_options
  when Hash
    worker_options
  when true, nil
    {}
  when false
    {disable: true}
  else
    fail ConfigurationError, "Got #{worker_options.inspect} for " \
                             "#{klass.name} options"
  end
end
get!(key) click to toggle source

Gets value for key converted to symbol and mark option as used

# File lib/image_optim/config.rb, line 73
def get!(key)
  key = key.to_sym
  @used << key
  @options[key]
end
key?(key) click to toggle source

Check if key is present

# File lib/image_optim/config.rb, line 80
def key?(key)
  key = key.to_sym
  @options.key?(key)
end
nice() click to toggle source

Nice level:

  • ‘10` by default and for `nil` or `true`

  • ‘0` for `false`

  • otherwise convert to integer

# File lib/image_optim/config.rb, line 98
def nice
  nice = get!(:nice)

  case nice
  when true, nil
    10
  when false
    0
  else
    nice.to_i
  end
end
pack() click to toggle source

Using image_optim_pack:

  • ‘false` to disable

  • ‘nil` to use if available

  • everything else to require

# File lib/image_optim/config.rb, line 145
def pack
  pack = get!(:pack)
  return false if pack == false

  require 'image_optim/pack'
  true
rescue LoadError => e
  raise "Cannot load image_optim_pack: #{e}" if pack

  false
end
skip_missing_workers() click to toggle source

Skip missing workers, converted to boolean

# File lib/image_optim/config.rb, line 158
def skip_missing_workers
  if key?(:skip_missing_workers)
    !!get!(:skip_missing_workers)
  else
    pack
  end
end
threads() click to toggle source

Number of parallel threads:

  • ‘processor_count` by default and for `nil` or `true`

  • ‘1` for `false`

  • otherwise convert to integer

# File lib/image_optim/config.rb, line 115
def threads
  threads = get!(:threads)

  case threads
  when true, nil
    processor_count
  when false
    1
  else
    threads.to_i
  end
end
timeout() click to toggle source

Timeout in seconds for each image:

  • not set by default and for ‘nil`

  • otherwise converted to float

# File lib/image_optim/config.rb, line 131
def timeout
  timeout = get!(:timeout)
  timeout ? timeout.to_f : nil
end
to_s() click to toggle source

yaml dump without document beginning prefix ‘—`

# File lib/image_optim/config.rb, line 202
def to_s
  YAML.dump(HashHelpers.deep_stringify_keys(@options)).sub(/\A---\n/, '')
end
verbose() click to toggle source

Verbose mode, converted to boolean

# File lib/image_optim/config.rb, line 137
def verbose
  !!get!(:verbose)
end

Private Instance Methods

processor_count() click to toggle source

stackoverflow.com/a/6420817

# File lib/image_optim/config.rb, line 209
def processor_count
  @processor_count ||= case host_os = RbConfig::CONFIG['host_os']
  when /darwin9/
    Cmd.capture 'hwprefs cpu_count'
  when /darwin/
    if (Cmd.capture 'which hwprefs') == ''
      Cmd.capture 'sysctl -n hw.ncpu'
    else
      Cmd.capture 'hwprefs thread_count'
    end
  when /linux/
    Cmd.capture 'grep -c processor /proc/cpuinfo'
  when /freebsd/
    Cmd.capture 'sysctl -n hw.ncpu'
  when /mswin|mingw/
    require 'win32ole'
    query = 'select NumberOfLogicalProcessors from Win32_Processor'
    result = WIN32OLE.connect('winmgmts://').ExecQuery(query)
    result.to_enum.first.NumberOfLogicalProcessors
  else
    warn "Unknown architecture (#{host_os}) assuming one processor."
    1
  end.to_i
end