Class: Kut::Bom2EL

Inherits:
Object
  • Object
show all
Defined in:
lib/kut/commands/gost/bom2el_list.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBom2EL

Returns a new instance of Bom2EL.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/kut/commands/gost/bom2el_list.rb', line 11

def initialize
  @name = 'gs-bom2el'
  @help_banner = 'Generate list of elements from bill of materials'
  @generators = []
  
  @options = OpenStruct.new
  @options.in_file_name = '-'
  @options.out_file_name = '-'
  @options.separator = ';'
  
  @option_parser = OptionParser.new do |opts|
    opts.banner = "Usage: kut #{self.name} [options]"
           
    opts.on("-i", "--input INPUT_FILE", "Innput pins file. if - to use stdin.") do |file_name|
      @options.in_file_name = file_name
    end
    
    opts.on("-o", "--output OUTPUT_FILE", "Output libary file. if - to use stdout.") do |file_name|
      @options.out_file_name = file_name
    end   
    
  end
  
end

Instance Attribute Details

#help_bannerObject (readonly)

Returns the value of attribute help_banner.



9
10
11
# File 'lib/kut/commands/gost/bom2el_list.rb', line 9

def help_banner
  @help_banner
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/kut/commands/gost/bom2el_list.rb', line 8

def name
  @name
end

Instance Method Details

#gs_bom2el(f_in, f_out) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/kut/commands/gost/bom2el_list.rb', line 110

def gs_bom2el(f_in, f_out)
  
  sep = @options.separator
 
  first_line = true
  header = nil
  rows = []
 
  f_in.each_line { |line|
    row = CSV.parse_line(line, sep)
    header = row if first_line
    rows << row unless first_line
    first_line = false if first_line
  }
      
  footprint_index = header.index("footprint")
  ref_index = header.index("ref")
  value_index = header.index("value")
  
  groups = {}
  
  rows.each { |row|
    gr_id = "#{row[value_index]} #{row[footprint_index]}"
    group = groups[gr_id]
    unless group
      group = [] 
      groups[gr_id] = group
    end
    group << row[ref_index].to_s
  }
  
  result = []
  
  groups.each { |key, value|
    count = value.size()
    refs = join_ref(value)
    result << "#{refs}#{sep}#{key}#{sep}#{count}\n" 
  }      
  
  result.sort!
  f_out << "refs#{sep}type#{sep}count\n"
  result.each { |v| f_out << v }
end

#help(args) ⇒ Object



36
37
38
39
# File 'lib/kut/commands/gost/bom2el_list.rb', line 36

def help(args)
  @option_parser.parse!(args)
  @option_parser.to_s()
end

#join_ref(ref_list) ⇒ Object



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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/kut/commands/gost/bom2el_list.rb', line 43

def join_ref(ref_list)
  return '' unless ref_list
  
  ref_list.sort! { |a, b|
    a =~ /(\D+)(\d*)/
    a_n = $1
    a_val = $2 ? $2.to_i : -1
    b =~ /(\D+)(\d*)/
    b_n = $1
    b_val = $2 ? $2.to_i : -1
    
    res = a_n <=> b_n
    res = a_val <=> b_val if res == 0
    res
  }
   
  ref_list.first =~ /(\D+)(\d+)/
  prev_pref = $1
  prev_num = $2.to_i
  prev_ref = ref_list.first
  
  result = ref_list.first
  ref_list.delete_at(0)
  counter = 0
  
#  while ! ref_list.empty?
#    ref = ref_list.first
#    ref_list.delete_at(0)
#  end
  
  flag = false
  
  ref_list.each { |ref|
    ref =~ /(\D+)(\d+)/
    pref = $1
    num = $2.to_i
    
    flag = (pref == prev_pref) && (num == (prev_num + 1))
      
    if !flag then
      if counter != 0 then
        result += counter > 1 ? '..' : ','
        result += prev_ref + ',' + ref
      else
        result += ',' + ref
      end
    end
    
    #result += '..' + prev_ref + ',' + ref unless flag && counter != 0
    #result += ',' + ref unless flag
    
    counter += 1 if flag
    counter = 0 unless flag
    
    prev_pref = pref
    prev_num = num    
    prev_ref = ref
  }
  
  if flag && counter != 0 then
    result += counter > 1 ? '..' : ','
    result += prev_ref
  end 
  
  result
end

#run(args) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/kut/commands/gost/bom2el_list.rb', line 154

def run(args)

  begin @option_parser.parse!(args)
  rescue OptionParser::InvalidOption => e
    $stderr << "Unknow option #{e}\n"
    exit
  end
                    
  f_in = $stdin
  f_in = File.new(@options.in_file_name) if @options.in_file_name && @options.in_file_name != '-'
  f_out = $stdout
  f_out = File.new(@options.out_file_name, 'w') if @options.out_file_name && @options.out_file_name != '-'
  
  gs_bom2el(f_in, f_out)
  
end