imw 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +34 -14
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/lib/imw.rb +9 -6
- data/lib/imw/{resources/archive.rb → archives.rb} +20 -10
- data/lib/imw/archives/rar.rb +19 -0
- data/lib/imw/archives/tar.rb +19 -0
- data/lib/imw/archives/tarbz2.rb +73 -0
- data/lib/imw/archives/targz.rb +73 -0
- data/lib/imw/archives/zip.rb +51 -0
- data/lib/imw/{resources/compressed_file.rb → compressed_files.rb} +16 -11
- data/lib/imw/compressed_files/bz2.rb +16 -0
- data/lib/imw/{resources → compressed_files}/compressible.rb +2 -4
- data/lib/imw/compressed_files/gz.rb +16 -0
- data/lib/imw/formats.rb +31 -0
- data/lib/imw/formats/delimited.rb +90 -0
- data/lib/imw/formats/excel.rb +125 -0
- data/lib/imw/formats/json.rb +51 -0
- data/lib/imw/formats/sgml.rb +69 -0
- data/lib/imw/formats/yaml.rb +51 -0
- data/lib/imw/resource.rb +108 -10
- data/lib/imw/schemes.rb +21 -0
- data/lib/imw/schemes/hdfs.rb +240 -0
- data/lib/imw/schemes/http.rb +166 -0
- data/lib/imw/schemes/local.rb +219 -0
- data/lib/imw/schemes/remote.rb +114 -0
- data/lib/imw/schemes/s3.rb +135 -0
- data/lib/imw/tools.rb +8 -0
- data/lib/imw/{transforms → tools}/archiver.rb +1 -1
- data/lib/imw/{transforms → tools}/transferer.rb +10 -10
- data/spec/imw/{resources/archive_spec.rb → archive_spec.rb} +3 -3
- data/spec/imw/{resources/archives_and_compressed → archives}/rar_spec.rb +2 -2
- data/spec/imw/{resources/archives_and_compressed → archives}/tar_spec.rb +2 -2
- data/spec/imw/{resources/archives_and_compressed → archives}/tarbz2_spec.rb +4 -4
- data/spec/imw/{resources/archives_and_compressed → archives}/targz_spec.rb +4 -4
- data/spec/imw/{resources/archives_and_compressed → archives}/zip_spec.rb +2 -2
- data/spec/imw/compressed_files/bz2_spec.rb +15 -0
- data/spec/imw/{resources → compressed_files}/compressible_spec.rb +1 -1
- data/spec/imw/compressed_files/gz_spec.rb +15 -0
- data/spec/imw/{resources/compressed_file_spec.rb → compressed_files_spec.rb} +3 -3
- data/spec/imw/{resources/formats → formats}/delimited_spec.rb +2 -2
- data/spec/imw/{resources/formats → formats}/json_spec.rb +2 -2
- data/spec/imw/{resources/formats → formats}/sgml_spec.rb +2 -2
- data/spec/imw/{resources/formats → formats}/yaml_spec.rb +2 -2
- data/spec/imw/resource_spec.rb +4 -4
- data/spec/imw/{resources/schemes → schemes}/hdfs_spec.rb +7 -7
- data/spec/imw/{resources/schemes → schemes}/http_spec.rb +2 -2
- data/spec/imw/{resources → schemes}/local_spec.rb +5 -5
- data/spec/imw/{resources → schemes}/remote_spec.rb +7 -3
- data/spec/imw/{resources/schemes → schemes}/s3_spec.rb +2 -2
- data/spec/imw/{transforms → tools}/archiver_spec.rb +2 -2
- data/spec/imw/tools/transferer_spec.rb +113 -0
- metadata +69 -71
- data/lib/imw/resources.rb +0 -118
- data/lib/imw/resources/archives_and_compressed.rb +0 -32
- data/lib/imw/resources/archives_and_compressed/bz2.rb +0 -18
- data/lib/imw/resources/archives_and_compressed/gz.rb +0 -18
- data/lib/imw/resources/archives_and_compressed/rar.rb +0 -23
- data/lib/imw/resources/archives_and_compressed/tar.rb +0 -23
- data/lib/imw/resources/archives_and_compressed/tarbz2.rb +0 -78
- data/lib/imw/resources/archives_and_compressed/targz.rb +0 -78
- data/lib/imw/resources/archives_and_compressed/zip.rb +0 -57
- data/lib/imw/resources/formats.rb +0 -32
- data/lib/imw/resources/formats/delimited.rb +0 -92
- data/lib/imw/resources/formats/excel.rb +0 -125
- data/lib/imw/resources/formats/json.rb +0 -53
- data/lib/imw/resources/formats/sgml.rb +0 -72
- data/lib/imw/resources/formats/yaml.rb +0 -53
- data/lib/imw/resources/local.rb +0 -198
- data/lib/imw/resources/remote.rb +0 -110
- data/lib/imw/resources/schemes.rb +0 -19
- data/lib/imw/resources/schemes/hdfs.rb +0 -242
- data/lib/imw/resources/schemes/http.rb +0 -161
- data/lib/imw/resources/schemes/s3.rb +0 -137
- data/lib/imw/transforms.rb +0 -8
- data/spec/imw/resources/archives_and_compressed/bz2_spec.rb +0 -15
- data/spec/imw/resources/archives_and_compressed/gz_spec.rb +0 -15
- data/spec/imw/transforms/transferer_spec.rb +0 -113
@@ -0,0 +1,113 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/../../spec_helper"
|
2
|
+
|
3
|
+
describe IMW::Tools::Transferer do
|
4
|
+
before do
|
5
|
+
@local = IMW.open("foobar.txt")
|
6
|
+
@http = IMW.open("http://www.google.com")
|
7
|
+
@hdfs = IMW.open("hdfs:///path/to/foobar.txt")
|
8
|
+
@s3 = IMW.open("s3://mybucket/foo/bar")
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should raise an error unless the action is one of :cp, :copy, :mv :move, or :mv!" do
|
12
|
+
IMW::Tools::Transferer.new(:cp, @local, @http).should be
|
13
|
+
IMW::Tools::Transferer.new(:copy, @local, @http).should be
|
14
|
+
IMW::Tools::Transferer.new(:mv, @local, @http).should be
|
15
|
+
IMW::Tools::Transferer.new(:move, @local, @http).should be
|
16
|
+
IMW::Tools::Transferer.new(:mv!, @local, @http).should be
|
17
|
+
lambda { IMW::Tools::Transferer.new(:foobar, @local, @http) }.should raise_error(IMW::ArgumentError)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should raise an error if the source and the destination have the same URI" do
|
21
|
+
lambda { IMW::Tools::Transferer.new(:cp, @local, @local) }.should raise_error(IMW::PathError)
|
22
|
+
end
|
23
|
+
|
24
|
+
describe "transfering local files" do
|
25
|
+
it "should raise an error if the source doesn't exist" do
|
26
|
+
lambda { IMW::Tools::Transferer.new(:cp, @local, 'barbaz.txt').transfer! }.should raise_error(IMW::PathError)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "can copy a local file" do
|
30
|
+
IMWTest::Random.file @local.path
|
31
|
+
IMW::Tools::Transferer.new(:cp, @local, 'barbaz.txt').transfer!
|
32
|
+
@local.exist?.should be_true
|
33
|
+
IMW.open('barbaz.txt').exist?.should be_true
|
34
|
+
end
|
35
|
+
|
36
|
+
it "can move a local file" do
|
37
|
+
IMWTest::Random.file @local.path
|
38
|
+
IMW::Tools::Transferer.new(:mv, @local, 'barbaz.txt').transfer!
|
39
|
+
@local.exist?.should be_false
|
40
|
+
IMW.open('barbaz.txt').exist?.should be_true
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
describe "transferring HTTP files" do
|
46
|
+
it "can copy a remote file to a local path" do
|
47
|
+
IMW::Tools::Transferer.new(:cp, @http, @local).transfer!
|
48
|
+
@local.exist?.should be_true
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
describe "transferring S3 files" do
|
53
|
+
|
54
|
+
it "can copy an S3 file to a local path" do
|
55
|
+
IMW::Schemes::S3.should_receive(:get).with(@s3, @local)
|
56
|
+
IMW::Tools::Transferer.new(:cp, @s3, @local).transfer!
|
57
|
+
end
|
58
|
+
|
59
|
+
it "can copy a local path to an S3 file" do
|
60
|
+
IMWTest::Random.file @local.path
|
61
|
+
IMW::Schemes::S3.should_receive(:put).with(@local, @s3)
|
62
|
+
IMW::Tools::Transferer.new(:cp, @local, @s3).transfer!
|
63
|
+
end
|
64
|
+
|
65
|
+
it "can copy between S3 files" do
|
66
|
+
@new_s3 = IMW.open('s3://mybucket/new/path')
|
67
|
+
IMW::Schemes::S3.should_receive(:copy).with(@s3, @new_s3)
|
68
|
+
IMW::Tools::Transferer.new(:cp, @s3, @new_s3).transfer!
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
describe "transferring HDFS files" do
|
73
|
+
before do
|
74
|
+
IMW::Schemes::HDFS.stub!(:fs)
|
75
|
+
end
|
76
|
+
|
77
|
+
it "can copy a local file to an HDFS path" do
|
78
|
+
IMWTest::Random.file @local.path
|
79
|
+
|
80
|
+
IMW::Schemes::HDFS.should_receive(:fs).with(:put, @local.path, @hdfs.path)
|
81
|
+
IMW::Tools::Transferer.new(:cp, @local, @hdfs).transfer!
|
82
|
+
end
|
83
|
+
|
84
|
+
it "can copy an HDFS file to a local path" do
|
85
|
+
IMW::Schemes::HDFS.should_receive(:fs).with(:get, @hdfs.path, @local.path)
|
86
|
+
IMW::Tools::Transferer.new(:cp, @hdfs, @local).transfer!
|
87
|
+
end
|
88
|
+
|
89
|
+
it "can copy between HDFS paths" do
|
90
|
+
@new_hdfs = IMW.open('hdfs:///a/new/path')
|
91
|
+
IMW::Schemes::HDFS.should_receive(:fs).with(:cp, @hdfs.path, @new_hdfs.path)
|
92
|
+
IMW::Tools::Transferer.new(:cp, @hdfs, @new_hdfs).transfer!
|
93
|
+
end
|
94
|
+
|
95
|
+
it "can move between HDFS paths" do
|
96
|
+
@new_hdfs = IMW.open('hdfs:///a/new/path')
|
97
|
+
IMW::Schemes::HDFS.should_receive(:fs).with(:mv, @hdfs.path, @new_hdfs.path)
|
98
|
+
IMW::Tools::Transferer.new(:mv, @hdfs, @new_hdfs).transfer!
|
99
|
+
end
|
100
|
+
|
101
|
+
it "can copy from S3 to HDFS" do
|
102
|
+
IMW::Schemes::HDFS.should_receive(:fs).with(:cp, @s3.s3n_url, @hdfs.path)
|
103
|
+
IMW::Tools::Transferer.new(:cp, @s3, @hdfs).transfer!
|
104
|
+
end
|
105
|
+
|
106
|
+
it "can copy from HDFS to S3" do
|
107
|
+
IMW::Schemes::HDFS.should_receive(:fs).with(:cp, @hdfs.path, @s3.s3n_url)
|
108
|
+
IMW::Tools::Transferer.new(:cp, @hdfs, @s3).transfer!
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: imw
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dhruv Bansal
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2010-05-
|
13
|
+
date: 2010-05-14 00:00:00 -05:00
|
14
14
|
default_executable: imw
|
15
15
|
dependencies: []
|
16
16
|
|
@@ -36,10 +36,26 @@ files:
|
|
36
36
|
- etc/imwrc.rb
|
37
37
|
- examples/dataset.rb
|
38
38
|
- lib/imw.rb
|
39
|
+
- lib/imw/archives.rb
|
40
|
+
- lib/imw/archives/rar.rb
|
41
|
+
- lib/imw/archives/tar.rb
|
42
|
+
- lib/imw/archives/tarbz2.rb
|
43
|
+
- lib/imw/archives/targz.rb
|
44
|
+
- lib/imw/archives/zip.rb
|
39
45
|
- lib/imw/boot.rb
|
46
|
+
- lib/imw/compressed_files.rb
|
47
|
+
- lib/imw/compressed_files/bz2.rb
|
48
|
+
- lib/imw/compressed_files/compressible.rb
|
49
|
+
- lib/imw/compressed_files/gz.rb
|
40
50
|
- lib/imw/dataset.rb
|
41
51
|
- lib/imw/dataset/paths.rb
|
42
52
|
- lib/imw/dataset/workflow.rb
|
53
|
+
- lib/imw/formats.rb
|
54
|
+
- lib/imw/formats/delimited.rb
|
55
|
+
- lib/imw/formats/excel.rb
|
56
|
+
- lib/imw/formats/json.rb
|
57
|
+
- lib/imw/formats/sgml.rb
|
58
|
+
- lib/imw/formats/yaml.rb
|
43
59
|
- lib/imw/parsers.rb
|
44
60
|
- lib/imw/parsers/html_parser.rb
|
45
61
|
- lib/imw/parsers/html_parser/matchers.rb
|
@@ -47,34 +63,16 @@ files:
|
|
47
63
|
- lib/imw/parsers/regexp_parser.rb
|
48
64
|
- lib/imw/repository.rb
|
49
65
|
- lib/imw/resource.rb
|
50
|
-
- lib/imw/resources.rb
|
51
|
-
- lib/imw/resources/archive.rb
|
52
|
-
- lib/imw/resources/archives_and_compressed.rb
|
53
|
-
- lib/imw/resources/archives_and_compressed/bz2.rb
|
54
|
-
- lib/imw/resources/archives_and_compressed/gz.rb
|
55
|
-
- lib/imw/resources/archives_and_compressed/rar.rb
|
56
|
-
- lib/imw/resources/archives_and_compressed/tar.rb
|
57
|
-
- lib/imw/resources/archives_and_compressed/tarbz2.rb
|
58
|
-
- lib/imw/resources/archives_and_compressed/targz.rb
|
59
|
-
- lib/imw/resources/archives_and_compressed/zip.rb
|
60
|
-
- lib/imw/resources/compressed_file.rb
|
61
|
-
- lib/imw/resources/compressible.rb
|
62
|
-
- lib/imw/resources/formats.rb
|
63
|
-
- lib/imw/resources/formats/delimited.rb
|
64
|
-
- lib/imw/resources/formats/excel.rb
|
65
|
-
- lib/imw/resources/formats/json.rb
|
66
|
-
- lib/imw/resources/formats/sgml.rb
|
67
|
-
- lib/imw/resources/formats/yaml.rb
|
68
|
-
- lib/imw/resources/local.rb
|
69
|
-
- lib/imw/resources/remote.rb
|
70
|
-
- lib/imw/resources/schemes.rb
|
71
|
-
- lib/imw/resources/schemes/hdfs.rb
|
72
|
-
- lib/imw/resources/schemes/http.rb
|
73
|
-
- lib/imw/resources/schemes/s3.rb
|
74
66
|
- lib/imw/runner.rb
|
75
|
-
- lib/imw/
|
76
|
-
- lib/imw/
|
77
|
-
- lib/imw/
|
67
|
+
- lib/imw/schemes.rb
|
68
|
+
- lib/imw/schemes/hdfs.rb
|
69
|
+
- lib/imw/schemes/http.rb
|
70
|
+
- lib/imw/schemes/local.rb
|
71
|
+
- lib/imw/schemes/remote.rb
|
72
|
+
- lib/imw/schemes/s3.rb
|
73
|
+
- lib/imw/tools.rb
|
74
|
+
- lib/imw/tools/archiver.rb
|
75
|
+
- lib/imw/tools/transferer.rb
|
78
76
|
- lib/imw/utils.rb
|
79
77
|
- lib/imw/utils/error.rb
|
80
78
|
- lib/imw/utils/extensions.rb
|
@@ -97,32 +95,32 @@ files:
|
|
97
95
|
- spec/data/sample.txt
|
98
96
|
- spec/data/sample.xml
|
99
97
|
- spec/data/sample.yaml
|
98
|
+
- spec/imw/archive_spec.rb
|
99
|
+
- spec/imw/archives/rar_spec.rb
|
100
|
+
- spec/imw/archives/tar_spec.rb
|
101
|
+
- spec/imw/archives/tarbz2_spec.rb
|
102
|
+
- spec/imw/archives/targz_spec.rb
|
103
|
+
- spec/imw/archives/zip_spec.rb
|
104
|
+
- spec/imw/compressed_files/bz2_spec.rb
|
105
|
+
- spec/imw/compressed_files/compressible_spec.rb
|
106
|
+
- spec/imw/compressed_files/gz_spec.rb
|
107
|
+
- spec/imw/compressed_files_spec.rb
|
100
108
|
- spec/imw/dataset/paths_spec.rb
|
101
109
|
- spec/imw/dataset/workflow_spec.rb
|
110
|
+
- spec/imw/formats/delimited_spec.rb
|
111
|
+
- spec/imw/formats/json_spec.rb
|
112
|
+
- spec/imw/formats/sgml_spec.rb
|
113
|
+
- spec/imw/formats/yaml_spec.rb
|
102
114
|
- spec/imw/parsers/line_parser_spec.rb
|
103
115
|
- spec/imw/parsers/regexp_parser_spec.rb
|
104
116
|
- spec/imw/resource_spec.rb
|
105
|
-
- spec/imw/
|
106
|
-
- spec/imw/
|
107
|
-
- spec/imw/
|
108
|
-
- spec/imw/
|
109
|
-
- spec/imw/
|
110
|
-
- spec/imw/
|
111
|
-
- spec/imw/
|
112
|
-
- spec/imw/resources/archives_and_compressed/zip_spec.rb
|
113
|
-
- spec/imw/resources/compressed_file_spec.rb
|
114
|
-
- spec/imw/resources/compressible_spec.rb
|
115
|
-
- spec/imw/resources/formats/delimited_spec.rb
|
116
|
-
- spec/imw/resources/formats/json_spec.rb
|
117
|
-
- spec/imw/resources/formats/sgml_spec.rb
|
118
|
-
- spec/imw/resources/formats/yaml_spec.rb
|
119
|
-
- spec/imw/resources/local_spec.rb
|
120
|
-
- spec/imw/resources/remote_spec.rb
|
121
|
-
- spec/imw/resources/schemes/hdfs_spec.rb
|
122
|
-
- spec/imw/resources/schemes/http_spec.rb
|
123
|
-
- spec/imw/resources/schemes/s3_spec.rb
|
124
|
-
- spec/imw/transforms/archiver_spec.rb
|
125
|
-
- spec/imw/transforms/transferer_spec.rb
|
117
|
+
- spec/imw/schemes/hdfs_spec.rb
|
118
|
+
- spec/imw/schemes/http_spec.rb
|
119
|
+
- spec/imw/schemes/local_spec.rb
|
120
|
+
- spec/imw/schemes/remote_spec.rb
|
121
|
+
- spec/imw/schemes/s3_spec.rb
|
122
|
+
- spec/imw/tools/archiver_spec.rb
|
123
|
+
- spec/imw/tools/transferer_spec.rb
|
126
124
|
- spec/imw/utils/paths_spec.rb
|
127
125
|
- spec/imw/utils/shared_paths_spec.rb
|
128
126
|
- spec/rcov.opts
|
@@ -163,32 +161,32 @@ signing_key:
|
|
163
161
|
specification_version: 3
|
164
162
|
summary: The Infinite Monkeywrench (IMW) makes acquiring, extracting, transforming, loading, and packaging data easy.
|
165
163
|
test_files:
|
166
|
-
- spec/imw/
|
167
|
-
- spec/imw/
|
168
|
-
- spec/imw/
|
169
|
-
- spec/imw/
|
170
|
-
- spec/imw/
|
171
|
-
- spec/imw/
|
172
|
-
- spec/imw/
|
173
|
-
- spec/imw/
|
174
|
-
- spec/imw/
|
175
|
-
- spec/imw/
|
176
|
-
- spec/imw/
|
177
|
-
- spec/imw/resources/compressible_spec.rb
|
178
|
-
- spec/imw/resources/formats/json_spec.rb
|
179
|
-
- spec/imw/resources/formats/yaml_spec.rb
|
180
|
-
- spec/imw/resources/formats/delimited_spec.rb
|
181
|
-
- spec/imw/resources/formats/sgml_spec.rb
|
182
|
-
- spec/imw/resources/schemes/http_spec.rb
|
183
|
-
- spec/imw/resources/schemes/hdfs_spec.rb
|
184
|
-
- spec/imw/resources/schemes/s3_spec.rb
|
164
|
+
- spec/imw/archives/targz_spec.rb
|
165
|
+
- spec/imw/archives/tar_spec.rb
|
166
|
+
- spec/imw/archives/zip_spec.rb
|
167
|
+
- spec/imw/archives/tarbz2_spec.rb
|
168
|
+
- spec/imw/archives/rar_spec.rb
|
169
|
+
- spec/imw/tools/archiver_spec.rb
|
170
|
+
- spec/imw/tools/transferer_spec.rb
|
171
|
+
- spec/imw/archive_spec.rb
|
172
|
+
- spec/imw/compressed_files/compressible_spec.rb
|
173
|
+
- spec/imw/compressed_files/bz2_spec.rb
|
174
|
+
- spec/imw/compressed_files/gz_spec.rb
|
185
175
|
- spec/imw/dataset/paths_spec.rb
|
186
176
|
- spec/imw/dataset/workflow_spec.rb
|
177
|
+
- spec/imw/compressed_files_spec.rb
|
187
178
|
- spec/imw/parsers/line_parser_spec.rb
|
188
179
|
- spec/imw/parsers/regexp_parser_spec.rb
|
189
180
|
- spec/imw/resource_spec.rb
|
190
|
-
- spec/imw/
|
191
|
-
- spec/imw/
|
181
|
+
- spec/imw/formats/json_spec.rb
|
182
|
+
- spec/imw/formats/yaml_spec.rb
|
183
|
+
- spec/imw/formats/delimited_spec.rb
|
184
|
+
- spec/imw/formats/sgml_spec.rb
|
185
|
+
- spec/imw/schemes/http_spec.rb
|
186
|
+
- spec/imw/schemes/local_spec.rb
|
187
|
+
- spec/imw/schemes/remote_spec.rb
|
188
|
+
- spec/imw/schemes/hdfs_spec.rb
|
189
|
+
- spec/imw/schemes/s3_spec.rb
|
192
190
|
- spec/imw/utils/paths_spec.rb
|
193
191
|
- spec/imw/utils/shared_paths_spec.rb
|
194
192
|
- spec/spec_helper.rb
|
data/lib/imw/resources.rb
DELETED
@@ -1,118 +0,0 @@
|
|
1
|
-
require 'imw/resources/formats'
|
2
|
-
require 'imw/resources/schemes'
|
3
|
-
require 'imw/resources/archives_and_compressed'
|
4
|
-
|
5
|
-
module IMW
|
6
|
-
|
7
|
-
# IMW::Resources is a namespace in which all the modules which
|
8
|
-
# define different kinds of behavior for IMW::Resource objects are
|
9
|
-
# defined.
|
10
|
-
#
|
11
|
-
# When an IMW::Resource is instantiated it eventually calls
|
12
|
-
# IMW::Resources#extend_resource! which will iterate through the
|
13
|
-
# handlers in IMW::Resources#handlers, extending the resource with
|
14
|
-
# modules whose handler conditions are satisfied.
|
15
|
-
#
|
16
|
-
# A handler is just an Array with two elements. The first should be
|
17
|
-
# a module or a string identifying a module.
|
18
|
-
#
|
19
|
-
# If the second element is a Regexp, the corresponding module will
|
20
|
-
# be used if the regexp matches the resource's URI (as a string)
|
21
|
-
#
|
22
|
-
# If the second element is a Proc, it will be called with the
|
23
|
-
# resource as its only argument and if it returns true then the
|
24
|
-
# module will be used.
|
25
|
-
#
|
26
|
-
# You can define your own handlers by appending them to
|
27
|
-
# IMW::Resources::USER_DEFINED_HANDLERS in your <tt>.imwrc</tt>
|
28
|
-
# file.
|
29
|
-
module Resources
|
30
|
-
|
31
|
-
autoload :LocalObj, 'imw/resources/local'
|
32
|
-
autoload :RemoteObj, 'imw/resources/remote'
|
33
|
-
autoload :StringObj, 'imw/resources/string'
|
34
|
-
autoload :Transferable, 'imw/resources/transferable'
|
35
|
-
|
36
|
-
# Iterate through IMW::Resources#handlers and extend the given
|
37
|
-
# +resource+ with modules whose handler conditions match the
|
38
|
-
# resource.
|
39
|
-
#
|
40
|
-
# @param [IMW::Resource] resource the resource to extend
|
41
|
-
# @return [IMW::Resource] the extended resource
|
42
|
-
def self.extend_resource! resource
|
43
|
-
handlers.each do |mod_name, handler|
|
44
|
-
case handler
|
45
|
-
when Regexp then extend_resource_with_mod_or_string!(resource, mod_name) if handler =~ resource.uri.to_s
|
46
|
-
when Proc then extend_resource_with_mod_or_string!(resource, mod_name) if handler.call(resource)
|
47
|
-
when TrueClass then extend_resource_with_mod_or_string!(resource, mod_name)
|
48
|
-
else
|
49
|
-
raise IMW::TypeError("A handler must be Regexp, Proc, or true")
|
50
|
-
end
|
51
|
-
end
|
52
|
-
resource
|
53
|
-
end
|
54
|
-
|
55
|
-
# Basic handlers to determine whether the resource is local,
|
56
|
-
# remote, or a string.
|
57
|
-
BASIC_HANDLERS = [
|
58
|
-
["LocalObj", Proc.new { |resource| resource.scheme == 'file' || resource.scheme.blank? } ],
|
59
|
-
["RemoteObj", Proc.new { |resource| resource.scheme != 'file' && resource.scheme.present? } ],
|
60
|
-
["StringObj", Proc.new { |resource| resource.is_stringio? } ]
|
61
|
-
]
|
62
|
-
|
63
|
-
# Define this constant in your configuration file to add your own
|
64
|
-
# handlers.
|
65
|
-
USER_DEFINED_HANDLERS = [] unless defined?(USER_DEFINED_HANDLERS)
|
66
|
-
|
67
|
-
# include handlers from other modules
|
68
|
-
include IMW::Resources::Formats
|
69
|
-
include IMW::Resources::Schemes
|
70
|
-
|
71
|
-
# A list of handlers to try. Define your own handlers in
|
72
|
-
# IMW::Resources::USER_DEFINED_HANDLERS.
|
73
|
-
#
|
74
|
-
# @return [Array]
|
75
|
-
def self.handlers
|
76
|
-
# order here is important
|
77
|
-
BASIC_HANDLERS + SCHEME_HANDLERS + ARCHIVE_AND_COMPRESSED_HANDLERS + FORMAT_HANDLERS + USER_DEFINED_HANDLERS
|
78
|
-
end
|
79
|
-
|
80
|
-
protected
|
81
|
-
|
82
|
-
# Extend +resource+ with +mod_or_string+. Will work hard to try
|
83
|
-
# and interpret +mod_or_string+ as a module if it's a string.
|
84
|
-
#
|
85
|
-
# @param [IMW::Resource] resource the resource to extend
|
86
|
-
#
|
87
|
-
# @param [Module, String] mod_or_string the module or string
|
88
|
-
# representing a module to extend the resource with
|
89
|
-
def self.extend_resource_with_mod_or_string! resource, mod_or_string
|
90
|
-
if mod_or_string.is_a?(Module)
|
91
|
-
resource.extend(mod_or_string)
|
92
|
-
else
|
93
|
-
# Given a string "Mod::SubMod::SubSubMod" first split it into
|
94
|
-
# its parts ["Mod", "SubMod", "SubSubMod"] and then begin
|
95
|
-
# class_eval'ing them in order so that each is class_eval'd in
|
96
|
-
# the scope of the one before it.
|
97
|
-
#
|
98
|
-
# There is almost certainly a better way to do this.
|
99
|
-
mod_names = mod_or_string.to_s.split('::')
|
100
|
-
mods = []
|
101
|
-
mod_names.each_with_index do |name, index|
|
102
|
-
if index == 0
|
103
|
-
mods << class_eval(name)
|
104
|
-
else
|
105
|
-
begin
|
106
|
-
mods << class_eval(name)
|
107
|
-
rescue NameError
|
108
|
-
mods << mods[index - 1].class_eval(name)
|
109
|
-
end
|
110
|
-
end
|
111
|
-
end
|
112
|
-
resource.extend(mods.last)
|
113
|
-
end
|
114
|
-
end
|
115
|
-
end
|
116
|
-
end
|
117
|
-
|
118
|
-
|
@@ -1,32 +0,0 @@
|
|
1
|
-
module IMW
|
2
|
-
module Resources
|
3
|
-
autoload :Compressible, 'imw/resources/compressible'
|
4
|
-
autoload :CompressedFile, 'imw/resources/compressed_file'
|
5
|
-
autoload :Archive, 'imw/resources/archive'
|
6
|
-
autoload :Archives, 'imw/resources/archive'
|
7
|
-
autoload :CompressedFiles, 'imw/resources/compressed_file'
|
8
|
-
|
9
|
-
# Handlers which augment the resource with methods for archiving,
|
10
|
-
# extracting, compressing, decompressing...
|
11
|
-
ARCHIVE_AND_COMPRESSED_HANDLERS = [
|
12
|
-
|
13
|
-
# try compressible first -- compressed files below will override it
|
14
|
-
["Compressible", Proc.new { |r| r.is_local? } ],
|
15
|
-
|
16
|
-
# order is important! -- tar.bz2 must come before .bz2, &c.
|
17
|
-
["Archives::Tarbz2", Proc.new { |r| r.is_local? && r.path =~ /\.tar\.bz2$/ } ],
|
18
|
-
["Archives::Tarbz2", Proc.new { |r| r.is_local? && r.path =~ /\.tbz2$/ } ],
|
19
|
-
["CompressedFiles::Bz2", Proc.new { |r| r.is_local? && r.path =~ /\.bz2$/ && r.path !~ /\.tar\.bz2$/ && r.path !~ /\.tbz2$/ } ],
|
20
|
-
["Archives::Targz", Proc.new { |r| r.is_local? && r.path =~ /\.tar\.gz$/ } ],
|
21
|
-
["Archives::Targz", Proc.new { |r| r.is_local? && r.path =~ /\.tgz$/ } ],
|
22
|
-
["CompressedFiles::Gz", Proc.new { |r| r.is_local? && r.path =~ /\.gz$/ && r.path !~ /\.tar\.gz$/ && r.path !~ /\.tgz$/ } ],
|
23
|
-
["Archives::Tar", Proc.new { |r| r.is_local? && r.path =~ /\.tar$/ } ],
|
24
|
-
["Archives::Rar", Proc.new { |r| r.is_local? && r.path =~ /\.rar$/ } ],
|
25
|
-
["Archives::Zip", Proc.new { |r| r.is_local? && r.path =~ /\.zip$/ } ]
|
26
|
-
|
27
|
-
]
|
28
|
-
|
29
|
-
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|