forked from zdavatz/oddb2xml
/
cli.rb
374 lines (371 loc) · 12.4 KB
/
cli.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# encoding: utf-8
require 'thread'
require 'oddb2xml/builder'
require 'oddb2xml/downloader'
require 'oddb2xml/extractor'
require 'oddb2xml/compressor'
require 'oddb2xml/options'
require 'oddb2xml/util'
require 'rubyXL'
require 'date' # for today
module Oddb2xml
class Cli
SUBJECTS = %w[product article]
ADDITIONS = %w[substance limitation interaction code]
OPTIONALS = %w[fi fi_product]
LANGUAGES = %w[DE FR] # EN does not exist
def initialize(args)
@options = args
Oddb2xml.save_options(@options)
@mutex = Mutex.new
# product
@items = {} # Items from Preparations.xml in BAG
@index = {} # Base index from swissINDEX
@flags = {} # narcotics flag files repo
@lppvs = {} # lppv.txt from files repo
@infos = {} # [option] FI from SwissmedicInfo
@packs = {} # [option] Packungen from Swissmedic for dat
@infos_zur_rose = {} # [addition] infos_zur_rose and other infos from zurrose transfer.txt
@migel = {} # [addition] additional Non Pharma products from files repo
@actions = [] # [addition] interactions from epha
@orphans = [] # [addition] Orphaned drugs from Swissmedic xls
@fridges = [] # [addition] ReFridge drugs from Swissmedic xls
# addres
@companies = [] # betrieb
@people = [] # medizinalperson
LANGUAGES.each do |lang|
@index[lang] = {}
end
@_message = false
end
def run
threads = []
if @options[:calc] and not @options[:extended]
threads << download(:package) # swissmedic
elsif @options[:address]
[:company, :person].each do |type|
threads << download(type) # medregbm.admin
end
else
if @options[:format] != :dat
if @options[:fi]
threads << download(:fachinfo) # swissmedic-info
end
[:orphan, :fridge].each do |type|
threads << download(type) # swissmedic
end
threads << download(:interaction) # epha
end
if @options[:nonpharma]
threads << download(:migel) # oddb2xml_files
end
threads << download(:zurrose)
threads << download(:package) # swissmedic
threads << download(:bm_update) # oddb2xml_files
threads << download(:lppv) # oddb2xml_files
threads << download(:bag) # bag.e-mediat
LANGUAGES.each do |lang|
types.each do |type|
threads << download(:index, type, lang) # swissindex
end
end
end
begin
threads.map(&:join)
rescue SystemExit
@mutex.synchronize do
if @_message
puts "(Aborted)"
puts "Please install SSLv3 CA certificates on your machine."
puts "You can check with `ruby -ropenssl -e 'p OpenSSL::X509::DEFAULT_CERT_FILE'`."
puts "See README."
end
end
exit
end
build
compress if @options[:compress_ext]
report
end
private
def build
Oddb2xml.log("Start build")
begin
# require 'pry'; binding.pry
@_files = {"calc"=>"oddb_calc.xml"} if @options[:calc] and not @options[:extended]
files.each_pair do |sbj, file|
builder = Builder.new(@options) do |builder|
if @options[:calc] and not @options[:extended]
builder.packs = @packs
builder.subject = sbj
elsif @options[:address]
builder.subject = sbj
builder.companies = @companies
builder.people = @people
else # product
if @options[:format] != :dat
index = {}
LANGUAGES.each do |lang|
index[lang] = {} unless index[lang]
types.each do |type|
index[lang].merge!(@index[lang][type]) if @index[lang][type]
end
end
builder.index = index
builder.subject = sbj
end
# common sources
builder.items = @items
builder.flags = @flags
builder.lppvs = @lppvs
# optional sources
builder.infos = @infos
builder.packs = @packs
# additional sources
%w[actions orphans fridges migel infos_zur_rose].each do |addition|
builder.send("#{addition}=".intern, self.instance_variable_get("@#{addition}"))
end
end
builder.tag_suffix = @options[:tag_suffix]
end
output = ''
if !@options[:address] and (@options[:format] == :dat)
types.each do |type|
index = {}
LANGUAGES.each do |lang|
index[lang] = @index[lang][type]
end
_sbj = (type == :pharma ? :dat : :with_migel_dat)
builder.index = index
builder.subject = _sbj
builder.ean14 = @options[:ean14]
if type == :nonpharma
output << "\n"
end
output << builder.to_dat
end
else
output = builder.to_xml
end
File.open(File.join(WorkDir, file), 'w:utf-8'){ |fh| fh << output }
if @options[:calc]
FileUtils.cp(File.join(WorkDir, file), File.join(WorkDir, file.sub('.xml', '_'+Time.now.strftime("%d.%m.%Y_%H.%M")+'.xml')), :verbose => true)
end
end
rescue Interrupt
files.values.each do |file|
if File.exist? file
File.unlink(file) # we don't save it as it might be only partly downloaded
end
end
raise Interrupt
end
end
def download(what, type=nil, lang=nil)
case what
when :company, :person
var = (what == :company ? 'companies' : 'people')
Thread.new do
downloader = MedregbmDownloader.new(what)
str = downloader.download
self.instance_variable_set(
"@#{var}".intern,
MedregbmExtractor.new(str, what).to_arry
)
end
when :fachinfo
Thread.new do
downloader = SwissmedicInfoDownloader.new
xml = downloader.download
@mutex.synchronize do
hsh = SwissmedicInfoExtractor.new(xml).to_hash
@infos = hsh
Oddb2xml.log("SwissmedicInfoExtractor added #{@infos.size} fachinfo")
end
end
when :orphan, :fridge
var = what.to_s + 's'
Thread.new do
downloader = SwissmedicDownloader.new(what)
bin = downloader.download
self.instance_variable_set(
"@#{var}".intern,
SwissmedicExtractor.new(bin, what).to_arry
)
# Oddb2xml.log("SwissmedicExtractor added #{self.instance_variable_get("@#{var}".intern).size} #{var}. File #{bin} was #{File.size(bin)} bytes")
end
when :interaction
Thread.new do
downloader = EphaDownloader.new
str = downloader.download
@mutex.synchronize do
@actions = EphaExtractor.new(str).to_arry
Oddb2xml.log("EphaExtractor added #{@actions.size} interactions")
end
end
when :migel
Thread.new do
downloader = MigelDownloader.new
bin = downloader.download
@mutex.synchronize do
@migel = MigelExtractor.new(bin).to_hash
Oddb2xml.log("MigelExtractor added #{@migel.size} migel items")
end
end
when :package
Thread.new do
downloader = SwissmedicDownloader.new(:package, @options)
bin = downloader.download
@mutex.synchronize do
@packs = SwissmedicExtractor.new(bin, :package).to_hash
Oddb2xml.log("SwissmedicExtractor added #{@packs.size} packs from #{bin}")
@packs
end
end
when :bm_update
Thread.new do
downloader = BMUpdateDownloader.new
str = downloader.download
@mutex.synchronize do
@flags = BMUpdateExtractor.new(str).to_hash
Oddb2xml.log("BMUpdateExtractor added #{@flags.size} flags")
end
end
when :lppv
Thread.new do
downloader = LppvDownloader.new
str = downloader.download
@mutex.synchronize do
@lppvs = LppvExtractor.new(str).to_hash
Oddb2xml.log("LppvExtractor added #{@lppvs.size} lppvs")
end
end
when :bag
Thread.new do
downloader = BagXmlDownloader.new(@options)
xml = downloader.download
@mutex.synchronize do
hsh = BagXmlExtractor.new(xml).to_hash
@items = hsh
Oddb2xml.log("BagXmlDownloader added #{@items.size} items")
end
end
when :zurrose
Thread.new do
downloader = ZurroseDownloader.new(@options, @options[:transfer_dat])
xml = downloader.download
Oddb2xml.log("zurrose xml #{xml.size} bytes")
@mutex.synchronize do
hsh = ZurroseExtractor.new(xml, @options[:extended]).to_hash
@infos_zur_rose = hsh
Oddb2xml.log("zurrose added #{@infos_zur_rose.size} items from xml with #{xml.size} bytes")
end
end
when :index
Thread.new do
downloader = SwissIndexDownloader.new(@options, type, lang)
begin
xml = downloader.download
rescue SystemExit
@mutex.synchronize do
unless @_message # hook only one exit
@_message = true
exit
end
end
end
@mutex.synchronize do
hsh = SwissIndexExtractor.new(xml, type).to_hash
@index[lang][type] = hsh
end
end
end
end
def compress
compressor = Compressor.new(prefix, @options)
files.values.each do |file|
work_file = File.join(WorkDir, file)
if File.exists?(work_file)
compressor.contents << work_file
end
end
compressor.finalize!
end
def files
unless @_files
@_files = {}
@_files[:calc] = "oddb_calc.xml" if @options[:calc]
if @options[:address]
@_files[:company] = "#{prefix}_betrieb.xml"
@_files[:person] = "#{prefix}_medizinalperson.xml"
elsif @options[:format] == :dat
@_files[:dat] = "#{prefix}.dat"
if @options[:nonpharma] # into one file
@_files[:dat] = "#{prefix}_with_migel.dat"
end
else # xml
##
# building order
# 1. additions
# 2. subjects
# 3. optional SUBJECTS
_files = (ADDITIONS + SUBJECTS)
_files += OPTIONALS if @options[:fi]
_files.each do|sbj|
@_files[sbj] = "#{prefix}_#{sbj.to_s}.xml"
end
end
end
@_files
end
def prefix
@_prefix ||= (@options[:tag_suffix] || 'oddb').gsub(/^_|_$/, '').downcase
end
def report
lines = []
if @options[:calc]
lines << Calc.dump_new_galenic_forms
lines << Calc.dump_names_without_galenic_forms
lines << Calc.report_conversion
end
unless @options[:address]
LANGUAGES.each do |lang|
lines << lang
types.each do |type|
if @index[lang][type]
indices = @index[lang][type].values.flatten.length
if type == :nonpharma
migel_xls = @migel.values.compact.select{|m| !m[:pharmacode].empty? }.map{|m| m[:pharmacode] }
nonpharmas = @index[lang][type].keys
indices += (migel_xls - nonpharmas).length # ignore duplicates, null
lines << sprintf("\tNonPharma products: %i", indices)
else
lines << sprintf("\tPharma products: %i", indices)
end
end
end
end
if @options[:extended]
lines << sprintf("\tInformation items zur Rose: %i", @infos_zur_rose.length)
end
else
{
'Betrieb' => :@companies,
'Person' => :@people
}.each do |type, var|
lines << sprintf(
"#{type} addresses: %i", self.instance_variable_get(var).length)
end
end
puts lines.join("\n")
end
def types # swissindex
@_types ||=
if @options[:nonpharma]
[:pharma, :nonpharma]
else
[:pharma]
end
end
end
end