forked from puppetlabs/puppet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexplainer.rb
596 lines (505 loc) · 14.2 KB
/
explainer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
# frozen_string_literal: true
module Puppet::Pops
module Lookup
# The ExplainNode contains information of a specific node in a tree traversed during
# lookup. The tree can be traversed using the `parent` and `branches` attributes of
# each node.
#
# Each leaf node contains information about what happened when the leaf of the branch
# was traversed.
class ExplainNode
def branches
@branches ||= []
end
def to_hash
hash = {}
hash[:branches] = @branches.map {|b| b.to_hash} unless @branches.nil? || @branches.empty?
hash
end
def explain
io = ''.dup
dump_on(io, '', '')
io
end
def inspect
to_s
end
def to_s
s = self.class.name
s = "#{s} with #{@branches.size} branches" unless @branches.nil?
s
end
def text(text)
@texts ||= []
@texts << text
end
def dump_on(io, indent, first_indent)
dump_texts(io, indent)
end
def dump_texts(io, indent)
@texts.each { |text| io << indent << text << "\n" } if instance_variable_defined?(:@texts)
end
end
class ExplainTreeNode < ExplainNode
attr_reader :parent, :event, :value
attr_accessor :key
def initialize(parent)
@parent = parent
@event = nil
end
def found_in_overrides(key, value)
@key = key.to_s
@value = value
@event = :found_in_overrides
end
def found_in_defaults(key, value)
@key = key.to_s
@value = value
@event = :found_in_defaults
end
def found(key, value)
@key = key.to_s
@value = value
@event = :found
end
def result(value)
@value = value
@event = :result
end
def not_found(key)
@key = key.to_s
@event = :not_found
end
def location_not_found
@event = :location_not_found
end
def increase_indent(indent)
indent + ' '
end
def to_hash
hash = super
hash[:key] = @key unless @key.nil?
hash[:value] = @value if [:found, :found_in_defaults, :found_in_overrides, :result].include?(@event)
hash[:event] = @event unless @event.nil?
hash[:texts] = @texts unless @texts.nil?
hash[:type] = type
hash
end
def type
:root
end
def dump_outcome(io, indent)
case @event
when :not_found
io << indent << 'No such key: "' << @key << "\"\n"
when :found, :found_in_overrides, :found_in_defaults
io << indent << 'Found key: "' << @key << '" value: '
dump_value(io, indent, @value)
io << ' in overrides' if @event == :found_in_overrides
io << ' in defaults' if @event == :found_in_defaults
io << "\n"
end
dump_texts(io, indent)
end
def dump_value(io, indent, value)
case value
when Hash
io << '{'
unless value.empty?
inner_indent = increase_indent(indent)
value.reduce("\n") do |sep, (k, v)|
io << sep << inner_indent
dump_value(io, inner_indent, k)
io << ' => '
dump_value(io, inner_indent, v)
",\n"
end
io << "\n" << indent
end
io << '}'
when Array
io << '['
unless value.empty?
inner_indent = increase_indent(indent)
value.reduce("\n") do |sep, v|
io << sep << inner_indent
dump_value(io, inner_indent, v)
",\n"
end
io << "\n" << indent
end
io << ']'
else
io << value.inspect
end
end
def to_s
"#{self.class.name}: #{@key}, #{@event}"
end
end
class ExplainTop < ExplainTreeNode
def initialize(parent, type, key)
super(parent)
@type = type
self.key = key.to_s
end
def dump_on(io, indent, first_indent)
io << first_indent << 'Searching for "' << key << "\"\n"
indent = increase_indent(indent)
branches.each {|b| b.dump_on(io, indent, indent)}
end
end
class ExplainInvalidKey < ExplainTreeNode
def initialize(parent, key)
super(parent)
@key = key.to_s
end
def dump_on(io, indent, first_indent)
io << first_indent << "Invalid key \"" << @key << "\"\n"
end
def type
:invalid_key
end
end
class ExplainMergeSource < ExplainNode
attr_reader :merge_source
def initialize(merge_source)
@merge_source = merge_source
end
def dump_on(io, indent, first_indent)
io << first_indent << 'Using merge options from "' << merge_source << "\" hash\n"
end
def to_hash
{ :type => type, :merge_source => merge_source }
end
def type
:merge_source
end
end
class ExplainModule < ExplainTreeNode
def initialize(parent, module_name)
super(parent)
@module_name = module_name
end
def dump_on(io, indent, first_indent)
case @event
when :module_not_found
io << indent << 'Module "' << @module_name << "\" not found\n"
when :module_provider_not_found
io << indent << 'Module data provider for module "' << @module_name << "\" not found\n"
end
end
def module_not_found
@event = :module_not_found
end
def module_provider_not_found
@event = :module_provider_not_found
end
def type
:module
end
end
class ExplainInterpolate < ExplainTreeNode
def initialize(parent, expression)
super(parent)
@expression = expression
end
def dump_on(io, indent, first_indent)
io << first_indent << 'Interpolation on "' << @expression << "\"\n"
indent = increase_indent(indent)
branches.each {|b| b.dump_on(io, indent, indent)}
end
def to_hash
hash = super
hash[:expression] = @expression
hash
end
def type
:interpolate
end
end
class ExplainMerge < ExplainTreeNode
def initialize(parent, merge)
super(parent)
@merge = merge
end
def dump_on(io, indent, first_indent)
return if branches.size == 0
# It's pointless to report a merge where there's only one branch
return branches[0].dump_on(io, indent, first_indent) if branches.size == 1
io << first_indent << 'Merge strategy ' << @merge.class.key.to_s << "\n"
indent = increase_indent(indent)
options = options_wo_strategy
unless options.nil?
io << indent << 'Options: '
dump_value(io, indent, options)
io << "\n"
end
branches.each {|b| b.dump_on(io, indent, indent)}
if @event == :result
io << indent << 'Merged result: '
dump_value(io, indent, @value)
io << "\n"
end
end
def to_hash
return branches[0].to_hash if branches.size == 1
hash = super
hash[:merge] = @merge.class.key
options = options_wo_strategy
hash[:options] = options unless options.nil?
hash
end
def type
:merge
end
def options_wo_strategy
options = @merge.options
if !options.nil? && options.include?('strategy')
options = options.dup
options.delete('strategy')
end
options.empty? ? nil : options
end
end
class ExplainGlobal < ExplainTreeNode
def initialize(parent, binding_terminus)
super(parent)
@binding_terminus = binding_terminus
end
def dump_on(io, indent, first_indent)
io << first_indent << 'Data Binding "' << @binding_terminus.to_s << "\"\n"
indent = increase_indent(indent)
branches.each {|b| b.dump_on(io, indent, indent)}
dump_outcome(io, indent)
end
def to_hash
hash = super
hash[:name] = @binding_terminus
hash
end
def type
:global
end
end
class ExplainDataProvider < ExplainTreeNode
def initialize(parent, provider)
super(parent)
@provider = provider
end
def dump_on(io, indent, first_indent)
io << first_indent << @provider.name << "\n"
indent = increase_indent(indent)
if @provider.respond_to?(:config_path)
path = @provider.config_path
io << indent << 'Using configuration "' << path.to_s << "\"\n" unless path.nil?
end
branches.each {|b| b.dump_on(io, indent, indent)}
dump_outcome(io, indent)
end
def to_hash
hash = super
hash[:name] = @provider.name
if @provider.respond_to?(:config_path)
path = @provider.config_path
hash[:configuration_path] = path.to_s unless path.nil?
end
hash[:module] = @provider.module_name if @provider.is_a?(ModuleDataProvider)
hash
end
def type
:data_provider
end
end
class ExplainLocation < ExplainTreeNode
def initialize(parent, location)
super(parent)
@location = location
end
def dump_on(io, indent, first_indent)
location = @location.location
type_name = type == :path ? 'Path' : 'URI'
io << indent << type_name << ' "' << location.to_s << "\"\n"
indent = increase_indent(indent)
io << indent << 'Original ' << type_name.downcase << ': "' << @location.original_location << "\"\n"
branches.each {|b| b.dump_on(io, indent, indent)}
io << indent << type_name << " not found\n" if @event == :location_not_found
dump_outcome(io, indent)
end
def to_hash
hash = super
location = @location.location
if type == :path
hash[:original_path] = @location.original_location
hash[:path] = location.to_s
else
hash[:original_uri] = @location.original_location
hash[:uri] = location.to_s
end
hash
end
def type
@location.location.is_a?(Pathname) ? :path : :uri
end
end
class ExplainSubLookup < ExplainTreeNode
def initialize(parent, sub_key)
super(parent)
@sub_key = sub_key
end
def dump_on(io, indent, first_indent)
io << indent << 'Sub key: "' << @sub_key.join('.') << "\"\n"
indent = increase_indent(indent)
branches.each {|b| b.dump_on(io, indent, indent)}
dump_outcome(io, indent)
end
def type
:sub_key
end
end
class ExplainKeySegment < ExplainTreeNode
def initialize(parent, segment)
super(parent)
@segment = segment
end
def dump_on(io, indent, first_indent)
dump_outcome(io, indent)
end
def type
:segment
end
end
class ExplainScope < ExplainTreeNode
def initialize(parent, name)
super(parent)
@name = name
end
def dump_on(io, indent, first_indent)
io << indent << @name << "\n"
indent = increase_indent(indent)
branches.each {|b| b.dump_on(io, indent, indent)}
dump_outcome(io, indent)
end
def to_hash
hash = super
hash[:name] = @name
hash
end
def type
:scope
end
end
class Explainer < ExplainNode
def initialize(explain_options = false, only_explain_options = false)
@current = self
@explain_options = explain_options
@only_explain_options = only_explain_options
end
def push(qualifier_type, qualifier)
node = case (qualifier_type)
when :global
ExplainGlobal.new(@current, qualifier)
when :location
ExplainLocation.new(@current, qualifier)
when :interpolate
ExplainInterpolate.new(@current, qualifier)
when :data_provider
ExplainDataProvider.new(@current, qualifier)
when :merge
ExplainMerge.new(@current, qualifier)
when :module
ExplainModule.new(@current, qualifier)
when :scope
ExplainScope.new(@current, qualifier)
when :sub_lookup
ExplainSubLookup.new(@current, qualifier)
when :segment
ExplainKeySegment.new(@current, qualifier)
when :meta, :data
ExplainTop.new(@current, qualifier_type, qualifier)
when :invalid_key
ExplainInvalidKey.new(@current, qualifier)
else
#TRANSLATORS 'Explain' is referring to the 'Explainer' class and should not be translated
raise ArgumentError, _("Unknown Explain type %{qualifier_type}") % { qualifier_type: qualifier_type }
end
@current.branches << node
@current = node
end
def only_explain_options?
@only_explain_options
end
def explain_options?
@explain_options
end
def pop
@current = @current.parent unless @current.parent.nil?
end
def accept_found_in_overrides(key, value)
@current.found_in_overrides(key, value)
end
def accept_found_in_defaults(key, value)
@current.found_in_defaults(key, value)
end
def accept_found(key, value)
@current.found(key, value)
end
def accept_merge_source(merge_source)
@current.branches << ExplainMergeSource.new(merge_source)
end
def accept_not_found(key)
@current.not_found(key)
end
def accept_location_not_found
@current.location_not_found
end
def accept_module_not_found(module_name)
push(:module, module_name)
@current.module_not_found
pop
end
def accept_module_provider_not_found(module_name)
push(:module, module_name)
@current.module_provider_not_found
pop
end
def accept_result(result)
@current.result(result)
end
def accept_text(text)
@current.text(text)
end
def dump_on(io, indent, first_indent)
branches.each { |b| b.dump_on(io, indent, first_indent) }
dump_texts(io, indent)
end
def to_hash
branches.size == 1 ? branches[0].to_hash : super
end
end
class DebugExplainer < Explainer
attr_reader :wrapped_explainer
def initialize(wrapped_explainer)
@wrapped_explainer = wrapped_explainer
if wrapped_explainer.nil?
@current = self
@explain_options = false
@only_explain_options = false
else
@current = wrapped_explainer
@explain_options = wrapped_explainer.explain_options?
@only_explain_options = wrapped_explainer.only_explain_options?
end
end
def dump_on(io, indent, first_indent)
@current.equal?(self) ? super : @current.dump_on(io, indent, first_indent)
end
def emit_debug_info(preamble)
io = String.new
io << preamble << "\n"
dump_on(io, ' ', ' ')
Puppet.debug(io.chomp!)
end
end
end
end