diagrammatron 0.5.1 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/bin/diagrammatron-copy +103 -0
  3. data/lib/copy.yaml +13 -0
  4. metadata +5 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f070bf8480ad655c260869aa9854d7553456491c68379403a16b50572ede42f4
4
- data.tar.gz: ee33a1019e9d512c0a6afd7e70528b5bfaad8d53de1b00f3087d0e3945af1d0a
3
+ metadata.gz: 6d1116130a55f28418c67f0522150a80ab39e275e0a473518052758a3b6959c7
4
+ data.tar.gz: 16447cd371aa60d1834356d7f712356400e2ae649124fa46ee6cef683072bc86
5
5
  SHA512:
6
- metadata.gz: c5fb77d00c5ddf0ac067793357ad3dd961d37d47a0fedf152a1c90fe3b1013c35c88ca6496c5265389ca8f14927b77ce8f88a3def1c384e6938aae5617f78016
7
- data.tar.gz: 665945789543418c34fe598e4aca5796bcabb0dfebcd19dced9da2c9fab1850136f8b6c76dbec180800f1478aac3e7f74fa10cc2cb9c1b30c1f1bf98a38d5de8
6
+ metadata.gz: 07c0d08e9244fcb9f05910a0513c9efe33e2dab58fa3860d7b168d06e53c8fbda3261aeca58258ec5e00f48568495cec99f5106bedefabe5d04ba59e7b80eaff
7
+ data.tar.gz: fbe5b2147cb128c398bdae0ca4e3afc681683947601d0f2b0034dfa26b253cc26be3328c3c526dcc17ef00f2024e9870dd345f048f093e194ec1b56c1c76f4eb
@@ -0,0 +1,103 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ # Copyright © 2023 Ismo Kärkkäinen
5
+ # Licensed under Universal Permissive License. See LICENSE.txt.
6
+
7
+ require_relative '../lib/common'
8
+ require 'optparse'
9
+
10
+ def make_dst2src(args)
11
+ dst2src = {}
12
+ (0...args.size).step(2) do |sidx|
13
+ src = args[sidx]
14
+ dst = args[sidx + 1]
15
+ dst2src[dst] = src
16
+ end
17
+ dst2src
18
+ end
19
+
20
+ def invert(dst2src)
21
+ src2dst = {}
22
+ dst2src.each do |dst, src|
23
+ src2dst[src] = src2dst.fetch(src, []).push(dst)
24
+ end
25
+ src2dst
26
+ end
27
+
28
+ def gather_values(item, src2dst)
29
+ vals = {}
30
+ src2dst.each_key do |src|
31
+ v = item.fetch(src, nil)
32
+ vals[src] = v unless v.nil?
33
+ end
34
+ vals
35
+ end
36
+
37
+ def copy_fields(item, src2dst)
38
+ vals = gather_values(item, src2dst)
39
+ copied = {}
40
+ vals.each do |src, v|
41
+ src2dst[src].each do |dst|
42
+ copied[dst] = v
43
+ end
44
+ end
45
+ copied
46
+ end
47
+
48
+ def main
49
+ input = nil
50
+ output = nil
51
+ input_output_schema = 'copy'
52
+ ENV['POSIXLY_CORRECT'] = '1' # Leaves field name pairs as they are.
53
+ parser = OptionParser.new do |opts|
54
+ opts.summary_indent = ' '
55
+ opts.summary_width = 20
56
+ opts.banner = 'Usage: diagrammatron-copy [options] source destination ...'
57
+ opts.separator ''
58
+ opts.separator 'Options:'
59
+ opts.on('-i', '--input FILE', 'Input file name. Read from stdin if not given.') do |filename|
60
+ input = filename
61
+ end
62
+ opts.on('-o', '--output FILE', 'Output file name. Write to stdout if not given.') do |filename|
63
+ output = filename
64
+ end
65
+ opts.on('-h', '--help', 'Print this help and exit.') do
66
+ $stdout.puts %(#{opts}
67
+ Source and destination are field names in pairs.
68
+
69
+ Input and output YAML file schema is returned by:
70
+ diagrammatron-schema #{input_output_schema}
71
+
72
+ Output is the input file with fields in nodes and edges copied from source,
73
+ when present, to destination field name. All copies are made at the same
74
+ time, so you can swap field contents, but chaining the copying is not
75
+ supported. In such case, provide the same source field in multiple pairs.
76
+ )
77
+ exit 0
78
+ end
79
+ end
80
+ parser.parse! ARGV
81
+
82
+ return aargh('Field name count is not multiple of 2', 1) if ARGV.size.odd?
83
+
84
+ # Same source can be copied to multiple destinations.
85
+ # Same destination copied to multiple times, last copy remains.
86
+ # Make destination from source mapping and then invert it for actual use.
87
+ src2dst = invert(make_dst2src(ARGV))
88
+
89
+ doc = load_verified(input, input_output_schema)
90
+ return 2 if doc.nil?
91
+
92
+ %w[edges nodes].each do |category|
93
+ items = doc.fetch(category, nil)
94
+ next if items.nil?
95
+ items.each do |item|
96
+ copies = copy_fields(item, src2dst)
97
+ item.merge!(copies)
98
+ end
99
+ end
100
+ save_verified(output, doc, 4, input_output_schema)
101
+ end
102
+
103
+ exit(main) if (defined? $unit_test).nil?
data/lib/copy.yaml ADDED
@@ -0,0 +1,13 @@
1
+ type: object
2
+ properties:
3
+ edges:
4
+ type: array
5
+ minItems: 0
6
+ items:
7
+ type: object
8
+ nodes:
9
+ type: array
10
+ minItems: 0
11
+ items:
12
+ type: object
13
+ additionalProperties: true
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: diagrammatron
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ismo Kärkkäinen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-12-21 00:00:00.000000000 Z
11
+ date: 2023-12-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json_schemer
@@ -36,6 +36,7 @@ description: |2
36
36
  programs that each perform one stage.
37
37
  email: ismokarkkainen@icloud.com
38
38
  executables:
39
+ - diagrammatron-copy
39
40
  - diagrammatron-get
40
41
  - diagrammatron-edges
41
42
  - diagrammatron-nodes
@@ -50,6 +51,7 @@ extensions: []
50
51
  extra_rdoc_files: []
51
52
  files:
52
53
  - LICENSE.txt
54
+ - bin/diagrammatron-copy
53
55
  - bin/diagrammatron-edges
54
56
  - bin/diagrammatron-get
55
57
  - bin/diagrammatron-nodes
@@ -61,6 +63,7 @@ files:
61
63
  - bin/diagrammatron-template
62
64
  - bin/dot_json2diagrammatron
63
65
  - lib/common.rb
66
+ - lib/copy.yaml
64
67
  - lib/edges.yaml
65
68
  - lib/nodes.yaml
66
69
  - lib/place.yaml