diff options
| author | Zog | 2018-02-19 09:58:49 +0100 | 
|---|---|---|
| committer | Johan Van Ryseghem | 2018-02-20 09:50:28 +0100 | 
| commit | 5fe1e8762051ef567191210ab65e6916cf12b932 (patch) | |
| tree | dfa90a85935d7aea613bbd4ae14c40c90aba5615 /lib/tasks | |
| parent | 736bd34d8c496b81d456d4e4897197eac344247d (diff) | |
| download | chouette-core-5fe1e8762051ef567191210ab65e6916cf12b932.tar.bz2 | |
Refs #5924 @14h; Extend importers
Mostly add a way to override the default behaviour and process each row
its own way
Diffstat (limited to 'lib/tasks')
| -rw-r--r-- | lib/tasks/imports.rake | 38 | 
1 files changed, 33 insertions, 5 deletions
| diff --git a/lib/tasks/imports.rake b/lib/tasks/imports.rake index 9c6b1fbcd..eca7e6849 100644 --- a/lib/tasks/imports.rake +++ b/lib/tasks/imports.rake @@ -1,3 +1,5 @@ +require 'csv' +  namespace :import do    desc "Notify parent imports when children finish"    task notify_parent: :environment do @@ -9,12 +11,35 @@ namespace :import do      NetexImport.abort_old    end +  def importer_output_to_csv importer +    filepath = "./#{importer.configuration_name}_#{Time.now.strftime "%y%m%d%H%M"}_out.csv" +    cols = %w(line kind event message error) +    if importer.reload.journal.size > 0 +      keys = importer.journal.first["row"].map(&:first) +      CSV.open(filepath, "w") do |csv| +        csv << cols + keys +        importer.journal.each do |j| +          csv << cols.map{|c| j[c]} + j["row"].map(&:last) +        end +      end +      puts "Import Output written in #{filepath}" +    end +  end +    desc "import the given file with the corresponding importer" -  task :import, [:configuration_name, :filepath] => :environment do |t, args| +  task :import, [:configuration_name, :filepath, :referential_id] => :environment do |t, args|      importer = SimpleImporter.create configuration_name: args[:configuration_name], filepath: args[:filepath] +    if args[:referential_id].present? +      referential = Referential.find args[:referential_id] +      importer.configure do |config| +        config.add_value :referential, referential +        config.context = {referential: referential} +      end +    end      puts "\e[33m***\e[0m Start importing"      importer.import(verbose: true)      puts "\n\e[33m***\e[0m Import done, status: " + (importer.status == "success" ? "\e[32m" : "\e[31m" ) + importer.status + "\e[0m" +    importer_output_to_csv importer    end    desc "import the given file with the corresponding importer in the given StopAreaReferential" @@ -28,21 +53,23 @@ namespace :import do      puts "\e[33m***\e[0m Start importing"      importer.import(verbose: true)      puts "\n\e[33m***\e[0m Import done, status: " + (importer.status == "success" ? "\e[32m" : "\e[31m" ) + importer.status + "\e[0m" +    importer_output_to_csv importer    end -  desc "import the given file with the corresponding importer in the given Referential and StopAreaReferential" -  task :import_in_referential_and_stop_area_referential, [:referential_id, :stop_area_referential_id, :configuration_name, :filepath] => :environment do |t, args| +  desc "import the given routes files" +  task :import_routes, [:referential_id, :configuration_name, :mapping_filepath, :filepath] => :environment do |t, args|      referential = Referential.find args[:referential_id]      referential.switch -    stop_area_referential = StopAreaReferential.find args[:stop_area_referential_id] +    stop_area_referential = referential.stop_area_referential      importer = SimpleImporter.create configuration_name: args[:configuration_name], filepath: args[:filepath]      importer.configure do |config|        config.add_value :stop_area_referential, referential -      config.context = {stop_area_referential: stop_area_referential} +      config.context = {stop_area_referential: stop_area_referential, mapping_filepath: args[:mapping_filepath]}      end      puts "\e[33m***\e[0m Start importing"      importer.import(verbose: true)      puts "\n\e[33m***\e[0m Import done, status: " + (importer.status == "success" ? "\e[32m" : "\e[31m" ) + importer.status + "\e[0m" +    importer_output_to_csv importer    end    desc "import the given file with the corresponding importer in the given LineReferential" @@ -56,5 +83,6 @@ namespace :import do      puts "\e[33m***\e[0m Start importing"      importer.import(verbose: true)      puts "\n\e[33m***\e[0m Import done, status: " + (importer.status == "success" ? "\e[32m" : "\e[31m" ) + importer.status + "\e[0m" +    importer_output_to_csv importer    end  end | 
