kvx 1.1.1 → 1.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/lib/kvx.rb +134 -134
  4. data.tar.gz.sig +0 -0
  5. metadata +5 -5
  6. metadata.gz.sig +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ea5530d34014e707b2437e10c43ba101ad30f72fbc213e4f130d1b8fd9771b56
4
- data.tar.gz: 2eb9ef7dbb42e1e664921f05745871cf3486aa55e8abf2b34e91ef803106aff1
3
+ metadata.gz: 7f4470d45e160be12a005f238aa279f1fbee524685577307ae63bf5092395b89
4
+ data.tar.gz: e2dc7d72fb2905b14d44e1707296fd91584964f7d0790ab2c922566c02b13e6d
5
5
  SHA512:
6
- metadata.gz: 63bb7be7589852804c5b4ea5e1555c26f366f3bccef0429e7c50adac24d91a90e18c6c91ba03442450e989dd317cc9c656b4ec3768cd931fe2c90e4f05168ead
7
- data.tar.gz: 96947516061e9d56379452bd1cdb5117c1c7af11ca182f03aa37da732844bfc834df82d415aca15bbce34a985f2935e996c501d2cbe993d333b2a14c11572364
6
+ metadata.gz: 4c3815f945237a18b5c3d6e6c659b04cf6fed79218c7584113918c6668abc2d017d59f426dcdbae67cb39ece6ce7f68b7d4ad43b686557df16bdce83f2b7cf83
7
+ data.tar.gz: d561ab4b23680593408bc67033d38e9d03fc3e0f67c3af5662934adbd13b5db4bffcb85de7a1067b0b63dcbf34ad42c7089a6d5a97789d36ff2d674240bf4328
checksums.yaml.gz.sig CHANGED
Binary file
data/lib/kvx.rb CHANGED
@@ -16,7 +16,7 @@ hkey_gems
16
16
  require kvx
17
17
  class Kvx
18
18
  media_type kvx
19
- '
19
+ '
20
20
  end
21
21
  end
22
22
 
@@ -40,19 +40,19 @@ class Kvx
40
40
  @identifier = 'kvx'
41
41
  @summary, @body = summary, body
42
42
  @ignore_blank_lines ||= false
43
-
43
+
44
44
  @attributes, @debug = attributes, debug
45
-
45
+
46
46
  h = {
47
- hash: :passthru,
48
- :'rexle::element' => :xml_to_h,
47
+ hash: :passthru,
48
+ :'rexle::element' => :xml_to_h,
49
49
  string: :parse_string,
50
50
  rexle: :doc_to_h,
51
51
  :"rexle::element::value" => :parse_string
52
52
  }
53
-
53
+
54
54
  if x then
55
-
55
+
56
56
  sym = h[x.class.to_s.downcase.to_sym]
57
57
  puts 'sym: ' + sym.inspect if @debug
58
58
  @body = method(sym).call x
@@ -62,127 +62,127 @@ class Kvx
62
62
  methodize(@body)
63
63
 
64
64
  end
65
-
65
+
66
66
  def import(s)
67
67
  @body = parse_string(s)
68
68
  methodize(@body)
69
69
  end
70
-
70
+
71
71
  def item()
72
72
  @body
73
73
  end
74
-
75
- alias body item
76
-
74
+
75
+ alias body item
76
+
77
77
  def save(filename)
78
78
  FileX.write filename, self.to_s
79
79
  end
80
-
81
- # flattening is helpful when passing the Hash object to
80
+
81
+ # flattening is helpful when passing the Hash object to
82
82
  # RecordX as a new record
83
83
  #
84
84
  def to_h(flatten: false)
85
-
85
+
86
86
  if @summary.empty? then
87
-
87
+
88
88
  deep_clone @body
89
-
89
+
90
90
  else
91
-
91
+
92
92
  if flatten then
93
93
  @summary.merge @body
94
94
  else
95
95
  {summary: deep_clone(@summary), body: deep_clone(@body)}
96
96
  end
97
-
97
+
98
98
  end
99
-
99
+
100
100
  end
101
-
101
+
102
102
  def to_doc()
103
-
103
+
104
104
  a = if @summary.empty? then
105
-
106
- [self.class.to_s.downcase, @attributes, '', *make_xml(@body)]
107
-
105
+
106
+ [self.class.to_s.downcase, @attributes, '', *make_xml(@body)]
107
+
108
108
  else
109
-
109
+
110
110
  summary = make_xml(@summary)
111
-
111
+
112
112
  tags_found = summary.assoc(:tags)
113
-
113
+
114
114
  if tags_found then
115
115
  tags = tags_found.pop
116
- tags_found.push *tags.split.map {|x| [:tag,{},x]}
116
+ tags_found.push *tags.split.map {|x| [:tag,{},x]}
117
117
  end
118
118
 
119
119
  summary = [:summary, {}, *summary]
120
-
120
+
121
121
  # -- use the nested description Hash object if there are multiple lines
122
122
  h = {}
123
-
123
+
124
124
  @body.each do |key, value|
125
-
125
+
126
126
  h[key] = value.is_a?(String) ? value : value[:description]
127
-
127
+
128
128
  end
129
-
129
+
130
130
  body = [:body, {}, *make_xml(h)]
131
- [self.class.to_s.downcase, @attributes, '', summary, body]
132
-
133
- end
134
-
131
+ [self.class.to_s.downcase, @attributes, '', summary, body]
132
+
133
+ end
134
+
135
135
  puts 'a: ' + a.inspect if @debug
136
136
  doc = Rexle.new a
137
137
  doc.instructions = @instructions || []
138
138
  doc
139
-
139
+
140
140
  end
141
-
141
+
142
142
  def to_s()
143
-
143
+
144
144
  header = ''
145
-
145
+
146
146
  if @header or (@summary and @summary.any?) then
147
-
147
+
148
148
  attr = @attributes ? ' ' + @attributes\
149
149
  .map {|x| "%s='%s'" % x }.join(' ') : ''
150
150
  header = '<?' + @identifier
151
151
  header += attr
152
152
  header += "?>\n"
153
-
153
+
154
154
  if @summary and @summary.any? then
155
155
  header += scan_to_s @summary
156
156
  header += "\n----------------------------------\n\n"
157
157
  end
158
-
158
+
159
159
  end
160
160
 
161
161
  # -- use the nested description Hash object if there are multiple lines
162
162
  h = {}
163
-
163
+
164
164
  @body.each do |key, value|
165
-
165
+
166
166
  h[key] = if value.is_a?(String) then
167
-
167
+
168
168
  if value.lines.length < 2 then
169
- value
169
+ value
170
170
  else
171
- "\n" + value.lines.map {|x| ' ' + x }.join
171
+ "\n" + value.lines.map {|x| ' ' + x }.join
172
172
  end
173
-
173
+
174
174
  else
175
175
  "\n" + value[:description].lines.map {|x| ' ' + x }.join
176
176
  end
177
-
178
- end
179
-
177
+
178
+ end
179
+
180
180
  header + scan_to_s(h)
181
181
 
182
- end
182
+ end
183
183
 
184
184
  def to_xml(options={pretty: true})
185
-
185
+
186
186
  doc = self.to_doc
187
187
  doc.xml(options)
188
188
 
@@ -243,7 +243,7 @@ s = "
243
243
  "
244
244
 
245
245
  end
246
-
246
+
247
247
  # used by RecordX to update a KVX record
248
248
  # id is unnecssary because there is only 1 record mapped to RecordX
249
249
  #
@@ -252,9 +252,9 @@ s = "
252
252
  end
253
253
 
254
254
  private
255
-
255
+
256
256
  def deep_clone(h)
257
-
257
+
258
258
  h.inject({}) do |r, x|
259
259
 
260
260
  h2 = if x.last.is_a? Hash then
@@ -264,31 +264,31 @@ s = "
264
264
  end
265
265
  r.merge h2[0] => h2[1]
266
266
  end
267
-
268
- end
269
-
267
+
268
+ end
269
+
270
270
  def doc_to_h(doc)
271
271
  xml_to_h(doc.root)
272
272
  end
273
273
 
274
274
  def get_attributes(raw_attributes)
275
- #
275
+ #
276
276
  r1 = /([\w\-:]+\='[^']*)'/
277
277
  r2 = /([\w\-:]+\="[^"]*)"/
278
-
278
+
279
279
  r = raw_attributes.scan(/#{r1}|#{r2}/).map(&:compact)\
280
280
  .flatten.inject(Attributes.new) do |r, x|
281
- attr_name, raw_val = x.split(/=/,2)
281
+ attr_name, raw_val = x.split(/=/,2)
282
282
  val = attr_name != 'class' ? raw_val[1..-1] : raw_val[1..-1].split
283
283
  r.merge(attr_name.to_sym => val)
284
284
  end
285
285
 
286
286
  return r
287
287
  end
288
-
288
+
289
289
  def hashify(e)
290
290
 
291
- v = if e.has_elements? then
291
+ v = if e.has_elements? then
292
292
  e.elements.inject({}) do |r, x|
293
293
  r.merge hashify(x)
294
294
  end
@@ -297,17 +297,17 @@ s = "
297
297
  end
298
298
 
299
299
  {e.name.to_sym => v}
300
- end
300
+ end
301
301
 
302
302
  def make_xml(h)
303
-
303
+
304
304
  puts 'inside make_xml: ' + h.inspect if @debug
305
305
  h2 = h.clone
306
306
  h2.each {|key,value| value.delete :items if value.is_a?(Hash) }
307
307
 
308
308
  RexleBuilder.new(h2, debug: false).to_a[3..-1]
309
309
  end
310
-
310
+
311
311
  def parse_string(s)
312
312
 
313
313
  buffer, type = RXFReader.read(s)
@@ -327,9 +327,9 @@ s = "
327
327
  end
328
328
 
329
329
  end
330
-
330
+
331
331
  def methodize(h)
332
-
332
+
333
333
  h.each do |k,v|
334
334
 
335
335
  define_singleton_method(k){v} unless self.methods.include? k
@@ -338,68 +338,68 @@ s = "
338
338
  define_singleton_method((k.to_s + '=').to_sym){|x| h[k] = x}
339
339
  end
340
340
 
341
- end
342
-
341
+ end
342
+
343
343
  end
344
344
 
345
345
  def parse_to_h(s, header_pattern: %r(^<\?kvx[\s\?]))
346
346
 
347
347
  raw_txt, _ = RXFReader.read(s)
348
348
 
349
- # does the raw_txt contain header information?
349
+ # does the raw_txt contain header information?
350
350
  a = s.strip.lines
351
351
 
352
352
  txt = if a[0] =~ header_pattern then
353
-
353
+
354
354
  raw_header = a.shift
355
355
  attr = get_attributes(raw_header)
356
-
356
+
357
357
  if attr[:created] then
358
358
  attr[:last_modified] = Time.now.to_s
359
359
  else
360
- attr[:created] = Time.now.to_s
360
+ attr[:created] = Time.now.to_s
361
361
  end
362
-
362
+
363
363
  @attributes.merge! attr
364
364
  @header = true
365
- body, summary = a.join.strip.split(/^----*$/).reverse
365
+ body, summary = a.join.strip.split(/^----*$/).reverse
366
366
  @summary = scan_to_h summary if summary
367
-
367
+
368
368
  body
369
369
  else
370
370
  raw_txt
371
371
  end
372
-
372
+
373
373
  scan_to_h(txt)
374
374
  end
375
375
 
376
376
  def passthru(x)
377
-
377
+
378
378
  if x[:summary] and x[:body]
379
379
  @summary = deep_clone x[:summary]
380
380
  deep_clone x[:body]
381
- else
381
+ else
382
382
  deep_clone x
383
383
  end
384
-
384
+
385
385
  end
386
-
386
+
387
387
  def pretty_print(a, indent='')
388
-
389
- a.map do |x|
388
+
389
+ a.map do |x|
390
390
  (x.is_a?(String) or x.nil?) ? x.to_s : pretty_print(x, indent + ' ')
391
391
  end.join("\n" + indent)
392
-
392
+
393
393
  end
394
-
395
-
394
+
395
+
396
396
  def scan_to_h(txt)
397
-
397
+
398
398
  txt.gsub!(/^\w+:(?=$)/,'\0 ')
399
399
  puts 'txt:' + txt.inspect if @debug
400
-
400
+
401
401
  # auto indent any multiline values which aren't already indented
402
-
402
+
403
403
  indent = ''
404
404
 
405
405
  lines = txt.gsub(/^-+$/m,'').lines.map do |line|
@@ -411,89 +411,89 @@ s = "
411
411
  line
412
412
  end
413
413
 
414
- end
414
+ end
415
415
  puts ('lines: ' + lines.inspect).debug if @debug
416
-
416
+
417
417
  puts ('inside scan_to_h').info if @debug
418
- raw_a = LineTree.new(lines.join.gsub(/(^-*$)|(?<=\S) +#.*/,'').strip,
418
+ raw_a = LineTree.new(lines.join.gsub(/(^-*$)|(?<=\S) +#.*/,'').strip,
419
419
  ignore_blank_lines: @ignore_blank_lines).to_a
420
420
  puts ('raw_a: ' + raw_a.inspect).debug if @debug
421
-
422
- # if there are any orphan lines which aren't nested underneath a
421
+
422
+ # if there are any orphan lines which aren't nested underneath a
423
423
  # label, they will be fixed using the following statement
424
-
424
+
425
425
  a = raw_a.chunk {|x| x[0][/^[^:]+:/]}.inject([]) do |r,y|
426
426
 
427
-
427
+
428
428
  puts 'r: ' + r.inspect if @debug
429
-
429
+
430
430
  if r.last and !y.first[/[^:]+:/] then
431
431
  r.last << y.last[-1]
432
432
  else
433
433
  puts 'y: ' + y.inspect if @debug
434
434
  r << y.last[-1]
435
435
  end
436
-
436
+
437
437
  r
438
-
438
+
439
439
  end
440
440
 
441
441
  @body = a.inject({}) do |r, line|
442
-
442
+
443
443
  s = line.shift
444
444
  puts ('s: ' + s.inspect).debug if @debug
445
-
445
+
446
446
  if line.join.length > 0 then
447
-
447
+
448
448
  puts 'line: ' + line.inspect if @debug
449
-
450
- padding = line[0].length < 2 ? "\n" : "\n "
449
+
450
+ padding = line[0].length < 2 ? "\n" : "\n "
451
451
  s10 = line.map{|x| x.join(padding)}.join("\n")
452
-
452
+
453
453
  r2 = if s10[/^ *\w+:[\n ]/] then
454
-
454
+
455
455
  scan_to_h(s10)
456
-
456
+
457
457
  else
458
458
 
459
- desc = pretty_print(line).split(/\n(?=\w+: )/)
459
+ desc = pretty_print(line).split(/\n(?=\w+: )/)
460
460
 
461
461
  txt2, remaining = desc
462
462
 
463
- h = txt2.lines.inject([]) do |r, x|
463
+ h = txt2.lines.inject([]) do |r, x|
464
464
  x.chomp!
465
465
  x.length > 0 ? r << x : r
466
466
  end
467
-
468
- r3 = {description: txt2, items: h}
467
+
468
+ r3 = {description: txt2, items: LineTree.new(txt2).to_a(normalize: true)}
469
469
 
470
470
  if remaining then
471
471
  r3.merge!(scan_to_h remaining + "\n ")
472
472
  end
473
-
473
+
474
474
  r3
475
475
  end
476
476
 
477
477
  r.merge({s[/[^:]+/].to_sym => r2})
478
-
478
+
479
479
  else
480
-
480
+
481
481
  value, name = s.split(/: */,2).reverse
482
- name ||= 'description'
482
+ name ||= 'description'
483
483
  v = value =~ /^\{\s*\}$/ ? {} : value.to_s
484
-
484
+
485
485
  r.merge({name.to_sym => v})
486
- end
486
+ end
487
487
 
488
488
  end
489
-
489
+
490
490
  puts ('@body: ' + @body.inspect).debug if @debug
491
491
  @body
492
492
 
493
- end
494
-
495
- def scan_to_s(h, indent='')
496
-
493
+ end
494
+
495
+ def scan_to_s(h, indent='')
496
+
497
497
  a = h.inject([]) do |r, x|
498
498
  if x.last.is_a? Hash then
499
499
  r << x.first.to_s + ":\n" + scan_to_s(x.last, ' ')
@@ -501,27 +501,27 @@ s = "
501
501
  r << "%s%s: %s" % [indent, *x]
502
502
  end
503
503
  end
504
-
504
+
505
505
  @to_s = a.join("\n")
506
506
  end
507
-
507
+
508
508
  def xml_to_h(node)
509
-
509
+
510
510
  puts 'node: ' + node.xml.inspect if @debug
511
511
  @attributes = node.attributes.to_h
512
-
512
+
513
513
  summary = node.element('summary')
514
-
514
+
515
515
  if summary then
516
516
 
517
517
  etags = summary.element 'tags'
518
-
518
+
519
519
  if etags then
520
520
 
521
521
  tags = etags.xpath('tag/text()')
522
522
  etags.delete 'tag'
523
523
  etags.text = tags.join(' ') if tags.any?
524
-
524
+
525
525
  end
526
526
 
527
527
  @summary = hashify(summary)[:summary]
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kvx
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
@@ -35,7 +35,7 @@ cert_chain:
35
35
  kHNn+ERume1+043nzGdoGxJEUVUPYjRHDzWB1QG7OyRO+e9RZvsTQyTp2+EWVCPD
36
36
  haPJ+LVlPvQPLLPI124YDQp8
37
37
  -----END CERTIFICATE-----
38
- date: 2022-05-24 00:00:00.000000000 Z
38
+ date: 2022-09-20 00:00:00.000000000 Z
39
39
  dependencies:
40
40
  - !ruby/object:Gem::Dependency
41
41
  name: line-tree
@@ -46,7 +46,7 @@ dependencies:
46
46
  version: '0.9'
47
47
  - - ">="
48
48
  - !ruby/object:Gem::Version
49
- version: 0.9.3
49
+ version: 0.9.4
50
50
  type: :runtime
51
51
  prerelease: false
52
52
  version_requirements: !ruby/object:Gem::Requirement
@@ -56,7 +56,7 @@ dependencies:
56
56
  version: '0.9'
57
57
  - - ">="
58
58
  - !ruby/object:Gem::Version
59
- version: 0.9.3
59
+ version: 0.9.4
60
60
  - !ruby/object:Gem::Dependency
61
61
  name: rxfreadwrite
62
62
  requirement: !ruby/object:Gem::Requirement
@@ -103,7 +103,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
103
103
  - !ruby/object:Gem::Version
104
104
  version: '0'
105
105
  requirements: []
106
- rubygems_version: 3.2.22
106
+ rubygems_version: 3.3.7
107
107
  signing_key:
108
108
  specification_version: 4
109
109
  summary: Kvx (Keys, Values, and XML) makes it convenient to store and retrieve the
metadata.gz.sig CHANGED
Binary file