kafo 0.2.2 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of kafo might be problematic. Click here for more details.

data/lib/kafo/logger.rb CHANGED
@@ -2,64 +2,66 @@
2
2
  require 'fileutils'
3
3
  require 'logging'
4
4
 
5
- class Logger
6
- class << self
7
- attr_accessor :loggers
8
- end
5
+ module Kafo
6
+ class Logger
7
+ class << self
8
+ attr_accessor :loggers
9
+ end
9
10
 
10
- PATTERN = "[%5l %d %c] %m\n"
11
- Logging.color_scheme('bright',
12
- :levels => {
13
- :info => :green,
14
- :warn => :yellow,
15
- :error => :red,
16
- :fatal => [:white, :on_red]
17
- },
18
- :date => :blue,
19
- :logger => :cyan,
20
- :line => :yellow,
21
- :file => :yellow,
22
- :method => :yellow
23
- )
24
- COLOR_LAYOUT = Logging::Layouts::Pattern.new(:pattern => PATTERN, :color_scheme => 'bright')
25
- NOCOLOR_LAYOUT = Logging::Layouts::Pattern.new(:pattern => PATTERN, :color_scheme => nil)
11
+ PATTERN = "[%5l %d %c] %m\n"
12
+ Logging.color_scheme('bright',
13
+ :levels => {
14
+ :info => :green,
15
+ :warn => :yellow,
16
+ :error => :red,
17
+ :fatal => [:white, :on_red]
18
+ },
19
+ :date => :blue,
20
+ :logger => :cyan,
21
+ :line => :yellow,
22
+ :file => :yellow,
23
+ :method => :yellow
24
+ )
25
+ COLOR_LAYOUT = Logging::Layouts::Pattern.new(:pattern => PATTERN, :color_scheme => 'bright')
26
+ NOCOLOR_LAYOUT = Logging::Layouts::Pattern.new(:pattern => PATTERN, :color_scheme => nil)
26
27
 
27
- def self.setup
28
- begin
29
- FileUtils.mkdir_p(KafoConfigure.config.app[:log_dir], :mode => 0750)
30
- rescue Errno::EACCES => e
31
- puts "No permissions to create log dir #{KafoConfigure.config.app[:log_dir]}"
32
- end
28
+ def self.setup
29
+ begin
30
+ FileUtils.mkdir_p(KafoConfigure.config.app[:log_dir], :mode => 0750)
31
+ rescue Errno::EACCES => e
32
+ puts "No permissions to create log dir #{KafoConfigure.config.app[:log_dir]}"
33
+ end
33
34
 
34
- logger = Logging.logger['main']
35
- filename = "#{KafoConfigure.config.app[:log_dir]}/#{KafoConfigure.config.app[:log_name] || 'configure.log'}"
36
- begin
37
- logger.appenders = ::Logging.appenders.rolling_file('configure',
38
- :filename => filename,
39
- :layout => NOCOLOR_LAYOUT,
40
- :truncate => true
41
- )
42
- # set owner and group (it's ignored if attribute is nil)
43
- FileUtils.chown KafoConfigure.config.app[:log_owner], KafoConfigure.config.app[:log_group], filename
44
- rescue ArgumentError => e
45
- puts "File #{filename} not writeable, won't log anything to file!"
46
- end
35
+ logger = Logging.logger['main']
36
+ filename = "#{KafoConfigure.config.app[:log_dir]}/#{KafoConfigure.config.app[:log_name] || 'configure.log'}"
37
+ begin
38
+ logger.appenders = ::Logging.appenders.rolling_file('configure',
39
+ :filename => filename,
40
+ :layout => NOCOLOR_LAYOUT,
41
+ :truncate => true
42
+ )
43
+ # set owner and group (it's ignored if attribute is nil)
44
+ FileUtils.chown KafoConfigure.config.app[:log_owner], KafoConfigure.config.app[:log_group], filename
45
+ rescue ArgumentError => e
46
+ puts "File #{filename} not writeable, won't log anything to file!"
47
+ end
47
48
 
48
- logger.level = KafoConfigure.config.app[:log_level]
49
- self.loggers = [logger]
50
- end
49
+ logger.level = KafoConfigure.config.app[:log_level]
50
+ self.loggers = [logger]
51
+ end
51
52
 
52
- def self.setup_verbose
53
- logger = Logging.logger['verbose']
54
- logger.level = KafoConfigure.config.app[:verbose_log_level]
55
- layout = KafoConfigure.config.app[:colors] ? COLOR_LAYOUT : NOCOLOR_LAYOUT
56
- logger.appenders = [::Logging.appenders.stdout(:layout => layout)]
57
- self.loggers<< logger
58
- end
53
+ def self.setup_verbose
54
+ logger = Logging.logger['verbose']
55
+ logger.level = KafoConfigure.config.app[:verbose_log_level]
56
+ layout = KafoConfigure.config.app[:colors] ? COLOR_LAYOUT : NOCOLOR_LAYOUT
57
+ logger.appenders = [::Logging.appenders.stdout(:layout => layout)]
58
+ self.loggers<< logger
59
+ end
59
60
 
60
- %w(fatal error warn info debug).each do |name|
61
- define_method(name) do |*args, &block|
62
- self.class.loggers.each { |logger| logger.send name, *args, &block }
61
+ %w(fatal error warn info debug).each do |name|
62
+ define_method(name) do |*args, &block|
63
+ self.class.loggers.each { |logger| logger.send name, *args, &block }
64
+ end
63
65
  end
64
66
  end
65
67
  end
data/lib/kafo/param.rb CHANGED
@@ -1,82 +1,105 @@
1
1
  # encoding: UTF-8
2
- class Param
3
- attr_reader :name, :module
4
- attr_accessor :default, :doc, :value_set
2
+ require 'kafo/condition'
5
3
 
6
- def initialize(builder, name)
7
- @name = name
8
- @module = builder
9
- end
4
+ module Kafo
5
+ class Param
6
+ attr_reader :name, :module
7
+ attr_accessor :default, :doc, :value_set, :condition
8
+ attr_writer :groups
10
9
 
11
- # we use @value_set flag because even nil can be valid value
12
- def value
13
- @value_set ? @value : default
14
- end
10
+ def initialize(builder, name)
11
+ @name = name
12
+ @module = builder
13
+ end
15
14
 
16
- def value=(value)
17
- @value_set = true
18
- @value = value == 'UNDEF' ? nil : value
19
- end
15
+ def groups
16
+ @groups || []
17
+ end
20
18
 
21
- def module_name
22
- self.module.name
23
- end
19
+ # we use @value_set flag because even nil can be valid value
20
+ def value
21
+ @value_set ? @value : default
22
+ end
24
23
 
25
- def to_s
26
- "#<#{self.class}:#{self.object_id} @name=#{name.inspect} @default=#{default.inspect} @value=#{value.inspect}>"
27
- end
24
+ def value=(value)
25
+ @value_set = true
26
+ @value = value == 'UNDEF' ? nil : value
27
+ end
28
28
 
29
- def set_default(defaults)
30
- if default == 'UNSET'
31
- self.value = nil
32
- else
33
- self.value = (value = defaults[default]) == :undef ? nil : value
29
+ def module_name
30
+ self.module.name
34
31
  end
35
- end
36
32
 
37
- def set_value_by_config(config)
38
- base = config[module_name]
39
- self.value = base[name] if base.has_key?(name)
40
- end
33
+ def to_s
34
+ "#<#{self.class}:#{self.object_id} @name=#{name.inspect} @default=#{default.inspect} @value=#{value.inspect}>"
35
+ end
41
36
 
42
- def valid?
43
- validations = self.module.validations(self)
44
- # we get validations that can also run on other arguments, we need to take only current param
45
- # also we want to clone validations so we don't interfere
46
- validations.map! do |v|
47
- v = v.clone
48
- if v.name == 'validate_re'
49
- # validate_re does not take more variables as arguments, instead we need to pass all arguments
50
- args = v.arguments
37
+ def set_default(defaults)
38
+ if default == 'UNSET'
39
+ self.value = nil
51
40
  else
52
- args = v.arguments.select { |a| a.to_s == "$#{self.name}" }
41
+ self.value = (value = defaults[default]) == :undef ? nil : value
53
42
  end
54
- v.arguments = Puppet::Parser::AST::ASTArray.new :children => args
55
- v
56
43
  end
57
44
 
58
- validator = Validator.new([self])
59
- validations.map! do |v|
60
- result = v.evaluate(validator)
61
- # validate_re returns nil if succeeds
62
- result = true if v.name == 'validate_re' && result.nil?
63
- result
45
+ def set_value_by_config(config)
46
+ base = config[module_name]
47
+ self.value = base[name] if base.has_key?(name)
64
48
  end
65
49
 
66
- validations.all?
67
- end
50
+ def valid?
51
+ validations = self.module.validations(self)
52
+ # we get validations that can also run on other arguments, we need to take only current param
53
+ # also we want to clone validations so we don't interfere
54
+ validations.map! do |v|
55
+ v = v.clone
56
+ if v.name == 'validate_re'
57
+ # validate_re does not take more variables as arguments, instead we need to pass all arguments
58
+ args = v.arguments
59
+ else
60
+ args = v.arguments.select { |a| a.to_s == "$#{self.name}" }
61
+ end
62
+ v.arguments = Puppet::Parser::AST::ASTArray.new :children => args
63
+ v
64
+ end
68
65
 
69
- # To be overwritten in children
70
- def multivalued?
71
- false
72
- end
66
+ validator = Validator.new([self])
67
+ validations.map! do |v|
68
+ result = v.evaluate(validator)
69
+ # validate_re returns nil if succeeds
70
+ result = true if v.name == 'validate_re' && result.nil?
71
+ result
72
+ end
73
+
74
+ validations.all?
75
+ end
76
+
77
+ # To be overwritten in children
78
+ def multivalued?
79
+ false
80
+ end
81
+
82
+ def <=> o
83
+ unless KafoConfigure.config.app[:no_prefix]
84
+ r = self.module_name <=> o.module_name
85
+ return r unless r == 0
86
+ end
87
+ self.name <=> o.name
88
+ end
89
+
90
+ def visible?(context = [])
91
+ condition.nil? || condition.empty? ? true : evaluate_condition(context)
92
+ end
93
+
94
+ def condition_value
95
+ value.to_s
96
+ end
97
+
98
+ private
73
99
 
74
- def <=> o
75
- unless KafoConfigure.config.app[:no_prefix]
76
- r = self.module_name <=> o.module_name
77
- return r unless r == 0
100
+ def evaluate_condition(context = [])
101
+ Condition.new(condition, context).evaluate
78
102
  end
79
- self.name <=> o.name
80
103
  end
81
104
  end
82
105
 
@@ -1,62 +1,86 @@
1
1
  # encoding: UTF-8
2
- class ParamBuilder
3
- ATTRIBUTE_RE = /^(type):(.*)/
2
+ require 'kafo/param_group'
4
3
 
5
- def initialize(mod, data)
6
- @data = data
7
- @module = mod
8
- end
4
+ module Kafo
5
+ class ParamBuilder
6
+ def initialize(mod, data)
7
+ @data = data
8
+ @module = mod
9
+ @groups = []
10
+ end
9
11
 
10
- def validate
11
- return true if KafoConfigure.config.app[:ignore_undocumented]
12
+ def validate
13
+ return true if KafoConfigure.config.app[:ignore_undocumented]
12
14
 
13
- parameters = @data['parameters'].keys.sort
14
- docs = @data['docs'].keys.sort
15
- if parameters == docs
16
- return true
17
- else
18
- undocumented = parameters - docs
19
- raise ConfigurationException, "undocumented parameters in #{@module.name}: #{undocumented.join(', ')}" unless undocumented.empty?
20
- deleted = docs - parameters
21
- raise ConfigurationException, "documentation mentioned unknown parameters in #{@module.name}: #{deleted.join(', ')}" unless deleted.empty?
22
- raise ConfigurationException, "unknown error in configuration in #{@module.name}"
15
+ parameters = @data[:parameters].sort
16
+ docs = @data[:docs].keys.sort
17
+ if parameters == docs
18
+ return true
19
+ else
20
+ undocumented = parameters - docs
21
+ raise ConfigurationException, "undocumented parameters in #{@module.name}: #{undocumented.join(', ')}" unless undocumented.empty?
22
+ deleted = docs - parameters
23
+ raise ConfigurationException, "documentation mentioned unknown parameters in #{@module.name}: #{deleted.join(', ')}" unless deleted.empty?
24
+ raise ConfigurationException, "unknown error in configuration in #{@module.name}"
25
+ end
23
26
  end
24
- end
25
27
 
26
- def build_params
27
- @data['parameters'].keys.map do |param_name|
28
- build(param_name, @data['parameters'][param_name], @data['docs'][param_name])
28
+ def build_params
29
+ @data[:parameters].map do |param_name|
30
+ build(param_name, @data)
31
+ end
29
32
  end
30
- end
31
33
 
32
- def build(name, default, docs)
33
- param = get_type(docs).new(@module, name)
34
- param.default = default
35
- param.doc = get_documentation(docs)
36
- param
37
- end
38
34
 
39
- private
35
+ def build_param_groups(params)
36
+ data = Hash[get_parameters_groups_by_param_name]
37
+ data.each do |param_name, param_groups|
38
+ param_groups.each_with_index do |group_name, i|
39
+ param_group = find_or_build_group(group_name)
40
+ if i + 1 < param_groups.size
41
+ param_group.add_child find_or_build_group(param_groups[i + 1])
42
+ end
40
43
 
41
- def get_documentation(docs)
42
- return nil if docs.nil?
43
- docs.select { |line| line !~ ATTRIBUTE_RE }
44
- end
44
+ param_group = find_or_build_group(param_groups.last)
45
+ param = params.detect { |p| p.name == param_name }
46
+ param_group.add_param param unless param.nil?
47
+ end
48
+ end
45
49
 
46
- def get_type(docs)
47
- type = (get_attributes(docs)[:type] || '').capitalize
48
- type.empty? || !Params.const_defined?(type) ? Params::String : Params.const_get(type)
49
- end
50
+ # top level groups
51
+ data.values.map(&:first).compact.uniq.map { |name| @groups.detect { |g| g.name == name } }
52
+ end
50
53
 
51
- def get_attributes(docs)
52
- data = {}
53
- return data if docs.nil?
54
+ def build(name, data)
55
+ param = get_type(data[:types][name]).new(@module, name)
56
+ param.default = data[:values][name]
57
+ param.doc = data[:docs][name]
58
+ param.groups = data[:groups][name]
59
+ param.condition = data[:conditions][name]
60
+ param
61
+ end
54
62
 
55
- docs.each do |line|
56
- if line =~ ATTRIBUTE_RE
57
- data[$1.to_sym] = $2
63
+ private
64
+
65
+ def get_parameters_groups_by_param_name
66
+ @data[:groups].map do |name, groups|
67
+ [name, groups.select { |g| g =~ /parameters/i }]
58
68
  end
59
69
  end
60
- data
70
+
71
+ def find_or_build_group(name)
72
+ param_group = @groups.detect { |g| g.name == name }
73
+ unless param_group
74
+ param_group = ParamGroup.new(name)
75
+ param_group.module = @module
76
+ @groups.push param_group
77
+ end
78
+ param_group
79
+ end
80
+
81
+ def get_type(type)
82
+ type = type.capitalize
83
+ Params.const_defined?(type) ? Params.const_get(type) : raise(TypeError, "undefined parameter type '#{type}'")
84
+ end
61
85
  end
62
86
  end
@@ -0,0 +1,27 @@
1
+ #encoding: UTF-8
2
+
3
+ module Kafo
4
+ class ParamGroup
5
+ attr_reader :children, :params, :name
6
+ attr_accessor :module
7
+
8
+
9
+ def initialize(name)
10
+ @children = []
11
+ @params = []
12
+ @name = name
13
+ end
14
+
15
+ def formatted_name
16
+ @formatted_name ||= @name.sub(/:\Z/,'')
17
+ end
18
+
19
+ def add_child(group)
20
+ @children.push group unless @children.include?(group)
21
+ end
22
+
23
+ def add_param(param)
24
+ @params.push param unless @params.include?(param)
25
+ end
26
+ end
27
+ end
@@ -1,18 +1,20 @@
1
- module Params
2
- class Array < Param
3
- def value=(value)
4
- super
5
- @value = typecast(@value)
6
- end
1
+ module Kafo
2
+ module Params
3
+ class Array < Param
4
+ def value=(value)
5
+ super
6
+ @value = typecast(@value)
7
+ end
7
8
 
8
- def multivalued?
9
- true
10
- end
9
+ def multivalued?
10
+ true
11
+ end
11
12
 
12
- private
13
+ private
13
14
 
14
- def typecast(value)
15
- value.nil? ? nil : [value].flatten
15
+ def typecast(value)
16
+ value.nil? ? nil : [value].flatten
17
+ end
16
18
  end
17
19
  end
18
20
  end