Skip to content

Commit

Permalink
Enforce samvera style rules (#49)
Browse files Browse the repository at this point in the history
* Add bixby 3.0.0
* Run style checker
* Remove travis
  • Loading branch information
bess authored Jun 10, 2020
1 parent 302e2db commit 16c0c22
Show file tree
Hide file tree
Showing 25 changed files with 258 additions and 159 deletions.
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ jobs:
command: |
echo "export PATH=$HOME/fits:$PATH" >> $BASH_ENV
source $BASH_ENV
- samvera/rubocop
- samvera/parallel_rspec

workflows:
Expand Down
10 changes: 10 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
inherit_from: .rubocop_todo.yml

inherit_gem:
bixby: bixby_default.yml

AllCops:
TargetRubyVersion: 2.6
Exclude:
- spec/lib/hydra/file_characterization_spec.rb
- vendor/bundle/**/*
92 changes: 92 additions & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2020-06-10 16:20:03 -0400 using RuboCop version 0.85.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 1
# Configuration parameters: Include.
# Include: **/*.gemfile, **/Gemfile, **/gems.rb
Bundler/DuplicatedGem:
Exclude:
- 'Gemfile'

# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyleAlignWith, AutoCorrect, Severity.
# SupportedStylesAlignWith: keyword, variable, start_of_line
Layout/EndAlignment:
Exclude:
- 'lib/hydra/file_characterization.rb'

# Offense count: 6
# Cop supports --auto-correct.
# Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Layout/LineLength:
Max: 293

# Offense count: 5
# Configuration parameters: CountComments, ExcludedMethods.
# ExcludedMethods: refine
Metrics/BlockLength:
Max: 50

# Offense count: 1
# Configuration parameters: CountComments, ExcludedMethods.
Metrics/MethodLength:
Max: 15

# Offense count: 1
# Configuration parameters: ExpectMatchingDefinition, CheckDefinitionPathHierarchy, Regex, IgnoreExecutableScripts, AllowedAcronyms.
# AllowedAcronyms: CLI, DSL, ACL, API, ASCII, CPU, CSS, DNS, EOF, GUID, HTML, HTTP, HTTPS, ID, IP, JSON, LHS, QPS, RAM, RHS, RPC, SLA, SMTP, SQL, SSH, TCP, TLS, TTL, UDP, UI, UID, UUID, URI, URL, UTF8, VM, XML, XMPP, XSRF, XSS
Naming/FileName:
Exclude:
- '**/Gemfile'
- '**/*.rake'
- 'Capfile'
- 'config/deploy/*'
- 'lib/hydra-file_characterization.rb'

# Offense count: 2
# Configuration parameters: Max.
RSpec/ExampleLength:
Exclude:
- 'spec/lib/hydra/file_characterization/to_temp_file_spec.rb'

# Offense count: 6
# Configuration parameters: AssignmentOnly.
RSpec/InstanceVariable:
Exclude:
- 'spec/lib/hydra/file_characterization/to_temp_file_spec.rb'

# Offense count: 4
# Configuration parameters: .
# SupportedStyles: have_received, receive
RSpec/MessageSpies:
EnforcedStyle: receive

# Offense count: 9
# Configuration parameters: IgnoreSharedExamples.
RSpec/NamedSubject:
Exclude:
- 'spec/lib/hydra/file_characterization/characterizer_spec.rb'
- 'spec/lib/hydra/file_characterization/characterizers/ffprobe_spec.rb'
- 'spec/lib/hydra/file_characterization/characterizers/fit_servlet_spec.rb'
- 'spec/lib/hydra/file_characterization/characterizers/fits_spec.rb'
- 'spec/lib/hydra/file_characterization/characterizers_spec.rb'
- 'spec/lib/hydra/file_characterization/to_temp_file_spec.rb'

# Offense count: 1
# Configuration parameters: AllowedChars.
Style/AsciiComments:
Exclude:
- 'lib/hydra/file_characterization.rb'

# Offense count: 1
# Configuration parameters: MinBodyLength.
Style/GuardClause:
Exclude:
- 'lib/hydra/file_characterization/characterizers.rb'
9 changes: 0 additions & 9 deletions .travis.yml

This file was deleted.

1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
source 'https://rubygems.org'

# Specify your gem's dependencies in hydra/file_characterization.gemspec
Expand Down
1 change: 1 addition & 0 deletions Guardfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
# A sample Guardfile
# More info at https://github.com/guard/guard#readme

Expand Down
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require "bundler/gem_tasks"
require 'rspec/core'
require 'rspec/core/rake_task'
Expand Down
12 changes: 7 additions & 5 deletions hydra-file_characterization.gemspec
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- encoding: utf-8 -*-
# frozen_string_literal: true
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'hydra/file_characterization/version'
Expand All @@ -12,23 +13,24 @@ Gem::Specification.new do |gem|
"Sue Richeson",
"Rajesh Balekai"
]
gem.email = [
gem.email = [
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]"
]
gem.description = %q{To provide a wrapper for file characterization}
gem.summary = %q{To provide a wrapper for file characterization}
gem.description = 'To provide a wrapper for file characterization'
gem.summary = 'To provide a wrapper for file characterization'
gem.homepage = "https://github.com/projecthydra/hydra-file_characterization"
gem.license = "APACHE2"

gem.files = `git ls-files`.split($/)
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
gem.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.require_paths = ["lib"]

gem.add_dependency "activesupport", ">= 3.0.0"
gem.add_development_dependency 'bixby', '~> 3.0.0'
gem.add_development_dependency 'coveralls'
gem.add_development_dependency 'github_changelog_generator'
gem.add_development_dependency "guard"
Expand Down
1 change: 1 addition & 0 deletions lib/hydra-file_characterization.rb
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# frozen_string_literal: true
require "hydra/file_characterization"
68 changes: 32 additions & 36 deletions lib/hydra/file_characterization.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require "hydra/file_characterization/version"
require "hydra/file_characterization/exceptions"
require "hydra/file_characterization/to_temp_file"
Expand All @@ -6,7 +7,6 @@
require "active_support/configurable"

module Hydra

module_function

# A convenience method
Expand All @@ -15,7 +15,6 @@ def characterize(*args, &block)
end

module FileCharacterization

class << self
attr_accessor :configuration
end
Expand Down Expand Up @@ -66,7 +65,7 @@ def self.characterize(*args)
tool_names = Array(tool_names).flatten.compact
custom_paths = {}
yield(custom_paths) if block_given?

tool_outputs = run_characterizers(content, filename, tool_names, custom_paths)
tool_names.size == 1 ? tool_outputs.first : tool_outputs
end
Expand All @@ -76,50 +75,47 @@ def self.configure
yield(configuration)
end

private

# Break up a list of arguments into two possible lists:
# option1: [String] content, [String] filename, [Array] tool_names
# option2: [File] content, [Array] tool_names
# In the case of option2, derive the filename from the file's path
# @return [String, File], [String], [Array]
def self.extract_arguments(args)
content = args.shift
filename = if content.is_a?(File) && !args[0].is_a?(String)
File.basename(content.path)
else
args.shift
end
tool_names = args
return content, filename, tool_names
# Break up a list of arguments into two possible lists:
# option1: [String] content, [String] filename, [Array] tool_names
# option2: [File] content, [Array] tool_names
# In the case of option2, derive the filename from the file's path
# @return [String, File], [String], [Array]
def self.extract_arguments(args)
content = args.shift
filename = if content.is_a?(File) && !args[0].is_a?(String)
File.basename(content.path)
else
args.shift
end
tool_names = args
[content, filename, tool_names]
end

# @param [File, String] content Either an open file or a string. If a string is passed
# a temp file will be created
# @param [String] filename Used in creating a temp file name
# @param [Array<Symbol>] tool_names A list of symbols referencing the characerization tools to run
# @param [Hash] custom_paths The paths to the executables of the tool.
def self.run_characterizers(content, filename, tool_names, custom_paths)
if content.is_a? File
run_characterizers_on_file(content, tool_names, custom_paths)
else
FileCharacterization::ToTempFile.open(filename, content) do |f|
run_characterizers_on_file(f, tool_names, custom_paths)
end
# @param [File, String] content Either an open file or a string. If a string is passed
# a temp file will be created
# @param [String] filename Used in creating a temp file name
# @param [Array<Symbol>] tool_names A list of symbols referencing the characerization tools to run
# @param [Hash] custom_paths The paths to the executables of the tool.
def self.run_characterizers(content, filename, tool_names, custom_paths)
if content.is_a? File
run_characterizers_on_file(content, tool_names, custom_paths)
else
FileCharacterization::ToTempFile.open(filename, content) do |f|
run_characterizers_on_file(f, tool_names, custom_paths)
end
end
end

def self.run_characterizers_on_file(f, tool_names, custom_paths)
tool_names.map do |tool_name|
FileCharacterization.characterize_with(tool_name, f.path, custom_paths[tool_name])
end
def self.run_characterizers_on_file(f, tool_names, custom_paths)
tool_names.map do |tool_name|
FileCharacterization.characterize_with(tool_name, f.path, custom_paths[tool_name])
end
end

class Configuration
def tool_path(tool_name, tool_path)
Hydra::FileCharacterization.characterizer(tool_name).tool_path = tool_path
end
end

end
end
71 changes: 35 additions & 36 deletions lib/hydra/file_characterization/characterizer.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'hydra/file_characterization/exceptions'
require 'open3'
require 'active_support/core_ext/class/attribute'
Expand All @@ -15,9 +16,7 @@ def initialize(filename, tool_path = nil)
end

def call
unless File.exists?(filename)
raise Hydra::FileCharacterization::FileNotFoundError.new("File: #{filename} does not exist.")
end
raise Hydra::FileCharacterization::FileNotFoundError, "File: #{filename} does not exist." unless File.exist?(filename)

post_process(output)
end
Expand All @@ -32,47 +31,47 @@ def logger

protected

# Override this method if you want your processor to mutate the
# raw output
def post_process(raw_output)
raw_output
end
# Override this method if you want your processor to mutate the
# raw output
def post_process(raw_output)
raw_output
end

def convention_based_tool_name
self.class.name.split("::").last.downcase
end
def convention_based_tool_name
self.class.name.split("::").last.downcase
end

def internal_call
stdin, stdout, stderr, wait_thr = popen3(command)
begin
out = stdout.read
err = stderr.read
exit_status = wait_thr.value
raise "Unable to execute command \"#{command}\"\n#{err}" unless exit_status.success?
out
ensure
stdin.close
stdout.close
stderr.close
end
def internal_call
stdin, stdout, stderr, wait_thr = popen3(command)
begin
out = stdout.read
err = stderr.read
exit_status = wait_thr.value
raise "Unable to execute command \"#{command}\"\n#{err}" unless exit_status.success?
out
ensure
stdin.close
stdout.close
stderr.close
end
end

def command
raise NotImplementedError, "Method #command should be overriden in child classes"
end
def command
raise NotImplementedError, "Method #command should be overriden in child classes"
end

private

def output
if tool_path.respond_to?(:call)
tool_path.call(filename)
else
internal_call
end
def output
if tool_path.respond_to?(:call)
tool_path.call(filename)
else
internal_call
end
end

def activefedora_logger
ActiveFedora::Base.logger if defined? ActiveFedora
end
def activefedora_logger
ActiveFedora::Base.logger if defined? ActiveFedora
end
end
end
Loading

0 comments on commit 16c0c22

Please sign in to comment.