daff 1.1.8 → 1.1.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/lib/coopy/coopy.rb +100 -37
  2. data/lib/lib/coopy/csv.rb +10 -5
  3. metadata +2 -2
@@ -5,18 +5,58 @@ module Coopy
5
5
  class Coopy
6
6
 
7
7
  def initialize
8
+ @extern_preference = false
9
+ @format_preference = nil
10
+ @delim_preference = nil
11
+ @output_format = "copy"
8
12
  end
9
13
 
10
14
  # protected - in ruby this doesn't play well with static/inline methods
11
15
 
12
16
  attr_accessor :format_preference
17
+ attr_accessor :delim_preference
18
+ attr_accessor :extern_preference
19
+ attr_accessor :output_format
13
20
  attr_accessor :io
14
21
  attr_accessor :mv
15
22
 
23
+ def check_format(name)
24
+ return @format_preference if @extern_preference
25
+ ext = ""
26
+ pt = name.rindex(".",nil || 0) || -1
27
+ if pt >= 0
28
+ ext = name[pt + 1..-1].to_lower_case
29
+ case(ext)
30
+ when "json"
31
+ @format_preference = "json"
32
+ when "csv"
33
+ @format_preference = "csv"
34
+ @delim_preference = ","
35
+ when "tsv"
36
+ @format_preference = "csv"
37
+ @delim_preference = "\t"
38
+ when "ssv"
39
+ @format_preference = "csv"
40
+ @delim_preference = ";"
41
+ else
42
+ ext = ""
43
+ end
44
+ end
45
+ return ext
46
+ end
47
+
48
+ def set_format(name)
49
+ @extern_preference = false
50
+ self.check_format("." + _hx_str(name))
51
+ @extern_preference = true
52
+ end
53
+
16
54
  def save_table(name,t)
55
+ self.set_format(@output_format) if @output_format != "copy"
17
56
  txt = ""
57
+ self.check_format(name)
18
58
  if @format_preference != "json"
19
- csv = ::Coopy::Csv.new
59
+ csv = ::Coopy::Csv.new(@delim_preference)
20
60
  txt = csv.render_table(t)
21
61
  else
22
62
  value = ::Coopy::Coopy.jsonify(t)
@@ -36,6 +76,7 @@ module Coopy
36
76
 
37
77
  def load_table(name)
38
78
  txt = @io.get_content(name)
79
+ ext = self.check_format(name)
39
80
  begin
40
81
  json = ::Haxe::Format::JsonParser.new(txt).parse_rec
41
82
  @format_preference = "json"
@@ -43,32 +84,33 @@ module Coopy
43
84
  raise "JSON failed" if t == nil
44
85
  return t
45
86
  rescue => e
46
- csv = ::Coopy::Csv.new
47
- @format_preference = "csv"
48
- data = csv.parse_table(txt)
49
- h = data.length
50
- w = 0
51
- w = data[0].length if h > 0
52
- output = ::Coopy::SimpleTable.new(w,h)
53
- begin
54
- _g = 0
55
- while(_g < h)
56
- i = _g
57
- _g+=1
58
- begin
59
- _g1 = 0
60
- while(_g1 < w)
61
- j = _g1
62
- _g1+=1
63
- val = data[i][j]
64
- output.set_cell(j,i,::Coopy::Coopy.cell_for(val))
65
- end
87
+ raise e if ext == "json"
88
+ end if ext == "json" || ext == ""
89
+ @format_preference = "csv"
90
+ csv = ::Coopy::Csv.new(@delim_preference)
91
+ data = csv.parse_table(txt)
92
+ h = data.length
93
+ w = 0
94
+ w = data[0].length if h > 0
95
+ output = ::Coopy::SimpleTable.new(w,h)
96
+ begin
97
+ _g = 0
98
+ while(_g < h)
99
+ i = _g
100
+ _g+=1
101
+ begin
102
+ _g1 = 0
103
+ while(_g1 < w)
104
+ j = _g1
105
+ _g1+=1
106
+ val = data[i][j]
107
+ output.set_cell(j,i,::Coopy::Coopy.cell_for(val))
66
108
  end
67
109
  end
68
110
  end
69
- output.trim_blank if output != nil
70
- return output
71
111
  end
112
+ output.trim_blank if output != nil
113
+ return output
72
114
  end
73
115
 
74
116
  attr_accessor :status
@@ -326,6 +368,16 @@ module Coopy
326
368
  color = true
327
369
  args.slice!(i,1)
328
370
  break
371
+ elsif tag == "--input-format"
372
+ more = true
373
+ self.set_format(args[i + 1])
374
+ args.slice!(i,2)
375
+ break
376
+ elsif tag == "--output-format"
377
+ more = true
378
+ @output_format = args[i + 1]
379
+ args.slice!(i,2)
380
+ break
329
381
  end
330
382
  end
331
383
  end
@@ -369,6 +421,7 @@ module Coopy
369
421
  io.write_stderr(" daff merge [--inplace] [--output OUTPUT.csv] parent.csv a.csv b.csv\n")
370
422
  io.write_stderr(" daff trim [--output OUTPUT.csv] source.csv\n")
371
423
  io.write_stderr(" daff render [--output OUTPUT.html] diff.csv\n")
424
+ io.write_stderr(" daff copy in.csv out.tsv\n")
372
425
  io.write_stderr(" daff git\n")
373
426
  io.write_stderr(" daff version\n")
374
427
  io.write_stderr("\n")
@@ -380,6 +433,8 @@ module Coopy
380
433
  io.write_stderr(" --context NUM: show NUM rows of context\n")
381
434
  io.write_stderr(" --all: do not prune unchanged rows\n")
382
435
  io.write_stderr(" --act ACT: show only a certain kind of change (update, insert, delete)\n")
436
+ io.write_stderr(" --input-format [csv|tsv|ssv|json]: set format to expect for input\n")
437
+ io.write_stderr(" --output-format [csv|tsv|ssv|json|copy]: set format for output\n")
383
438
  io.write_stderr("\n")
384
439
  io.write_stderr(" daff diff --git path old-file old-hex old-mode new-file new-hex new-mode\n")
385
440
  io.write_stderr(" --git: process arguments provided by git to diff drivers\n")
@@ -392,7 +447,7 @@ module Coopy
392
447
  end
393
448
  cmd1 = args[0]
394
449
  offset = 1
395
- if !Lambda.has(["diff","patch","merge","trim","render","git","version"],cmd1)
450
+ if !Lambda.has(["diff","patch","merge","trim","render","git","version","copy"],cmd1)
396
451
  if (cmd1.index(".",nil || 0) || -1) != -1 || (cmd1.index("--",nil || 0) || -1) == 0
397
452
  cmd1 = "diff"
398
453
  offset = 0
@@ -419,7 +474,7 @@ module Coopy
419
474
  args.push(old_file)
420
475
  args.push(new_file)
421
476
  end
422
- tool = ::Coopy::Coopy.new
477
+ tool = self
423
478
  tool.io = io
424
479
  parent = nil
425
480
  if args.length - offset >= 3
@@ -429,7 +484,13 @@ module Coopy
429
484
  aname = args[offset]
430
485
  a = tool.load_table(aname)
431
486
  b = nil
432
- b = tool.load_table(args[1 + offset]) if args.length - offset >= 2
487
+ if args.length - offset >= 2
488
+ if cmd1 != "copy"
489
+ b = tool.load_table(args[1 + offset])
490
+ else
491
+ output = args[1 + offset]
492
+ end
493
+ end
433
494
  if inplace
434
495
  io.write_stderr("Please do not use --inplace when specifying an output.\n") if output != nil
435
496
  output = aname
@@ -468,6 +529,8 @@ module Coopy
468
529
  renderer.complete_html if !fragment
469
530
  tool.save_text(output,renderer.html)
470
531
  tool.save_text(css_output,renderer.sample_css) if css_output != nil
532
+ elsif cmd1 == "copy"
533
+ tool.save_table(output,a)
471
534
  end
472
535
  if ok
473
536
  return 0
@@ -506,35 +569,35 @@ module Coopy
506
569
  def Coopy.random_tests
507
570
  st = ::Coopy::SimpleTable.new(15,6)
508
571
  tab = st
509
- ::Haxe::Log._trace.call("table size is " + _hx_str(tab.get_width) + "x" + _hx_str(tab.get_height),{ file_name: "Coopy.hx", line_number: 44, class_name: "coopy.Coopy", method_name: "randomTests"})
572
+ ::Haxe::Log._trace.call("table size is " + _hx_str(tab.get_width) + "x" + _hx_str(tab.get_height),{ file_name: "Coopy.hx", line_number: 51, class_name: "coopy.Coopy", method_name: "randomTests"})
510
573
  tab.set_cell(3,4,::Coopy::SimpleCell.new(33))
511
574
  ::Haxe::Log._trace.call("element is " + _hx_str(lambda{ s = tab.get_cell(3,4)
512
- _r = s.to_s}.call()),{ file_name: "Coopy.hx", line_number: 46, class_name: "coopy.Coopy", method_name: "randomTests"})
575
+ _r = s.to_s}.call()),{ file_name: "Coopy.hx", line_number: 53, class_name: "coopy.Coopy", method_name: "randomTests"})
513
576
  compare = ::Coopy::Compare.new
514
577
  d1 = ::Coopy::ViewedDatum.get_simple_view(::Coopy::SimpleCell.new(10))
515
578
  d2 = ::Coopy::ViewedDatum.get_simple_view(::Coopy::SimpleCell.new(10))
516
579
  d3 = ::Coopy::ViewedDatum.get_simple_view(::Coopy::SimpleCell.new(20))
517
580
  report = ::Coopy::Report.new
518
581
  compare.compare(d1,d2,d3,report)
519
- ::Haxe::Log._trace.call("report is " + _hx_str(report.to_s),{ file_name: "Coopy.hx", line_number: 54, class_name: "coopy.Coopy", method_name: "randomTests"})
582
+ ::Haxe::Log._trace.call("report is " + _hx_str(report.to_s),{ file_name: "Coopy.hx", line_number: 61, class_name: "coopy.Coopy", method_name: "randomTests"})
520
583
  d2 = ::Coopy::ViewedDatum.get_simple_view(::Coopy::SimpleCell.new(50))
521
584
  report.clear
522
585
  compare.compare(d1,d2,d3,report)
523
- ::Haxe::Log._trace.call("report is " + _hx_str(report.to_s),{ file_name: "Coopy.hx", line_number: 58, class_name: "coopy.Coopy", method_name: "randomTests"})
586
+ ::Haxe::Log._trace.call("report is " + _hx_str(report.to_s),{ file_name: "Coopy.hx", line_number: 65, class_name: "coopy.Coopy", method_name: "randomTests"})
524
587
  d2 = ::Coopy::ViewedDatum.get_simple_view(::Coopy::SimpleCell.new(20))
525
588
  report.clear
526
589
  compare.compare(d1,d2,d3,report)
527
- ::Haxe::Log._trace.call("report is " + _hx_str(report.to_s),{ file_name: "Coopy.hx", line_number: 62, class_name: "coopy.Coopy", method_name: "randomTests"})
590
+ ::Haxe::Log._trace.call("report is " + _hx_str(report.to_s),{ file_name: "Coopy.hx", line_number: 69, class_name: "coopy.Coopy", method_name: "randomTests"})
528
591
  d1 = ::Coopy::ViewedDatum.get_simple_view(::Coopy::SimpleCell.new(20))
529
592
  report.clear
530
593
  compare.compare(d1,d2,d3,report)
531
- ::Haxe::Log._trace.call("report is " + _hx_str(report.to_s),{ file_name: "Coopy.hx", line_number: 66, class_name: "coopy.Coopy", method_name: "randomTests"})
594
+ ::Haxe::Log._trace.call("report is " + _hx_str(report.to_s),{ file_name: "Coopy.hx", line_number: 73, class_name: "coopy.Coopy", method_name: "randomTests"})
532
595
  comp = ::Coopy::TableComparisonState.new
533
596
  ct = ::Coopy::CompareTable.new
534
597
  comp.a = st
535
598
  comp.b = st
536
599
  ct.attach(comp)
537
- ::Haxe::Log._trace.call("comparing tables",{ file_name: "Coopy.hx", line_number: 74, class_name: "coopy.Coopy", method_name: "randomTests"})
600
+ ::Haxe::Log._trace.call("comparing tables",{ file_name: "Coopy.hx", line_number: 81, class_name: "coopy.Coopy", method_name: "randomTests"})
538
601
  t1 = ::Coopy::SimpleTable.new(3,2)
539
602
  t2 = ::Coopy::SimpleTable.new(3,2)
540
603
  t3 = ::Coopy::SimpleTable.new(3,2)
@@ -542,13 +605,13 @@ module Coopy
542
605
  dt2 = ::Coopy::ViewedDatum.new(t2,::Coopy::SimpleView.new)
543
606
  dt3 = ::Coopy::ViewedDatum.new(t3,::Coopy::SimpleView.new)
544
607
  compare.compare(dt1,dt2,dt3,report)
545
- ::Haxe::Log._trace.call("report is " + _hx_str(report.to_s),{ file_name: "Coopy.hx", line_number: 82, class_name: "coopy.Coopy", method_name: "randomTests"})
608
+ ::Haxe::Log._trace.call("report is " + _hx_str(report.to_s),{ file_name: "Coopy.hx", line_number: 89, class_name: "coopy.Coopy", method_name: "randomTests"})
546
609
  t3.set_cell(1,1,::Coopy::SimpleCell.new("hello"))
547
610
  compare.compare(dt1,dt2,dt3,report)
548
- ::Haxe::Log._trace.call("report is " + _hx_str(report.to_s),{ file_name: "Coopy.hx", line_number: 85, class_name: "coopy.Coopy", method_name: "randomTests"})
611
+ ::Haxe::Log._trace.call("report is " + _hx_str(report.to_s),{ file_name: "Coopy.hx", line_number: 92, class_name: "coopy.Coopy", method_name: "randomTests"})
549
612
  t1.set_cell(1,1,::Coopy::SimpleCell.new("hello"))
550
613
  compare.compare(dt1,dt2,dt3,report)
551
- ::Haxe::Log._trace.call("report is " + _hx_str(report.to_s),{ file_name: "Coopy.hx", line_number: 88, class_name: "coopy.Coopy", method_name: "randomTests"})
614
+ ::Haxe::Log._trace.call("report is " + _hx_str(report.to_s),{ file_name: "Coopy.hx", line_number: 95, class_name: "coopy.Coopy", method_name: "randomTests"})
552
615
  v = ::Coopy::Viterbi.new
553
616
  td = ::Coopy::TableDiff.new(nil,nil)
554
617
  idx = ::Coopy::Index.new
@@ -654,7 +717,7 @@ module Coopy
654
717
  txt += "\n"
655
718
  end
656
719
  end
657
- ::Haxe::Log._trace.call(txt,{ file_name: "Coopy.hx", line_number: 627, class_name: "coopy.Coopy", method_name: "show"})
720
+ ::Haxe::Log._trace.call(txt,{ file_name: "Coopy.hx", line_number: 694, class_name: "coopy.Coopy", method_name: "show"})
658
721
  end
659
722
 
660
723
  def Coopy.jsonify(t)
@@ -4,9 +4,14 @@
4
4
  module Coopy
5
5
  class Csv
6
6
 
7
- def initialize
7
+ def initialize(delim = ",")
8
8
  @cursor = 0
9
9
  @row_ended = false
10
+ if delim == nil
11
+ @delim = ","
12
+ else
13
+ @delim = delim
14
+ end
10
15
  end
11
16
 
12
17
  protected
@@ -14,6 +19,7 @@ module Coopy
14
19
  attr_accessor :cursor
15
20
  attr_accessor :row_ended
16
21
  attr_accessor :has_structure
22
+ attr_accessor :delim
17
23
 
18
24
  public
19
25
 
@@ -33,7 +39,7 @@ module Coopy
33
39
  while(_g1 < w)
34
40
  x = _g1
35
41
  _g1+=1
36
- txt += "," if x > 0
42
+ txt += @delim if x > 0
37
43
  txt += self.render_cell(v,t.get_cell(x,y))
38
44
  end
39
45
  end
@@ -46,7 +52,6 @@ module Coopy
46
52
  def render_cell(v,d)
47
53
  return "NULL" if d == nil
48
54
  str = v.to_s(d)
49
- delim = ","
50
55
  need_quote = false
51
56
  begin
52
57
  _g1 = 0
@@ -55,7 +60,7 @@ module Coopy
55
60
  i = _g1
56
61
  _g1+=1
57
62
  ch = str[i]
58
- if ch == "\"" || ch == "'" || ch == delim || ch == "\r" || ch == "\n" || ch == "\t" || ch == " "
63
+ if ch == "\"" || ch == "'" || ch == @delim || ch == "\r" || ch == "\n" || ch == "\t" || ch == " "
59
64
  need_quote = true
60
65
  break
61
66
  end
@@ -125,7 +130,7 @@ module Coopy
125
130
  first_non_underscore = i if ch != 95 && i < first_non_underscore
126
131
  if @has_structure
127
132
  if !quoting
128
- break if ch == 44
133
+ break if ch == (@delim[0].ord rescue nil)
129
134
  if ch == 13 || ch == 10
130
135
  ch2 = (txt[i + 1].ord rescue nil)
131
136
  if ch2 != nil
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: daff
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.8
4
+ version: 1.1.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2014-07-18 00:00:00.000000000 Z
13
+ date: 2014-08-29 00:00:00.000000000 Z
14
14
  dependencies: []
15
15
  description: Diff and patch tables
16
16
  email: