lib/oddb2xml/cli.rb in oddb2xml-2.1.7 vs lib/oddb2xml/cli.rb in oddb2xml-2.1.8
- old
+ new
@@ -11,21 +11,21 @@
require 'date' # for today
module Oddb2xml
class Cli
+ attr_reader :options
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
+ @items = {} # Items from Preparations.xml in BAG, using GTINs as key
+ @refdata_types = {} # Base index from refdata
@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
@@ -34,13 +34,10 @@
@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 = []
files2rm = Dir.glob(File.join(Downloads, '*'))
@@ -67,18 +64,16 @@
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
+ types.each do |type|
+ threads << download(:refdata, type) # refdata
end
end
begin
- threads.map(&:join)
+ # threads.map(&:join) # TODO
rescue SystemExit
@mutex.synchronize do
if @_message
puts "(Aborted)"
puts "Please install SSLv3 CA certificates on your machine."
@@ -93,10 +88,11 @@
report
end
private
def build
Oddb2xml.log("Start build")
+ puts "#{File.basename(__FILE__)}:#{__LINE__}: @refdata_types.keys #{@refdata_types.keys}"
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|
@@ -107,18 +103,15 @@
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
+ refdata = {}
+ types.each do |type|
+ refdata.merge!(@refdata_types[type]) if @refdata_types[type]
end
- builder.index = index
+ builder.refdata = refdata
builder.subject = sbj
end
# common sources
builder.items = @items
builder.flags = @flags
@@ -134,16 +127,13 @@
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
+ refdata1 = {}
_sbj = (type == :pharma ? :dat : :with_migel_dat)
- builder.index = index
+ builder.refdata = @refdata_types[type]
builder.subject = _sbj
builder.ean14 = @options[:ean14]
if type == :nonpharma
output << "\n"
end
@@ -164,126 +154,129 @@
end
end
raise Interrupt
end
end
- def download(what, type=nil, lang=nil)
+ def download(what, type=nil)
case what
when :company, :person
var = (what == :company ? 'companies' : 'people')
- Thread.new do
+ begin # instead of 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
+ begin # instead of 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
+ begin # instead of 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
+ begin # instead of 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
+ begin # instead of 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
+ begin # instead of 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
+ begin # instead of 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
+ begin # instead of 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
+ begin # instead of 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")
+ Oddb2xml.log("BagXmlDownloader added #{@items.size} items. #{@items.keys}")
end
end
when :zurrose
- Thread.new do
+ begin # instead of 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
+ Oddb2xml.log("zurrose added #{hsh.size} items from xml with #{xml.size} bytes")
@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)
+ when :refdata
+ begin # instead of Thread.new do
+ downloader = RefdataDownloader.new(@options, type)
begin
xml = downloader.download
+ Oddb2xml.log("refdata #{type} xml #{xml.size} bytes")
+ xml
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
+ hsh = RefdataExtractor.new(xml, type).to_hash
+ @refdata_types[type] = hsh
+ Oddb2xml.log("refdata #{type} added #{hsh.size} keys now #{@refdata_types.keys} items from xml with #{xml.size} bytes")
end
end
end
end
def compress
@@ -333,23 +326,20 @@
lines << Calc.dump_names_without_galenic_forms
lines << Calc.report_conversion
lines << ParseComposition.report
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
+ types.each do |type|
+ if @refdata_types[type]
+ indices = @refdata_types[type].values.flatten.length
+ if type == :nonpharma
+ migel_xls = @migel.values.compact.select{|m| !m[:pharmacode].empty? }.map{|m| m[:pharmacode] }
+ nonpharmas = @refdata_types[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
if @options[:extended]
lines << sprintf("\tInformation items zur Rose: %i", @infos_zur_rose.length)
@@ -363,10 +353,10 @@
"#{type} addresses: %i", self.instance_variable_get(var).length)
end
end
puts lines.join("\n")
end
- def types # swissindex
+ def types # RefData
@_types ||=
if @options[:nonpharma]
[:pharma, :nonpharma]
else
[:pharma]