csv-utils 0.2.2 → 0.3.3

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: f85c431ad42ed20382fbe91c3696153be9437a8ee755ede313be2d6f488b3770
4
- data.tar.gz: b620cfb208a7a28573160103155564875b05990f965f9206ad89bd7cb6b5fcc7
3
+ metadata.gz: a28b89b25aa7a6a90137b799c580aa9060d783f481000fd173281bc7e2367baf
4
+ data.tar.gz: 7317ece2b8970a816b5e4b5b84623a2c966c3a2f7afa7c2dd30de831f7e3ac64
5
5
  SHA512:
6
- metadata.gz: 02fe7a0d34f61c54a3788739cc5455dc685cad7b627a9091f2b6e5b3ed0323bf5d162cff53150e89a431c6ab42e76e014b1350c6b47f4aeed66ef727ffe76554
7
- data.tar.gz: 117d50507b9661c1d70b89df658a97649ea2562604c9dd0764f160e9294586293edbf10e9d968da0107a2f072b0e1814d105c364f91ed2c22cca509fb48f2fd6
6
+ metadata.gz: e057cc9795559c630e4b939c67d00547a25e0cf83ed75b15713a6c381677a718dd2ac06f975486e5cdf45a1cbfd962caf2aa13105e57a544aa4b6fd177926445
7
+ data.tar.gz: ee7552522db9fb683b8cbd73a94706a0a41e92135aed0c2ebec6c70754f1bcad210234b8e5b18da87322bbc6737b6f323c515a1920f09fda3230a5cdef2393d1
@@ -0,0 +1,60 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'csv'
4
+ begin
5
+ require 'rchardet'
6
+ rescue LoadError
7
+ $stderr.puts 'gem install rchardet'
8
+ exit 1
9
+ end
10
+
11
+ def utf8?(str)
12
+ str
13
+ .force_encoding('utf-8')
14
+ .valid_encoding?
15
+ end
16
+
17
+ def convert_to_utf8(str, current_encoding)
18
+ str.force_encoding(current_encoding)
19
+ return nil unless str.valid_encoding?
20
+
21
+ str.encode('utf-8')
22
+ end
23
+
24
+ def detect_encoding(col)
25
+ CharDet.detect(col)['encoding']
26
+ end
27
+
28
+ csv = CSV.open(ARGV[0], 'rb')
29
+ out = CSV.open(ARGV[1], 'wb') if ARGV[1]
30
+
31
+ headers = csv.shift
32
+ out << headers if out
33
+ csv_lineno = 1
34
+
35
+ while (row = csv.shift)
36
+ csv_lineno += 1
37
+
38
+ unless row.size == headers.size
39
+ $stderr.puts "row(#{csv_lineno}): invalid number of columns, expected #{headers.size} got #{row.size}"
40
+ end
41
+
42
+ converted = false
43
+ row.each_with_index do |col, idx|
44
+ next if utf8?(col)
45
+
46
+ $stderr.puts "row(#{csv_lineno}),col(#{idx + 1}) #{headers[idx]}: none UTF-8 characters found in \"#{col}\""
47
+ if (col_utf8_encoded = convert_to_utf8(col, detect_encoding(col)))
48
+ converted = true
49
+ puts "row(#{csv_lineno}),col(#{idx + 1}) #{headers[idx]}: converted to UTF-8 from #{detect_encoding(col)} \"#{col_utf8_encoded}\""
50
+ row[idx] = col_utf8_encoded
51
+ else
52
+ $stderr.puts "row(#{csv_lineno}),col(#{idx + 1}) #{headers[idx]}: unknown character encoding"
53
+ end
54
+ end
55
+
56
+ out << row if out && converted
57
+ end
58
+
59
+ csv.close
60
+ out.close if out
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'csv-utils'
5
- s.version = '0.2.2'
5
+ s.version = '0.3.3'
6
6
  s.licenses = ['MIT']
7
7
  s.summary = 'CSV Utils'
8
8
  s.description = 'Tools for debugging malformed CSV files'
@@ -7,4 +7,6 @@ module CSVUtils
7
7
  autoload :CSVReport, 'csv_utils/csv_report'
8
8
  autoload :CSVRow, 'csv_utils/csv_row'
9
9
  autoload :CSVSort, 'csv_utils/csv_sort'
10
+ autoload :CSVTransformer, 'csv_utils/csv_transformer'
11
+ autoload :CSVWrapper, 'csv_utils/csv_wrapper'
10
12
  end
@@ -1,20 +1,15 @@
1
1
  # Utility class for appending data to a csv file.
2
2
  class CSVUtils::CSVExtender
3
- attr_reader :csv_file,
4
- :new_csv_file,
5
- :csv_options
6
-
7
- def initialize(csv_file, new_csv_file, csv_options = {})
8
- @csv_file = csv_file
9
- @new_csv_file = new_csv_file
10
- @csv_options = csv_options
3
+ def initialize(src_csv, dest_csv, csv_options = {})
4
+ @src_csv = CSVUtils::CSVWrapper.new(src_csv, 'rb', csv_options)
5
+ @dest_csv = CSVUtils::CSVWrapper.new(dest_csv, 'wb', csv_options)
11
6
  end
12
7
 
13
8
  def append(additional_headers)
14
9
  process(additional_headers) do |current_headers|
15
- while (row = src.shift)
10
+ while (row = @src_csv.shift)
16
11
  additional_columns = yield row, current_headers
17
- dest << (row + additional_columns)
12
+ @dest_csv << (row + additional_columns)
18
13
  end
19
14
  end
20
15
  end
@@ -27,13 +22,13 @@ class CSVUtils::CSVExtender
27
22
  additional_rows = yield batch, current_headers
28
23
 
29
24
  batch.each_with_index do |row, idx|
30
- dest << (row + additional_rows[idx])
25
+ @dest_csv << (row + additional_rows[idx])
31
26
  end
32
27
 
33
28
  batch = []
34
29
  end
35
30
 
36
- while (row = src.shift)
31
+ while (row = @src_csv.shift)
37
32
  batch << row
38
33
 
39
34
  process_batch_proc.call if batch.size >= batch_size
@@ -43,6 +38,8 @@ class CSVUtils::CSVExtender
43
38
  end
44
39
  end
45
40
 
41
+ private
42
+
46
43
  def process(additional_headers)
47
44
  current_headers = append_headers(additional_headers)
48
45
 
@@ -51,26 +48,16 @@ class CSVUtils::CSVExtender
51
48
  close
52
49
  end
53
50
 
54
- def src
55
- @src ||= CSV.open(csv_file, 'rb', csv_options)
56
- end
57
-
58
- def dest
59
- @dest ||= CSV.open(new_csv_file, 'wb', csv_options)
60
- end
61
-
62
51
  def close
63
- src.close
64
- dest.close
52
+ @src_csv.close
53
+ @dest_csv.close
65
54
  end
66
55
 
67
- private
68
-
69
56
  def append_headers(additional_headers)
70
57
  return nil unless additional_headers
71
58
 
72
- current_headers = src.shift
73
- dest << (current_headers + additional_headers)
59
+ current_headers = @src_csv.shift
60
+ @dest_csv << (current_headers + additional_headers)
74
61
  current_headers
75
62
  end
76
63
  end
@@ -4,7 +4,7 @@ module CSVUtils
4
4
  attr_reader :csv,
5
5
  :must_close
6
6
 
7
- def initialize(csv, csv_options = {}, &block)
7
+ def initialize(csv, headers = nil, csv_options = {}, &block)
8
8
  @csv =
9
9
  if csv.is_a?(String)
10
10
  @must_close = true
@@ -15,10 +15,11 @@ module CSVUtils
15
15
  csv
16
16
  end
17
17
 
18
- generate(&block) if block
18
+ generate(headers, &block) if block
19
19
  end
20
20
 
21
- def generate
21
+ def generate(headers = nil)
22
+ add_headers(headers) if headers
22
23
  yield self
23
24
  @csv.close if @must_close
24
25
  end
@@ -23,10 +23,16 @@ module CSVUtils
23
23
 
24
24
  add_value_to_class_method(:csv_columns, header => options)
25
25
  end
26
- end
27
26
 
28
- def csv_headers
29
- self.class.csv_columns.values.map { |column_options| csv_column_header(column_options) }
27
+ def csv_headers
28
+ csv_columns.values.map { |column_options| csv_column_header(column_options) }
29
+ end
30
+
31
+ private
32
+
33
+ def csv_column_header(column_options)
34
+ column_options[:header]
35
+ end
30
36
  end
31
37
 
32
38
  def csv_row
@@ -34,12 +40,12 @@ module CSVUtils
34
40
  end
35
41
  alias_method :to_a, :csv_row
36
42
 
37
- private
38
-
39
- def csv_column_header(column_options)
40
- column_options[:header]
43
+ def csv_headers
44
+ self.class.csv_headers
41
45
  end
42
46
 
47
+ private
48
+
43
49
  def csv_column_value(column_options)
44
50
  if column_options[:proc]
45
51
  instance_eval(&column_options[:proc])
@@ -0,0 +1,119 @@
1
+ # Transforms a CSV given a series of steps
2
+ class CSVUtils::CSVTransformer
3
+ attr_reader :headers
4
+
5
+ def initialize(src_csv, dest_csv, csv_options = {})
6
+ @src_csv = CSVUtils::CSVWrapper.new(src_csv, 'rb', csv_options)
7
+ @dest_csv = CSVUtils::CSVWrapper.new(dest_csv, 'wb', csv_options)
8
+ end
9
+
10
+ def read_headers
11
+ @headers = @src_csv.shift
12
+ self
13
+ end
14
+
15
+ def additional_data(&block)
16
+ steps << [:additional_data, @headers, block]
17
+ self
18
+ end
19
+
20
+ def select(&block)
21
+ steps << [:select, @headers, block]
22
+ self
23
+ end
24
+
25
+ def reject(&block)
26
+ steps << [:reject, @headers, block]
27
+ self
28
+ end
29
+
30
+ def map(new_headers, &block)
31
+ steps << [:map, @headers, block]
32
+ @headers = new_headers
33
+ self
34
+ end
35
+
36
+ def append(additional_headers, &block)
37
+ steps << [:append, @headers, block]
38
+
39
+ if additional_headers
40
+ @headers += additional_headers
41
+ else
42
+ @headers = nil
43
+ end
44
+
45
+ self
46
+ end
47
+
48
+ def each(&block)
49
+ steps << [:each, @headers, block]
50
+ self
51
+ end
52
+
53
+ def set_headers(headers)
54
+ @headers = headers
55
+ self
56
+ end
57
+
58
+ def process(batch_size = 10_000, &block)
59
+ batch = []
60
+
61
+ @dest_csv << @headers if @headers
62
+
63
+ steps_proc = Proc.new do
64
+ steps.each do |step_type, current_headers, proc|
65
+ batch = process_step(step_type, current_headers, batch, &proc)
66
+ end
67
+
68
+ batch.each { |row| @dest_csv << row }
69
+
70
+ batch = []
71
+ end
72
+
73
+ while (row = @src_csv.shift)
74
+ batch << row
75
+ steps_proc.call if batch.size >= batch_size
76
+ end
77
+
78
+ steps_proc.call if batch.size > 0
79
+
80
+ @src_csv.close
81
+ @dest_csv.close
82
+ end
83
+
84
+ private
85
+
86
+ def steps
87
+ @steps ||= []
88
+ end
89
+
90
+
91
+ def process_step(step_type, current_headers, batch, &block)
92
+ case step_type
93
+ when :select
94
+ batch.select! do |row|
95
+ block.call row, current_headers, @additional_data
96
+ end
97
+ when :reject
98
+ batch.reject! do |row|
99
+ block.call row, current_headers, @additional_data
100
+ end
101
+ when :map
102
+ batch.map! do |row|
103
+ block.call row, current_headers, @additional_data
104
+ end
105
+ when :append
106
+ batch.map! do |row|
107
+ row + block.call(row, current_headers, @additional_data)
108
+ end
109
+ when :additional_data
110
+ @additional_data = block.call(batch, current_headers)
111
+ when :each
112
+ batch.each do |row|
113
+ block.call(row, current_headers, @additional_data)
114
+ end
115
+ end
116
+
117
+ batch
118
+ end
119
+ end
@@ -0,0 +1,47 @@
1
+ # Wraps a CSV object, if wrapper opens the csv file it will close it
2
+ class CSVUtils::CSVWrapper
3
+ attr_reader :csv
4
+
5
+ def initialize(csv, mode, csv_options)
6
+ open(csv, mode, csv_options)
7
+ end
8
+
9
+ def self.open(file, mode, csv_options = {})
10
+ csv = new(file, mode, csv_options)
11
+
12
+ if block_given?
13
+ yield csv
14
+ csv.close
15
+ else
16
+ csv
17
+ end
18
+ end
19
+
20
+ def open(csv, mode, csv_options)
21
+ if csv.is_a?(String)
22
+ @close_when_done = true
23
+ @csv = CSV.open(csv, mode, csv_options)
24
+ else
25
+ @close_when_done = false
26
+ @csv = csv
27
+ end
28
+ end
29
+
30
+ def <<(row)
31
+ csv << row
32
+ end
33
+
34
+ def shift
35
+ csv.shift
36
+ end
37
+
38
+ def close
39
+ csv.close if close_when_done?
40
+ end
41
+
42
+ private
43
+
44
+ def close_when_done?
45
+ @close_when_done
46
+ end
47
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: csv-utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Doug Youch
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-06 00:00:00.000000000 Z
11
+ date: 2020-07-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: inheritance-helper
@@ -30,6 +30,7 @@ executables:
30
30
  - csv-change-eol
31
31
  - csv-find-error
32
32
  - csv-readline
33
+ - csv-validator
33
34
  extensions: []
34
35
  extra_rdoc_files: []
35
36
  files:
@@ -43,6 +44,7 @@ files:
43
44
  - bin/csv-change-eol
44
45
  - bin/csv-find-error
45
46
  - bin/csv-readline
47
+ - bin/csv-validator
46
48
  - csv-utils.gemspec
47
49
  - lib/csv-utils.rb
48
50
  - lib/csv_utils/csv_extender.rb
@@ -50,6 +52,8 @@ files:
50
52
  - lib/csv_utils/csv_report.rb
51
53
  - lib/csv_utils/csv_row.rb
52
54
  - lib/csv_utils/csv_sort.rb
55
+ - lib/csv_utils/csv_transformer.rb
56
+ - lib/csv_utils/csv_wrapper.rb
53
57
  - script/console
54
58
  homepage: https://github.com/dougyouch/csv-utils
55
59
  licenses: