lib/oddb2xml/cli.rb in oddb2xml-1.0.10 vs lib/oddb2xml/cli.rb in oddb2xml-1.1.0

- old
+ new

@@ -7,10 +7,11 @@ require 'oddb2xml/compressor' module Oddb2xml class Cli SUBJECTS = %w[product article] + ADDITIONS = %w[substance limitation] LANGUAGES = %w[DE FR] # EN does not exist def initialize(args) @options = args @mutex = Mutex.new @items = {} # Items from Preparations.xml in BAG @@ -33,11 +34,14 @@ end # bag threads << Thread.new do downloader = BagXmlDownloader.new xml = downloader.download - @items = BagXmlExtractor.new(xml).to_hash + @mutex.synchronize do + hsh = BagXmlExtractor.new(xml).to_hash + @items = hsh + end end LANGUAGES.each do |lang| # swissindex types.each do |type| threads << Thread.new do @@ -50,17 +54,15 @@ end end end threads.map(&:join) build + compress if @options[:compress_ext] report end private def build - files = {} - prefix = (@options[:tag_suffix] || 'oddb').gsub(/^_|_$/, '').downcase - SUBJECTS.each{ |sbj| files[sbj] = "#{prefix}_#{sbj}.xml" } begin files.each_pair do |sbj, file| builder = Builder.new do |builder| index = {} LANGUAGES.each do |lang| @@ -74,36 +76,43 @@ builder.items = @items builder.orphans = @orphans builder.fridges = @fridges builder.tag_suffix = @options[:tag_suffix] end - if file =~ /(product)/ - xml = builder.to_xml('substance') - File.open(file.gsub($1, 'substance'), 'w:utf-8'){ |fh| fh << xml } - end xml = builder.to_xml File.open(file, 'w:utf-8'){ |fh| fh << xml } end - if @options[:compress_ext] - compressor = Compressor.new(prefix, @options[:compress_ext]) - files.values.each do |file| - if file =~ /(product)/ - compressor.contents << file.gsub($1, 'substance') - end - compressor.contents << file - end - compressor.finalize! - end rescue Interrupt files.values.each do |file| if File.exist? file File.unlink file end end raise Interrupt end end + def compress + compressor = Compressor.new(prefix, @options[:compress_ext]) + files.values.each do |file| + if File.exists?(file) + compressor.contents << file + end + end + compressor.finalize! + end + def files + unless @_files + @_files = {} + (ADDITIONS + SUBJECTS).each do|sbj| + @_files[sbj] = "#{prefix}_#{sbj.to_s}.xml" + end + end + @_files + end + def prefix + @_prefix ||= (@options[:tag_suffix] || 'oddb').gsub(/^_|_$/, '').downcase + end def report lines = [] LANGUAGES.each do |lang| lines << lang types.each do |type| @@ -112,13 +121,14 @@ end end puts lines.join("\n") end def types # swissindex - if @options[:nonpharma] - [:pharma, :nonpharma] - else - [:pharma] - end + @_types ||= + if @options[:nonpharma] + [:pharma, :nonpharma] + else + [:pharma] + end end end end