Class: RDF::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/rdf/cli.rb

Constant Summary collapse

COMMANDS =
{
  "count"       => lambda do |argv, opts|
    start = Time.new
    count = 0
    self.parse(argv, opts) do |reader|
      reader.each_statement do |statement|
        count += 1
      end
    end
    secs = Time.new - start
    $stdout.puts "Parsed #{count} statements with #{@readers.join(', ')} in #{secs} seconds @ #{count/secs} statements/second."
  end,
  "lenghts"     => lambda do |argv, opts|
    self.parse(argv, opts) do |reader|
      reader.each_statement do |statement|
        $stdout.puts statement.to_s.size
      end
    end
  end,
  "objects"     => lambda do |argv, opts|
    self.parse(argv, opts) do |reader|
      reader.each_statement do |statement|
        $stdout.puts statement.object.to_ntriples
      end
    end
  end,
  "predicates"   => lambda do |argv, opts|
    self.parse(argv, opts) do |reader|
      reader.each_statement do |statement|
        $stdout.puts statement.predicate.to_ntriples
      end
    end
  end,
  "serialize" => lambda do |argv, opts|
    writer_class = RDF::Writer.for(opts[:output_format]) || RDF::NTriples::Writer
    out = opts[:output] || $stdout
    opts = opts.merge(:prefixes => {})
    writer_opts = opts.merge(:standard_prefixes => true)
    self.parse(argv, opts) do |reader|
      writer_class.new(out, writer_opts) do |writer|
        writer << reader
      end
    end
  end,
  "subjects"   => lambda do |argv, opts|
    self.parse(argv, opts) do |reader|
      reader.each_statement do |statement|
        $stdout.puts statement.subject.to_ntriples
      end
    end
  end
}

Class Method Summary collapse

Class Method Details

.abort(msg) ⇒ void

This method returns an undefined value.

Parameters:

  • msg (String)


196
197
198
# File 'lib/rdf/cli.rb', line 196

def self.abort(msg)
  Kernel.abort "#{basename}: #{msg}"
end

.basenameString

Returns:

  • (String)


75
# File 'lib/rdf/cli.rb', line 75

def self.basename() File.basename($0) end

.commandsArray<String>

Returns list of executable commands.

Returns:

  • (Array<String>)

    list of executable commands



164
165
166
# File 'lib/rdf/cli.rb', line 164

def self.commands
  COMMANDS.keys
end

.exec_command(command, args, options = {}) ⇒ Boolean

Parameters:

  • command (String)
  • args (Array<String>)

Returns:

  • (Boolean)


154
155
156
157
158
159
160
# File 'lib/rdf/cli.rb', line 154

def self.exec_command(command, args, options = {})
  unless COMMANDS.has_key?(command)
    abort "#{File.basename($0)}: unknown command `#{command}'"
  end
  
  COMMANDS[command].call(args, options)
end

.options {|options| ... } ⇒ OptionParser

Yields:

Yield Parameters:

Returns:



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/rdf/cli.rb', line 81

def self.options(&block)
  options = OptionParser.new
  opts = options.options = {
    :base_uri       => nil,
    :canonicalize   => false,
    :debug          => false,
    :evaluate       => nil,
    :format         => nil,
    :output         => $stdout,
    :output_format  => :ntriples,
    :validate       => false,
  }

  # Command-specific options
  if block_given?
    case block.arity
      when 1 then block.call(options)
      else options.instance_eval(&block)
    end
  end
  options.banner ||= "Usage: #{self.basename} [options] command [args...]"

  options.on('--canonicalize', 'Canonicalize input.') do
    opts[:canonicalize] = true
  end

  options.on('-d', '--debug',   'Enable debug output for troubleshooting.') do
    opts[:debug] = $DEBUG = true
  end

  options.on("-e", "--evaluate STRING", "Evaluate argument as RDF input, if no files are specified") do |arg|
    opts[:evaluate] = arg
  end

  options.on("--input-format FORMAT", "Format of input file, uses heuristic if not specified") do |arg|
    opts[:format] = arg.downcase.to_sym
  end

  options.on("-o", "--output FILE", "File to write output, defaults to STDOUT") do |arg|
    opts[:output] = File.open(arg, "w")
  end

  options.on("--output-format FORMAT", "Format of output file, defaults to NTriples") do |arg|
    opts[:output_format] = arg.downcase.to_sym
  end

  options.on('--uri URI', 'Base URI of input file, defaults to the filename.') do |arg|
    opts[:base_uri] = arg
  end

  options.on('--validate', 'Validate input file.') do
    opts[:validate] = true
  end

  options.on_tail("-h", "--help", "Show this message") do
    $stdout.puts options
    $stdout.puts "Available commands:\n\t#{self.commands.join("\n\t")}"
    exit
  end
  
  begin
    options.parse!
  rescue OptionParser::InvalidOption => e
    abort e
  end
  
  options
end

.parse(files, options = {}) {|reader| ... } ⇒ nil

Parse each file, STDIN or specified string in options yielding a reader

Parameters:

  • files (Array<String>)

Yields:

  • (reader)

Yield Parameters:

Returns:

  • (nil)


176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/rdf/cli.rb', line 176

def self.parse(files, options = {}, &block)      
  if files.empty?
    # If files are empty, either use options[:execute]
    input = options[:evaluate] ? StringIO.new(options[:evaluate]) : STDIN
    RDF::Reader.for(options[:format] || :ntriples).new(input, options) do |reader|
      yield(reader)
    end
  else
    files.each do |file|
      RDF::Reader.open(file, options) do |reader|
        (@readers ||= []) << reader.class.to_s
        yield(reader)
      end
    end
  end
end