kafo_parsers 0.1.5 → 0.1.6

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 050298bac1a3127c0f8a02392ae7789d601d2136
4
- data.tar.gz: fc1fd7e4f29774e9882ffd4b67d9b08267cbff07
3
+ metadata.gz: 0a6477915864cbf2a6ec73458c997e3665cb70d6
4
+ data.tar.gz: 5556804196ff59db1f71c01f490544c90f6b92f5
5
5
  SHA512:
6
- metadata.gz: 2e2ebe79c2084ba65c66ee986d03097121e5429142aefce6a41c04fe7c671bd4c0051526dc31256e2986f7cc493519661b6cd0fd43521ea077bf2053b612f5e9
7
- data.tar.gz: 2abdd8b59c17f7bd10d1a93a4b223bde07aad6fcd8f4acde7d30ce42ee4f67b60497fe2ce569063e556775695f5dabfbd3f8e8a6f00c800c1ceefd16e84571da
6
+ metadata.gz: 1e1f5e24969c68aa7769e9969cd337d45292520780db95e97e4f11dd280bb1b6846d146829b1e0e51480fa103b9608241d2915466493a9e91ed0f971ab5e96e7
7
+ data.tar.gz: a447adc8b3af98f7fb4430b11ba659ddd6ba0518c139d652b27fe2e1a16f05dbe06f0ad0e0902dabae46653b84da25bf01b36887a0df26a7b6671e6b7227d616
data/README.md CHANGED
@@ -72,6 +72,86 @@ hash = KafoParsers::PuppetStringsModuleParser.parse('/puppet/module/manifests/in
72
72
  Output will be similar to PuppetModuleParser, only validations are not supported,
73
73
  since they are not parsed by puppet-strings.
74
74
 
75
+ ## Documentation syntax
76
+
77
+ ### RDoc syntax
78
+
79
+ Classes and defined types should be prefixed with a comment section with an RDoc
80
+ block, containing a description, headings for different parameter groups and
81
+ parameters laid out as shown below:
82
+
83
+ ```puppet
84
+ # Example class that installs Example
85
+ #
86
+ # Supports version 1 to 3.
87
+ #
88
+ # === Parameters::
89
+ #
90
+ # $foo:: Sets the value of foo in the Example config
91
+ #
92
+ # === Advanced parameters::
93
+ #
94
+ # $bar:: Sets the value of bar in the advanced config
95
+ ```
96
+
97
+ Parameters may have multi-line descriptions and can have extra attributes
98
+ defined on new lines below them. Supports:
99
+
100
+ ```puppet
101
+ # $foo:: Sets the value of foo in the Example config
102
+ # condition: $bar == 'use_foo'
103
+ # type: Optional[String]
104
+ ```
105
+
106
+ Supports:
107
+
108
+ * `condition:` an expression to determine if the parameter is used
109
+ * `type:` the data type of the parameter
110
+
111
+ Used by:
112
+
113
+ * `PuppetModuleParser`
114
+ * `PuppetStringsModuleParser` (but deprecated, prefer YARD)
115
+
116
+ ### YARD syntax
117
+
118
+ Classes and defined types should be prefixed with a comment section in YARD
119
+ following the Puppet Strings documentation standard, as shown below:
120
+
121
+ ```puppet
122
+ # Example class that installs Example
123
+ #
124
+ # Supports version 1 to 3.
125
+ #
126
+ # @param foo Sets the value of foo in the Example config
127
+ # @param bar Sets the value of bar in the advanced config
128
+ # group: Advanced parameters
129
+ ```
130
+
131
+ Parameters may have multi-line descriptions and can have extra attributes
132
+ defined on new lines below them. Supports:
133
+
134
+ ```puppet
135
+ # @param foo Sets the value of foo in the Example config
136
+ # condition: $bar == 'use_foo'
137
+ ```
138
+
139
+ Supports:
140
+
141
+ * `condition:` an expression to determine if the parameter is used
142
+ * `group:` comma-separated list of groups, increasing in specificity
143
+
144
+ Data types are given in the parameter list of the class, or can be given inline
145
+ for Puppet 3 compatibility:
146
+
147
+ ```puppet
148
+ # @param foo [Integer] Sets the value of foo in the Example config
149
+ ```
150
+
151
+ Used by:
152
+
153
+ * `PuppetStringsModuleParser`
154
+
75
155
  # License
76
156
 
77
157
  This project is licensed under the GPLv3+.
@@ -3,6 +3,7 @@ require 'rdoc'
3
3
  require 'rdoc/markup' # required for RDoc < 0.9.5
4
4
  require 'rdoc/markup/parser' # required for RDoc < 0.9.5
5
5
  require 'kafo_parsers/exceptions'
6
+ require 'kafo_parsers/param_doc_parser'
6
7
 
7
8
  module KafoParsers
8
9
  class DocParser
@@ -15,7 +16,7 @@ module KafoParsers
15
16
  @docs = {}
16
17
  @groups = {}
17
18
  @conditions = {}
18
- @types = Hash.new('string')
19
+ @types = {}
19
20
  @rdoc = rdoc_parser.parse(@text)
20
21
  end
21
22
 
@@ -46,34 +47,17 @@ module KafoParsers
46
47
  return if label.nil?
47
48
  key = label.gsub(/[^A-Za-z0-9_-]/, '')
48
49
  @groups[key] = current_groups
49
- text_parts = para.parts.first.parts.map!(&:strip)
50
- attributes, docs = text_parts.partition { |line| line =~ ATTRIBUTE_LINE }
51
- parse_attributes(key, attributes)
52
- @docs[key] = docs
53
- end
50
+ text_parts = para.parts.first.parts
54
51
 
55
- def parse_attributes(parameter, attributes)
56
- condition = nil
57
- attributes.each do |attribute|
58
- data = attribute.match(ATTRIBUTE_LINE)
59
- name, value = data[1], data[2]
60
-
61
- case name
62
- when 'type'
63
- @types[parameter] = value
64
- when 'condition'
65
- if condition.nil?
66
- condition = value
67
- else
68
- raise KafoParsers::DocParseError, "Two or more conditions defined for #{name}"
69
- end
70
- else
71
- raise KafoParsers::DocParseError, "Unknown attribute #{name}"
72
- end
52
+ param_parser = ParamDocParser.new(key, text_parts)
53
+ @docs[key] = param_parser.doc
54
+ parse_attributes(key, param_parser)
55
+ end
73
56
 
74
- end
75
- condition = [current_condition, condition].compact.join(' && ')
57
+ def parse_attributes(parameter, parser)
58
+ condition = [current_condition, parser.condition].compact.join(' && ')
76
59
  @conditions[parameter] = condition.empty? ? nil : condition
60
+ @types[parameter] = parser.type unless parser.type.nil?
77
61
  end
78
62
 
79
63
  def parse_header(heading)
@@ -0,0 +1,42 @@
1
+ require 'kafo_parsers/exceptions'
2
+
3
+ module KafoParsers
4
+ class ParamDocParser
5
+ ATTRIBUTE_LINE = /^(condition|group|type)\s*:\s*(.*)/
6
+
7
+ def initialize(param, text)
8
+ @param = param
9
+ @metadata = {}
10
+ parse_paragraph([text].flatten)
11
+ end
12
+
13
+ attr_reader :param, :doc
14
+ [:condition, :group, :type].each do |attr|
15
+ define_method(attr) do
16
+ @metadata[attr]
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def parse_paragraph(text)
23
+ text_parts = text.map(&:strip)
24
+ attributes, docs = text_parts.partition { |line| line =~ ATTRIBUTE_LINE }
25
+ parse_attributes(attributes)
26
+ @doc = docs
27
+ end
28
+
29
+ def parse_attributes(attributes)
30
+ attributes.each do |attribute|
31
+ data = attribute.match(ATTRIBUTE_LINE)
32
+ name, value = data[1], data[2]
33
+ raise KafoParsers::DocParseError, "Two or more #{name} lines defined for #{param}" if @metadata.key?(name)
34
+ @metadata[name.to_sym] = value
35
+ end
36
+
37
+ if @metadata.key?(:group)
38
+ @metadata[:group] = @metadata[:group].split(/\s*,\s*/)
39
+ end
40
+ end
41
+ end
42
+ end
@@ -83,14 +83,14 @@ module KafoParsers
83
83
  def values
84
84
  parameters = {}
85
85
  arguments = @object.respond_to?(:arguments) ? @object.arguments : {}
86
- arguments.each { |k, v| parameters[k] = v.respond_to?(:value) ? v.value : nil }
86
+ arguments.each { |k, v| parameters[k] = interpret_ast([v])[0] }
87
87
  parameters
88
88
  end
89
89
 
90
90
  def validations(param = nil)
91
91
  return [] if @object.code.nil?
92
92
  @object.code.select { |stmt| stmt.is_a?(Puppet::Parser::AST::Function) && stmt.name =~ /^validate_/ }.map do |v|
93
- Validation.new(v.name, interpret_validation_args(v.arguments))
93
+ Validation.new(v.name, interpret_ast(v.arguments))
94
94
  end
95
95
  end
96
96
 
@@ -110,7 +110,7 @@ module KafoParsers
110
110
  parser = DocParser.new(@object.doc).parse
111
111
  data[:docs] = parser.docs
112
112
  data[:groups] = parser.groups
113
- data[:types] = parser.types
113
+ data[:types] = Hash.new('string').merge(parser.types)
114
114
  data[:conditions] = parser.conditions
115
115
  data[:object_type] = @object.type.to_s
116
116
  end
@@ -119,14 +119,14 @@ module KafoParsers
119
119
 
120
120
  private
121
121
 
122
- def interpret_validation_args(args)
122
+ def interpret_ast(args)
123
123
  args.map do |arg|
124
124
  if arg.is_a?(Puppet::Parser::AST::Variable)
125
125
  arg.to_s
126
126
  elsif arg.is_a?(Puppet::Parser::AST::ASTArray)
127
- interpret_validation_args(arg.to_a)
127
+ interpret_ast(arg.to_a)
128
128
  elsif arg.is_a?(Puppet::Parser::AST::Concat)
129
- interpret_validation_args(arg.value).join
129
+ interpret_ast(arg.value).join
130
130
  elsif arg.respond_to? :value
131
131
  arg.value
132
132
  else
@@ -1,6 +1,7 @@
1
1
  # encoding: UTF-8
2
2
  require 'json'
3
3
  require 'kafo_parsers/doc_parser'
4
+ require 'kafo_parsers/param_doc_parser'
4
5
 
5
6
  module KafoParsers
6
7
  class PuppetStringsModuleParser
@@ -91,7 +92,7 @@ module KafoParsers
91
92
 
92
93
  # returns data in following form
93
94
  # {
94
- # :docs => { $param1 => 'documentation without types and conditions'}
95
+ # :docs => { $param1 => ['documentation without types and conditions']}
95
96
  # :types => { $param1 => 'boolean'},
96
97
  # :groups => { $param1 => ['Parameters', 'Advanced']},
97
98
  # :conditions => { $param1 => '$db_type == "mysql"'},
@@ -101,11 +102,29 @@ module KafoParsers
101
102
  if @parsed_hash.nil?
102
103
  raise KafoParsers::DocParseError, "no documentation found for manifest #{@file}, parsing error?"
103
104
  elsif !@parsed_hash['docstring'].nil? && !@parsed_hash['docstring']['text'].nil?
104
- parser = DocParser.new(@parsed_hash['docstring']['text']).parse
105
- data[:docs] = parser.docs
106
- data[:groups] = parser.groups
107
- data[:types] = merge_doc_types(parser.types)
108
- data[:conditions] = parser.conditions
105
+ # Lowest precedence: types given in the strings hash from class definition
106
+ tag_params.each do |param|
107
+ data[:types][param['name']] = param['types'][0] unless param['types'].nil?
108
+ end
109
+
110
+ # Next: types and other data from RDoc parser
111
+ rdoc_parser = DocParser.new(@parsed_hash['docstring']['text']).parse
112
+ data[:docs] = rdoc_parser.docs
113
+ data[:groups] = rdoc_parser.groups
114
+ data[:conditions] = rdoc_parser.conditions
115
+ data[:types].merge! rdoc_parser.types
116
+
117
+ # Highest precedence: data in YARD @param stored in the 'text' field
118
+ tag_params.each do |param|
119
+ param_name = param['name']
120
+ unless param['text'].nil? || param['text'].empty?
121
+ param_parser = ParamDocParser.new(param_name, param['text'].split($/))
122
+ data[:docs][param_name] = param_parser.doc if param_parser.doc
123
+ data[:groups][param_name] = param_parser.group if param_parser.group
124
+ data[:conditions][param_name] = param_parser.condition if param_parser.condition
125
+ data[:types][param_name] = param_parser.type if param_parser.type
126
+ end
127
+ end
109
128
  end
110
129
  data
111
130
  end
@@ -113,7 +132,6 @@ module KafoParsers
113
132
  private
114
133
 
115
134
  # default values using puppet strings includes $ symbol, e.g. "$::foreman::params::ssl"
116
- # to keep the same API we strip $ if it's present
117
135
  #
118
136
  # values are reported as strings which is issue especially for :under
119
137
  # strings are double quoted
@@ -122,16 +140,11 @@ module KafoParsers
122
140
  if (value.start_with?("'") && value.end_with?("'")) || (value.start_with?('"') && value.end_with?('"'))
123
141
  value = value[1..-2]
124
142
  end
125
- value = value[1..-1] if value.start_with?('$')
126
143
  value = :undef if value == 'undef'
127
144
 
128
145
  value
129
146
  end
130
147
 
131
- def merge_doc_types(types)
132
- Hash[tag_params.select { |param| !param['types'].nil? }.map { |param| [ param['name'], param['types'].first ] }].merge(types)
133
- end
134
-
135
148
  def tag_params
136
149
  if @parsed_hash['docstring']['tags']
137
150
  @parsed_hash['docstring']['tags'].select { |tag| tag['tag_name'] == 'param' }
@@ -1,3 +1,3 @@
1
1
  module KafoParsers
2
- VERSION = "0.1.5"
2
+ VERSION = "0.1.6"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kafo_parsers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marek Hulan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-11-23 00:00:00.000000000 Z
11
+ date: 2017-01-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -122,6 +122,7 @@ files:
122
122
  - lib/kafo_parsers.rb
123
123
  - lib/kafo_parsers/doc_parser.rb
124
124
  - lib/kafo_parsers/exceptions.rb
125
+ - lib/kafo_parsers/param_doc_parser.rb
125
126
  - lib/kafo_parsers/parsers.rb
126
127
  - lib/kafo_parsers/puppet_module_parser.rb
127
128
  - lib/kafo_parsers/puppet_strings_module_parser.rb