-
Notifications
You must be signed in to change notification settings - Fork 5
/
csv2yaml.rb
executable file
·90 lines (86 loc) · 2.33 KB
/
csv2yaml.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env ruby
require 'csv'
require 'yaml'
require './mgetc'
require 'pry'
def convert(csvfn, ymlfn, outfn)
data = YAML.load_file(ymlfn)
existing = {}
data['mappings'].each do |row|
existing[row[1]] = row[0]
#existing[row[1]] = row[0].gsub('\\\\', '\\')
end
map = {}
CSV.foreach(csvfn, headers: true) do |row|
h = row.to_h
n = h['SFDC NAME']
next if n.nil? || n == ''
c = h['Calculated']
next if c == 'FALSE'
dn = h['DA Name']
map[n] = [] unless map.key?(n)
map[n] << dn
end
unknowns = {}
lines = {}
map.each do |n, names|
names = names.sort.uniq
re = '^[[:space:]]*'
l = names.length
re += '(' if l > 1
names.each_with_index do |name, i|
name.each_char do |r|
if (r >= 'a' && r <= 'z') || (r >= '0' && r <= '9') || r.ord > 0x80
re += r
elsif r >= 'A' && r <= 'Z'
re += r.downcase
elsif ['!', '/', ',', '-', '&', ']', '~', "'", '"'].include?(r)
re += r + '?'
elsif [' ', "\t"].include?(r)
re += '[[:space:]]*'
elsif ['|', '(', ')', '.', '*', '['].include?(r)
re += '\\\\' + r + '?'
else
unknowns[r] = 0 unless unknowns.key?(r)
unknowns[r] += 1
end
end
re += '|' if i < l-1
end
re += ')' if l > 1
re += '[[:space:]]*$'
if existing.key?(n)
if existing[n] == re
puts "Exact mapping already present in YAML: " + re
else
puts "Collision for " + n + ", choose:"
puts "(E)xisting: " + existing[n]
puts "(N)ew: " + re
print "> "
answer = mgetc.downcase
puts ''
return if answer == 'q'
re = existing[n] if answer == 'e'
end
end
lines[n] = " - ['" + re + "', '" + n + "']"
end
existing.each do |n, re|
unless map.key?(n)
lines[n] = " - ['" + re + "', '" + n + "']"
end
end
binding.pry if unknowns.length > 0
File.open(outfn, "w") do |f|
f.puts("---")
f.puts("# regexp format: mysql")
f.puts("mappings:")
lines.keys.sort.each { |key| f.puts(lines[key]) }
end
puts "Please check manually those cases (case insensitive): nokia, orange, red hat, pantheon"
end
if ARGV.size < 3
puts "Missing arguments: export.csv mapping.yaml new_mapping.yaml"
exit(1)
end
convert(ARGV[0], ARGV[1], ARGV[2])