edicta 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +17 -0
  3. data/bin/edicta +179 -0
  4. metadata +53 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 69aace0e064fde8366df6c40d09234b76a3f8f39dfcfc6e6e83cdd3f985d71ff
4
+ data.tar.gz: 4be0350a6122f20f5035bea0dff96b58c0a773ba72846df3e9fcb3c81d9b4783
5
+ SHA512:
6
+ metadata.gz: a54cd3dbc9c82e133043f3a5ba6221396257fab008cb00fefe74fe45bccb876f84119fe1e4cb70c95114b7214f4e226ecd3e7e20f9ee5956f539eb9e3f5dcce3
7
+ data.tar.gz: bea53ce3d6de0711a5b4c57a1ad7d53b6cba8b3b69d1b9f3bfe844819b26111639440afefcffa6695da99208e9fc71ed6f0f8730a3e686d43c4d2679d829e713
data/LICENSE.txt ADDED
@@ -0,0 +1,17 @@
1
+ Copyright (c) 2019, 2020 Ismo Kärkkäinen
2
+
3
+ The Universal Permissive License (UPL), Version 1.0
4
+
5
+ Subject to the condition set forth below, permission is hereby granted to any person obtaining a copy of this software, associated documentation and/or data (collectively the "Software"), free of charge and under any and all copyright rights in the Software, and any and all patent rights owned or freely licensable by each licensor hereunder covering either (i) the unmodified Software as contributed to or provided by such licensor, or (ii) the Larger Works (as defined below), to deal in both
6
+
7
+ (a) the Software, and
8
+
9
+ (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if one is included with the Software (each a “Larger Work” to which the Software is contributed by such licensors),
10
+
11
+ without restriction, including without limitation the rights to copy, create derivative works of, display, perform, and distribute the Software and make, use, sell, offer for sale, import, export, have made, and have sold the Software and the Larger Work(s), and to sublicense the foregoing rights on either these or other terms.
12
+
13
+ This license is subject to the following condition:
14
+
15
+ The above copyright notice and either this complete permission notice or at a minimum a reference to the UPL must be included in all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/bin/edicta ADDED
@@ -0,0 +1,179 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ # Copyright © 2019-2021 Ismo Kärkkäinen
5
+ # Licensed under Universal Permissive License. See LICENSE.txt.
6
+
7
+ require 'optparse'
8
+ require 'yaml'
9
+ require 'json'
10
+
11
+
12
+ def aargh(message, exit_code = nil)
13
+ $stderr.puts message
14
+ exit exit_code unless exit_code.nil?
15
+ end
16
+
17
+ def env(var, value = nil)
18
+ k = var.to_s.upcase
19
+ ENV[k] = { false => '0', true => '1' }.fetch(value, value) unless value.nil?
20
+ v = ENV.fetch(k, nil)
21
+ case v
22
+ when '0' then false
23
+ when '1' then true
24
+ else
25
+ v
26
+ end
27
+ end
28
+
29
+ def default_env(var, value)
30
+ v = env(var)
31
+ env(var, value) if v.nil?
32
+ end
33
+
34
+ default_env(:format, 'JSON')
35
+ default_env(:values, false)
36
+ default_env(:duplicate, true)
37
+ default_env(:singles, false)
38
+ default_env(:verbose, false)
39
+ default_env(:text, false)
40
+ default_env(:in, '')
41
+ default_env(:out, '')
42
+
43
+ ENV['POSIXLY_CORRECT'] = '1'
44
+ parser = OptionParser.new do |opts|
45
+ opts.summary_indent = ' '
46
+ opts.summary_width = 23
47
+ opts.banner = 'Usage: edicta [options] [keys...]'
48
+ opts.separator ''
49
+ opts.separator 'Options (equivalent environment variable and value in parentheses):'
50
+ opts.on('-j', '--json', 'Output in JSON format (FORMAT=JSON).') { env(:format, 'JSON') }
51
+ opts.on('-y', '--yaml', 'Output in YAML format (FORMAT=YAML).') { env(:format, 'YAML') }
52
+ opts.on('-v', '--values', 'Output only values (VALUES=1).') { env(:values, true) }
53
+ opts.on('-s', '--singles', 'Output each (key-)value (pair) separately (SINGLES=1).') do
54
+ env(:singles, true)
55
+ end
56
+ opts.on('-o', '--output FILENAME', '(OUT=FILENAME)') { |f| env(:out, f) }
57
+ opts.on('-i', '--input FILENAME', '(IN=FILENAME)') { |f| env(:in, f) }
58
+ opts.on('--ignore-duplicates', 'Ignore duplicate keys (error otherwise, DUPLICATE=0).') do
59
+ env(:duplicate, false)
60
+ end
61
+ opts.on('-t', '--text', 'Output text, not dictionaries (TEXT=1).') { env(:text, true) }
62
+ opts.on('--verbose', 'Verbose output to stderr (VERBOSE=1).') { env(:verbose, true) }
63
+ opts.on('-h', '--help', 'Print this help and exit.') do
64
+ $stdout.puts opts
65
+ exit 0
66
+ end
67
+ end
68
+ parser.parse!
69
+
70
+ aargh("Format neither JSON nor YAML: #{env(:format)}", 1) unless %w[JSON YAML].include? env(:format)
71
+
72
+ def file(val, default, mode)
73
+ val.empty? ? default : File.open(val, mode)
74
+ rescue StandardError
75
+ aargh "Failed to open for #{mode}: #{val}", 1
76
+ end
77
+
78
+ input = file(env(:in), $stdin, 'r')
79
+ # Avoid creating file before we know all values were found.
80
+ output = env(:text) ? file(env(:out), $stdout, 'w') : nil
81
+
82
+ # Values to look for and in which order.
83
+ look_for = Hash.new(ARGV.empty?)
84
+ ARGV.each_index { |k| look_for[ARGV[k]] = k }
85
+
86
+ def dict(lines, look_for, edicts, counter)
87
+ return false if lines.empty?
88
+ begin
89
+ aargh("Decoding #{counter - lines.length}-#{counter - 1}") if env(:verbose)
90
+ contents = YAML.safe_load(lines.join)
91
+ rescue StandardError
92
+ aargh('Failed.') if env(:verbose) && !env(:text)
93
+ return false
94
+ end
95
+ return false unless contents.is_a? Hash
96
+ return true if env(:text) # Result will go unused.
97
+ contents.each_pair do |k, v|
98
+ next unless look_for[k]
99
+ aargh("Found #{k}") if env(:verbose)
100
+ aargh("Duplicate key: #{k}", 2) if env(:duplicate) && edicts.key?(k)
101
+ edicts[k] = v
102
+ if look_for.default && !look_for.key?(k)
103
+ look_for[k] = edicts.size # Retain order in which encountered.
104
+ end
105
+ end
106
+ true
107
+ end
108
+
109
+ def check(lines, look_for, edicts, counter, output)
110
+ return if dict(lines, look_for, edicts, counter)
111
+ return unless env(:text)
112
+ lines.each { |line| output.puts(line) }
113
+ end
114
+
115
+ edicts = {}
116
+ accumulated = []
117
+ inside = false
118
+ counter = 0
119
+ until (line = input.gets).nil?
120
+ counter += 1
121
+ ended = false
122
+ docend = false
123
+ if line == '---' || line.start_with?('---') && line[3].strip.empty?
124
+ ended = inside
125
+ inside = true
126
+ elsif line.start_with? '...'
127
+ docend = true
128
+ ended = inside
129
+ inside = false
130
+ end
131
+ if ended
132
+ accumulated.push(line) if docend
133
+ check(accumulated, look_for, edicts, counter + (docend ? 1 : 0), output)
134
+ accumulated = []
135
+ end
136
+ if inside
137
+ accumulated.push(line)
138
+ elsif env(:text) && !docend
139
+ output.puts(line)
140
+ end
141
+ end
142
+ check(accumulated, look_for, edicts, counter + 1, output) if inside
143
+
144
+ exit(0) if env(:text)
145
+
146
+ not_found = false
147
+ look_for.each_key do |key|
148
+ next if edicts.key? key
149
+ aargh "Not found: #{key}"
150
+ not_found = true
151
+ end
152
+ exit(3) if not_found
153
+
154
+ output = file(env(:out), $stdout, 'w')
155
+
156
+ if env(:values) || env(:singles)
157
+ order = []
158
+ edicts.each_pair { |k, v| order.push({ key: k, value: v }) }
159
+ order.sort! { |a, b| look_for[a[:key]] <=> look_for[b[:key]] }
160
+ order.each_index do |k|
161
+ item = order[k]
162
+ order[k] = env(:values) ? item[:value] : { item[:key] => item[:value] }
163
+ next unless env(:singles)
164
+ case env(:format)
165
+ when 'JSON' then output.puts order[k].to_json
166
+ when 'YAML' then output.puts YAML.dump(order[k])
167
+ end
168
+ end
169
+ exit(0) if env(:singles)
170
+ case env(:format)
171
+ when 'JSON' then output.puts JSON.generate(order)
172
+ when 'YAML' then output.puts YAML.dump(order)
173
+ end
174
+ else
175
+ case env(:format)
176
+ when 'JSON' then output.puts JSON.generate(edicts)
177
+ when 'YAML' then output.puts YAML.dump(edicts)
178
+ end
179
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: edicta
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ismo Kärkkäinen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-09-16 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: |2
14
+
15
+ Reads information from YAML-blocks inside a text file. The intent is that
16
+ information can be stored inside a human-readable file, and obtained
17
+ automatically without any need for a human to copy-paste the information.
18
+
19
+ Source: https://github.com/ismo-karkkainen/edicta
20
+
21
+ Licensed under Universal Permissive License, see LICENSE.txt.
22
+ email: ismokarkkainen@icloud.com
23
+ executables:
24
+ - edicta
25
+ extensions: []
26
+ extra_rdoc_files: []
27
+ files:
28
+ - LICENSE.txt
29
+ - bin/edicta
30
+ homepage: http://xn--ismo-krkkinen-gfbd.fi/edicta/index.html
31
+ licenses:
32
+ - UPL-1.0
33
+ metadata: {}
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubygems_version: 3.1.2
50
+ signing_key:
51
+ specification_version: 4
52
+ summary: Reads information from YAML-blocks inside a text file.
53
+ test_files: []