simple-data 0.1.0 → 0.3.1

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: ac192701653932fa65dfad509a17ff7b1e97ff530cf0d7adc7fced1c8a97474f
4
- data.tar.gz: 28deb77f46642a96c789ed0b0e83ac9ecc069416bf57c4ecf452ca714ce57940
3
+ metadata.gz: 58034c7baa9961e9d038600d0d3a5aeb5b0d9e2d794ca5c1df06a9387bc944c2
4
+ data.tar.gz: 85b415e5e37de378f97e9797c1ca2304bbf454f30821da39303cc52fad84b9aa
5
5
  SHA512:
6
- metadata.gz: 62e1ea2a685d816c61be2c4c9d0724a9bf7f9f34aa23341f94009c135704d453af9a7c00f8ff6c4b225f069e47682bc1557290540f622051fc7771bea6a30053
7
- data.tar.gz: cb3f6b31f8dfeafe924eec3fb89f6672501363accfae6d8150471c111f53654a9c9e97b54f349ed15d43254634a7ce0f933fae9c8aec07434d931e89a183d404
6
+ metadata.gz: a4b31167024aec064b90ecf531769a98bc10b5b4e702637c5fa3bf4e8c320b11a5c0955b6ec236007a8f9d5a810dbc1fac784ba1d27598420546d0c3ca034c9b
7
+ data.tar.gz: f35b584990bb6dce182c3e1d3adc3cd48bb74727b60c5e369ae1cb212e172f57552ba1dc9029b9c8d55a96bacc315410fee4ad2f64372f51d61030e146e11826
data/README.md CHANGED
@@ -6,11 +6,11 @@ This is the ruby implementation of the [simple-data spec][sda]
6
6
  require 'simple-data'
7
7
 
8
8
  SimpleData.generate('spacecraft.sda',
9
- [ [ :cstr, 'name', 'spacecraft' ],
10
- [ :cstr, 'origin', 'serie or movie' ],
11
- [ :f64, 'x' ],
12
- [ :f64, 'y' ],
13
- [ :f64, 'z' ] ],
9
+ [ [ :cstr, 'name', nil, 'spacecraft' ],
10
+ [ :cstr, 'origin', nil, 'serie or movie' ],
11
+ [ :f64, 'x', 'm' ],
12
+ [ :f64, 'y', 'm' ],
13
+ [ :f64, 'z', 'm' ] ],
14
14
  tags: { author: "Stephane D'Alu",
15
15
  url: "http://www.sdalu.com/",
16
16
  license: "MIT" }) do |sda|
@@ -21,6 +21,7 @@ SimpleData.generate('spacecraft.sda',
21
21
  end
22
22
 
23
23
  SimpleData.open('spacecraft.sda') do |sda|
24
+ puts sda.fields.inspect
24
25
  while row = sda.get do
25
26
  puts row.inspect
26
27
  end
@@ -29,11 +29,12 @@ class SimpleData
29
29
 
30
30
  def write(data)
31
31
  @written += data.size
32
- if (@written.size > @write_size)
32
+ if (@written > @write_size)
33
33
  @io.write(@zstd.flush)
34
- else
35
- @zstd << data
34
+ @io.flush
35
+ @written = 0
36
36
  end
37
+ @io.write(@zstd.compress(data))
37
38
  end
38
39
  end
39
40
 
@@ -53,7 +54,7 @@ class SimpleData
53
54
  type = MAGIC.find {|k, v| str.start_with?(k) }&.last
54
55
 
55
56
  # Sanity check
56
- rasie "data is not Zstd compressed" if type != :zstd
57
+ raise "data is not Zstd compressed" if type != :zstd
57
58
 
58
59
  # Decoder
59
60
  zstd = Zstd::StreamingDecompress.new
@@ -61,6 +62,14 @@ class SimpleData
61
62
  end
62
63
 
63
64
  def eof?
65
+ return false unless @sio.eof?
66
+ return true if @io.eof?
67
+
68
+ # Refill
69
+ cstr = @io.read(@read_size)
70
+ @sio.string = @decoder.call(cstr)
71
+
72
+ # Recheck
64
73
  @io.eof? && @sio.eof?
65
74
  end
66
75
 
@@ -1,4 +1,4 @@
1
1
  class SimpleData
2
- VERSION = '0.1.0'
2
+ VERSION = '0.3.1'
3
3
  end
4
4
 
data/lib/simple-data.rb CHANGED
@@ -5,9 +5,10 @@ class SimpleData
5
5
  # Various regex
6
6
  REGEX_MAGIC = /\A# simple-data:(?<version>\d+)\s*\z/
7
7
  REGEX_SECTION = /\A# --<(?<section>[^>:]+)(?::(?<extra>[^>]+))?>--+\s*\z/
8
- REGEX_FIELD = /\A\#\s* (?<type>\w+ ) \s*:\s*
9
- (?<name>[\w\-.]+)
10
- \s* (?:\((?<desc>.* )\))? \s*\z
8
+ REGEX_FIELD = /\A\#\s* (?<type>\w+ ) \s*:\s*
9
+ (?<name>[\w\-.:]+)
10
+ \s* (?:\[(?<unit>.*? )\])?
11
+ \s* (?:\((?<desc>.* )\))? \s*\z
11
12
  /ix
12
13
  REGEX_TAG = /\A@(?<tag>\w+)\s+(?<value>.*?)\s*\z/
13
14
  REGEX_EMPTY = /\A#\s*\z/
@@ -87,7 +88,7 @@ class SimpleData
87
88
  when :cstr then [ d ].pack('Z*')
88
89
  when :blob then raise ParserError, 'not implemented'
89
90
  when :char then [ d ].pack('c' )
90
- when :bool then [ d ? 'T' : 'F' ].pack('c')
91
+ when :bool then [ d ? 'T' : 'F' ]
91
92
  end
92
93
  }.join
93
94
 
@@ -116,7 +117,8 @@ class SimpleData
116
117
  when :f64 then @io.read(8).unpack1('E' )
117
118
  when :cstr then @io.each_byte.lazy.take_while {|b| !b.zero? }
118
119
  .map {|b| b.chr }.to_a.join
119
- when :blob then raise ParserError, 'not implemented'
120
+ when :blob then @io.read(2).unpack1('s<')
121
+ .then {|size| @io.read(size) }
120
122
  when :char then @io.read(1)
121
123
  when :bool then @io.read(1) == 'T'
122
124
  end
@@ -128,7 +130,8 @@ class SimpleData
128
130
  end
129
131
 
130
132
  # Generating file
131
- def self.generate(file, fields, compress = false,
133
+ def self.generate(file, fields,
134
+ compress: const_defined?(:IOCompressedWrite),
132
135
  tags: nil, sections: nil, &block)
133
136
  # Sanity check
134
137
  if compress && !const_defined?(:IOCompressedWrite)
@@ -152,19 +155,32 @@ class SimpleData
152
155
  # Spec
153
156
  io.puts "#"
154
157
  io.puts "# --<spec>--"
155
- maxlen = fields.map {|(_, name)| name.size }.max
156
- fields.each do |(type, name, desc)|
158
+ name_maxlen = fields.map {|(_, name)| name.size }.max
159
+ unit_maxlen = fields.map {|(_, _, unit)| unit&.size || 0 }.max
160
+
161
+ fields.each do |(type, name, unit, desc)|
157
162
  if desc
158
- io.puts "# %-4s : %-*s (%s)" % [ type, maxlen, name, desc ]
163
+ if unit
164
+ io.puts "# %-4s : %-*s [%-*s] (%s)" % [
165
+ type, name_maxlen, name, unit_maxlen, unit, desc ]
166
+ else
167
+ io.puts "# %-4s : %-*s %-*s (%s)" % [
168
+ type, name_maxlen, name, unit_maxlen, '', desc ]
169
+ end
159
170
  else
160
- io.puts "# %-4s : %s" % [ type, name ]
171
+ if unit
172
+ io.puts "# %-4s : %-*s [%-*s]" % [
173
+ type, name_maxlen, name, unit_maxlen, unit, ]
174
+ else
175
+ io.puts "# %-4s : %s" % [ type, name ]
176
+ end
161
177
  end
162
178
  end
163
179
 
164
180
  # Custom sections
165
181
  if sections && !sections.empty?
166
- io.puts "#"
167
182
  sections.each do |name, value|
183
+ io.puts "#"
168
184
  io.puts "# --<#{name}>--"
169
185
  value.split(/\r?\n/).each do |line|
170
186
  io.puts "# #{line}"
@@ -205,7 +221,7 @@ class SimpleData
205
221
  fields, tags, sections, dataopt = self.get_metadata(io)
206
222
 
207
223
  # Deal with compression
208
- if dataopt.include?(:compressed)
224
+ if dataopt&.include?(:compressed)
209
225
  unless const_defined?(:IOCompressedRead)
210
226
  raise Error, 'compression not supported (add zstd-ruby gem)'
211
227
  end
@@ -220,6 +236,18 @@ class SimpleData
220
236
  sda.close if sda && block
221
237
  end
222
238
 
239
+ # Iterate over sda file
240
+ def self.each(file)
241
+ return to_enum(:each) unless block_given?
242
+
243
+ self.open(file, :read) do |sda|
244
+ while d = sda.get
245
+ yield(d)
246
+ end
247
+ end
248
+ end
249
+
250
+
223
251
  private
224
252
 
225
253
  def self.get_magic(io)
@@ -255,7 +283,7 @@ class SimpleData
255
283
  raise ParserError, "wrong spec" if field.nil?
256
284
 
257
285
  # Normalize
258
- t, n, d = field
286
+ t, n, u, d = field
259
287
  t = t.downcase.to_sym
260
288
  n .force_encoding('UTF-8')
261
289
  d&.force_encoding('UTF-8')
@@ -266,10 +294,12 @@ class SimpleData
266
294
  end
267
295
 
268
296
  # Cleaned-up field description
269
- [ t, n, d ].compact
297
+ [ t, n, u, d ]
270
298
  }
271
299
  # Extract description
272
300
  else
301
+ grp = grp.map {|l| l.sub(/^#\s?/, '') }
302
+ grp = grp[...-1] if grp.last&.empty?
273
303
  sections[s] = grp.join("\n")
274
304
  end
275
305
  else
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple-data
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stéphane D'Alu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-10-07 00:00:00.000000000 Z
11
+ date: 2023-02-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: zstd-ruby
@@ -88,7 +88,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
88
88
  - !ruby/object:Gem::Version
89
89
  version: '0'
90
90
  requirements: []
91
- rubygems_version: 3.3.21
91
+ rubygems_version: 3.3.26
92
92
  signing_key:
93
93
  specification_version: 4
94
94
  summary: Simple Data (CSV alternative)