kafo_parsers 0.1.4 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: b0c3926a766e0ebd3f3d156451176f9d02e68a21
4
- data.tar.gz: 98db6338da1c9acf47304c93dc4d56a75a07d6bb
2
+ SHA256:
3
+ metadata.gz: 62b061db2fb2d7f269541ff5a7865f7482703fb4766432e3117980d51fb2cc0e
4
+ data.tar.gz: 2e6135c7beb9ccb754008f0ea1842c67a82b27cd8bcb96a43960f6e415f1a640
5
5
  SHA512:
6
- metadata.gz: d23d9e3fc141268358563fef5ccf804d9b073a37b093cefb287652aff56a792c67c6fa222a3e42a48add29c5910fb053fd3fb7a6c66b255a755c728974ffb24b
7
- data.tar.gz: 4ab94d2975ca76e8e634e655cb08d41816006539de778af31550af18f12d5973dc0dfa04d9b1c93be371beb51aa82fe60ad5c103a30257ecfe050f4a4cb1c47b
6
+ metadata.gz: e9ada8c7ac628dc9631a97cd9587200e2f5320449919ab7954f41c53b112c5115e436d2880fd5158bde2293538d7521c9c7b235a63fc295eeaf0155514c54c82
7
+ data.tar.gz: 7adf7c7d9c4851c0bc5668dfd1b34adc0e0792dccf30d4e83faf62f915cef876913cd6fe60904ed68073ee215c4a361dec99d04d034738b5346906a6e7523175
data/README.md CHANGED
@@ -44,33 +44,99 @@ KafoParsers::Parsers.find_available(:logger => logger)
44
44
 
45
45
  To load a specific parser:
46
46
  ```ruby
47
- require 'kafo_parsers/puppet_module_parser'
48
- hash = KafoParsers::PuppetModuleParser.parse('/puppet/module/manifests/init.pp')
47
+ require 'kafo_parsers/puppet_strings_module_parser'
48
+ hash = KafoParsers::PuppetStringsModuleParser.parse('/puppet/module/manifests/init.pp')
49
49
  ```
50
50
 
51
- #### PuppetModuleParser
52
-
53
- The standard PuppetModuleParser loads Puppet as a regular library or gem, so it
54
- must be installed in the same Ruby that's running kafo_parsers.
55
-
56
- Only Puppet versions 2.6.x, 2.7.x and 3.x are supported.
57
-
58
- Add `gem 'puppet', '< 4'` to your application's Gemfile to use this.
59
-
60
51
  #### PuppetStringsModuleParser
61
52
 
62
- If you use Puppet 4 you prefer PuppetStringsModuleParser which leverages
63
- puppet-strings to parse puppet manifests. This requires puppet-strings 0.99 or
64
- higher and may be installed either as a gem in the same environment, or in a
65
- Puppet AIO installation.
53
+ Leverage puppet-strings to parse puppet manifests. This requires puppet-strings
54
+ 1.2.0 or higher and may be installed either as a gem in the same environment,
55
+ or in a Puppet AIO installation.
66
56
 
67
57
  ```ruby
68
58
  require 'kafo_parsers/puppet_strings_module_parser'
69
59
  hash = KafoParsers::PuppetStringsModuleParser.parse('/puppet/module/manifests/init.pp')
70
60
  ```
71
61
 
72
- Output will be similar to PuppetModuleParser, only validations are not supported,
73
- since they are not parsed by puppet-strings.
62
+ ## Documentation syntax
63
+
64
+ ### RDoc syntax
65
+
66
+ Classes and defined types should be prefixed with a comment section with an RDoc
67
+ block, containing a description, headings for different parameter groups and
68
+ parameters laid out as shown below:
69
+
70
+ ```puppet
71
+ # Example class that installs Example
72
+ #
73
+ # Supports version 1 to 3.
74
+ #
75
+ # === Parameters::
76
+ #
77
+ # $foo:: Sets the value of foo in the Example config
78
+ #
79
+ # === Advanced parameters::
80
+ #
81
+ # $bar:: Sets the value of bar in the advanced config
82
+ ```
83
+
84
+ Parameters may have multi-line descriptions and can have extra attributes
85
+ defined on new lines below them. Supports:
86
+
87
+ ```puppet
88
+ # $foo:: Sets the value of foo in the Example config
89
+ # condition: $bar == 'use_foo'
90
+ # type: Optional[String]
91
+ ```
92
+
93
+ Supports:
94
+
95
+ * `condition:` an expression to determine if the parameter is used
96
+ * `type:` the data type of the parameter
97
+
98
+ Used by:
99
+
100
+ * `PuppetStringsModuleParser` (but deprecated, prefer YARD)
101
+
102
+ ### YARD syntax
103
+
104
+ Classes and defined types should be prefixed with a comment section in YARD
105
+ following the Puppet Strings documentation standard, as shown below:
106
+
107
+ ```puppet
108
+ # Example class that installs Example
109
+ #
110
+ # Supports version 1 to 3.
111
+ #
112
+ # @param foo Sets the value of foo in the Example config
113
+ # @param bar Sets the value of bar in the advanced config
114
+ # group: Advanced parameters
115
+ ```
116
+
117
+ Parameters may have multi-line descriptions and can have extra attributes
118
+ defined on new lines below them. Supports:
119
+
120
+ ```puppet
121
+ # @param foo Sets the value of foo in the Example config
122
+ # condition: $bar == 'use_foo'
123
+ ```
124
+
125
+ Supports:
126
+
127
+ * `condition:` an expression to determine if the parameter is used
128
+ * `group:` comma-separated list of groups, increasing in specificity
129
+
130
+ Data types are given in the parameter list of the class, or can be given inline
131
+ for Puppet 3 compatibility:
132
+
133
+ ```puppet
134
+ # @param foo [Integer] Sets the value of foo in the Example config
135
+ ```
136
+
137
+ Used by:
138
+
139
+ * `PuppetStringsModuleParser`
74
140
 
75
141
  # License
76
142
 
@@ -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
@@ -1,10 +1,9 @@
1
- require 'kafo_parsers/puppet_module_parser.rb'
2
1
  require 'kafo_parsers/puppet_strings_module_parser.rb'
3
2
 
4
3
  module KafoParsers
5
4
  module Parsers
6
5
  def self.all
7
- [ PuppetModuleParser, PuppetStringsModuleParser ]
6
+ [ PuppetStringsModuleParser ]
8
7
  end
9
8
 
10
9
  def self.find_available(options = {})
@@ -1,6 +1,9 @@
1
1
  # encoding: UTF-8
2
2
  require 'json'
3
+ require 'open3'
4
+
3
5
  require 'kafo_parsers/doc_parser'
6
+ require 'kafo_parsers/param_doc_parser'
4
7
 
5
8
  module KafoParsers
6
9
  class PuppetStringsModuleParser
@@ -24,25 +27,25 @@ module KafoParsers
24
27
  end
25
28
 
26
29
  def self.available?
27
- `#{puppet_bin} help strings 2>&1`
28
- if $?.success?
30
+ _stdout, _stderr, status = run_puppet(['help', 'strings'])
31
+ if status.success?
29
32
  return true
30
33
  else
31
- raise KafoParsers::ParserNotAvailable.new("#{puppet_bin} does not have strings module installed")
34
+ raise KafoParsers::ParserNotAvailable.new("#{puppet_bin} does not have strings module installed.")
32
35
  end
33
36
  end
34
37
 
35
38
  def initialize(file)
36
- @file = file
37
- raise KafoParsers::ModuleName, "File not found #{file}, check your answer file" unless File.exists?(file)
39
+ @file = file = File.expand_path(file)
40
+ raise KafoParsers::ModuleName, "File not found #{file}, check your answer file" unless File.exist?(file)
41
+
42
+ command = ['strings', 'generate', '--format', 'json', file]
43
+ @raw_json, stderr, status = self.class.run_puppet(command)
38
44
 
39
- command = "#{self.class.puppet_bin} strings generate --emit-json-stdout #{file}"
40
- @raw_json = `#{command}`
41
- unless $?.success?
42
- raise KafoParsers::ParseError, "'#{command}' returned error\n#{@raw_json}"
45
+ unless status.success?
46
+ raise KafoParsers::ParseError, "'#{command}' returned error:\n #{@raw_json}\n #{stderr}"
43
47
  end
44
48
 
45
- Encoding.default_external = Encoding::UTF_8 if defined?(Encoding)
46
49
  begin
47
50
  @complete_hash = ::JSON.parse(@raw_json)
48
51
  rescue ::JSON::ParserError => e
@@ -53,15 +56,17 @@ module KafoParsers
53
56
  self
54
57
  end
55
58
 
56
- # AIO and system default puppet bins are tested for existence, fallback to just `puppet` otherwise
57
59
  def self.puppet_bin
58
- @puppet_bin ||= begin
59
- found_puppet_path = (::ENV['PATH'].split(File::PATH_SEPARATOR) + ['/opt/puppetlabs/bin']).find do |path|
60
- binary = File.join(path, 'puppet')
61
- binary if File.executable?(binary)
62
- end
63
- found_puppet_path.nil? ? 'puppet' : File.join(found_puppet_path, 'puppet')
60
+ @puppet_bin ||= self.find_puppet_bin
61
+ end
62
+
63
+ # AIO and system default puppet bins are tested for existence, fallback to just `puppet` otherwise
64
+ def self.find_puppet_bin
65
+ found_puppet_path = (::ENV['PATH'].split(File::PATH_SEPARATOR) + ['/opt/puppetlabs/bin']).find do |path|
66
+ binary = File.join(path, 'puppet')
67
+ binary if File.executable?(binary) && !File.symlink?(binary)
64
68
  end
69
+ found_puppet_path.nil? ? 'puppet' : File.join(found_puppet_path, 'puppet')
65
70
  end
66
71
 
67
72
  def data_type
@@ -91,7 +96,7 @@ module KafoParsers
91
96
 
92
97
  # returns data in following form
93
98
  # {
94
- # :docs => { $param1 => 'documentation without types and conditions'}
99
+ # :docs => { $param1 => ['documentation without types and conditions']}
95
100
  # :types => { $param1 => 'boolean'},
96
101
  # :groups => { $param1 => ['Parameters', 'Advanced']},
97
102
  # :conditions => { $param1 => '$db_type == "mysql"'},
@@ -101,19 +106,52 @@ module KafoParsers
101
106
  if @parsed_hash.nil?
102
107
  raise KafoParsers::DocParseError, "no documentation found for manifest #{@file}, parsing error?"
103
108
  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
109
+ # Lowest precedence: types given in the strings hash from class definition
110
+ tag_params.each do |param|
111
+ data[:types][param['name']] = param['types'][0] unless param['types'].nil?
112
+ end
113
+
114
+ # Next: types and other data from RDoc parser
115
+ rdoc_parser = DocParser.new(@parsed_hash['docstring']['text']).parse
116
+ data[:docs] = rdoc_parser.docs
117
+ data[:groups] = rdoc_parser.groups
118
+ data[:conditions] = rdoc_parser.conditions
119
+ data[:types].merge! rdoc_parser.types
120
+
121
+ # Highest precedence: data in YARD @param stored in the 'text' field
122
+ tag_params.each do |param|
123
+ param_name = param['name']
124
+ unless param['text'].nil? || param['text'].empty?
125
+ param_parser = ParamDocParser.new(param_name, param['text'].split($/))
126
+ data[:docs][param_name] = param_parser.doc if param_parser.doc
127
+ data[:groups][param_name] = param_parser.group if param_parser.group
128
+ data[:conditions][param_name] = param_parser.condition if param_parser.condition
129
+ data[:types][param_name] = param_parser.type if param_parser.type
130
+ end
131
+ end
109
132
  end
110
133
  data
111
134
  end
112
135
 
113
136
  private
114
137
 
138
+ def self.run_puppet(command)
139
+ env_vars = self.puppet_bin.start_with?('/opt/puppetlabs') ? clean_env_vars : ::ENV
140
+ command = command.unshift(self.puppet_bin)
141
+ Open3.capture3(env_vars, *command, :unsetenv_others => true)
142
+ end
143
+
144
+ def self.clean_env_vars
145
+ # Cleaning ENV vars and keeping required vars only because,
146
+ # When using SCL it adds GEM_HOME and GEM_PATH ENV vars.
147
+ whitelisted_vars = %w[HOME USER LANG]
148
+
149
+ cleaned_env = ::ENV.select { |var| whitelisted_vars.include?(var) || var.start_with?('LC_') }
150
+ cleaned_env['PATH'] = '/sbin:/bin:/usr/sbin:/usr/bin:/opt/puppetlabs/bin'
151
+ cleaned_env
152
+ end
153
+
115
154
  # 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
155
  #
118
156
  # values are reported as strings which is issue especially for :under
119
157
  # strings are double quoted
@@ -122,16 +160,11 @@ module KafoParsers
122
160
  if (value.start_with?("'") && value.end_with?("'")) || (value.start_with?('"') && value.end_with?('"'))
123
161
  value = value[1..-2]
124
162
  end
125
- value = value[1..-1] if value.start_with?('$')
126
163
  value = :undef if value == 'undef'
127
164
 
128
165
  value
129
166
  end
130
167
 
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
168
  def tag_params
136
169
  if @parsed_hash['docstring']['tags']
137
170
  @parsed_hash['docstring']['tags'].select { |tag| tag['tag_name'] == 'param' }
@@ -1,3 +1,3 @@
1
1
  module KafoParsers
2
- VERSION = "0.1.4"
2
+ VERSION = "1.1.1"
3
3
  end
metadata CHANGED
@@ -1,85 +1,85 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kafo_parsers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 1.1.1
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-10-31 00:00:00.000000000 Z
11
+ date: 2020-08-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.5'
19
+ version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '1.5'
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "<"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '11'
33
+ version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "<"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '11'
40
+ version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: minitest
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '4.0'
47
+ version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: '4.0'
54
+ version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: simplecov
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - "<"
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
- version: '0.9'
61
+ version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - "<"
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
- version: '0.9'
68
+ version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
- name: ci_reporter
70
+ name: ci_reporter_minitest
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - "~>"
73
+ - - ">="
74
74
  - !ruby/object:Gem::Version
75
- version: 1.9.0
75
+ version: '0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - "~>"
80
+ - - ">="
81
81
  - !ruby/object:Gem::Version
82
- version: 1.9.0
82
+ version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: rdoc
85
85
  requirement: !ruby/object:Gem::Requirement
@@ -122,14 +122,13 @@ 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
- - lib/kafo_parsers/puppet_module_parser.rb
127
127
  - lib/kafo_parsers/puppet_strings_module_parser.rb
128
- - lib/kafo_parsers/validation.rb
129
128
  - lib/kafo_parsers/version.rb
130
129
  homepage: https://github.com/theforeman/kafo_parsers
131
130
  licenses:
132
- - GPLv3+
131
+ - GPL-3.0+
133
132
  metadata: {}
134
133
  post_install_message:
135
134
  rdoc_options: []
@@ -146,8 +145,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
146
145
  - !ruby/object:Gem::Version
147
146
  version: '0'
148
147
  requirements: []
149
- rubyforge_project:
150
- rubygems_version: 2.4.5
148
+ rubygems_version: 3.1.2
151
149
  signing_key:
152
150
  specification_version: 4
153
151
  summary: Puppet module parsers
@@ -1,125 +0,0 @@
1
- # encoding: UTF-8
2
- require 'kafo_parsers/doc_parser'
3
- require 'kafo_parsers/validation'
4
-
5
- module KafoParsers
6
- # Based on ideas from puppet-parse by Johan van den Dorpe
7
- # we don't build any tree structure since e.g. params from doc does not
8
- # have to be defined in puppet DSL and vice versa, we just gather all info
9
- # we can read from the whole manifest
10
- class PuppetModuleParser
11
- @@puppet_initialized = false
12
-
13
- def self.available?
14
- require 'puppet'
15
- [2, 3].include?(Puppet::PUPPETVERSION.to_i)
16
- rescue LoadError => e
17
- raise KafoParsers::ParserNotAvailable.new(e)
18
- end
19
-
20
- # You can call this method to get all supported information from a given manifest
21
- #
22
- # @param [ String ] manifest file path to parse
23
- # @return [ Hash ] hash containing values, validations, documentation, types, groups and conditions
24
- def self.parse(file)
25
- content = new(file)
26
- docs = content.docs
27
-
28
- data = {
29
- :values => content.values,
30
- :validations => content.validations
31
- }
32
- data[:parameters] = data[:values].keys
33
- data.merge!(docs)
34
- data
35
- end
36
-
37
- def initialize(file)
38
- @file = file
39
- raise KafoParsers::ModuleName, "File not found #{file}, check your answer file" unless File.exists?(file)
40
-
41
- unless @@puppet_initialized
42
- require 'puppet'
43
- if Puppet::PUPPETVERSION.to_i >= 3
44
- Puppet.initialize_settings
45
- else
46
- Puppet.parse_config
47
- end
48
- Encoding.default_external = Encoding::UTF_8 if defined?(Encoding)
49
- @@puppet_initialized = true
50
- end
51
-
52
- env = Puppet::Node::Environment.new
53
- parser = Puppet::Parser::Parser.new(env)
54
- parser.import(@file)
55
-
56
- # Find object corresponding to class defined in init.pp in list of hostclasses
57
- ast_types = parser.environment.known_resource_types.hostclasses.map(&:last)
58
- @object = ast_types.find { |ast_type| ast_type.file == file }
59
-
60
- # Find object in list of definitions if not found among hostclasses
61
- if @object.nil?
62
- ast_types = parser.environment.known_resource_types.definitions.map(&:last)
63
- @object = ast_types.find { |ast_type| ast_type.file == file }
64
- end
65
-
66
- parser
67
- end
68
-
69
- # TODO - store parsed object type (Puppet::Parser::AST::Variable must be dumped later)
70
- def values
71
- parameters = {}
72
- arguments = @object.respond_to?(:arguments) ? @object.arguments : {}
73
- arguments.each { |k, v| parameters[k] = v.respond_to?(:value) ? v.value : nil }
74
- parameters
75
- end
76
-
77
- def validations(param = nil)
78
- return [] if @object.code.nil?
79
- @object.code.select { |stmt| stmt.is_a?(Puppet::Parser::AST::Function) && stmt.name =~ /^validate_/ }.map do |v|
80
- Validation.new(v.name, interpret_validation_args(v.arguments))
81
- end
82
- end
83
-
84
- # returns data in following form
85
- # {
86
- # :docs => { $param1 => 'documentation without types and conditions'}
87
- # :types => { $param1 => 'boolean'},
88
- # :groups => { $param1 => ['Parameters', 'Advanced']},
89
- # :conditions => { $param1 => '$db_type == "mysql"'},
90
- # :object_type => 'hostclass' # or definition
91
- # }
92
- def docs
93
- data = { :docs => {}, :types => {}, :groups => {}, :conditions => {}, :object_type => '' }
94
- if @object.nil?
95
- raise KafoParsers::DocParseError, "no documentation found for manifest #{@file}, parsing error?"
96
- elsif !@object.doc.nil?
97
- parser = DocParser.new(@object.doc).parse
98
- data[:docs] = parser.docs
99
- data[:groups] = parser.groups
100
- data[:types] = parser.types
101
- data[:conditions] = parser.conditions
102
- data[:object_type] = @object.type.to_s
103
- end
104
- data
105
- end
106
-
107
- private
108
-
109
- def interpret_validation_args(args)
110
- args.map do |arg|
111
- if arg.is_a?(Puppet::Parser::AST::Variable)
112
- arg.to_s
113
- elsif arg.is_a?(Puppet::Parser::AST::ASTArray)
114
- interpret_validation_args(arg.to_a)
115
- elsif arg.is_a?(Puppet::Parser::AST::Concat)
116
- interpret_validation_args(arg.value).join
117
- elsif arg.respond_to? :value
118
- arg.value
119
- else
120
- arg
121
- end
122
- end
123
- end
124
- end
125
- end
@@ -1,14 +0,0 @@
1
- module KafoParsers
2
- class Validation
3
- attr_reader :name, :arguments
4
-
5
- def initialize(name, arguments)
6
- @name = name
7
- @arguments = arguments
8
- end
9
-
10
- def ==(other)
11
- name == other.name && arguments == other.arguments
12
- end
13
- end
14
- end