hydra-file_characterization 0.2.9 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/hydra/file_characterization.rb +47 -8
- data/lib/hydra/file_characterization/characterizer.rb +5 -1
- data/lib/hydra/file_characterization/exceptions.rb +0 -6
- data/lib/hydra/file_characterization/version.rb +1 -1
- data/spec/lib/hydra/file_characterization/characterizer_spec.rb +1 -5
- data/spec/lib/hydra/file_characterization_spec.rb +56 -46
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e4919a2449be3e3a6127fdc17a5f8552e3b64821
|
4
|
+
data.tar.gz: 1ab2ab15d03a9e4ddfd83b15e2f78a2be5548d86
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d5494b689812d74a60ecca77216cc31302b8da2694635ee59be8b5b98c844bb936bd86a7ae3b09901375cc441c795ff7389b9209c36d41daaa5ba039e00cd6dd
|
7
|
+
data.tar.gz: 5e828f94c57282b40fd42a6bdaaf7e30a74e8e3d85871ec3eef8e43f444fefb9eeca6a0946629a061078715f291e5dd22bdb8abd85bfa5f7c9bc900cddd107e8
|
@@ -39,6 +39,9 @@ module Hydra
|
|
39
39
|
# @example
|
40
40
|
# fits_xml, ffprobe_xml = Hydra::FileCharacterization.characterize(contents_of_a_file, 'file.rb', :fits, :ffprobe)
|
41
41
|
#
|
42
|
+
# @example With an open file
|
43
|
+
# fits_xml, ffprobe_xml = Hydra::FileCharacterization.characterize(File.open('foo.mkv'), :fits, :ffprobe)
|
44
|
+
#
|
42
45
|
# @param [String] content - The contents of the original file
|
43
46
|
# @param [String] filename - The original file's filename; Some
|
44
47
|
# characterization tools take hints from the file names
|
@@ -55,16 +58,13 @@ module Hydra
|
|
55
58
|
# key to the yieldparam with a value, that value will be used as the path
|
56
59
|
#
|
57
60
|
# @see Hydra::FileCharacterization.configure
|
58
|
-
def self.characterize(
|
59
|
-
|
61
|
+
def self.characterize(*args)
|
62
|
+
content, filename, tool_names = extract_arguments(args)
|
60
63
|
tool_names = Array(tool_names).flatten.compact
|
61
64
|
custom_paths = {}
|
62
65
|
yield(custom_paths) if block_given?
|
63
|
-
|
64
|
-
|
65
|
-
tool_outputs << FileCharacterization.characterize_with(tool_name, f.path, custom_paths[tool_name])
|
66
|
-
end
|
67
|
-
end
|
66
|
+
|
67
|
+
tool_outputs = run_characterizers(content, filename, tool_names, custom_paths)
|
68
68
|
tool_names.size == 1 ? tool_outputs.first : tool_outputs
|
69
69
|
end
|
70
70
|
|
@@ -73,6 +73,45 @@ module Hydra
|
|
73
73
|
yield(configuration)
|
74
74
|
end
|
75
75
|
|
76
|
+
private
|
77
|
+
|
78
|
+
# Break up a list of arguments into two possible lists:
|
79
|
+
# option1: [String] content, [String] filename, [Array] tool_names
|
80
|
+
# option2: [File] content, [Array] tool_names
|
81
|
+
# In the case of option2, derive the filename from the file's path
|
82
|
+
# @return [String, File], [String], [Array]
|
83
|
+
def self.extract_arguments(args)
|
84
|
+
content = args.shift
|
85
|
+
filename = if content.is_a? File
|
86
|
+
File.basename(content.path)
|
87
|
+
else
|
88
|
+
args.shift
|
89
|
+
end
|
90
|
+
tool_names = args
|
91
|
+
return content, filename, tool_names
|
92
|
+
end
|
93
|
+
|
94
|
+
# @param [File, String] content Either an open file or a string. If a string is passed
|
95
|
+
# a temp file will be created
|
96
|
+
# @param [String] filename Used in creating a temp file name
|
97
|
+
# @param [Array<Symbol>] tool_names A list of symbols referencing the characerization tools to run
|
98
|
+
# @param [Hash] custom_paths The paths to the executables of the tool.
|
99
|
+
def self.run_characterizers(content, filename, tool_names, custom_paths)
|
100
|
+
if content.is_a? File
|
101
|
+
run_characterizers_on_file(content, tool_names, custom_paths)
|
102
|
+
else
|
103
|
+
FileCharacterization::ToTempFile.open(filename, content) do |f|
|
104
|
+
run_characterizers_on_file(f, tool_names, custom_paths)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
def self.run_characterizers_on_file(f, tool_names, custom_paths)
|
110
|
+
tool_names.map do |tool_name|
|
111
|
+
FileCharacterization.characterize_with(tool_name, f.path, custom_paths[tool_name])
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
76
115
|
class Configuration
|
77
116
|
def tool_path(tool_name, tool_path)
|
78
117
|
Hydra::FileCharacterization.characterizer(tool_name).tool_path = tool_path
|
@@ -80,4 +119,4 @@ module Hydra
|
|
80
119
|
end
|
81
120
|
|
82
121
|
end
|
83
|
-
end
|
122
|
+
end
|
@@ -27,11 +27,15 @@ module Hydra::FileCharacterization
|
|
27
27
|
end
|
28
28
|
|
29
29
|
def tool_path
|
30
|
-
@tool_path || self.class.tool_path ||
|
30
|
+
@tool_path || self.class.tool_path || convention_based_tool_name
|
31
31
|
end
|
32
32
|
|
33
33
|
protected
|
34
34
|
|
35
|
+
def convention_based_tool_name
|
36
|
+
self.class.name.split("::").last.downcase
|
37
|
+
end
|
38
|
+
|
35
39
|
def internal_call
|
36
40
|
stdin, stdout, stderr, wait_thr = popen3(command)
|
37
41
|
begin
|
@@ -45,11 +45,7 @@ module Hydra::FileCharacterization
|
|
45
45
|
end
|
46
46
|
|
47
47
|
context 'without a specified tool_path' do
|
48
|
-
|
49
|
-
expect {
|
50
|
-
subject.tool_path
|
51
|
-
}.to raise_error(Hydra::FileCharacterization::UnspecifiedToolPathError)
|
52
|
-
end
|
48
|
+
its(:tool_path) { should eq 'characterizer' }
|
53
49
|
end
|
54
50
|
end
|
55
51
|
end
|
@@ -7,56 +7,67 @@ module Hydra
|
|
7
7
|
describe FileCharacterization do
|
8
8
|
|
9
9
|
describe '.characterize' do
|
10
|
-
|
11
|
-
|
12
|
-
|
10
|
+
describe "for content in memory" do
|
11
|
+
let(:content) { "class Test; end\n" }
|
12
|
+
let(:filename) { 'test.rb' }
|
13
|
+
subject { Hydra::FileCharacterization.characterize(content, filename, tool_names) }
|
13
14
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
15
|
+
describe 'for fits' do
|
16
|
+
let(:tool_names) { [:fits] }
|
17
|
+
it { should match(/#{'<identity format="Plain text" mimetype="text/plain"'}/) }
|
18
|
+
end
|
18
19
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
20
|
+
describe 'with configured path' do
|
21
|
+
it {
|
22
|
+
response = Hydra::FileCharacterization.characterize(content, filename, :fits) do |config|
|
23
|
+
config[:fits] = `which fits || which fits.sh`.strip
|
24
|
+
end
|
25
|
+
expect(response).to match(/#{'<identity format="Plain text" mimetype="text/plain"'}/)
|
26
|
+
}
|
27
|
+
end
|
27
28
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
29
|
+
describe 'with multiple runs' do
|
30
|
+
it {
|
31
|
+
response_1, response_2, response_3 = Hydra::FileCharacterization.characterize(content, filename, :fits, :fits)
|
32
|
+
expect(response_1).to match(/#{'<identity format="Plain text" mimetype="text/plain"'}/)
|
33
|
+
expect(response_2).to match(/#{'<identity format="Plain text" mimetype="text/plain"'}/)
|
34
|
+
expect(response_3).to be_nil
|
35
|
+
}
|
36
|
+
end
|
36
37
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
38
|
+
describe 'for a bogus tool' do
|
39
|
+
let(:tool_names) { [:cookie_monster] }
|
40
|
+
it {
|
41
|
+
expect {
|
42
|
+
subject
|
43
|
+
}.to raise_error(Hydra::FileCharacterization::ToolNotFoundError)
|
44
|
+
}
|
45
|
+
end
|
45
46
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
47
|
+
describe 'for a mix of bogus and valid tools' do
|
48
|
+
let(:tool_names) { [:fits, :cookie_monster] }
|
49
|
+
it {
|
50
|
+
expect {
|
51
|
+
subject
|
52
|
+
}.to raise_error(Hydra::FileCharacterization::ToolNotFoundError)
|
53
|
+
}
|
54
|
+
end
|
54
55
|
|
55
|
-
|
56
|
-
|
57
|
-
|
56
|
+
describe 'for no tools' do
|
57
|
+
let(:tool_names) { nil }
|
58
|
+
it { should eq [] }
|
59
|
+
end
|
58
60
|
end
|
59
61
|
|
62
|
+
describe "for a file on disk" do
|
63
|
+
let(:file) { File.open(fixture_file('brendan_behan.jpeg')) }
|
64
|
+
subject { Hydra::FileCharacterization.characterize(file, tool_names) }
|
65
|
+
|
66
|
+
describe 'for fits' do
|
67
|
+
let(:tool_names) { [:fits] }
|
68
|
+
it { should match(/#{'<identity format="JPEG File Interchange Format" mimetype="image/jpeg"'}/) }
|
69
|
+
end
|
70
|
+
end
|
60
71
|
end
|
61
72
|
describe '.configure' do
|
62
73
|
let(:content) { "class Test; end\n" }
|
@@ -71,12 +82,11 @@ module Hydra
|
|
71
82
|
Hydra::FileCharacterization.configure do |config|
|
72
83
|
config.tool_path(:fits, nil)
|
73
84
|
end
|
85
|
+
response = Hydra::FileCharacterization.characterize(content, filename, :fits)
|
86
|
+
expect(response).to match(/#{'<identity format="Plain text" mimetype="text/plain"'}/)
|
74
87
|
|
75
|
-
expect {
|
76
|
-
Hydra::FileCharacterization.characterize(content, filename, :fits)
|
77
|
-
}.to raise_error(Hydra::FileCharacterization::UnspecifiedToolPathError)
|
78
88
|
end
|
79
89
|
end
|
80
90
|
|
81
91
|
end
|
82
|
-
end
|
92
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hydra-file_characterization
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Treacy
|
@@ -11,7 +11,7 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date: 2013-10-
|
14
|
+
date: 2013-10-24 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: activesupport
|