Module: ROF::CLI

Defined in:
lib/rof/cli.rb

Class Method Summary collapse

Class Method Details

.compare_files(file1, file2, outfile = STDOUT) ⇒ Integer

TODO:

Should the outfile default to STDERR

compare two rofs

Parameters:

  • file1 (String)
    • path to “left” file in the comparison

  • file2 (String)
    • path to “right” file in the comparison

  • outfile (#write) (defaults to: STDOUT)
    • where to write any errors (default STDOUT)

Returns:

  • (Integer)

    number of comparisons that failed. If 0, then the two given files are logically equal



133
134
135
136
137
138
# File 'lib/rof/cli.rb', line 133

def self.compare_files(file1, file2, outfile = STDOUT)
  fedora_rof = ROF::Utility.load_items_from_json_file(file1, outfile)
  bendo_rof =  ROF::Utility.load_items_from_json_file(file2, outfile)

  ROF::CompareRof.fedora_vs_bendo(fedora_rof, bendo_rof, outfile)
end

.csv_to_rof(csv, config = {}, outfile = STDOUT) ⇒ Object

Convert the given CSV to ROF JSON document

Parameters:

  • csv (String)
    • The contents of a CSV file

  • config (Hash) (defaults to: {})
  • outfile (NilClass, String, #write) (defaults to: STDOUT)
    • where should we write things

See Also:



107
108
109
110
111
112
# File 'lib/rof/cli.rb', line 107

def self.csv_to_rof(csv, config = {}, outfile = STDOUT)
  result = ROF::Translators::CsvToRof.call(csv, config)
  with_outfile_handling(outfile) do |writer|
    writer.write(JSON.pretty_generate(result))
  end
end

.fedora_to_rof(pids, config = {}, outfile = STDOUT) ⇒ Object

Convert the given fedora PIDs to ROF JSON document

Parameters:

  • pids (Array)
    • The path to the OSF Project file

  • config (Hash) (defaults to: {})
  • outfile (NilClass, String, #write) (defaults to: STDOUT)
    • where should we write things

See Also:



94
95
96
97
98
99
# File 'lib/rof/cli.rb', line 94

def self.fedora_to_rof(pids, config = {}, outfile = STDOUT)
  result = ROF::Translators::FedoraToRof.call(pids, config)
  with_outfile_handling(outfile) do |writer|
    writer.write(JSON.pretty_generate(result))
  end
end

.filter_file(filter, fname, outfile = STDOUT) ⇒ Object

Responsible for loading the given :fname into an array of items, the processing those items via the :filter, and finally writing results to the :output

Parameters:

  • filter (#process)
    • the object that processes the items loaded from the fname

  • fname (String)
    • the filename from which to load items

  • outfile (NilClass, String, #write) (defaults to: STDOUT)
    • where should we write things

Returns:

  • void

See Also:



68
69
70
71
72
73
74
# File 'lib/rof/cli.rb', line 68

def self.filter_file(filter, fname, outfile = STDOUT)
  items = ROF::Utility.load_items_from_json_file(fname, STDERR)
  result = filter.process(items)
  with_outfile_handling(outfile) do |writer|
    writer.write(JSON.pretty_generate(result))
  end
end

.ingest_array(items, search_paths = [], outfile = STDOUT, fedora = nil, bendo = nil) ⇒ Object

Parameters:

  • outfile (NilClass, String, #write) (defaults to: STDOUT)
    • where should we write things

See Also:



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
# File 'lib/rof/cli.rb', line 28

def self.ingest_array(items, search_paths = [], outfile = STDOUT, fedora = nil, bendo = nil)
  fedora = Rubydora.connect(fedora) if fedora
  item_count = 1
  error_count = 0
  verb = fedora.nil? ? 'Verifying' : 'Ingesting'
  with_outfile_handling(outfile) do |writer|
    overall_benchmark = Benchmark.measure do
      items.each do |item|
        begin
          writer.write("#{item_count}. #{verb} #{item['pid']} ...")
          item_count += 1
          individual_benchmark = Benchmark.measure do
            ROF.Ingest(item, fedora, search_paths, bendo)
          end
          writer.write("ok. %0.3fs\n" % individual_benchmark.real)
        rescue Exception => e
          error_count += 1
          writer.write("error. #{e}\n")
          # TODO(dbrower): add option to toggle displaying backtraces
          if e.backtrace
            writer.write(e.backtrace.join("\n\t"))
            writer.write("\n")
          end
        end
      end
    end
    writer.write("Total time %0.3fs\n" % overall_benchmark.real)
    writer.write("#{error_count} errors\n")
  end
  error_count
end

.ingest_file(fname, search_paths = [], outfile = STDOUT, fedora = nil, bendo = nil) ⇒ Object

Ingest the file fname that is a level 0 rof file. It may contain any number of fedora objects; they will be delt with in the order they appear in the file. Any external files (except fname) are searched for using the search_path array of directories. If fedora is present, it is a hash having the keys url, user, and password. Omitting fedora has the effect of verifying the format of fname.

All output is sent to outfile.

Returns the number of errors.



21
22
23
24
# File 'lib/rof/cli.rb', line 21

def self.ingest_file(fname, search_paths = [], outfile = STDOUT, fedora = nil, bendo = nil)
  items = ROF::Utility.load_items_from_json_file(fname, outfile)
  ingest_array(items, search_paths, outfile, fedora, bendo)
end

.jsonld_to_rof(jsonld, config = {}, outfile = STDOUT) ⇒ Object

Convert the given JSON-LD to ROF JSON document

Parameters:

  • jsonld (Hash)
    • contents of a CSV file

  • config (Hash) (defaults to: {})
  • outfile (NilClass, String, #write) (defaults to: STDOUT)
    • where should we write things

See Also:



120
121
122
123
124
125
# File 'lib/rof/cli.rb', line 120

def self.jsonld_to_rof(jsonld, config = {}, outfile = STDOUT)
  result = ROF::Translators::JsonldToRof.call(jsonld, config)
  with_outfile_handling(outfile) do |writer|
    writer.write(JSON.pretty_generate(result))
  end
end

.osf_to_rof(project_file, config = {}, outfile = STDOUT) ⇒ Object

convert OSF archive tar.gz to rof file

Parameters:

  • project_file (String)
    • The path to the OSF Project file

  • config (Hash) (defaults to: {})
  • outfile (NilClass, String, #write) (defaults to: STDOUT)
    • where should we write things

See Also:



81
82
83
84
85
86
87
# File 'lib/rof/cli.rb', line 81

def self.osf_to_rof(project_file, config = {}, outfile = STDOUT)
  osf_projects = ROF::Utility.load_items_from_json_file(project_file, outfile)
  result = ROF::Translators::OsfToRof.call(osf_projects[0], config)
  with_outfile_handling(outfile) do |writer|
    writer.write(JSON.pretty_generate(result))
  end
end

.with_outfile_handling(outfile) {|writer| ... } ⇒ Object

Provides a normalized handling of outfiles:

Parameters:

  • outfile (NilClass, String, #write)
    • The IO point that we will write to

Yield Parameters:

  • writer (#write)
    • An object that can be written to



144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/rof/cli.rb', line 144

def self.with_outfile_handling(outfile)
  need_close = false
  # use outfile is_a String
  if outfile.is_a?(String)
    outfile = File.open(outfile, 'w')
    need_close = true
  elsif outfile.nil?
    outfile = File.open('/dev/null', 'w')
    need_close = true
  end
  yield(outfile)
ensure
  outfile.close if outfile && (need_close || outfile.respond_to?(:close))
end