cfa 0.6.4 → 0.7.0

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
2
  SHA256:
3
- metadata.gz: b16f5b90e04efbadc819f5a47ef4855118d4ea2b286cfa790edd64ee333ce129
4
- data.tar.gz: 8ec15e83b710cbc323077b16309d4f2b58584890dd40e8d7bf21ee4019bcc82d
3
+ metadata.gz: 0256a5d406e5ed0a4bbedd2c59fe6ed3947b35286697301543285c8e588e1753
4
+ data.tar.gz: 0361b40d6ee2a3fc94a0f2218a2cec6c4c6c2c833942d62fe67513b65d776450
5
5
  SHA512:
6
- metadata.gz: f2ef2156c4810935459f0dbed8c15e365b61ec82b80652efb65d056df6262d4151f824ea5887972abe3dd9650b92f96977ca0e9df7437ee783d04c7e8ed81e2d
7
- data.tar.gz: 375e2b81646579375e7824b2852960223414c146cdf342129d95b3cbec66e5fa6951e3db4726e79fa650b41e9f0800d257b5fe3e5c176e85259a4697d56aa26b
6
+ metadata.gz: d0b02d8195de9b6f72e347924253ad534b76de671f44f8c598f324ab1059473aa7f5039c0f1b16e5a2eb85651ed0fc51d162dd5c49cd4b0ff0efc3be6c5b66d3
7
+ data.tar.gz: d576b43dab99a7711f6012a4c270d53a409e5b7f2b9c7868dd9e31a87de6d616a0c4565e22970a1e481801b0572c8bbe1229b5f56d6f677d66419fa651da91df
@@ -35,6 +35,66 @@ module CFA
35
35
  class AugeasElement < Hash
36
36
  end
37
37
 
38
+ # Error that is raised when augeas fail for some reason.
39
+ # Base class for more specialized exceptions.
40
+ class AugeasError < RuntimeError
41
+ end
42
+
43
+ # Error in augeas itself like broken lens
44
+ class AugeasInternalError < AugeasError
45
+ attr_reader :details
46
+ attr_reader :aug_message
47
+
48
+ def initialize(message, details)
49
+ @aug_message = message
50
+ @details = details
51
+
52
+ super("Augeas error: #{message}. Details: #{details}.")
53
+ end
54
+ end
55
+
56
+ # Parsing error
57
+ class AugeasParsingError < AugeasError
58
+ attr_reader :aug_message
59
+ attr_reader :file
60
+ attr_reader :line
61
+ attr_reader :character
62
+ attr_reader :lens
63
+ attr_reader :file_content
64
+
65
+ def initialize(params)
66
+ @aug_message = params[:message]
67
+ @file = params[:file]
68
+ @line = params[:line]
69
+ @char = params[:char]
70
+ @lens = params[:lens]
71
+ @file_content = params[:file_content]
72
+
73
+ super("Augeas parsing error: #{@aug_message}" \
74
+ " at #{@file}:#{@line}:#{@char}, lens #{@lens}"
75
+ )
76
+ end
77
+ end
78
+
79
+ # Serializing error
80
+ class AugeasSerializingError < AugeasError
81
+ attr_reader :aug_message
82
+ attr_reader :file
83
+ attr_reader :lens
84
+ attr_reader :aug_tree
85
+
86
+ def initialize(params)
87
+ @aug_message = params[:message]
88
+ @file = params[:file]
89
+ @lens = params[:lens]
90
+ @aug_tree = params[:aug_tree]
91
+
92
+ super("Augeas serializing error: #{@aug_message}" \
93
+ " for #{@file} with lens #{@lens}"
94
+ )
95
+ end
96
+ end
97
+
38
98
  # Represents list of same config options in augeas.
39
99
  # For example comments are often stored in collections.
40
100
  class AugeasCollection
@@ -168,6 +228,7 @@ module CFA
168
228
  id = 1
169
229
  loop do
170
230
  return id.to_s unless ids.include?(id.to_s)
231
+
171
232
  id += 1
172
233
  end
173
234
  end
@@ -180,6 +241,7 @@ module CFA
180
241
  # @param [String, Matcher] matcher
181
242
  def delete(matcher)
182
243
  return if matcher.nil?
244
+
183
245
  unless matcher.is_a?(CFA::Matcher)
184
246
  matcher = CFA::Matcher.new(key: matcher)
185
247
  end
@@ -236,6 +298,7 @@ module CFA
236
298
 
237
299
  def ==(other)
238
300
  return false if self.class != other.class
301
+
239
302
  other_data = other.data # do not compute again
240
303
  data.each_with_index do |entry, index|
241
304
  other_entry = other_data[index]
@@ -329,7 +392,7 @@ module CFA
329
392
  root = load_path = nil
330
393
  Augeas.open(root, load_path, Augeas::NO_MODL_AUTOLOAD) do |aug|
331
394
  aug.set("/input", raw_string)
332
- report_error(aug, "parsing", file_name) \
395
+ report_error(aug, :parsing, file_name, raw_string) \
333
396
  unless aug.text_store(@lens, "/input", "/store")
334
397
 
335
398
  return AugeasReader.read(aug, "/store")
@@ -349,7 +412,7 @@ module CFA
349
412
  AugeasWriter.new(aug).write("/store", data)
350
413
 
351
414
  res = aug.text_retrieve(@lens, "/input", "/store", "/output")
352
- report_error(aug, "serializing", file_name) unless res
415
+ report_error(aug, :serializing, file_name, data) unless res
353
416
 
354
417
  return aug.get("/output")
355
418
  end
@@ -364,19 +427,38 @@ module CFA
364
427
  private
365
428
 
366
429
  # @param aug [::Augeas]
367
- # @param activity ["parsing", "serializing"] for better error messages
430
+ # @param activity [:parsing, :serializing] for better error messages
368
431
  # @param file_name [String,nil] a file name
369
- def report_error(aug, activity, file_name)
370
- error = aug.error
371
- # zero is no error, so problem in lense
372
- if error[:code].nonzero?
373
- raise "Augeas error: #{error[:message]}. Details: #{error[:details]}."
374
- end
432
+ # @param data [AugeasTree, String] used data so augeas tree for
433
+ # serializing or file content for parsing
434
+ def report_error(aug, activity, file_name, data = nil)
435
+ report_internal_error!(aug)
375
436
 
376
437
  file_name ||= "(unknown file)"
377
- raise format("Augeas #{activity} error: %<message>s" \
378
- " at #{file_name}:%<line>s:%<char>s, lens %<lens>s",
379
- aug_get_error(aug))
438
+ args = aug_get_error(aug)
439
+ args[:file] = file_name
440
+ report_activity_error!(args, activity, data)
441
+ end
442
+
443
+ def report_internal_error!(aug)
444
+ error = aug.error
445
+ # zero is no error, so there's a problem in the lens
446
+ return if error[:code].zero?
447
+
448
+ raise AugeasInternalError.new(error[:message], error[:details])
449
+ end
450
+
451
+ def report_activity_error!(args, activity, data)
452
+ case activity
453
+ when :parsing
454
+ args[:file_content] = data
455
+ raise AugeasParsingError, args
456
+ when :serializing
457
+ args[:aug_tree] = data
458
+ raise AugeasSerializingError, args
459
+ else
460
+ raise ArgumentError, "invalid activity #{activity.inspect}"
461
+ end
380
462
  end
381
463
 
382
464
  def aug_get_error(aug)
@@ -22,6 +22,7 @@ module CFA
22
22
  loop do
23
23
  matches = aug.match(search_path)
24
24
  break if matches.empty?
25
+
25
26
  assign_matches(matches, @cache)
26
27
 
27
28
  search_path += "/*"
@@ -139,6 +139,7 @@ module CFA
139
139
  # the entries preceding this entry
140
140
  def preceding_entries
141
141
  return [] if index.zero? # first entry
142
+
142
143
  tree.all_data[0..(index - 1)]
143
144
  end
144
145
 
@@ -43,6 +43,7 @@ module CFA
43
43
  return false unless collection_match?(element, collection)
44
44
  return false unless value_match?(element, value_matcher)
45
45
  return false unless !block || yield(element[:key], element[:value])
46
+
46
47
  return true
47
48
  end
48
49
  end
@@ -71,6 +72,7 @@ module CFA
71
72
  when nil then true
72
73
  when Regexp
73
74
  return false unless element[:value].is_a?(String)
75
+
74
76
  matcher =~ element[:value]
75
77
  else
76
78
  matcher == element[:value]
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cfa
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.4
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Josef Reidinger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-04-25 00:00:00.000000000 Z
11
+ date: 2018-11-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruby-augeas