|
| 1 | +module Sierra |
| 2 | + module Argot |
| 3 | + module TrajectLoader |
| 4 | + # This module taken almost exactly from spec files of: |
| 5 | + # https://github.com/trln/marc-to-argot |
| 6 | + # under the following license: |
| 7 | + # |
| 8 | + # The MIT License (MIT) |
| 9 | + # |
| 10 | + # Copyright (c) 2017 Luke Aeschleman |
| 11 | + # |
| 12 | + # Permission is hereby granted, free of charge, to any person obtaining a copy |
| 13 | + # of this software and associated documentation files (the "Software"), to deal |
| 14 | + # in the Software without restriction, including without limitation the rights |
| 15 | + # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 16 | + # copies of the Software, and to permit persons to whom the Software is |
| 17 | + # furnished to do so, subject to the following conditions: |
| 18 | + # |
| 19 | + # The above copyright notice and this permission notice shall be included in |
| 20 | + # all copies or substantial portions of the Software. |
| 21 | + # |
| 22 | + # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 23 | + # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 24 | + # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 25 | + # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 26 | + # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 27 | + # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 28 | + # THE SOFTWARE. |
| 29 | + |
| 30 | + def create_settings(collection, data_dir, extension) |
| 31 | + spec = MarcToArgot::SpecGenerator.new(collection) |
| 32 | + marc_source_type = extension == 'mrc' ? 'binary' : 'xml' |
| 33 | + flatten_attributes = YAML.load_file("#{data_dir}/flatten_attributes.yml") |
| 34 | + override = File.exist?("#{data_dir}/#{collection}/overrides.yml") ? YAML.load_file("#{data_dir}/#{collection}/overrides.yml") : [] |
| 35 | + |
| 36 | + { |
| 37 | + 'argot_writer.flatten_attributes' => flatten_attributes, |
| 38 | + 'argot_writer.pretty_print' => false, |
| 39 | + 'writer_class_name' => 'Traject::ArgotWriter', |
| 40 | + 'specs' => spec.generate_spec, |
| 41 | + 'processing_thread_pool' => 1, |
| 42 | + 'marc_source.type' => marc_source_type, |
| 43 | + 'marc_source.encoding' => 'utf-8', |
| 44 | + 'override' => override, |
| 45 | + 'log_level' => :error |
| 46 | + } |
| 47 | + end |
| 48 | + |
| 49 | + def load_indexer(collection = 'argot', extension = 'xml') |
| 50 | + data_dir = MTA_DATA_DIR |
| 51 | + conf_files = ["#{data_dir}/extensions.rb", "#{data_dir}/argot/traject_config.rb", "#{data_dir}/#{collection}/traject_config.rb"] |
| 52 | + indexer_class = MarcToArgot::Indexers.find(collection.to_sym) |
| 53 | + traject_indexer = indexer_class.new create_settings(collection, data_dir, extension) |
| 54 | + conf_files.each do |conf_path| |
| 55 | + begin |
| 56 | + traject_indexer.load_config_file(conf_path) |
| 57 | + rescue Errno::ENOENT, Errno::EACCES => e |
| 58 | + raise "Could not read configuration file '#{conf_path}', exiting..." |
| 59 | + rescue Traject::Indexer::ConfigLoadError => e |
| 60 | + raise e |
| 61 | + rescue StandardError => e |
| 62 | + raise e |
| 63 | + end |
| 64 | + end |
| 65 | + traject_indexer |
| 66 | + end |
| 67 | + end |
| 68 | + |
| 69 | + # A Traject Indexer to transform MARC into Argot |
| 70 | + class Indexer |
| 71 | + include TrajectLoader |
| 72 | + |
| 73 | + def indexer |
| 74 | + @indexer ||= load_indexer(COLLECTION, 'mrc') |
| 75 | + end |
| 76 | + end |
| 77 | + end |
| 78 | +end |
0 commit comments