Brian Silverman | 1fc17c0 | 2013-12-10 12:57:47 -0800 | [diff] [blame^] | 1 | #!/usr/bin/env ruby |
| 2 | |
| 3 | # This generates something designed to be copied into Mouser's BOM creation |
| 4 | # copy/paste box. It turns out that it's usually easier to just manually add |
| 5 | # everything to your cart with the "EZBuy" thingie, but it's still a reasonable |
| 6 | # format. |
| 7 | # |
| 8 | # Usage: generate_mouser_bom.rb FILE [COPIES] |
| 9 | |
| 10 | lines = File.open(ARGV[0]) do |f| |
| 11 | lines = f.readlines |
| 12 | lines.shift |
| 13 | lines.collect do |line| |
| 14 | line.split(', ') |
| 15 | end |
| 16 | end |
| 17 | |
| 18 | $parts = {} |
| 19 | |
| 20 | def print_part(pn_string) |
| 21 | #puts pn + '|1' |
| 22 | pn = pn_string.intern |
| 23 | if $parts[pn] |
| 24 | $parts[pn] = $parts[pn] + 1 |
| 25 | else |
| 26 | $parts[pn] = 1 |
| 27 | end |
| 28 | end |
| 29 | |
| 30 | lines.each do |line| |
| 31 | pn = line[4] |
| 32 | if pn.index(';') |
| 33 | parts = pn.split('; ') |
| 34 | parts.each do |part_string| |
| 35 | part = part_string.match(/(.+) x([0-9]+)?/) |
| 36 | if part |
| 37 | name = part[1] |
| 38 | number = part[2] |
| 39 | number.to_i.times do |
| 40 | print_part name |
| 41 | end |
| 42 | else |
| 43 | print_part part_string |
| 44 | end |
| 45 | end |
| 46 | else |
| 47 | print_part pn unless pn.empty? |
| 48 | end |
| 49 | end |
| 50 | |
| 51 | times = (ARGV[1] || 1).to_i |
| 52 | $parts.each do |pn, number| |
| 53 | puts "#{pn}|#{number * times}" |
| 54 | end |