format_parser 0.9.4 → 0.10.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
- SHA1:
3
- metadata.gz: 1ea761c606eb23bb10fd39f158afa2a628930018
4
- data.tar.gz: eda078a306d8ded4dbdbdb4570608fe99b80b3e8
2
+ SHA256:
3
+ metadata.gz: d4253990f4ed6f05476ba982726932b0298efee7da4a5267980eb176f12d6e68
4
+ data.tar.gz: 74683875ccfeeed68fc7cdf13af16a7694b81ada533f9138165c26ff6a3e0cfa
5
5
  SHA512:
6
- metadata.gz: 5596e607f371f5b05503d25fc749964af5dd3b945a3ec5011efcb4262a03c3325ea344a00e103cea13cf76e85bea41f10d4633d7f6f3a67dfeb17a7cbe04f2ea
7
- data.tar.gz: d25fff95fbb51bd7b414e3e29104b798a8b5d6f11e1a4711266c16cadd532a436463be6ad0414acdda7904562a196b262738483a05e7cfa73d2138ebb4022200
6
+ metadata.gz: 19701f96beaeee111eaa2d9381b833a13a0954ea1e6deab9dc7095021ae95c809bf4a96abe04b2cbbe10d9d21902b3bdb5520e162151894931365f2bc188aef8
7
+ data.tar.gz: bf7b0fe93336654c06d58a852f88c89725974759ed30da89575b3559be5c4658ef60c3def92b50f1a4afaca3659996b0f79a9b245e7a0e9f1161b0fc05feb68e
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 0.10.0
2
+ * Adds the `format_parser_inspect` binary for parsing a file from the commandline
3
+ and returning results in JSON
4
+ * Adds the `FormatParser.parse_at(path)` convenience method
5
+
1
6
  ## 0.9.4
2
7
  * Fix a TIFF parsing regression introduced in 0.3.1 that led to all TIFFs being incorrectly parsed
3
8
 
@@ -0,0 +1,44 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '../lib/format_parser'
4
+ require 'json'
5
+ require 'optparse'
6
+
7
+ options = {results: :first}
8
+ OptionParser.new do |opts|
9
+ opts.banner = 'Usage: format_parser_inspect --first my_file.jpg'
10
+ opts.on('-a', '--all', 'Return all results instead of just the first one') do |_v|
11
+ options[:results] = :all
12
+ end
13
+ opts.on('--natures[=NATURES]', 'Only scan specific natures (comma-separated: image, audio)', Array) do |v|
14
+ options[:natures] = v.map { |e| e.strip.downcase.to_sym }
15
+ end
16
+ opts.on('--formats[=FORMATS]', 'Only scan specific formats (comma-separated: jpg, tif)', Array) do |v|
17
+ options[:formats] = v.map { |e| e.strip.downcase.to_sym }
18
+ end
19
+ end.parse!
20
+
21
+ did_detect = false
22
+ return_values = ARGV.map do |path_or_url|
23
+ method_name = path_or_url =~ /^http(s?)\:\/\// ? :parse_http : :parse_file_at
24
+ result_or_results = FormatParser.public_send(method_name, path_or_url, **options)
25
+ if options[:results] != :first
26
+ did_detect = true if result_or_results.any?
27
+ {
28
+ source_path_or_url: path_or_url,
29
+ options: options,
30
+ ambiguous: result_or_results.length > 1,
31
+ results: result_or_results,
32
+ }
33
+ else
34
+ did_detect = true if result_or_results
35
+ {
36
+ source_path_or_url: path_or_url,
37
+ options: options,
38
+ result: result_or_results,
39
+ }
40
+ end
41
+ end
42
+
43
+ puts JSON.pretty_generate(return_values)
44
+ did_detect ? exit(0) : exit(1)
data/lib/format_parser.rb CHANGED
@@ -72,6 +72,18 @@ module FormatParser
72
72
  parse(RemoteIO.new(url), **kwargs)
73
73
  end
74
74
 
75
+ # Parses the file at the given `path` and returns the results as if it were any IO
76
+ # given to `.parse`. The accepted keyword arguments are the same as the ones for `parse`.
77
+ #
78
+ # @param path[String] the path to the file to parse on the local filesystem
79
+ # @param kwargs the keyword arguments to be delegated to `.parse`
80
+ # @see {.parse}
81
+ def self.parse_file_at(path, **kwargs)
82
+ File.open(path, 'rb') do |io|
83
+ parse(io, **kwargs)
84
+ end
85
+ end
86
+
75
87
  # Parses the resource contained in the given IO-ish object, and returns either the first matched
76
88
  # result (omitting all the other parsers), the first N results or all results.
77
89
  #
@@ -1,3 +1,3 @@
1
1
  module FormatParser
2
- VERSION = '0.9.4'
2
+ VERSION = '0.10.0'
3
3
  end
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+ require 'shellwords'
3
+
4
+ describe '/exe/format_parser_inspect binary' do
5
+ let(:bin_path) do
6
+ Shellwords.escape(File.expand_path(__dir__ + '/../exe/format_parser_inspect'))
7
+ end
8
+
9
+ it 'performs parsing on full default' do
10
+ fixture_path = fixtures_dir + 'JPEG/divergent_pixel_dimensions_exif.jpg'
11
+
12
+ result = `#{bin_path} #{Shellwords.escape(fixture_path)}`
13
+
14
+ retval = JSON.parse(result, symbolize_names: true)
15
+ parsed_result = retval.first
16
+ expect(parsed_result[:source_path_or_url]).to end_with('divergent_pixel_dimensions_exif.jpg')
17
+ expect(parsed_result[:options][:results]).to eq('first')
18
+ expect(parsed_result[:result]).not_to be_nil
19
+ end
20
+
21
+ it 'performs parsing with --all' do
22
+ fixture_path = fixtures_dir + 'JPEG/divergent_pixel_dimensions_exif.jpg'
23
+
24
+ result = `#{bin_path} --all #{Shellwords.escape(fixture_path)}`
25
+
26
+ retval = JSON.parse(result, symbolize_names: true)
27
+ parsed_result = retval.first
28
+ expect(parsed_result[:source_path_or_url]).to end_with('divergent_pixel_dimensions_exif.jpg')
29
+ expect(parsed_result[:options][:results]).to eq('all')
30
+ expect(parsed_result[:ambiguous]).to eq(false)
31
+ expect(parsed_result[:results]).not_to be_empty
32
+ end
33
+
34
+ it 'performs parsing with --natures option' do
35
+ fixture_path = fixtures_dir + 'JPEG/divergent_pixel_dimensions_exif.jpg'
36
+
37
+ result = `#{bin_path} --natures=IMAGE #{Shellwords.escape(fixture_path)}`
38
+
39
+ retval = JSON.parse(result, symbolize_names: true)
40
+ parsed_result = retval.first
41
+ expect(parsed_result[:source_path_or_url]).to end_with('divergent_pixel_dimensions_exif.jpg')
42
+ expect(parsed_result[:options][:natures]).to eq(['image'])
43
+ expect(parsed_result[:result]).not_to be_nil
44
+ end
45
+
46
+ it 'performs parsing with --formats option' do
47
+ fixture_path = fixtures_dir + 'JPEG/divergent_pixel_dimensions_exif.jpg'
48
+
49
+ result = `#{bin_path} --formats=zip #{Shellwords.escape(fixture_path)}`
50
+
51
+ retval = JSON.parse(result, symbolize_names: true)
52
+ parsed_result = retval.first
53
+ expect(parsed_result[:source_path_or_url]).to end_with('divergent_pixel_dimensions_exif.jpg')
54
+ expect(parsed_result[:options][:formats]).to eq(['zip'])
55
+ expect(parsed_result[:result]).to be_nil
56
+ end
57
+ end
@@ -47,7 +47,7 @@ describe FormatParser do
47
47
  let(:audio) { FormatParser::Audio.new(format: :aiff, num_audio_channels: 1) }
48
48
  let(:image) { FormatParser::Image.new(format: :dpx, width_px: 1, height_px: 1) }
49
49
 
50
- context '#parse called hash options' do
50
+ context '.parse called with options' do
51
51
  before do
52
52
  expect_any_instance_of(FormatParser::AIFFParser).to receive(:call).and_return(audio)
53
53
  expect_any_instance_of(FormatParser::DPXParser).to receive(:call).and_return(image)
@@ -59,7 +59,7 @@ describe FormatParser do
59
59
  it { is_expected.to include(audio) }
60
60
  end
61
61
 
62
- context '#parse called without hash options' do
62
+ context '.parse called without hash options' do
63
63
  before do
64
64
  expect_any_instance_of(FormatParser::DPXParser).to receive(:call).and_return(image)
65
65
  end
@@ -70,6 +70,14 @@ describe FormatParser do
70
70
  end
71
71
  end
72
72
 
73
+ describe '.parse_file_at' do
74
+ it 'parses a fixture when given a path to it' do
75
+ path = fixtures_dir + '/WAV/c_M1F1-Alaw-AFsp.wav'
76
+ result = FormatParser.parse_file_at(path)
77
+ expect(result.nature).to eq(:audio)
78
+ end
79
+ end
80
+
73
81
  describe 'when parsing fixtures' do
74
82
  Dir.glob(fixtures_dir + '/**/*.*').sort.each do |fixture_path|
75
83
  it "parses #{fixture_path} without raising any errors" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: format_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.4
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Noah Berman
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2018-04-22 00:00:00.000000000 Z
12
+ date: 2018-04-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: ks
@@ -144,7 +144,8 @@ description: |-
144
144
  email:
145
145
  - noah@noahberman.org
146
146
  - me@julik.nl
147
- executables: []
147
+ executables:
148
+ - format_parser_inspect
148
149
  extensions: []
149
150
  extra_rdoc_files: []
150
151
  files:
@@ -159,6 +160,7 @@ files:
159
160
  - LICENSE.txt
160
161
  - README.md
161
162
  - Rakefile
163
+ - exe/format_parser_inspect
162
164
  - format_parser.gemspec
163
165
  - lib/archive.rb
164
166
  - lib/attributes_json.rb
@@ -201,6 +203,7 @@ files:
201
203
  - spec/care_spec.rb
202
204
  - spec/esoteric_formats_spec.rb
203
205
  - spec/file_information_spec.rb
206
+ - spec/format_parser_inspect_spec.rb
204
207
  - spec/format_parser_spec.rb
205
208
  - spec/io_utils_spec.rb
206
209
  - spec/measurometer_spec.rb
@@ -247,7 +250,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
247
250
  version: '0'
248
251
  requirements: []
249
252
  rubyforge_project:
250
- rubygems_version: 2.5.2
253
+ rubygems_version: 2.7.3
251
254
  signing_key:
252
255
  specification_version: 4
253
256
  summary: A library for efficient parsing of file metadata