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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3359fdef2f631bbd7c3af4ac3c56062c6fbb391a
4
- data.tar.gz: daa36c8addb90aec847568e30c3d98512fddb783
3
+ metadata.gz: e4919a2449be3e3a6127fdc17a5f8552e3b64821
4
+ data.tar.gz: 1ab2ab15d03a9e4ddfd83b15e2f78a2be5548d86
5
5
  SHA512:
6
- metadata.gz: 11a66dc69441eb624743bfdd49f851aa88fa15d8463dad586472c5463fc1141a5a23db496b0ce32023f2b658a91fbefe96ac1d30d58e4b5b53dda59e716b8692
7
- data.tar.gz: 362f5bfc8cb21590d9290ef18ad6d56cea968d5959bd47e7e1ec2ebaeba0cbc2af7d5e611a8802c50d537f7fca9bfc1e6f3bc1ab7dc2f7265988951b9520e3cb
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(content, filename, *tool_names)
59
- tool_outputs = []
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
- FileCharacterization::ToTempFile.open(filename, content) do |f|
64
- tool_names.each do |tool_name|
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 || (raise Hydra::FileCharacterization::UnspecifiedToolPathError.new(self.class))
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
@@ -1,11 +1,5 @@
1
1
  module Hydra::FileCharacterization
2
2
 
3
- class UnspecifiedToolPathError < RuntimeError
4
- def initialize(tool_class)
5
- super("Unspecified tool path for #{tool_class}")
6
- end
7
- end
8
-
9
3
  class FileNotFoundError < RuntimeError
10
4
  end
11
5
 
@@ -1,5 +1,5 @@
1
1
  module Hydra
2
2
  module FileCharacterization
3
- VERSION = "0.2.9"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
@@ -45,11 +45,7 @@ module Hydra::FileCharacterization
45
45
  end
46
46
 
47
47
  context 'without a specified tool_path' do
48
- it 'should raise Hydra::FileCharacterization::UnspecifiedToolPathError' do
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
- let(:content) { "class Test; end\n" }
11
- let(:filename) { 'test.rb' }
12
- subject { Hydra::FileCharacterization.characterize(content, filename, tool_names) }
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
- describe 'for fits' do
15
- let(:tool_names) { [:fits] }
16
- it { should match(/#{'<identity format="Plain text" mimetype="text/plain"'}/) }
17
- end
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
- describe 'with configured path' do
20
- it {
21
- response = Hydra::FileCharacterization.characterize(content, filename, :fits) do |config|
22
- config[:fits] = `which fits || which fits.sh`.strip
23
- end
24
- expect(response).to match(/#{'<identity format="Plain text" mimetype="text/plain"'}/)
25
- }
26
- end
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
- describe 'with multiple runs' do
29
- it {
30
- response_1, response_2, response_3 = Hydra::FileCharacterization.characterize(content, filename, :fits, :fits)
31
- expect(response_1).to match(/#{'<identity format="Plain text" mimetype="text/plain"'}/)
32
- expect(response_2).to match(/#{'<identity format="Plain text" mimetype="text/plain"'}/)
33
- expect(response_3).to be_nil
34
- }
35
- end
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
- describe 'for a bogus tool' do
38
- let(:tool_names) { [:cookie_monster] }
39
- it {
40
- expect {
41
- subject
42
- }.to raise_error(Hydra::FileCharacterization::ToolNotFoundError)
43
- }
44
- end
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
- describe 'for a mix of bogus and valid tools' do
47
- let(:tool_names) { [:fits, :cookie_monster] }
48
- it {
49
- expect {
50
- subject
51
- }.to raise_error(Hydra::FileCharacterization::ToolNotFoundError)
52
- }
53
- end
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
- describe 'for no tools' do
56
- let(:tool_names) { nil }
57
- it { should eq [] }
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.2.9
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-12 00:00:00.000000000 Z
14
+ date: 2013-10-24 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: activesupport