dcm 0.0.10 → 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/lib/cli.rb +20 -6
  3. data/lib/codinginfo.rb +29 -19
  4. data/lib/version.rb +1 -1
  5. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e5f3bbfdbb0284f5b1e4dea4e3428e49049724ee9fc5e059337fccb9f953ec37
4
- data.tar.gz: 52215d5d5ad42b0157d6b6b61110a57b1a2c97a64a3192e59e0ace8ab486868b
3
+ metadata.gz: 24fe5532820ef2590dd51fcb42433a4a3972b716ce5f77269653a58d33f93435
4
+ data.tar.gz: 96b84123a8fe6b36916aa1648729373f896712a0a2e6a8b5301854896091c409
5
5
  SHA512:
6
- metadata.gz: f157553d0a3d945aa43c90359e540310ecdd38efda40a1644799e28a30eb9a43a31447e5369ac7ce3dd5b9f56b21349ad5c286083cd4ca2ce3364875ae198caf
7
- data.tar.gz: 4f49b2a5aa5280dfef2f4933dc6fcb308e537097a6d28730faf31084a1f8fe78e3bbde5f615f76bc38654f31783c8c0819fbb5e166d8f8c8fea34259b89dba79
6
+ metadata.gz: 6725d774fdaa8b235463a141a3b3d504d4fbc5b52e77f8dc1f0fd0b5d56388b4381f4dd718f43b7a0c588ae4aeb1d64e816bb4f1c849709274facff262872657
7
+ data.tar.gz: ca12146d598edd1b8264eb19c766416c945e99df79d0ed1c020b68ef64fba92ce738416e9f4e0e05b1ceb6471b27e6e43a1a7244b394b7af6192358a0cbb9e7f
data/lib/cli.rb CHANGED
@@ -18,6 +18,8 @@ module Dcm
18
18
 
19
19
  class CLI < Thor
20
20
 
21
+ ALLOWED_FILTERKEYS = [:swfk_id, :typekey, :devkey]
22
+
21
23
  desc "show FILE", "Show all labels in FILE"
22
24
  option :oneline, type: :boolean, aliases: ["-l"]
23
25
  def show(file=nil)
@@ -40,20 +42,20 @@ module Dcm
40
42
  loop { LabelSelector.choose_from(list) }
41
43
  end
42
44
 
43
- desc "creta2begu ID CODING FILE", "Convert a hierarchical DCM to a flat DCM"
44
- def creta2begu(id, codingpath, file)
45
+ desc "creta2begu FILTER CODING FILE", "Convert a hierarchical DCM to a flat DCM"
46
+ def creta2begu(expr, codingpath, file)
45
47
  list = parse_file(file)
46
48
 
47
- puts Codinginfo.new([id], codingpath)
49
+ puts Codinginfo.new(codingpath, parse_filter(expr))
48
50
  .flatten_list(list)
49
51
  .to_dcm
50
52
  end
51
53
 
52
- desc "begu2creta IDS CODING FILE", "Convert a flat DCM to a hierarchical DCM"
53
- def begu2creta(ids, codingpath, file)
54
+ desc "begu2creta FILTER CODING FILE", "Convert a flat DCM to a hierarchical DCM"
55
+ def begu2creta(expr, codingpath, file)
54
56
  list = parse_file(file)
55
57
 
56
- puts Codinginfo.new(ids.split(","), codingpath)
58
+ puts Codinginfo.new(codingpath, parse_filter(expr))
57
59
  .unflatten_list(list)
58
60
  .to_dcm
59
61
  end
@@ -254,6 +256,18 @@ module Dcm
254
256
  Ecu::LabelList.new([label])
255
257
  end
256
258
 
259
+ def check_filter(expr)
260
+ return if ALLOWED_FILTERKEYS.include?(expr.split(":").first)
261
+
262
+ fail "Unknown filter expression #{expr}. Possible keys are #{ALLOWED_FILTERKEYS.join(", ")}. Use key:value1,value2 syntax."
263
+ end
264
+
265
+ def parse_filter(expr)
266
+ expr
267
+ .split(":")
268
+ .then { { _1.to_sym => _2.split(",") } }
269
+ end
270
+
257
271
  def display_list(list:, detail:, format: :tty)
258
272
  case format
259
273
  when :tty
data/lib/codinginfo.rb CHANGED
@@ -25,8 +25,8 @@ class Codinginfo
25
25
  def to_s = "#{name}=#{value}"
26
26
  def ==(other) = name == other.name
27
27
 
28
- def merge(other)
29
- fail "Cannot merge #{name} with #{other.name}" unless self == other
28
+ def combine(other)
29
+ fail "Cannot combine #{name} with #{other.name}" unless self == other
30
30
 
31
31
  self.class.new \
32
32
  name: name,
@@ -47,7 +47,7 @@ class Codinginfo
47
47
  end
48
48
  end
49
49
 
50
- def initialize(ids, filepath)
50
+ def initialize(filepath, filter)
51
51
  @doc = SimpleXlsxReader.open(filepath)
52
52
  @headers = []
53
53
  @variants = []
@@ -56,11 +56,8 @@ class Codinginfo
56
56
  overview_sheet
57
57
  .rows
58
58
  .each do |row|
59
- parse_headers(row) if @headers.empty?
60
- next if @headers.empty?
61
- next if row[1].nil? || row[3].nil?
62
- next unless ids.any? { row[1].match?(/#{_1}/i) }
63
- next unless row[3].match?(/CVAR/)
59
+ next unless headers_parsed?(row)
60
+ next unless matches_filter?(row, *filter.to_a.first)
64
61
 
65
62
  @variants << Variant.new(@headers, row)
66
63
  end
@@ -88,6 +85,14 @@ class Codinginfo
88
85
  .tap { fail "Cannot find sheet Parameterdefinition" if _1.nil? }
89
86
  end
90
87
 
88
+ def matches_filter?(row, key, values)
89
+ return false unless row[3]
90
+ return false unless row[3].match?(/CVAR/)
91
+ return false unless @headers.include?(key)
92
+
93
+ values.any? { row[@headers.index(key)].match?(/#{_1}/i) }
94
+ end
95
+
91
96
  def variant
92
97
  fail "No variant matching #{id} found!" if @variants.empty?
93
98
  fail "More than one variant matching #{id} found!" if @variants.size > 1
@@ -99,11 +104,11 @@ class Codinginfo
99
104
  Ecu::LabelList.new \
100
105
  list.flat_map { |label|
101
106
  if has_cvar_assignment?(label)
102
- @variants.map { label.with(name: add_cvar_prefix(label, _1)) }
107
+ @variants.map { label.with(name: add_cvar_prefix(label.name, _1)) }
103
108
  else
104
109
  label
105
110
  end
106
- }
111
+ }.uniq
107
112
  end
108
113
 
109
114
  def flatten_list(list)
@@ -124,28 +129,33 @@ class Codinginfo
124
129
  variant.cvars.any? { label.name.match?(_1.label_prefix) }
125
130
  end
126
131
 
127
- def add_cvar_prefix(label, variant)
132
+ def add_cvar_prefix(name, variant)
128
133
  variant
129
134
  .cvars
130
- .find { _1 == @assignments[label.name] }
131
- .then { _1.merge(@assignments[label.name]) }
132
- .then { _1.label_prefix + label.name }
135
+ .find { _1 == @assignments[name] }
136
+ .then { _1.combine(@assignments[name]) }
137
+ .then { _1.label_prefix + name }
133
138
  end
134
139
 
135
140
  def remove_cvar_prefix(name)
136
141
  name.sub(CVAR_PREFIX_REGEXP, "")
137
142
  end
138
143
 
139
- def parse_headers(row)
140
- return unless row[1]&.match?(/SWFK-ID/)
141
- return unless row[1]&.match?(/CVAR_BeguData/)
142
- return unless row[2]&.match?(/Kommentar/)
144
+ def headers_parsed?(row)
145
+ return true if @headers.any?
146
+
147
+ return false unless row[1]&.match?(/SWFK-ID/)
148
+ return false unless row[1]&.match?(/CVAR_BeguData/)
149
+ return false unless row[2]&.match?(/Kommentar/)
143
150
 
144
151
  @headers = row
145
152
  .map(&:chomp)
146
153
  .map(&:lstrip)
147
154
  .map(&:strip)
148
155
  .map { _1.sub(/\n.*/, "") }
149
- .tap { _1[0] = "typestr" }
156
+ .tap { _1[0] = :typestr }
157
+ .tap { _1[1] = :swfk_id }
158
+ .tap { _1[4] = :typekey }
159
+ .tap { _1[6] = :devkey }
150
160
  end
151
161
  end
data/lib/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Dcm
2
- VERSION = "0.0.10"
2
+ VERSION = "0.0.11"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dcm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonas Mueller