Class: Ecu::LabelList
- Inherits:
-
Object
- Object
- Ecu::LabelList
- Includes:
- Enumerable
- Defined in:
- lib/ecu/labels/label_list.rb,
lib/ecu/interfaces/a2l/label_list.rb,
lib/ecu/interfaces/dcm/label_list.rb,
lib/ecu/interfaces/lab/label_list.rb,
lib/ecu/interfaces/mfile/label_list.rb
Constant Summary collapse
- A2LREGEXP =
%r{/begin CHARACTERISTIC\s+([\S]+)\s+"([^"]*)"}- DCM_HEADER =
"KONSERVIERUNG_FORMAT 2.0"- BLANKLINE_REGEX =
/^\s*$/- COMMENT_REGEX =
/^\*.*/
Instance Attribute Summary collapse
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#subheaders ⇒ Object
Returns the value of attribute subheaders.
Class Method Summary collapse
- .from_a2l(str) ⇒ Object
- .from_dcm(str) ⇒ Object
- .from_file(file_path) ⇒ Object
- .from_lab(str) ⇒ Object
- .labelclasses ⇒ Object
- .normalize_whitespace(line) ⇒ Object
Instance Method Summary collapse
- #+(other) ⇒ Object
- #<<(label) ⇒ Object
- #==(other) ⇒ Object
- #contains?(name) ⇒ Boolean
- #delete_if(&blk) ⇒ Object
- #each(&blk) ⇒ Object
- #empty? ⇒ Boolean
- #group_by(&blk) ⇒ Object
-
#initialize(labels = [], headers = [], subheaders = [], safe = false) ⇒ LabelList
constructor
A new instance of LabelList.
- #inspect ⇒ Object
- #map_int(&blk) ⇒ Object
- #merge(other) ⇒ Object
- #merge!(other) ⇒ Object
- #reject(&blk) ⇒ Object
- #reject!(&blk) ⇒ Object
- #select(&blk) ⇒ Object
- #size ⇒ Object
- #sort_by(&blk) ⇒ Object
- #to_a ⇒ Object
- #to_dcm(indented = false) ⇒ Object
- #to_lab ⇒ Object
- #to_mfile ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(labels = [], headers = [], subheaders = [], safe = false) ⇒ LabelList
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/ecu/labels/label_list.rb', line 40 def initialize(labels=[], headers=[], subheaders=[], safe=false) unless safe names = labels.map(&:name) if names.uniq.size != names.size duplicates = names.select { |name| names.count(name) > 1 }.uniq fail ArgumentError, "Label list must not contain duplicates: #{duplicates.join(", ")}" end end @labels = labels @headers = headers @subheaders = subheaders end |
Instance Attribute Details
#headers ⇒ Object
Returns the value of attribute headers.
54 55 56 |
# File 'lib/ecu/labels/label_list.rb', line 54 def headers @headers end |
#subheaders ⇒ Object
Returns the value of attribute subheaders.
54 55 56 |
# File 'lib/ecu/labels/label_list.rb', line 54 def subheaders @subheaders end |
Class Method Details
.from_a2l(str) ⇒ Object
7 8 9 10 11 12 13 14 15 |
# File 'lib/ecu/interfaces/a2l/label_list.rb', line 7 def self.from_a2l(str) str.gsub!(%r{/\*.*?\*/}, "") labels = str.scan(A2LREGEXP).map do |name, description| # Ugly hack: Create dummy Festwert until better parsing # is implemented Ecu::Festwert.new(name: name, description: description, value: 0) end new(labels) end |
.from_dcm(str) ⇒ Object
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 |
# File 'lib/ecu/interfaces/dcm/label_list.rb', line 21 def self.from_dcm(str) buffer = DcmBuffer.new headers = [] subheaders = [] functions = [] labels = {} str.each_line.lazy.with_index(1).each do |line, n| line = normalize_whitespace(line) case line in BLANKLINE_REGEX then next in COMMENT_REGEX case buffer.header in :pre then headers << line[1..].strip in :after then subheaders << line[1..].strip in :done then # Header time over, do nothing end in DCM_HEADER then buffer.header_seen! in ^(Functions.dcm_header) then buffer.start!(Functions, [line]) in ^(Festwert.dcm_header) then buffer.start!(Festwert, [line]) in ^(Festwerteblock.dcm_header) then buffer.start!(Festwerteblock, [line]) in ^(Kennlinie.dcm_header) then buffer.start!(Kennlinie, [line]) in ^(Gruppenkennlinie.dcm_header) then buffer.start!(Gruppenkennlinie, [line]) in ^(Festkennlinie.dcm_header) then buffer.start!(Festkennlinie, [line]) in ^(Kennfeld.dcm_header) then buffer.start!(Kennfeld, [line]) in ^(Gruppenkennfeld.dcm_header) then buffer.start!(Gruppenkennfeld, [line]) in ^(Festkennfeld.dcm_header) then buffer.start!(Festkennfeld, [line]) in ^(Stuetzstellenverteilung.dcm_header) then buffer.start!(Stuetzstellenverteilung, [line]) in "END" case obj = buffer.finish!(line) in Label fail "Duplicate label #{obj.name}" unless labels[obj.name].nil? labels[obj.name] = obj in Functions fail "Duplicate functions definition" unless functions.empty? functions = obj else fail "Unknown object #{obj}" end else buffer.append!(line) end rescue StandardError => e raise MalformedDcmError.new(e., n, str), e. end new(labels.values, headers, subheaders, true) end |
.from_file(file_path) ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/ecu/labels/label_list.rb', line 21 def self.from_file(file_path) case File.extname(file_path) when ".lab" then self.from_lab(File.read_encoded(file_path)) when ".a2l" then self.from_a2l(File.read_encoded(file_path)) when ".dcm" then self.from_dcm(File.read_encoded(file_path)) else fail "Unknown file extension: #{file_path}!" end end |
.from_lab(str) ⇒ Object
6 7 8 9 |
# File 'lib/ecu/interfaces/lab/label_list.rb', line 6 def self.from_lab(str) _, labels, headers, subheaders = LabParser.call(str) new(labels, headers, subheaders) end |
.labelclasses ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/ecu/labels/label_list.rb', line 30 def self.labelclasses [ Festwert, Festwerteblock, Kennlinie, Kennfeld, Stuetzstellenverteilung ] end |
.normalize_whitespace(line) ⇒ Object
72 73 74 |
# File 'lib/ecu/interfaces/dcm/label_list.rb', line 72 def self.normalize_whitespace(line) line.chomp.gsub(/[[:space:]]/, " ").rstrip end |
Instance Method Details
#+(other) ⇒ Object
115 |
# File 'lib/ecu/labels/label_list.rb', line 115 def +(other); merge(other) end |
#<<(label) ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/ecu/labels/label_list.rb', line 56 def <<(label) unless self.class.labelclasses.any? { |klass| label.is_a?(klass) } fail ArgumentError, "Can only add recognised labels" end if self.map(&:name).include? label.name fail ArgumentError, "Cannot add duplicate labels" end @labels << label end |
#==(other) ⇒ Object
66 67 68 69 70 |
# File 'lib/ecu/labels/label_list.rb', line 66 def ==(other) @labels.all? do |label| other.find { |l| l == label } end end |
#contains?(name) ⇒ Boolean
125 126 127 |
# File 'lib/ecu/labels/label_list.rb', line 125 def contains?(name) map(&:name).include?(name) end |
#delete_if(&blk) ⇒ Object
92 93 94 |
# File 'lib/ecu/labels/label_list.rb', line 92 def delete_if(&blk) @labels.delete_if(&blk) end |
#each(&blk) ⇒ Object
72 73 74 |
# File 'lib/ecu/labels/label_list.rb', line 72 def each(&blk) @labels.each(&blk) end |
#empty? ⇒ Boolean
121 122 123 |
# File 'lib/ecu/labels/label_list.rb', line 121 def empty? @labels.empty? end |
#group_by(&blk) ⇒ Object
96 97 98 |
# File 'lib/ecu/labels/label_list.rb', line 96 def group_by(&blk) @labels.group_by(&blk).map { |k, v| [k, self.class.new(v)] }.to_h end |
#inspect ⇒ Object
135 |
# File 'lib/ecu/labels/label_list.rb', line 135 def inspect; to_s end |
#map_int(&blk) ⇒ Object
76 77 78 |
# File 'lib/ecu/labels/label_list.rb', line 76 def map_int(&blk) self.class.new(@labels.map(&blk), @headers, @subheaders) end |
#merge(other) ⇒ Object
104 105 106 |
# File 'lib/ecu/labels/label_list.rb', line 104 def merge(other) LabelListComparison.new(self, other).merge(priority: :right) end |
#merge!(other) ⇒ Object
108 109 110 111 112 113 |
# File 'lib/ecu/labels/label_list.rb', line 108 def merge!(other) merged = merge(other) @labels = merged.to_a @headers = merged.headers @subheaders = merged.subheaders end |
#reject(&blk) ⇒ Object
88 89 90 |
# File 'lib/ecu/labels/label_list.rb', line 88 def reject(&blk) self.class.new(@labels.reject(&blk), @headers, @subheaders) end |
#reject!(&blk) ⇒ Object
84 85 86 |
# File 'lib/ecu/labels/label_list.rb', line 84 def reject!(&blk) @labels.reject!(&blk) end |
#select(&blk) ⇒ Object
80 81 82 |
# File 'lib/ecu/labels/label_list.rb', line 80 def select(&blk) self.class.new(@labels.select(&blk), @headers, @subheaders) end |
#size ⇒ Object
133 |
# File 'lib/ecu/labels/label_list.rb', line 133 def size; count end |
#sort_by(&blk) ⇒ Object
100 101 102 |
# File 'lib/ecu/labels/label_list.rb', line 100 def sort_by(&blk) self.class.new(@labels.sort_by(&blk), @headers, @subheaders) end |
#to_a ⇒ Object
117 118 119 |
# File 'lib/ecu/labels/label_list.rb', line 117 def to_a @labels end |
#to_dcm(indented = false) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/ecu/interfaces/dcm/label_list.rb', line 76 def to_dcm(indented=false) out = [] unless headers.empty? out += headers.map { "* " + _1 }.push("") end out << DCM_HEADER << "" unless subheaders.empty? out += subheaders.map { "* " + _1 }.push("") end out += map { _1.to_dcm(indented) } out.join("\n") end |
#to_lab ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/ecu/interfaces/lab/label_list.rb', line 11 def to_lab out = [] out.append(*headers.map { |l| "; #{l}" }, "") unless headers.empty? out.append("[SETTINGS]") out.append("Version;V1.1") out.append("MultiRasterSeparator;&") out.append("") out.append(*subheaders.map { |l| "; #{l}" }, "") unless subheaders.empty? out.append("[Label]") out.append(*map(&:to_lab)) out.join("\n") end |
#to_mfile ⇒ Object
9 10 11 |
# File 'lib/ecu/interfaces/mfile/label_list.rb', line 9 def to_mfile self.map(&:to_mfile).join("\n") end |
#to_s ⇒ Object
129 130 131 |
# File 'lib/ecu/labels/label_list.rb', line 129 def to_s "[#{@labels.map(&:to_s).join(", ")}]" end |