Class: RDF::Tabular::Reader
- Includes:
- Util::Logger
- Defined in:
- vendor/bundler/ruby/3.3.0/bundler/gems/rdf-tabular-52804f52e930/lib/rdf/tabular/reader.rb
Overview
A Tabular Data to RDF parser in Ruby.
Constant Summary
Constants included from Util::Logger
Instance Attribute Summary collapse
-
#input ⇒ :read
readonly
Input open to read.
-
#metadata ⇒ Metadata
readonly
Metadata associated with the CSV.
Attributes inherited from Reader
Attributes included from Enumerable
Class Method Summary collapse
-
.options ⇒ Object
Writer options.
Instance Method Summary collapse
-
#initialize(input = $stdin, **options) {|reader| ... } ⇒ Reader
constructor
Initializes the RDF::Tabular Reader instance.
- #minimal? ⇒ Boolean
- #prov? ⇒ Boolean
-
#to_hash(**options) ⇒ Hash, Array
Return a hash representation of the data for JSON serialization.
-
#to_json(options = @options) ⇒ String
Transform to JSON.
-
#validate! ⇒ Object
Do we have valid metadata?.
Methods included from Util::Logger
#log_debug, #log_depth, #log_error, #log_fatal, #log_info, #log_recover, #log_recovering?, #log_statistics, #log_warn, #logger
Methods inherited from Reader
#base_uri, #canonicalize?, #close, each, #each_pg_statement, #encoding, #fail_object, #fail_predicate, #fail_subject, for, format, #intern?, #lineno, open, #prefix, #prefixes, #prefixes=, #read_statement, #read_triple, #rewind, #to_sym, to_sym, #valid?, #validate?
Methods included from Util::Aliasing::LateBound
Methods included from Enumerable
add_entailment, #canonicalize, #canonicalize!, #dump, #each_graph, #each_object, #each_predicate, #each_quad, #each_subject, #each_term, #entail, #enum_graph, #enum_object, #enum_predicate, #enum_quad, #enum_statement, #enum_subject, #enum_term, #enum_triple, #graph?, #graph_names, #invalid?, #method_missing, #object?, #objects, #predicate?, #predicates, #project_graph, #quad?, #quads, #respond_to_missing?, #statement?, #statements, #subject?, #subjects, #supports?, #term?, #terms, #to_a, #to_h, #to_set, #triple?, #triples, #valid?
Methods included from Isomorphic
#bijection_to, #isomorphic_with?
Methods included from Countable
Methods included from Readable
Constructor Details
#initialize(input = $stdin, **options) {|reader| ... } ⇒ Reader
Initializes the RDF::Tabular Reader instance.
75 76 77 78 79 80 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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-tabular-52804f52e930/lib/rdf/tabular/reader.rb', line 75 def initialize(input = $stdin, **, &block) super do # Base would be how we are to take this @options[:base] ||= base_uri.to_s if base_uri @options[:base] ||= input.base_uri if input.respond_to?(:base_uri) @options[:base] ||= input.path if input.respond_to?(:path) @options[:base] ||= input.filename if input.respond_to?(:filename) if RDF::URI(@options[:base]).relative? && File.exist?(@options[:base].to_s) file_uri = (Gem.win_platform? ? "file:/" : "file:") + @options[:base] @options[:base] = RDF::URI(file_uri.to_s).normalize end log_debug("Reader#initialize") {"input: #{input.inspect}, base: #{@options[:base]}"} # Minimal implies noProv @options[:noProv] ||= @options[:minimal] @input = case input when String then StringIO.new(input) when Array then StringIO.new(input.map {|r| r.join(",")}.join("\n")) else input end log_depth do # If input is JSON, then the input is the metadata content_type = @input.respond_to?(:content_type) ? @input.content_type : "" if @options[:base] =~ /\.json(?:ld)?$/ || content_type =~ %r(application/(csvm\+|ld\+)?json) @metadata = Metadata.new(@input, filenames: @options[:base], **@options) # If @metadata is for a Table, turn it into a TableGroup @metadata = @metadata.to_table_group if @metadata.is_a?(Table) @metadata.normalize! @input = @metadata elsif (@options[:base].to_s.end_with?(".html") || %w(text/html application/xhtml+html).include?(content_type)) && !RDF::URI(@options[:base].to_s).fragment require 'nokogiri' unless defined?(:Nokogiri) doc = Nokogiri::HTML.parse(input) doc.xpath("//script[@type='application/csvm+json']/text()").each do |script| def script.content_type; "application/csvm+json"; end log_debug("Reader#initialize") {"Process HTML script block"} @input = script @metadata = Metadata.new(@input, filenames: @options[:base], **@options) # If @metadata is for a Table, turn it into a TableGroup @metadata = @metadata.to_table_group if @metadata.is_a?(Table) @metadata.normalize! @input = @metadata end elsif @options[:no_found_metadata] # Extract embedded metadata and merge = @options[:metadata] || Table.new({}, context: "http://www.w3.org/ns/csvw") dialect = .dialect.dup # HTTP flags for setting header values dialect.header = false if (input.headers.fetch(:content_type, '').split(';').include?('header=absent') rescue false) dialect.encoding = input.charset if (input.charset rescue nil) dialect.separator = "\t" if (input.content_type == "text/tsv" rescue nil) = @options.dup [:lang] = .lang if .lang = dialect.(input, @options[:metadata], **) if (@metadata = @options[:metadata]) && @metadata.tableSchema @metadata.verify_compatible!() else @metadata = .normalize! end lang = input.headers[:content_language] rescue nil lang = nil if lang.to_s.include?(',') # Not for multiple languages # Set language, if unset and provided @metadata.lang ||= lang if lang @metadata.dialect = dialect else # It's tabluar data. Find metadata and proceed as if it was specified in the first place @options[:original_input] = @input unless @options[:metadata] @input = @metadata = Metadata.for_input(@input, **@options).normalize! end log_debug("Reader#initialize") {"input: #{input}, metadata: #{.inspect}"} if block_given? case block.arity when 0 then instance_eval(&block) else block.call(self) end end end end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class RDF::Enumerable
Instance Attribute Details
#input ⇒ :read (readonly)
Input open to read
21 22 23 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-tabular-52804f52e930/lib/rdf/tabular/reader.rb', line 21 def input @input end |
#metadata ⇒ Metadata (readonly)
Metadata associated with the CSV
16 17 18 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-tabular-52804f52e930/lib/rdf/tabular/reader.rb', line 16 def @metadata end |
Class Method Details
.options ⇒ Object
Writer options
26 27 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 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-tabular-52804f52e930/lib/rdf/tabular/reader.rb', line 26 def self. super + [ RDF::CLI::Option.new( symbol: :metadata, datatype: RDF::URI, control: :url2, on: ["--metadata URI"], description: "user supplied metadata, merged on top of extracted metadata. If provided as a URL, Metadata is loade from that location.") {|arg| RDF::URI(arg)}, RDF::CLI::Option.new( symbol: :minimal, control: :checkbox, datatype: TrueClass, on: ["--minimal"], description: "Includes only the information gleaned from the cells of the tabular data.") {true}, RDF::CLI::Option.new( symbol: :noProv, datatype: TrueClass, control: :checkbox, on: ["--no-prov"], description: "do not output optional provenance information.") {true}, RDF::CLI::Option.new( symbol: :decode_uri, datatype: TrueClass, control: :checkbox, on: ["--decode-uri"], description: "decode %-encodings in the result of a URI Template operation." ) ] end |
Instance Method Details
#minimal? ⇒ Boolean
639 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-tabular-52804f52e930/lib/rdf/tabular/reader.rb', line 639 def minimal?; @options[:minimal]; end |
#prov? ⇒ Boolean
640 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-tabular-52804f52e930/lib/rdf/tabular/reader.rb', line 640 def prov?; !(@options[:noProv]); end |
#to_hash(**options) ⇒ Hash, Array
Return a hash representation of the data for JSON serialization
Produces an array if run in minimal mode.
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-tabular-52804f52e930/lib/rdf/tabular/reader.rb', line 456 def to_hash(**) # Construct metadata from that passed from file open, along with information from the file. if input.is_a?(Metadata) log_debug("each_statement: metadata") {input.inspect} log_depth do # Get Metadata to invoke and open referenced files begin # Validate metadata input.validate! tables = [] table_group = {} table_group['@id'] = input.id.to_s if input.id # Common Properties input.each do |key, value| next unless key.to_s.include?(':') || key == :notes table_group[key] = input.common_properties(nil, key, value) table_group[key] = [table_group[key]] if key == :notes && !table_group[key].is_a?(Array) end table_group['tables'] = tables if [:original_input] && !input.describes_file?([:base_uri]) Reader.new([:original_input], **.merge( metadata: input.tables.first, base: input.tables.first.url, minimal: minimal?, no_found_metadata: true, )) do |r| case t = r.to_hash(**) when Array then tables += t unless input.tables.first.suppressOutput when Hash then tables << t unless input.tables.first.suppressOutput end end else input.each_table do |table| next if table.suppressOutput && !validate? Reader.open(table.url, **.merge( metadata: table, base: table.url, minimal: minimal?, no_found_metadata: true, )) do |r| case t = r.to_hash(**) when Array then tables += t unless table.suppressOutput when Hash then tables << t unless table.suppressOutput end end end end # Lastly, if validating, validate foreign key integrity validate_foreign_keys(input) if validate? # Result is table_group or array minimal? ? tables : table_group end end else rows = [] table = {} table['@id'] = .id.to_s if .id table['url'] = .url.to_s table.merge!("row" => rows) # Input is file containing CSV data. # Output ROW-Level statements primary_keys = [] .each_row(input) do |row| if row.is_a?(RDF::Statement) # May add additional comments table['rdfs:comment'] ||= [] table['rdfs:comment'] << row.object.to_s next end # Collect primary and foreign keys if validating if validate? primary_keys << row.primaryKey collect_foreign_key_references(, [:fks_referencing_table], row) end # Output row-level metadata r, a, values = {}, {}, {} r["url"] = row.id.to_s r["rownum"] = row.number # Row titles Array(row.titles).each { |t| merge_compacted_value(r, "titles", t.to_s) unless t.nil?} row.values.each_with_index do |cell, index| column = .tableSchema.columns[index] # Collect cell errors unless Array(cell.errors).empty? self.send(validate? ? :log_error : :log_warn, "Table #{.url} row #{row.number}(src #{row.sourceNumber}, col #{cell.column.sourceNumber}): ") do cell.errors.join("\n") end end # Ignore suppressed columns next if column.suppressOutput # Skip valueUrl cells where the valueUrl is null next if cell.column.valueUrl && cell.valueUrl.nil? # Skip empty sequences next if !cell.column.valueUrl && cell.value.is_a?(Array) && cell.value.empty? subject = cell.aboutUrl || 'null' co = (a[subject.to_s] ||= {}) co['@id'] = subject.to_s unless subject == 'null' prop = case cell.propertyUrl when RDF.type then '@type' when nil then CGI.unescape(column.name) # Use URI-decoded name else # Compact the property to a term or prefixed name .context.compact_iri(cell.propertyUrl, vocab: true) end value = case when prop == '@type' .context.compact_iri(cell.valueUrl || cell.value, vocab: true) when cell.valueUrl unless subject == cell.valueUrl values[cell.valueUrl.to_s] ||= {o: co, prop: prop, count: 0} values[cell.valueUrl.to_s][:count] += 1 end cell.valueUrl.to_s when cell.value.is_a?(RDF::Literal::Double) cell.value.object.nan? || cell.value.object.infinite? ? cell.value : cell.value.object when cell.value.is_a?(RDF::Literal::Integer) cell.value.object.to_i when cell.value.is_a?(RDF::Literal::Numeric) cell.value.object.to_f when cell.value.is_a?(RDF::Literal::Boolean) cell.value.object when cell.value cell.value end # Add or merge value merge_compacted_value(co, prop, value) unless value.nil? end # Check for nesting values.keys.each do |valueUrl| next unless a.has_key?(valueUrl) ref = values[valueUrl] co = ref[:o] prop = ref[:prop] next if ref[:count] != 1 raise "Expected #{ref[o][prop].inspect} to include #{valueUrl.inspect}" unless Array(co[prop]).include?(valueUrl) co[prop] = Array(co[prop]).map {|e| e == valueUrl ? a.delete(valueUrl) : e} co[prop] = co[prop].first if co[prop].length == 1 end r["describes"] = a.values if minimal? rows.concat(r["describes"]) else rows << r end end # Validate primary keys validate_primary_keys(, primary_keys) if validate? # Use string values notes and common properties .each do |key, value| next unless key.to_s.include?(':') || key == :notes table[key] = .common_properties(nil, key, value) table[key] = [table[key]] if key == :notes && !table[key].is_a?(Array) end unless minimal? minimal? ? table["row"] : table end end |
#to_json(options = @options) ⇒ String
Transform to JSON. Note that this must be run from within the reader context if the input is an open IO stream.
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-tabular-52804f52e930/lib/rdf/tabular/reader.rb', line 411 def to_json( = @options) io = case when IO, StringIO then when Hash then [:io] end json_state = case when Hash case when .has_key?(:state) then [:state] when .has_key?(:indent) then else ::JSON::LD::JSON_STATE end when ::JSON::State, ::JSON::Ext::Generator::State, ::JSON::Pure::Generator::State else ::JSON::LD::JSON_STATE end = {} unless .is_a?(Hash) hash_fn = :to_hash = .merge(noProv: @options[:noProv]) res = if io ::JSON:: = json_state ::JSON.dump(self.send(hash_fn, **), io) else hash = self.send(hash_fn, **) ::JSON.generate(hash, json_state) end if validate? && log_statistics[:error] raise RDF::Tabular::Error, "Errors found during processing" end res rescue IOError => e raise RDF::Tabular::Error, e. end |
#validate! ⇒ Object
Do we have valid metadata?
378 379 380 381 382 383 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-tabular-52804f52e930/lib/rdf/tabular/reader.rb', line 378 def validate! @options[:validate] = true each_statement {} rescue RDF::ReaderError => e raise Error, e. end |