lib/prc.rb in lorj-0.2.0 vs lib/prc.rb in lorj-1.0.0

- old
+ new

@@ -13,117 +13,149 @@ # limitations under the License.module Lorj require 'fileutils' require 'logger' - +# General lorj library. Used as library data configuration +# List of possible library settings: +# PrcLib.log : PrcLib::Logging object. Used internally by PrcLib +# logging system. +# This object is automatically created as soon as +# a message is printed out +# PrcLib.core_level : lorj debug level. from 0 to 5. +# PrcLib.pdata_path : Define the private data local directory. Usually used +# for any private keys, passwords, etc... +# By default: ~/.config/<app_name> +# PrcLib.data_path : Define the data local directory. +# By default: ~/.<app_name> +# PrcLib.app_name : Define the application name. By default 'lorj' +# PrcLib.app_defaults : Used by Lorj::Config to load application default data. +# By default nil. +# PrcLib.log_file : Define the log file name used. +# By default, defined as ~/.<app_name>/<app_name>.log +# PrcLib.level : logger level used. +# Can be set at runtime, with PrcLib.set_level +# PrcLib.model : Model loaded. module PrcLib + # Check if dir exists and is fully accessible (rwx) + def self.dir_exists?(path) + return false unless File.exist?(path) - def PrcLib.dir_exists?(path) - if File.exists?(path) - if not File.directory?(path) - msg = "'%s' is not a directory. Please fix it." % path - unless log_object.nil? - log_object.fatal(1, msg) - else - raise msg - end - end - if not File.readable?(path) or not File.writable?(path) or not File.executable?(path) - msg = "%s is not a valid directory. Check permissions and fix it." % path - unless log_object.nil? - log_object.fatal(1, msg) - else - raise msg - end - end - return true - end - false - end + unless File.directory?(path) + msg = format("'%s' is not a directory. Please fix it.", path) + fatal_error(1, msg) + end + unless File.readable?(path) && + File.writable?(path) && + File.executable?(path) + msg = format("'%s is not a valid directory. "\ + 'Check permissions and fix it.', path) - def PrcLib.ensure_dir_exists(path) - if not dir_exists?(path) - FileUtils.mkpath(path) if not File.directory?(path) - end - end + fatal_error(1, msg) + end + true + end + def self.fatal_error(rc, msg) + fail msg if log.nil? + log.fatal(rc, msg) + end - class << self - attr_accessor :log, :core_level - end + # ensure dir exists and is fully accessible (rwx) + def self.ensure_dir_exists(path) + FileUtils.mkpath(path) unless dir_exists?(path) + rescue => e + fatal_error(1, e.message) + end - module_function + # Define module data for lorj library configuration + class << self + attr_accessor :log, :core_level + attr_reader :pdata_path, :data_path, :app_defaults, :log_file, :level, + :model + end - def data_path= v - @data_path = File.expand_path(v) if not @data_path - PrcLib.ensure_dir_exists(@data_path) - end + module_function - def data_path - @data_path - end + def pdata_path + return @pdata_path unless @pdata_path.nil? + @pdata_path = File.expand_path(File.join('~', '.config', app_name)) + end - def app_name= v - @app_name = v if not @app_name - end + def app_name + @app_name = 'Lorj' unless @app_name + @app_name + end - def app_name - @app_name - end + def pdata_path=(v) + @pdata_path = File.expand_path(v) unless @pdata_path + PrcLib.ensure_dir_exists(@pdata_path) + begin + FileUtils.chmod(0700, @pdata_path) # no-op on windows + rescue => e + fatal_error(1, e.message) + end + end - def app_defaults= v - @app_defaults = File.expand_path(v) if not @app_defaults - end + def data_path + return @data_path unless @data_path.nil? - def app_defaults - @app_defaults - end + default_path = File.join('~', '.' + app_name) + @data_path = File.expand_path(default_path) + end - def log_file= v - sFile = File.basename(v) - sDir = File.dirname(File.expand_path(v)) - if not File.exists?(sDir) - raise "'%s' doesn't exist. Unable to create file '%s'" % [sDir, sFile] - end - @log_file = File.join(sDir, sFile) - end + def data_path=(v) + @data_path = File.expand_path(v) unless @data_path + PrcLib.ensure_dir_exists(@data_path) + end - def log_file - @log_file - end + # TODO: Low. Be able to support multiple model. + def app_name=(v) + @app_name = v unless @app_name + @model = Lorj::Model.new + end - def level= v + # TODO: Support for several defaults, depending on controllers loaded. + def app_defaults=(v) + return if @app_defaults - @level = v - unless PrcLib.log.nil? - PrcLib.set_level(v) - end - end + v = File.join(File.dirname(__FILE__), v) unless v[0] == '/' - def level - @level - end + @app_defaults = File.expand_path(v) + end - def lib_path=(v) - @lib_path = v if @lib_path.nil? - end + def log_file=(v) + file = File.basename(v) + dir = File.dirname(File.expand_path(v)) + unless File.exist?(dir) + fail format("'%s' doesn't exist. Unable to create file '%s'", dir, file) + end + @log_file = File.join(dir, file) + end - def lib_path() - @lib_path - end + def level=(v) + @level = v - def controller_path() - File.expand_path(File.join(@lib_path, "providers")) - end + PrcLib.level = v unless PrcLib.log.nil? + end - def process_path() - File.join(@lib_path, "core_process") - end -end + def lib_path=(v) + @lib_path = v if @lib_path.nil? + end + attr_reader :lib_path + def controller_path + File.expand_path(File.join(@lib_path, 'providers')) + end + + def process_path + File.join(@lib_path, 'core_process') + end +end + +# Redefine Object to add a boolean? function. class Object # Simplify boolean test on objects def boolean? self.is_a?(TrueClass) || self.is_a?(FalseClass) end