Class: D3::Configuration

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/d3/configuration.rb

Overview

A class for working with settings & preferences for the D3 module

This is a singleton class, only one instance can exist at a time.

When the D3 module loads, the Configuration instance is created and stored in the constant CONFIG.

Known attributes are listed and defined in the d3.conf.default file in the rubygem folder’s data folder (e.g. /Library/Ruby/Gems/2.0.0/gems/depot3-3.0.0/data/d3.conf.default)

The current settings may be saved using #save. With no parameter, #save writes to the CONF_FILE otherwise provide a String or Pathname file path. NOTE: This overwrites any existing file.

To re-load the settings use #reload. This clears the current settings, and re-reads both the CONF_FILE. If a pathname is provided, e.g.

D3::CONFIG.reload '/path/to/other/file'

the current settings are cleared and reloaded from that other file.

To view the current settings, use #print.

Constant Summary collapse

CONF_FILE =

The filename for storing the prefs, globally

Pathname.new "/etc/d3.conf"
CONF_KEYS =

The attribute keys we maintain, and the String#method to convert them to the correct ruby class See the d3.conf.default file in the rubygem’s data folder for detailed descriptions of these keys. (e.g. /Library/Ruby/Gems/2.0.0/gems/depot3-3.0.0/data/d3.conf.default)

{

  :jss_default_pkg_category => :to_s,
  :jss_default_script_category => :to_s,

  :log_file => :to_s,
  :log_level => :to_sym,
  :log_timestamp_format => :to_s,

  :client_expiration_allowed => :jss_to_bool,
  :client_expiration_policy => :to_s,
  :client_jss_ro_user => :to_s,
  :client_jss_ropw_path => :to_s,
  :client_db_ro_user => :to_s,
  :client_db_ropw_path => :to_s,
  :client_distpoint_ropw_path => :to_s,
  :client_http_ropw_path => :to_s,
  :client_try_cloud_distpoint => :jss_to_bool,
  :client_prohibited_admin_names =>  [:split,/\s*,\s*/],

  :puppy_notification_policy => :to_s,
  :puppy_notification_frequency => :to_i,
  :puppy_last_notification => :jss_to_time,
  :puppy_reboot_policy => :to_s,

  :puppy_notify_image_path => :to_s,
  :puppy_optout_seconds => :to_i,
  :puppy_optout_text => :to_s,
  :puppy_optout_image_path => :to_s,
  :puppy_slideshow_folder_path => :to_s,
  :puppy_display_captions => :jss_to_bool,
  :puppy_no_captions_text => :to_s,
  :puppy_image_size => :to_i,
  :puppy_title => :to_s,
  :puppy_display_secs => :to_i,

  :notification_image_path => :jss_to_pathname,

  :admin_make_live_script => :to_s,
  :admin_auto_clean =>  :jss_to_bool,
  :admin_auto_clean_keep_deprecated => :to_i,
  :admin_auto_clean_keep_latest_pilots => :jss_to_bool,

  :report_receipts_ext_attr_name => :to_s,
  :report_puppyq_ext_attr_name => :to_s,
  :report_db_server => :to_s
}

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Initialize!



141
142
143
# File 'lib/d3/configuration.rb', line 141

def initialize
  read_conf
end

Instance Method Details

#clear_allvoid

This method returns an undefined value.

Clear all values



164
165
166
167
# File 'lib/d3/configuration.rb', line 164

def clear_all
  log "Clearing all config values", :debug unless @initializing
  CONF_KEYS.keys.each {|k| self.send "#{k}=".to_sym, nil}
end

#log(msg, level) ⇒ Object

Since config must be loaded before logging can start use this to send debug messages to stderr before the logger is set up, if the client app has set debugging



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

def log (msg, level)
  if D3.respond_to? :loaded? and D3.loaded?
     D3.log msg, level
  else
    STDERR.puts "#{level}: #{msg}" if ENV['D3_DEBUG']
  end
end

This method returns an undefined value.

Print out the current settings to stdout



245
246
247
# File 'lib/d3/configuration.rb', line 245

def print
  CONF_KEYS.keys.sort.each{|k| puts "#{k}: #{to_string k}"}
end

#read_confvoid

This method returns an undefined value.

(Re)read the global prefs, if it exists.



174
175
176
# File 'lib/d3/configuration.rb', line 174

def read_conf
  read CONF_FILE if CONF_FILE.file? and CONF_FILE.readable?
end

#reload(file = nil) ⇒ void

This method returns an undefined value.

Clear the settings and reload the prefs files, or another file if provided

Parameters:

  • file (String, Pathname) (defaults to: nil)

    a non-standard prefs file to load



184
185
186
187
188
189
190
191
192
# File 'lib/d3/configuration.rb', line 184

def reload(file = nil)
  clear_all
  if file
    read file
    return true
  end
  read_conf
  return true
end

#save(file = CONF_FILE) ⇒ void

This method returns an undefined value.

Save the prefs into a file.

Parameters:

  • file (String, Pathname) (defaults to: CONF_FILE)

    an arbitrary file into which the config is saved. defaults to CONF_FILE



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/d3/configuration.rb', line 201

def save(file = CONF_FILE)

  file = Pathname.new file

  # file already exists? read it in and update the values.
  if file.readable?
    data = file.read

    # go thru the known attributes/keys
    CONF_KEYS.keys.sort.each do |k|

      savable_value = to_string k

      # if the key exists, update it.
      if data =~ /^#{k}:/
        log "Updating config file value #{k}: #{savable_value}", :debug
        data.sub!(/^#{k}:.*$/, "#{k}: #{savable_value}")

      # if not, add it to the end unless it's nil
      else
        log "Adding config file value #{k}: #{savable_value}", :debug
        data += "\n#{k}: #{savable_value}" unless self.send(k).nil?
      end # if data =~ /^#{k}:/
    end #each do |k|

  else # not readable, make a new file
    data = ""
    log "Config file #{file} not found, creating.", :debug
    CONF_KEYS.keys.sort.each do |k|
      data << "#{k}: #{savable_value}\n" unless self.send(k).nil?
    end
  end # if path readable

  # make sure we end with a newline, the save it.
  data << "\n" unless data.end_with?("\n")
  file.jss_save data
  log "Config file #{file} saved.", :debug
end