easy_app_helper 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,111 +1,111 @@
1
- ################################################################################
2
- # EasyAppHelper
3
- #
4
- # Copyright (c) 2013 L.Briais under MIT license
5
- # http://opensource.org/licenses/MIT
6
- ################################################################################
7
-
8
- require 'logger'
9
- require 'singleton'
10
-
11
- # Official Ruby Logger re-opened to introduce a method to hand-over the temporary history from a temporary logger
12
- # to the definitive one.
13
- # TODO: Ensure only the messages that are above the current level are displayed when handing over to the definitive logger.
14
- class Logger
15
- def handing_over_to(log)
16
- history = []
17
- history = @logdev.dev.history if @logdev.dev.respond_to? :history
18
- @logdev.close
19
- @logdev = LogDevice.new log
20
- history.each do |msg|
21
- @logdev.write msg if ENV['DEBUG_EASY_MODULES'] or (msg =~ /^[WE]/)
22
- end
23
- end
24
- end
25
-
26
- # This is the logger that will be used by the application and any class that include {EasyAppHelper} module. It is
27
- # configured by the {EasyAppHelper::Core::Config Config} object, and provides a temporary logger until the config
28
- # is fully loaded.
29
- class EasyAppHelper::Core::Logger < Logger
30
- include Singleton
31
-
32
-
33
- def initialize
34
- @config = {}
35
- super(TempLogger.new)
36
- self.level = Severity::DEBUG
37
- debug "Temporary initialisation logger created..."
38
- end
39
-
40
- # Change the log level while keeping the config in sync.
41
- def level=(level)
42
- super
43
- @config[:'log-level'] = level
44
- end
45
-
46
- # Displays the message according to application verbosity and logs it as info.
47
- def puts_and_logs(msg)
48
- puts msg if @config[:verbose]
49
- info(msg)
50
- end
51
-
52
- # Reset the logger regarding the config provided
53
- def set_app_config(config)
54
- @config = config
55
- add_cmd_line_options
56
- @config.load_config
57
- debug "Config layers:\n#{@config.internal_configs.to_yaml}"
58
- debug "Merged config:\n#{@config.to_yaml}"
59
- if config[:debug]
60
- if config[:'log-file']
61
- handing_over_to config[:'log-file']
62
- elsif config[:"debug-on-err"]
63
- handing_over_to STDERR
64
- else
65
- handing_over_to STDOUT
66
- end
67
- else
68
- close
69
- end
70
- self.level = config[:'log-level'] ? config[:'log-level'] : Severity::WARN
71
- self
72
- end
73
-
74
- private
75
-
76
-
77
- def add_cmd_line_options
78
- @config.add_command_line_section('Debug and logging options') do |slop|
79
- slop.on :debug, 'Run in debug mode.', :argument => false
80
- slop.on 'debug-on-err', 'Run in debug mode with output to stderr.', :argument => false
81
- slop.on 'log-level', "Log level from 0 to 5, default #{Severity::WARN}.", :argument => true, :as => Integer
82
- slop.on 'log-file', 'File to log to.', :argument => true
83
- end
84
- end
85
-
86
- # This class will act as a temporary logger, actually just keeping the history until the real
87
- # configuration for the logger is known. Then the history is displayed or not regarding the
88
- # definitive logger configuration.
89
- class TempLogger
90
- attr_reader :history
91
-
92
- def initialize
93
- @history = []
94
- end
95
-
96
- def write(data)
97
- return if closed?
98
- @history << data if @history
99
- end
100
-
101
- def close
102
- @closed = true
103
- end
104
-
105
- def opened?() not @closed ; end
106
- def closed?() @closed ; end
107
- end
108
-
109
- end
110
-
111
-
1
+ ################################################################################
2
+ # EasyAppHelper
3
+ #
4
+ # Copyright (c) 2013 L.Briais under MIT license
5
+ # http://opensource.org/licenses/MIT
6
+ ################################################################################
7
+
8
+ require 'logger'
9
+ require 'singleton'
10
+
11
+ # Official Ruby Logger re-opened to introduce a method to hand-over the temporary history from a temporary logger
12
+ # to the definitive one.
13
+ # TODO: Ensure only the messages that are above the current level are displayed when handing over to the definitive logger.
14
+ class Logger
15
+ def handing_over_to(log)
16
+ history = []
17
+ history = @logdev.dev.history if @logdev.dev.respond_to? :history
18
+ @logdev.close
19
+ @logdev = LogDevice.new log
20
+ history.each do |msg|
21
+ @logdev.write msg if ENV['DEBUG_EASY_MODULES'] or (msg =~ /^[WE]/)
22
+ end
23
+ end
24
+ end
25
+
26
+ # This is the logger that will be used by the application and any class that include {EasyAppHelper} module. It is
27
+ # configured by the {EasyAppHelper::Core::Config Config} object, and provides a temporary logger until the config
28
+ # is fully loaded.
29
+ class EasyAppHelper::Core::Logger < Logger
30
+ include Singleton
31
+
32
+
33
+ def initialize
34
+ @config = {}
35
+ super(TempLogger.new)
36
+ self.level = Severity::DEBUG
37
+ debug "Temporary initialisation logger created..."
38
+ end
39
+
40
+ # Change the log level while keeping the config in sync.
41
+ def level=(level, update_config = true)
42
+ super(level)
43
+ @config[:'log-level'] = level if update_config
44
+ end
45
+
46
+ # Displays the message according to application verbosity and logs it as info.
47
+ def puts_and_logs(msg)
48
+ puts msg if @config[:verbose]
49
+ info(msg)
50
+ end
51
+
52
+ # Reset the logger regarding the config provided
53
+ def set_app_config(config)
54
+ @config = config
55
+ add_cmd_line_options
56
+ @config.load_config
57
+ debug "Config layers:\n#{@config.internal_configs.to_yaml}"
58
+ debug "Merged config:\n#{@config.to_yaml}"
59
+ if config[:debug]
60
+ if config[:'log-file']
61
+ handing_over_to config[:'log-file']
62
+ elsif config[:"debug-on-err"]
63
+ handing_over_to STDERR
64
+ else
65
+ handing_over_to STDOUT
66
+ end
67
+ else
68
+ close
69
+ end
70
+ self.level = config[:'log-level'] ? config[:'log-level'] : Severity::WARN
71
+ self
72
+ end
73
+
74
+ private
75
+
76
+
77
+ def add_cmd_line_options
78
+ @config.add_command_line_section('Debug and logging options') do |slop|
79
+ slop.on :debug, 'Run in debug mode.', :argument => false
80
+ slop.on 'debug-on-err', 'Run in debug mode with output to stderr.', :argument => false
81
+ slop.on 'log-level', "Log level from 0 to 5, default #{Severity::WARN}.", :argument => true, :as => Integer
82
+ slop.on 'log-file', 'File to log to.', :argument => true
83
+ end
84
+ end
85
+
86
+ # This class will act as a temporary logger, actually just keeping the history until the real
87
+ # configuration for the logger is known. Then the history is displayed or not regarding the
88
+ # definitive logger configuration.
89
+ class TempLogger
90
+ attr_reader :history
91
+
92
+ def initialize
93
+ @history = []
94
+ end
95
+
96
+ def write(data)
97
+ return if closed?
98
+ @history << data if @history
99
+ end
100
+
101
+ def close
102
+ @closed = true
103
+ end
104
+
105
+ def opened?() not @closed ; end
106
+ def closed?() @closed ; end
107
+ end
108
+
109
+ end
110
+
111
+
@@ -1,37 +1,37 @@
1
- ################################################################################
2
- # EasyAppHelper
3
- #
4
- # Copyright (c) 2013 L.Briais under MIT license
5
- # http://opensource.org/licenses/MIT
6
- ################################################################################
7
-
8
- # This module proposes different merge policies for two hashes.
9
- module EasyAppHelper::Core::HashesMergePolicies
10
-
11
- # Performs a merge at the second level of hashes.
12
- # simple entries and arrays are overridden.
13
- def hashes_second_level_merge(h1, h2)
14
- h2.each do |key, v|
15
- if h1[key] and h1[key].is_a?(Hash)
16
- # Merges hashes
17
- h1[key].merge! h2[key]
18
- else
19
- # Overrides the rest
20
- h1[key] = h2[key] unless h2[key].nil?
21
- end
22
- end
23
- h1
24
- end
25
-
26
- # Uses the standard "merge!" method
27
- def simple_merge(h1, h2)
28
- h1.merge! h2
29
- end
30
-
31
- # Brutal override
32
- def override_merge(h1, h2)
33
- h1 = nil
34
- h1 = h2
35
-
36
- end
37
- end
1
+ ################################################################################
2
+ # EasyAppHelper
3
+ #
4
+ # Copyright (c) 2013 L.Briais under MIT license
5
+ # http://opensource.org/licenses/MIT
6
+ ################################################################################
7
+
8
+ # This module proposes different merge policies for two hashes.
9
+ module EasyAppHelper::Core::HashesMergePolicies
10
+
11
+ # Performs a merge at the second level of hashes.
12
+ # simple entries and arrays are overridden.
13
+ def hashes_second_level_merge(h1, h2)
14
+ h2.each do |key, v|
15
+ if h1[key] and h1[key].is_a?(Hash)
16
+ # Merges hashes
17
+ h1[key].merge! h2[key]
18
+ else
19
+ # Overrides the rest
20
+ h1[key] = h2[key] unless h2[key].nil?
21
+ end
22
+ end
23
+ h1
24
+ end
25
+
26
+ # Uses the standard "merge!" method
27
+ def simple_merge(h1, h2)
28
+ h1.merge! h2
29
+ end
30
+
31
+ # Brutal override
32
+ def override_merge(h1, h2)
33
+ h1 = nil
34
+ h1 = h2
35
+
36
+ end
37
+ end
@@ -1,52 +1,52 @@
1
- ################################################################################
2
- # EasyAppHelper
3
- #
4
- # Copyright (c) 2013 L.Briais under MIT license
5
- # http://opensource.org/licenses/MIT
6
- ################################################################################
7
-
8
- # Possible places regarding the OS
9
- # TODO: Add equivalent for Mac
10
- class EasyAppHelper::Core::Config::Places
11
- module Unix
12
- # Where could be stored admin configuration that rules all EasyAppHelper
13
- # based applications.
14
- POSSIBLE_PLACES = {
15
-
16
- system: ["/etc"],
17
-
18
- # Where could be stored global wide configuration
19
- global: ["/etc",
20
- "/usr/local/etc"],
21
-
22
- # Where could be stored user configuration
23
- user: ["#{ENV['HOME']}/.config"]
24
- }
25
- end
26
-
27
- module Windows
28
- # Where could be stored admin configuration that rules all EasyAppHelper
29
- # based applications.
30
- POSSIBLE_PLACES = {
31
-
32
- system: ["#{ENV['systemRoot']}/Config"],
33
-
34
- # Where could be stored global configuration
35
- global: ['C:/Windows/Config',
36
- "#{ENV['ALLUSERSPROFILE']}/Application Data"],
37
-
38
- # Where could be stored user configuration
39
- user: [ENV['APPDATA']]
40
- }
41
- end
42
-
43
- CONF ={
44
- mingw32: Windows
45
- }
46
- DEFAULT = Unix
47
-
48
- def self.get_OS_module
49
- conf = CONF[RbConfig::CONFIG['target_os'].to_sym]
50
- conf.nil? ? DEFAULT : conf
51
- end
1
+ ################################################################################
2
+ # EasyAppHelper
3
+ #
4
+ # Copyright (c) 2013 L.Briais under MIT license
5
+ # http://opensource.org/licenses/MIT
6
+ ################################################################################
7
+
8
+ # Possible places regarding the OS
9
+ # TODO: Add equivalent for Mac
10
+ class EasyAppHelper::Core::Config::Places
11
+ module Unix
12
+ # Where could be stored admin configuration that rules all EasyAppHelper
13
+ # based applications.
14
+ POSSIBLE_PLACES = {
15
+
16
+ system: ["/etc"],
17
+
18
+ # Where could be stored global wide configuration
19
+ global: ["/etc",
20
+ "/usr/local/etc"],
21
+
22
+ # Where could be stored user configuration
23
+ user: ["#{ENV['HOME']}/.config"]
24
+ }
25
+ end
26
+
27
+ module Windows
28
+ # Where could be stored admin configuration that rules all EasyAppHelper
29
+ # based applications.
30
+ POSSIBLE_PLACES = {
31
+
32
+ system: ["#{ENV['systemRoot']}/Config"],
33
+
34
+ # Where could be stored global configuration
35
+ global: ['C:/Windows/Config',
36
+ "#{ENV['ALLUSERSPROFILE']}/Application Data"],
37
+
38
+ # Where could be stored user configuration
39
+ user: [ENV['APPDATA']]
40
+ }
41
+ end
42
+
43
+ CONF ={
44
+ mingw32: Windows
45
+ }
46
+ DEFAULT = Unix
47
+
48
+ def self.get_OS_module
49
+ conf = CONF[RbConfig::CONFIG['target_os'].to_sym]
50
+ conf.nil? ? DEFAULT : conf
51
+ end
52
52
  end
@@ -1,68 +1,68 @@
1
- ################################################################################
2
- # EasyAppHelper
3
- #
4
- # Copyright (c) 2013 L.Briais under MIT license
5
- # http://opensource.org/licenses/MIT
6
- ################################################################################
7
-
8
- module EasyAppHelper::Core
9
-
10
- end
11
-
12
- require 'easy_app_helper/core/logger'
13
- require 'easy_app_helper/core/base'
14
- require 'easy_app_helper/core/config'
15
-
16
- # This module contains the exposed methods of the framework
17
- # It is included and extended into EasyAppHelper
18
- module EasyAppHelper::ModuleManager
19
-
20
- # @return [EasyAppHelper::Core::Logger] The application logger
21
- def logger
22
- @@logger
23
- end
24
-
25
- # @return [EasyAppHelper::Core::Config] The application config
26
- def config
27
- @@config
28
- end
29
-
30
- # Convenient method that logs at info level, but also outputs the message to STDOUT if
31
- # verbose is set in the config.
32
- # @param [String] msg to be displayed
33
- def puts_and_logs(msg)
34
- @@logger.puts_and_logs msg
35
- end
36
-
37
- # Method to do something (expects a block) unless --simulate is specified on the command line.
38
- # See {EasyAppHelper::Core::Base#safely_exec original implementation}.
39
- def safely_exec(message, *args, &block)
40
- @@config.safely_exec message, *args, &block
41
- end
42
-
43
-
44
- def self.included(base)
45
- init_core_modules
46
- base.extend self
47
- end
48
-
49
- ################################################################################
50
- private
51
-
52
- def self.init_logger
53
- @@logger ||= EasyAppHelper::Core::Logger.instance
54
- @@logger
55
- end
56
-
57
- def self.init_config
58
- @@config ||= EasyAppHelper::Core::Config.new @@logger
59
- @@logger.set_app_config(@@config)
60
- @@config
61
- end
62
-
63
- def self.init_core_modules
64
- init_logger
65
- init_config
66
- end
67
-
1
+ ################################################################################
2
+ # EasyAppHelper
3
+ #
4
+ # Copyright (c) 2013 L.Briais under MIT license
5
+ # http://opensource.org/licenses/MIT
6
+ ################################################################################
7
+
8
+ module EasyAppHelper::Core
9
+
10
+ end
11
+
12
+ require 'easy_app_helper/core/logger'
13
+ require 'easy_app_helper/core/base'
14
+ require 'easy_app_helper/core/config'
15
+
16
+ # This module contains the exposed methods of the framework
17
+ # It is included and extended into EasyAppHelper
18
+ module EasyAppHelper::ModuleManager
19
+
20
+ # @return [EasyAppHelper::Core::Logger] The application logger
21
+ def logger
22
+ @@logger
23
+ end
24
+
25
+ # @return [EasyAppHelper::Core::Config] The application config
26
+ def config
27
+ @@config
28
+ end
29
+
30
+ # Convenient method that logs at info level, but also outputs the message to STDOUT if
31
+ # verbose is set in the config.
32
+ # @param [String] msg to be displayed
33
+ def puts_and_logs(msg)
34
+ @@logger.puts_and_logs msg
35
+ end
36
+
37
+ # Method to do something (expects a block) unless --simulate is specified on the command line.
38
+ # See {EasyAppHelper::Core::Base#safely_exec original implementation}.
39
+ def safely_exec(message, *args, &block)
40
+ @@config.safely_exec message, *args, &block
41
+ end
42
+
43
+
44
+ def self.included(base)
45
+ init_core_modules
46
+ base.extend self
47
+ end
48
+
49
+ ################################################################################
50
+ private
51
+
52
+ def self.init_logger
53
+ @@logger ||= EasyAppHelper::Core::Logger.instance
54
+ @@logger
55
+ end
56
+
57
+ def self.init_config
58
+ @@config ||= EasyAppHelper::Core::Config.new @@logger
59
+ @@logger.set_app_config(@@config)
60
+ @@config
61
+ end
62
+
63
+ def self.init_core_modules
64
+ init_logger
65
+ init_config
66
+ end
67
+
68
68
  end