rbbt-util 4.0.2 → 4.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.
@@ -0,0 +1,25 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
2
+ require 'rbbt/tsv'
3
+ require 'rbbt/tsv/util'
4
+ require 'rbbt/util/tmpfile'
5
+ require 'test/unit'
6
+
7
+ class TestTSVUtil < Test::Unit::TestCase
8
+
9
+ def test_field_counts
10
+ content =<<-EOF
11
+ #Id ValueA ValueB OtherID
12
+ row1 a|aa|aaa b Id1|Id2
13
+ row2 A B Id3
14
+ EOF
15
+
16
+ TmpFile.with_file(content) do |filename|
17
+ tsv = TSV.open(filename, :sep => /\s+/)
18
+
19
+ assert_equal 2, TSV.field_match_counts(tsv, ["a","A","a","b","Id3"])["ValueA"]
20
+
21
+ end
22
+
23
+ end
24
+
25
+ end
@@ -133,6 +133,14 @@ class TestMisc < Test::Unit::TestCase
133
133
  end
134
134
  end
135
135
 
136
+ def test_positions2hash
137
+ inputs = Misc.positional2hash([:one, :two, :three], 1, :two => 2, :four => 4)
138
+ assert_equal 1, inputs[:one]
139
+ assert_equal 2, inputs[:two]
140
+ assert_equal nil, inputs[:three]
141
+ assert_equal nil, inputs[:four]
142
+ end
143
+
136
144
  # def test_divide
137
145
  # assert_equal 2, Misc.divide(%w(1 2 3 4 5 6 7 8 9),2).length
138
146
  # end
metadata CHANGED
@@ -5,9 +5,9 @@ version: !ruby/object:Gem::Version
5
5
  prerelease:
6
6
  segments:
7
7
  - 4
8
+ - 1
8
9
  - 0
9
- - 2
10
- version: 4.0.2
10
+ version: 4.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Miguel Vazquez
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-10-03 00:00:00 +02:00
18
+ date: 2011-11-17 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -161,7 +161,6 @@ files:
161
161
  - lib/rbbt/util/log.rb
162
162
  - lib/rbbt/util/misc.rb
163
163
  - lib/rbbt/util/open.rb
164
- - lib/rbbt/util/persistence.rb
165
164
  - lib/rbbt/util/simpleDSL.rb
166
165
  - lib/rbbt/util/simpleopt.rb
167
166
  - lib/rbbt/util/task/job.rb
@@ -197,9 +196,11 @@ files:
197
196
  - test/rbbt/tsv/test_filter.rb
198
197
  - test/rbbt/tsv/test_index.rb
199
198
  - test/rbbt/tsv/test_manipulate.rb
199
+ - test/rbbt/tsv/test_util.rb
200
200
  - test/rbbt/workflow/test_soap.rb
201
201
  - test/rbbt/workflow/test_step.rb
202
202
  - test/rbbt/workflow/test_task.rb
203
+ - test/rbbt/test_persist.rb
203
204
  - test/test_rbbt.rb
204
205
  - bin/tsv.rb
205
206
  - bin/tchash.rb
@@ -264,7 +265,9 @@ test_files:
264
265
  - test/rbbt/tsv/test_filter.rb
265
266
  - test/rbbt/tsv/test_index.rb
266
267
  - test/rbbt/tsv/test_manipulate.rb
268
+ - test/rbbt/tsv/test_util.rb
267
269
  - test/rbbt/workflow/test_soap.rb
268
270
  - test/rbbt/workflow/test_step.rb
269
271
  - test/rbbt/workflow/test_task.rb
272
+ - test/rbbt/test_persist.rb
270
273
  - test/test_rbbt.rb
@@ -1,406 +0,0 @@
1
- require 'rbbt/util/tsv'
2
- require 'rbbt/util/misc'
3
- require 'rbbt/util/open'
4
- require 'digest/md5'
5
- require 'yaml'
6
-
7
- module Persistence
8
- require 'rbbt/util/tc_hash'
9
- TSV = TCHash
10
-
11
- CACHEDIR="/tmp/tsv_persistent_cache"
12
- FileUtils.mkdir CACHEDIR unless File.exist? CACHEDIR
13
-
14
- def self.cachedir=(cachedir)
15
- CACHEDIR.replace cachedir
16
- FileUtils.mkdir_p CACHEDIR unless File.exist? CACHEDIR
17
- end
18
-
19
- def self.cachedir
20
- CACHEDIR
21
- end
22
-
23
- def self.get_persistence_file(file, prefix, options = {})
24
- persistence_dir = Misc.process_options options, :dir
25
- persistence_dir ||= CACHEDIR
26
-
27
- if options.include? :filters
28
- options[:filters].each do |match,value|
29
- file = file + "&F[#{match}=#{Misc.digest(value.inspect)}]"
30
- end
31
- end
32
-
33
- name = prefix.to_s.dup << ":" << file.to_s << ":"
34
-
35
- options_md5 = Misc.hash2md5 options
36
- File.join(persistence_dir, name.to_s.gsub(/\s/,'_').gsub(/\//,'>') + options_md5)
37
- end
38
-
39
- def self.get_filename(file)
40
- case
41
- when (String === file and File.exists? file)
42
- File.expand_path file
43
- when File === file
44
- File.expand_path file.path
45
- when Object::TSV === file
46
- file.filename
47
- when String === file
48
- file
49
- else
50
- file.class.to_s
51
- end
52
- end
53
-
54
- def self.persist_string(file, prefix = "", options = {})
55
- options =
56
- Misc.add_defaults options, :persistence_update => false, :persistence_file => nil, :filename => nil
57
- persistence_update, persistence_file, filename =
58
- Misc.process_options options, :persistence_update, :persistence_file, :filename
59
-
60
- filename ||= get_filename(file)
61
- persistence_file ||= get_persistence_file(filename, prefix, options)
62
-
63
- if persistence_update or not File.exists? persistence_file
64
- Log.debug "Creating #{ persistence_file }. Prefix = #{prefix}"
65
-
66
- res = yield file, options, filename, persistence_file
67
- Open.write(persistence_file, res.to_s)
68
- res
69
- else
70
- Log.debug "Loading #{ persistence_file }. Prefix = #{prefix}"
71
-
72
- Open.read(persistence_file)
73
- end
74
- end
75
-
76
- def self.persist_marshal(file, prefix = "", options = {})
77
- options =
78
- Misc.add_defaults options, :persistence_update => false, :persistence_file => nil, :filename => nil
79
- persistence_update, persistence_file, filename =
80
- Misc.process_options options, :persistence_update, :persistence_file, :filename
81
-
82
- filename ||= get_filename(file)
83
- persistence_file ||= get_persistence_file(filename, prefix, options)
84
-
85
- if persistence_update or not File.exists? persistence_file
86
- Log.debug "Creating #{ persistence_file }. Prefix = #{prefix}"
87
- res = yield file, options
88
- Open.write(persistence_file, Marshal.dump(res))
89
- res
90
- else
91
- Log.debug "Loading #{ persistence_file }. Prefix = #{prefix}"
92
- Marshal.load(Open.open(persistence_file))
93
- end
94
- end
95
-
96
- def self.persist_yaml(file, prefix = "", options = {})
97
- options =
98
- Misc.add_defaults options, :persistence_update => false, :persistence_file => nil, :filename => nil
99
- persistence_update, persistence_file, filename =
100
- Misc.process_options options, :persistence_update, :persistence_file, :filename
101
-
102
- filename ||= get_filename(file)
103
- persistence_file ||= get_persistence_file(filename, prefix, options)
104
-
105
- if persistence_update or not File.exists? persistence_file
106
- Log.debug "Creating #{ persistence_file }. Prefix = #{prefix}"
107
- res = yield file, options, filename, persistence_file
108
- Open.write(persistence_file, YAML.dump(res))
109
- res
110
- else
111
- Log.debug "Loading #{ persistence_file }. Prefix = #{prefix}"
112
- YAML.load(Open.open(persistence_file))
113
- end
114
- end
115
-
116
- def self.persist_tsv_string(file, prefix = "", options = {})
117
- options =
118
- Misc.add_defaults options, :persistence_update => false, :persistence_file => nil, :filename => nil
119
- persistence_update, persistence_file, filename =
120
- Misc.process_options options, :persistence_update, :persistence_file, :filename
121
-
122
- filename ||= get_filename(file)
123
- persistence_file ||= get_persistence_file(filename, prefix, options)
124
-
125
- if persistence_update or not File.exists? persistence_file
126
- Log.debug "Creating #{ persistence_file }. Prefix = #{prefix}"
127
- res = yield file, options, filename, persistence_file
128
- Open.write(persistence_file, res.to_s)
129
- res
130
- else
131
- Log.debug "Loading #{ persistence_file }. Prefix = #{prefix}"
132
- tsv = Object::TSV.new persistence_file
133
- tsv.filename = filename
134
- tsv
135
- end
136
- end
137
-
138
- def self.tsv_serializer(data, extra = nil)
139
- return data.serializer if Persistence::TSV === data
140
- if Object::TSV === data
141
- return :integer if (data.cast == "to_i" or data.cast == :to_i) and data.type == :single
142
- return :integer_array if (data.cast == "to_i" or data.cast == :to_i) and (data.type == :list or data.type == :flat)
143
-
144
- case
145
- when (data.type == :list or data.type == :flat)
146
- :list
147
- when data.type == :single
148
- :single
149
- else
150
- :double
151
- end
152
- else
153
- return :marshal if extra.nil?
154
- return :integer if (extra[:cast] == "to_i" or extra[:cast] == :to_i) and extra[:type] == :single
155
- return :integer_array if (extra[:cast] == "to_i" or extra[:cast] == :to_i) and (extra[:type] == :list or extra[:type] == :flat)
156
-
157
- case
158
- when (extra[:type] == :list or extra[:type] == :flat)
159
- :list
160
- when extra[:type] == :single
161
- :single
162
- else
163
- :double
164
- end
165
- end
166
- end
167
-
168
- def self.persist_tsv(file, prefix = "", options = {})
169
- options =
170
- Misc.add_defaults options, :persistence_update => false, :persistence_file => nil, :filename => nil
171
- persistence_update, persistence_file, filename =
172
- Misc.process_options options, :persistence_update, :persistence_file, :filename
173
-
174
- filename ||= get_filename(file)
175
- persistence_file ||= get_persistence_file(filename, prefix, options)
176
-
177
- if persistence_update or not File.exists? persistence_file
178
- Log.debug "Creating #{ persistence_file }. Prefix = #{prefix}"
179
-
180
- res = yield file, options, filename, persistence_file
181
-
182
- serializer = tsv_serializer res
183
-
184
- if TCHash === res
185
- per = res
186
- else
187
-
188
- if File.exists? persistence_file
189
- Log.debug "Erasing old #{ persistence_file }. Prefix = #{prefix}"
190
- FileUtils.rm persistence_file
191
- end
192
-
193
- Log.debug "Dump data into '#{persistence_file}'"
194
- per = Persistence::TSV.get persistence_file, true, serializer
195
-
196
- per.write
197
- per.merge! res
198
-
199
- Persistence::TSV::FIELD_INFO_ENTRIES.keys.each do |key|
200
- if res.respond_to?(key.to_sym) and per.respond_to?("#{key}=".to_sym)
201
- per.send "#{key}=".to_sym, res.send(key.to_sym)
202
- end
203
- end
204
- end
205
-
206
- tsv = Object::TSV.new per
207
-
208
- per.read
209
-
210
- tsv
211
- else
212
- Log.debug "Loading #{ persistence_file }. Prefix = #{prefix}"
213
-
214
- per = Persistence::TSV.get persistence_file, false, serializer
215
- tsv = Object::TSV.new per
216
- Persistence::TSV::FIELD_INFO_ENTRIES.keys.each do |key|
217
- if tsv.respond_to?(key.to_sym) and per.respond_to?(key.to_sym)
218
- tsv.send "#{key}=".to_sym, per.send(key.to_sym)
219
- end
220
- end
221
-
222
- per.read
223
-
224
- tsv
225
- end
226
- end
227
-
228
- def self.persist_tsv_extra(file, prefix = "", options = {})
229
- options =
230
- Misc.add_defaults options, :persistence_update => false, :persistence_file => nil, :filename => nil
231
- persistence_update, persistence_file, filename =
232
- Misc.process_options options, :persistence_update, :persistence_file, :filename
233
-
234
- filename ||= get_filename(file)
235
- persistence_file ||= get_persistence_file(filename, prefix, options)
236
-
237
- if persistence_update or not File.exists?(persistence_file)
238
- Log.debug "Creating #{ persistence_file }. Prefix = #{prefix}"
239
- res, extra = yield file, options, filename, persistence_file
240
-
241
- serializer = tsv_serializer res, extra
242
-
243
- per = nil
244
- if not Persistence::TSV === res
245
- begin
246
- per = Persistence::TSV.get persistence_file, true, serializer
247
-
248
- per.write
249
- per.merge! res
250
- Persistence::TSV::FIELD_INFO_ENTRIES.keys.each do |key|
251
- if extra.include?(key.to_sym) and per.respond_to?(key.to_sym)
252
- per.send "#{key}=".to_sym, extra[key.to_sym]
253
- end
254
- end
255
-
256
- rescue Exception
257
- per.close
258
- raise $!
259
- end
260
- else
261
- per = res
262
- end
263
-
264
- [ per, extra ]
265
- else
266
- Log.debug "Loading #{ persistence_file }. Prefix = #{prefix}"
267
- begin
268
- per = Persistence::TSV.get persistence_file, false, serializer
269
-
270
- extra = {}
271
- Persistence::TSV::FIELD_INFO_ENTRIES.keys.each do |key|
272
- if per.respond_to?(key.to_sym)
273
- extra[key] = per.send(key.to_sym)
274
- end
275
- end
276
-
277
- rescue Interrupt
278
- raise "Interrupted"
279
- rescue Exception
280
- per.close unless per.nil?
281
- raise $!
282
- end
283
-
284
- [ per, extra ]
285
- end
286
- end
287
-
288
- def self.persist_fwt(file, prefix = "", options = {})
289
- options =
290
- Misc.add_defaults options, :persistence_update => false, :persistence_file => nil, :filename => nil
291
- persistence_update, persistence_file, filename =
292
- Misc.process_options options, :persistence_update, :persistence_file, :filename
293
-
294
- filename ||= get_filename(file)
295
- persistence_file ||= get_persistence_file(filename, prefix, options)
296
-
297
- if persistence_update or not File.exists? persistence_file
298
- Log.debug "Creating FWT #{ persistence_file }. Prefix = #{prefix}"
299
-
300
- range = options[:range]
301
-
302
- res = yield file, options, filename, persistence_file
303
-
304
- if File.exists? persistence_file
305
- Log.debug "Erasing old #{ persistence_file }. Prefix = #{prefix}"
306
- FileUtils.rm persistence_file
307
- end
308
-
309
-
310
- if FixWidthTable === res and res.filename == :memory
311
- Log.debug "Dumping memory FWT into #{ persistence_file }. Prefix = #{prefix}"
312
- FileUtils.mkdir_p File.dirname(persistence_file) unless File.exists? File.dirname(persistence_file)
313
- Open.write(persistence_file, res.dump)
314
- fwt = FixWidthTable.get persistence_file
315
- else
316
-
317
- max_length = res.collect{|k,v| k.length}.max
318
-
319
- if range
320
- begin
321
- fwt = FixWidthTable.get persistence_file, max_length, true
322
- fwt.add_range res
323
- rescue
324
- FileUtils.rm persistence_file if File.exists? persistence_file
325
- raise $!
326
- end
327
- else
328
- begin
329
- fwt = FixWidthTable.get persistence_file, max_length, false
330
- fwt.add_point res
331
- rescue
332
- FileUtils.rm persistence_file
333
- raise $!
334
- end
335
- end
336
-
337
- fwt.read
338
- end
339
-
340
- fwt
341
- else
342
- Log.debug "Loading #{ persistence_file }. Prefix = #{prefix}"
343
-
344
- fwt = FixWidthTable.new persistence_file, nil, nil
345
-
346
- fwt
347
- end
348
- end
349
-
350
- def self.persist(file, prefix = "", persistence_type = :string, options = {}, &block)
351
- options = Misc.add_defaults options, :persistence => true
352
-
353
- persistence =
354
- Misc.process_options options, :persistence
355
-
356
- filename = get_filename(file)
357
-
358
- o = options.dup
359
- o =
360
- Misc.add_defaults o, :persistence_update => false, :persistence_file => nil, :filename => nil
361
- persistence_update, persistence_dir, persistence_file, filename =
362
- Misc.process_options o, :persistence_update, :persistence_dir, :persistence_file, :filename
363
-
364
- filename ||= get_filename(file)
365
- persistence_file ||= get_persistence_file(filename, prefix, o.merge(:persistence_dir => persistence_dir))
366
-
367
- if persistence == :no_create
368
- persistence = false if not File.exists? persistence_file
369
- end
370
-
371
- if not persistence
372
- Log.low "Non Persistent Loading for #{filename}. Prefix: #{prefix}"
373
- yield file, options, filename
374
- else
375
- Log.low "Persistent Loading for #{filename}. Prefix: #{prefix}. Type #{persistence_type.to_s}"
376
- Misc.lock(persistence_file, file, prefix, options, block) do |persistence_file,file,prefix,options,block|
377
- case persistence_type.to_sym
378
- when :string
379
- persist_string(file, prefix, options, &block)
380
- when :marshal
381
- persist_marshal(file, prefix, options, &block)
382
- when :yaml
383
- persist_yaml(file, prefix, options, &block)
384
- when :tsv
385
- persist_tsv(file, prefix, options, &block)
386
- when :tsv_string
387
- persist_tsv_string(file, prefix, options, &block)
388
- when :tsv_extra
389
- persist_tsv_extra(file, prefix, options, &block)
390
- when :fwt
391
- persist_fwt(file, prefix, options, &block)
392
- end
393
- end
394
- end
395
- end
396
- end
397
-
398
- module LocalPersist
399
-
400
- attr_accessor :local_persistence_dir
401
- def local_persist(name, prefix, type, options= {}, &block)
402
- Persistence.persist(name, prefix, type, options.merge({:persistence_dir => @local_persistence_dir}), &block)
403
- end
404
-
405
- end
406
-