class NicInfo::Config
Handles configuration of the application
Attributes
conf_msgs[RW]
config[RW]
factory[RW]
logger[RW]
options[RW]
rdap_bootstrap_dir[RW]
rdap_cache_dir[RW]
Public Class Methods
clean()
click to toggle source
# File lib/nicinfo/config.rb, line 232 def self.clean FileUtils::rm_r( formulate_app_data_dir() ) end
formulate_app_data_dir()
click to toggle source
# File lib/nicinfo/config.rb, line 238 def self.formulate_app_data_dir if Gem.win_platform? data_dir = File.join(ENV['APPDATA'], "NicInfo") elsif RUBY_PLATFORM =~ /linux/ data_dir = File.join(ENV['HOME'], ".NicInfo") elsif RUBY_PLATFORM =~ /darwin/ data_dir = File.join(ENV['HOME'], ".NicInfo") elsif RUBY_PLATFORM =~ /freebsd/ data_dir = File.join(ENV['HOME'], ".NicInfo") elsif RUBY_PLATFORM =~ /cygwin/ data_dir = File.join(ENV['HOME'], ".NicInfo") else raise ScriptError, "system platform is not recognized." end return data_dir end
formulate_config_file_name(data_dir)
click to toggle source
# File lib/nicinfo/config.rb, line 255 def self.formulate_config_file_name data_dir File.join( data_dir, "config.yaml" ) end
new(app_data)
click to toggle source
Intializes the configuration with a place to look for the config file If the file doesn’t exist, a default is used. Main
routines will do something like NicInfo::Config.new
( NicInfo::Config.formulate_app_data_dir()
)
# File lib/nicinfo/config.rb, line 34 def initialize app_data @options = OpenStruct.new @app_data = app_data @logger = NicInfo::Logger.new @conf_msgs = Array.new @factory = NicInfo::Factory.new( self ) config_file_name = Config.formulate_config_file_name( @app_data ) if File.exist?( config_file_name ) @config = load_config( config_file_name ) else @config = YAML.load( @@yaml_config ) end configure_logger() end
Public Instance Methods
bsfiles_last_update_filename()
click to toggle source
# File lib/nicinfo/config.rb, line 143 def bsfiles_last_update_filename File.join(@rdap_bootstrap_dir, NicInfo::BSFILE_LAST_CHECK_FILENAME) end
check_bsfiles_age?()
click to toggle source
# File lib/nicinfo/config.rb, line 147 def check_bsfiles_age? retval = false if @config[ NicInfo::BOOTSTRAP ][ NicInfo::CHECK_BSFILES_AGE ] t = get_bsfiles_last_update_time if t != nil configed_age = @config[ NicInfo::BOOTSTRAP ][ NicInfo::BSFILES_AGE ] age = t + configed_age retval = true if age < Time.now else retval = true end end return retval end
check_config_version()
click to toggle source
# File lib/nicinfo/config.rb, line 95 def check_config_version # check to see if the configuration is old config_section = @config[NicInfo::CONFIG] if config_section != nil config_version = config_section[NicInfo::VERSION_CONFIG] end if config_version == nil || config_version < NicInfo::CONFIG_VERSION # if a reset hasn't been asked for if !@options.reset_config @logger.mesg( "Your configuration is old. Use --reset to create a new one.", NicInfo::AttentionType::ERROR ) end end end
configure_logger()
click to toggle source
Configures the logger
# File lib/nicinfo/config.rb, line 203 def configure_logger output = @config[ NicInfo::OUTPUT ] return if output == nil @logger.message_level = output[ NicInfo::MESSAGES ] @logger.validate_message_level messages_file = output[ NicInfo::MESSAGES_FILE ] if messages_file != nil @logger.message_out = File.open( messages_file, "w+" ) end @logger.data_amount = output[ NicInfo::DATA ] @logger.validate_data_amount data_file = output[ NicInfo::DATA_FILE ] if data_file != nil @logger.data_out= File.open( data_file, "w+" ) end @logger.pager=output[ NicInfo::PAGER ] @logger.auto_wrap=output[ NicInfo::AUTO_WRAP ] @logger.default_width=output[ NicInfo::DEFAULT_WIDTH ] @logger.detect_width=output[ NicInfo::DETECT_WIDTH ] @logger.color_scheme = output[ NicInfo::COLOR_SCHEME ] @logger.validate_color_scheme end
copy_bsfiles()
click to toggle source
# File lib/nicinfo/config.rb, line 120 def copy_bsfiles src_dir = File.join( File.dirname( __FILE__ ), NicInfo::BOOTSTRAP_FILE_DIR ) FileUtils::cp_r( src_dir, @rdap_bootstrap_dir ) end
get_bsfiles_last_update_time()
click to toggle source
# File lib/nicinfo/config.rb, line 131 def get_bsfiles_last_update_time retval = nil fname = bsfiles_last_update_filename if File.exist?( fname ) f = File.open( fname, "r" ) data = f.read f.close retval = Time.parse( data ) end return retval end
load(name)
click to toggle source
# File lib/nicinfo/config.rb, line 178 def load name data_file = File.open( File.join( @app_data, name ), "r" ) retval = data_file.read data_file.close return retval end
load_as_yaml(name, default = nil)
click to toggle source
# File lib/nicinfo/config.rb, line 185 def load_as_yaml name, default = nil file_name = make_file_name( name ) retval = default if File.exist?( file_name ) data_file = File.open( File.join( @app_data, name ), "r" ) retval = YAML::load( data_file ) data_file.close elsif default == nil raise "#{file_name} does not exist" end return retval end
load_config(config_file_name)
click to toggle source
# File lib/nicinfo/config.rb, line 109 def load_config config_file_name @config = YAML.load( @@yaml_config ) @config[NicInfo::CONFIG].delete(NicInfo::VERSION_CONFIG) merger = proc do |key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : Array === v1 && Array === v2 ? v1 | v2 : [:undefined, nil, :nil].include?(v2) ? v1 : v2 end @config.merge!( YAML.load( File.open( config_file_name ) ), &merger ) end
make_file_name(name)
click to toggle source
# File lib/nicinfo/config.rb, line 198 def make_file_name name File.join( @app_data, name ) end
save(name, data)
click to toggle source
# File lib/nicinfo/config.rb, line 166 def save name, data data_file = File.open( File.join( @app_data, name ), "w" ) data_file.write data data_file.close end
save_as_yaml(name, obj)
click to toggle source
# File lib/nicinfo/config.rb, line 172 def save_as_yaml name, obj data_file = File.open( File.join( @app_data, name ), "w" ) data_file.puts YAML::dump(obj) data_file.close end
set_bsfiles_last_update_time(t = Time.now)
click to toggle source
# File lib/nicinfo/config.rb, line 125 def set_bsfiles_last_update_time t = Time.now f = File.open(bsfiles_last_update_filename, "w" ) f.write t.strftime( "%Y-%m-%d %H:%M:%S") f.close end
setup_workspace()
click to toggle source
Setups work space for the application and lays down default config If directory is nil, then it uses its own value
# File lib/nicinfo/config.rb, line 55 def setup_workspace @rdap_bootstrap_dir = File.join( @app_data, NicInfo::BOOTSTRAP_FILE_DIR ) if ! File.exist?( @app_data ) @logger.trace "Creating configuration in " + @app_data Dir.mkdir( @app_data ) f = File.open( Config.formulate_config_file_name( @app_data ), "w" ) f.puts @@yaml_config f.close @rdap_cache_dir = File.join( @app_data, "rdap_cache" ) Dir.mkdir( @rdap_cache_dir ) copy_bsfiles else if @options.reset_config config_file_name = Config.formulate_config_file_name( @app_data ) @logger.trace "Resetting configuration in " + config_file_name f = File.open( config_file_name, "w" ) f.puts @@yaml_config f.close @config = YAML.load( File.open( config_file_name ) ) @logger.trace "Resetting bootstrap files in " + @rdap_bootstrap_dir begin FileUtils::rm_r( @rdap_bootstrap_dir ) rescue Errno::ENOENT # do nothing end copy_bsfiles end @logger.trace "Using configuration found in " + @app_data @rdap_cache_dir = File.join( @app_data, "rdap_cache" ) end end
update_bsfiles?(aged)
click to toggle source
# File lib/nicinfo/config.rb, line 162 def update_bsfiles? aged return aged && @config[ NicInfo::BOOTSTRAP ][ NicInfo::UPDATE_BSFILES ] end