rdf-gzip 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,25 @@
1
+ rdf-gzip: gzip support for rdf.rb readers and writers
2
+
3
+ Usage:
4
+
5
+ require 'rdf'
6
+ require 'rdf/ntriples'
7
+ require 'rdf/json' # or whatever serialization
8
+ require 'rdf/gzip'
9
+
10
+ graph = RDF::Graph.new
11
+
12
+ RDF::Reader.for(:ntriples).gzopen("/path/to/rdf.nt.gz") do | reader |
13
+ reader.each_statement do | statement |
14
+ graph << statement
15
+ end
16
+ end
17
+
18
+ RDF::JSONWriter.gzopen("/path/to/rdf.json.gz") do | writer |
19
+ graph.each_statement do | statement |
20
+ writer << statement
21
+ end
22
+ end
23
+
24
+ That is to say, exactly like how you would normally user RDF::Reader and RDF::Writer, but use gzopen rather than open. The format needs to be called explicitly (that is, no file format detection) due to the fact that the file suffix may not exist in the file name anywhere.
25
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1
@@ -0,0 +1,8 @@
1
+ require 'zlib'
2
+ require File.dirname(__FILE__) + '/util/file'
3
+ require 'rdf/reader'
4
+ require File.dirname(__FILE__) + '/reader'
5
+ require 'rdf/writer'
6
+ require File.dirname(__FILE__) + '/writer'
7
+
8
+
@@ -0,0 +1,33 @@
1
+ module RDF
2
+ class Reader
3
+ ##
4
+ # Parses input from the given gzip file name or URL.
5
+ #
6
+ # @param [String, #to_s] filename
7
+ # @param [Hash{Symbol => Object}] options
8
+ # any additional options (see {RDF::Reader#initialize} and {RDF::Format.for})
9
+ # @option options [Symbol] :format (:ntriples)
10
+ # @yield [reader]
11
+ # @yieldparam [RDF::Reader] reader
12
+ # @yieldreturn [void] ignored
13
+ # @raise [RDF::FormatError] if no reader found for the specified format
14
+ def self.gzopen(filename, options = {}, &block)
15
+ Util::File.open_gzipfile(filename, options) do |file|
16
+ format_options = options.dup
17
+ format_options[:content_type] ||= file.content_type if file.respond_to?(:content_type)
18
+ format_options[:file_name] ||= filename
19
+ reader = self.for(format_options[:format] || format_options) do
20
+ # Return a sample from the input file
21
+ sample = file.read(1000)
22
+ file.rewind
23
+ sample
24
+ end
25
+ if reader
26
+ reader.new(file, options, &block)
27
+ else
28
+ raise FormatError, "unknown RDF format: #{format_options.inspect}"
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,30 @@
1
+ module RDF; module Util
2
+ ##
3
+ # Wrapper for Kernel.open. Allows implementations to override to get
4
+ # more suffisticated behavior for HTTP resources (e.g., Accept header).
5
+ #
6
+ # Also supports the file: scheme for access to local files.
7
+ #
8
+ # Classes include this module when they represent some form of a file
9
+ # as a base resource, for instance an HTTP resource representing the
10
+ # serialization of a Graph.
11
+ #
12
+ # This module may be monkey-patched to allow for more options
13
+ # and interfaces.
14
+ #
15
+ # @since 0.2.4
16
+ module File
17
+ ##
18
+ # Open the file, returning or yielding an IO stream and mime_type.
19
+ #
20
+ # @param [String] filename_or_url to open
21
+ # @param [Hash{Symbol => Object}] options
22
+ # any options to pass through to the underlying UUID library
23
+ # @return [IO] File stream
24
+ # @yield [IO] File stream
25
+ def self.open_gzipfile(filename_or_url, options = {}, &block)
26
+ filename_or_url = $1 if filename_or_url.to_s.match(/^file:(.*)$/)
27
+ f = Zlib::GzipReader.open(filename_or_url.to_s, &block)
28
+ end
29
+ end # File
30
+ end; end # RDF::Util
@@ -0,0 +1,19 @@
1
+ module RDF
2
+ class Writer
3
+ ##
4
+ # Writes output to the given `gzip_filename`.
5
+ #
6
+ # @param [String, #to_s] gzip_filename
7
+ # @param [Hash{Symbol => Object}] options
8
+ # any additional options (see {RDF::Writer#initialize} and {RDF::Format.for})
9
+ # @option options [Symbol] :format (nil)
10
+ # @return [RDF::Writer]
11
+ def self.gzopen(gzip_filename, options = {}, &block)
12
+ Zlib::GzipWriter.open(gzip_filename) do |file|
13
+ format_options = options.dup
14
+ format_options[:file_name] ||= gzip_filename
15
+ self.for(options[:format] || format_options).new(file, options, &block)
16
+ end
17
+ end
18
+ end
19
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rdf-gzip
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ross Singer
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-02 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rdf
16
+ requirement: &70216709727900 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.3.1
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70216709727900
25
+ description: Gzip file support for RDF.rb readers and writers.
26
+ email: public-rdf-ruby@w3.org
27
+ executables: []
28
+ extensions: []
29
+ extra_rdoc_files: []
30
+ files:
31
+ - README
32
+ - VERSION
33
+ - lib/rdf/gzip.rb
34
+ - lib/rdf/reader.rb
35
+ - lib/rdf/util/file.rb
36
+ - lib/rdf/writer.rb
37
+ homepage: http://github.com/rsinger/rdf-gzip
38
+ licenses:
39
+ - Public Domain
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: 1.8.1
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubyforge_project:
58
+ rubygems_version: 1.8.10
59
+ signing_key:
60
+ specification_version: 3
61
+ summary: Gzip file support for RDF.rb readers and writers.
62
+ test_files: []