Skip to content

Commit

Permalink
Fix whitespace related offenses
Browse files Browse the repository at this point in the history
  • Loading branch information
tagliala committed Sep 12, 2024
1 parent 721fd24 commit 1bcc850
Show file tree
Hide file tree
Showing 37 changed files with 236 additions and 235 deletions.
16 changes: 8 additions & 8 deletions bin/autoheathen
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ mode = :return_to_sender
mail_to = nil

OptionParser.new do |opts|
opts.on( '-r', '--return-to-sender', 'Converted files will be emailed back to sender' ) { mode = :return_to_sender }
opts.on( '-t', '--to EMAIL', 'Converted files will be emailed to this address' ) { |e| mode = :email; mail_to = e }
opts.on( '-s', '--summary', 'Don\'t deposit the converted file, just log a summary' ) { cfg[:deliver] = false }
opts.on( '-l', '--language', 'Document language' ) { |l| cfg[:language] = l }
opts.on( '-M', '--mail-host MAILHOST', 'Mail server for sending replies' ) { |h| cfg[:mail_host] = h }
opts.on( '-P', '--mail-port PORT', Integer, 'Mail server port' ) { |p| cfg[:mail_port] = p }
opts.on( '-C', '--config FILE', 'Configuration YAML file' ) { |file| cfg[:config_file] = file }
opts.on( '-v', '--verbose', 'Running commentary' ) { cfg[:logger] = Logger.new(STDOUT) }
opts.on('-r', '--return-to-sender', 'Converted files will be emailed back to sender') { mode = :return_to_sender }
opts.on('-t', '--to EMAIL', 'Converted files will be emailed to this address') { |e| mode = :email; mail_to = e }
opts.on('-s', '--summary', 'Don\'t deposit the converted file, just log a summary') { cfg[:deliver] = false }
opts.on('-l', '--language', 'Document language') { |l| cfg[:language] = l }
opts.on('-M', '--mail-host MAILHOST', 'Mail server for sending replies') { |h| cfg[:mail_host] = h }
opts.on('-P', '--mail-port PORT', Integer, 'Mail server port') { |p| cfg[:mail_port] = p }
opts.on('-C', '--config FILE', 'Configuration YAML file') { |file| cfg[:config_file] = file }
opts.on('-v', '--verbose', 'Running commentary') { cfg[:logger] = Logger.new(STDOUT) }
end.parse!

email = Mail.read_from_string $stdin.read
Expand Down
23 changes: 11 additions & 12 deletions bin/cvheathen
Original file line number Diff line number Diff line change
Expand Up @@ -22,38 +22,37 @@ verbose = false
mode = :file

OptionParser.new do |opts|
opts.on( '-a', '--action ACTION', 'Action to perform' ) { |a| action = a }
opts.on( '-f', '--file FILENAME', 'Input file or directory' ) { |f| in_file = Pathname.new(f) }
opts.on( '-o', '--outfile FILENAME', 'Output file or directory' ) { |f| out_file = Pathname.new(f) }
opts.on( '-r', '--recurse', 'Recurse through input directory' ) { recurse = true }
opts.on( '-l', '--language LANG', 'Language of the input file(s)' ) { |l| language = l }
opts.on( '-v', '--verbose', 'Verbose output' ) { verbose = true }
opts.on( '-h', '--help', 'This message' ) { puts opts; exit }
opts.on('-a', '--action ACTION', 'Action to perform') { |a| action = a }
opts.on('-f', '--file FILENAME', 'Input file or directory') { |f| in_file = Pathname.new(f) }
opts.on('-o', '--outfile FILENAME', 'Output file or directory') { |f| out_file = Pathname.new(f) }
opts.on('-r', '--recurse', 'Recurse through input directory') { recurse = true }
opts.on('-l', '--language LANG', 'Language of the input file(s)') { |l| language = l }
opts.on('-v', '--verbose', 'Verbose output') { verbose = true }
opts.on('-h', '--help', 'This message') { puts opts; exit }
end.parse!

logger = Logger.new( verbose ? STDOUT : nil )
logger = Logger.new(verbose ? STDOUT : nil)
converter = Heathen::Converter.new logger: logger

files = []

if in_file.file?
abort "Output is a directory, but expected a file" if out_file.directory?
content = converter.convert action, File.read(in_file), language
File.open( out_file, 'wb' ) { |f| f.write content }
File.open(out_file, 'wb') { |f| f.write content }
elsif in_file.directory?
abort "Invalid output directory" unless out_file.directory?
Dir.glob( in_file + ( recurse ? '**/*' : '*') ).each do |file|
Dir.glob(in_file + (recurse ? '**/*' : '*')).each do |file|
next unless Pathname.new(file).file?
begin
content = converter.convert action, File.read(file), language
new_file = Heathen::Filename.suggest_in_new_dir file, content.mime_type, in_file.to_s, out_file.to_s
Pathname.new(new_file).parent.mkpath
File.open( new_file, 'wb' ) { |f| f.write content }
File.open(new_file, 'wb') { |f| f.write content }
rescue StandardError => e
logger.error "Failed to convert #{file}: #{e.message}"
end
end
else
abort "Invalid input file" unless in_file.file?
end

8 changes: 4 additions & 4 deletions bin/docpath
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ storage_dir = nil
config_file = BASE + 'config' + 'app.yml'

OptionParser.new do |opts|
opts.on( '-a', '--app APP', 'Name of the app' ) { |a| app = a }
opts.on( '-d', '--doc-id DOCID', 'Document ID' ) { |d| doc_id = d }
opts.on( '-s', '--storage-dir DIR', 'Storage directory' ) { |d| storage_dir = d }
opts.on( '-h', '--help', 'This message' ) { puts opts; exit }
opts.on('-a', '--app APP', 'Name of the app') { |a| app = a }
opts.on('-d', '--doc-id DOCID', 'Document ID') { |d| doc_id = d }
opts.on('-s', '--storage-dir DIR', 'Storage directory') { |d| storage_dir = d }
opts.on('-h', '--help', 'This message') { puts opts; exit }
opts.separator ''
opts.separator 'Examples:'
opts.separator ' docpath -a myapp -d 123456 -s /opt/storage'
Expand Down
2 changes: 1 addition & 1 deletion config.ru
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
require 'pathname'
require "sinatra"

BASE=Pathname.new(__FILE__).realpath.parent
BASE = Pathname.new(__FILE__).realpath.parent
$: << BASE
$: << BASE + 'lib'
require 'config/initializers/sidekiq'
Expand Down
44 changes: 22 additions & 22 deletions lib/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module Colore
class App < Sinatra::Base
set :backtrace, true
before do
@storage_dir = Pathname.new( C_.storage_directory )
@storage_dir = Pathname.new(C_.storage_directory)
@legacy_url_base = C_.legacy_url_base || url('/')
@logger = Logger.new(C_.conversion_log || STDOUT)
@errlog = Logger.new(C_.error_log || STDERR)
Expand Down Expand Up @@ -42,12 +42,12 @@ class App < Sinatra::Base
# - callback_url
# - file
# - author
put '/document/:app/:doc_id/:filename' do |app,doc_id,filename|
put '/document/:app/:doc_id/:filename' do |app, doc_id, filename|
begin
doc_key = DocKey.new app,doc_id
doc_key = DocKey.new app, doc_id
doc = Document.create @storage_dir, doc_key # will raise if doc exists
doc.title = params[:title] if params[:title]
call env.merge( 'REQUEST_METHOD'=>'POST' )
call env.merge('REQUEST_METHOD' => 'POST')
rescue StandardError => e
respond_with_error e
end
Expand All @@ -64,10 +64,10 @@ class App < Sinatra::Base
# - callback_url
# - file
# - author
post '/document/:app/:doc_id/:filename' do |app,doc_id,filename|
post '/document/:app/:doc_id/:filename' do |app, doc_id, filename|
begin
doc_key = DocKey.new app,doc_id
doc = Document.load( @storage_dir, doc_key )
doc_key = DocKey.new app, doc_id
doc = Document.load(@storage_dir, doc_key)
raise InvalidParameter.new :file unless params[:file]
version = doc.new_version do |version|
doc.add_file version, filename, params[:file][:tempfile], params[:author]
Expand All @@ -86,18 +86,18 @@ class App < Sinatra::Base
respond 201, "Document stored", {
app: app,
doc_id: doc_id,
path: doc.file_path( Colore::Document::CURRENT, filename ),
path: doc.file_path(Colore::Document::CURRENT, filename),
}
rescue StandardError => e
respond_with_error e
end
end

# Updates the document title
post '/document/:app/:doc_id/title/:title' do |app,doc_id,title|
post '/document/:app/:doc_id/title/:title' do |app, doc_id, title|
begin
doc_key = DocKey.new app,doc_id
doc = Document.load( @storage_dir, doc_key )
doc_key = DocKey.new app, doc_id
doc = Document.load(@storage_dir, doc_key)
doc.title = title
doc.save_metadata
respond 200, 'Title updated'
Expand All @@ -111,7 +111,7 @@ class App < Sinatra::Base
#
# POST params:
# - callback_url
post '/document/:app/:doc_id/:version/:filename/:action' do |app,doc_id,version,filename,action|
post '/document/:app/:doc_id/:version/:filename/:action' do |app, doc_id, version, filename, action|
begin
doc_key = DocKey.new app, doc_id
raise DocumentNotFound.new unless Document.exists? @storage_dir, doc_key
Expand All @@ -130,7 +130,7 @@ class App < Sinatra::Base
# DELETE params:
delete '/document/:app/:doc_id' do |app, doc_id|
begin
Document.delete @storage_dir, DocKey.new(app,doc_id)
Document.delete @storage_dir, DocKey.new(app, doc_id)
respond 200, 'Document deleted'
rescue StandardError => e
respond_with_error e
Expand All @@ -143,7 +143,7 @@ class App < Sinatra::Base
# DELETE params:
delete '/document/:app/:doc_id/:version' do |app, doc_id, version|
begin
doc = Document.load @storage_dir, DocKey.new(app,doc_id)
doc = Document.load @storage_dir, DocKey.new(app, doc_id)
doc.delete_version version
doc.save_metadata
respond 200, 'Document version deleted'
Expand All @@ -157,8 +157,8 @@ class App < Sinatra::Base
#
get '/document/:app/:doc_id/:version/:filename' do |app, doc_id, version, filename|
begin
doc = Document.load @storage_dir, DocKey.new(app,doc_id)
ctype, file = doc.get_file( version, filename )
doc = Document.load @storage_dir, DocKey.new(app, doc_id)
ctype, file = doc.get_file(version, filename)
content_type ctype
file
rescue StandardError => e
Expand All @@ -171,7 +171,7 @@ class App < Sinatra::Base
#
get '/document/:app/:doc_id' do |app, doc_id|
begin
doc = Document.load @storage_dir, DocKey.new(app,doc_id)
doc = Document.load @storage_dir, DocKey.new(app, doc_id)
respond 200, 'Information retrieved', doc.to_hash
rescue StandardError => e
respond_with_error e
Expand All @@ -196,7 +196,7 @@ class App < Sinatra::Base
end

body = params[:file][:tempfile].read
content = Converter.new(logger:@logger).convert_file( params[:action], body, params[:language] )
content = Converter.new(logger: @logger).convert_file(params[:action], body, params[:language])
content_type content.mime_type
content
rescue StandardError => e
Expand All @@ -215,12 +215,12 @@ class App < Sinatra::Base
post "/#{LegacyConverter::LEGACY}/convert" do
begin
body = if params[:file]
params[:file][:tempfile].read
params[:file][:tempfile].read
elsif params[:url]
Net::HTTP.get URI(params[:url])
else
raise DocumentNotFound.new "Please specify either 'file' or 'url' POST variable"
end
end
path = LegacyConverter.new.convert_file params[:action], body, params[:language]
converted_url = @legacy_url_base + path
content_type 'application/json'
Expand Down Expand Up @@ -252,7 +252,7 @@ class App < Sinatra::Base

helpers do
# Renders all responses (including errors) in a standard JSON format.
def respond status, message, extra={}
def respond status, message, extra = {}
case status
when Colore::Error
status = status.http_code
Expand Down Expand Up @@ -281,7 +281,7 @@ def respond_with_error error
end

# Renders all responses (including errors) in a standard JSON format.
def legacy_error status, message, extra={}
def legacy_error status, message, extra = {}
case status
when Error
status = status.http_code
Expand Down
4 changes: 2 additions & 2 deletions lib/autoheathen/config.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module AutoHeathen
module Config
def load_config defaults={}, config_file=nil, overwrites={}
def load_config defaults = {}, config_file = nil, overwrites = {}
cfg = symbolize_keys(defaults)
if config_file && File.exist?(config_file)
cfg.merge! symbolize_keys(YAML::load_file config_file)
Expand All @@ -10,7 +10,7 @@ def load_config defaults={}, config_file=nil, overwrites={}
end

def symbolize_keys(hash)
(hash||{}).inject({}){|result, (key, value)|
(hash || {}).inject({}) { |result, (key, value)|
new_key = key.is_a?(String) ? key.to_sym : key
new_value = value.is_a?(Hash) ? symbolize_keys(value) : value
result[new_key] = new_value
Expand Down
27 changes: 12 additions & 15 deletions lib/autoheathen/email_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class EmailProcessor
include AutoHeathen::Config

# The only valid email headers we will allow forward to LEG_wikilex
ONWARD_HEADERS = ['Date','From','To','Subject','Content-Type','Content-Transfer-Encoding','Mime-Version']
ONWARD_HEADERS = ['Date', 'From', 'To', 'Subject', 'Content-Type', 'Content-Transfer-Encoding', 'Mime-Version']

attr_reader :cfg, :logger

Expand All @@ -27,8 +27,8 @@ class EmailProcessor
# text_template: 'config/response.text.haml' Template for text part of response email (mode in [:return_to_sender,:email])
# html_template: 'config/response.html.haml' Template for HTML part of response email (ditto)
# logger: nil Optional logger object
def initialize cfg={}, config_file=nil
@cfg = load_config( { # defaults
def initialize cfg = {}, config_file = nil
@cfg = load_config({ # defaults
deliver: true,
language: 'en',
from: 'autoheathen',
Expand All @@ -40,7 +40,7 @@ def initialize cfg={}, config_file=nil
logger: nil,
text_template: 'config/autoheathen.text.haml',
html_template: 'config/autoheathen.html.haml',
}, config_file, cfg )
}, config_file, cfg)
@logger = @cfg[:logger] || Logger.new(nil)
@logger.level = @cfg[:verbose] ? Logger::DEBUG : Logger::INFO
end
Expand All @@ -52,7 +52,7 @@ def process_rts email
# Processes the given email, submits attachments to the Heathen server, delivers responses as configured
# @param email [String] The encoded email (suitable to be decoded using Mail.read(input))
# @return [Hash] a hash of the decoded attachments (or the reason why they could not be decoded)
def process email, mail_to, is_rts=false
def process email, mail_to, is_rts = false
documents = []

unless email.has_attachments?
Expand All @@ -67,7 +67,7 @@ def process email, mail_to, is_rts=false
#
email.attachments.each do |attachment|
begin
converter = Heathen::Converter.new( logger: logger )
converter = Heathen::Converter.new(logger: logger)
language = @cfg[:language]
input_source = attachment.body.decoded
action = get_action input_source.content_type
Expand Down Expand Up @@ -114,9 +114,9 @@ def deliver_onward email, documents, mail_to
# something weird goes on with Sharepoint, where the doc is dropped on the floor
# so, remove any offending headers
email.message_id = nil # make sure of message_id too
good_headers = ONWARD_HEADERS.map{ |h| h.downcase }
good_headers = ONWARD_HEADERS.map { |h| h.downcase }
inspect_headers = email.header.map(&:name)
inspect_headers .each do |name|
inspect_headers.each do |name|
unless good_headers.include? name.downcase
email.header[name] = nil
end
Expand All @@ -142,13 +142,13 @@ def deliver_rts email, documents, mail_to
mail.from @cfg[:from]
mail.to mail_to
# CCs to the original email will get a copy of the converted files as well
mail.cc (email.cc - email.to - (@cfg[:cc_blacklist]||[]) ) if email.cc # Prevent autoheathen infinite loop!
mail.cc (email.cc - email.to - (@cfg[:cc_blacklist] || [])) if email.cc # Prevent autoheathen infinite loop!
# Don't prepend yet another Re:
mail.subject "#{'Re: ' unless email.subject.start_with? 'Re:'}#{email.subject}"
# Construct received path
# TODO: is this in the right order?
#rcv = "by localhost(autoheathen); #{Time.now.strftime '%a, %d %b %Y %T %z'}"
#[email.received,rcv].flatten.each { |rec| mail.received rec.to_s }
# rcv = "by localhost(autoheathen); #{Time.now.strftime '%a, %d %b %Y %T %z'}"
# [email.received,rcv].flatten.each { |rec| mail.received rec.to_s }
mail.return_path email.return_path if email.return_path
mail.header['X-Received'] = email.header['X-Received'] if email.header['X-Received']
documents.each do |doc|
Expand Down Expand Up @@ -206,12 +206,9 @@ def get_action content_type
'application/vnd.ms-powerpoint' => 'pdf',
'application/vnd.openxmlformats-officedocument.presentationml.presentation' => 'pdf',
}[ct]
op = 'ocr' if ! op && ct.start_with?('image/')
op = 'ocr' if !op && ct.start_with?('image/')
raise "Conversion from #{ct} is not supported" unless op
op
end

end
end


2 changes: 1 addition & 1 deletion lib/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def self.config

def self.method_missing *args
if config.respond_to? args[0].to_sym
config.send( *args )
config.send(*args)
else
super
end
Expand Down
10 changes: 5 additions & 5 deletions lib/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def initialize storage_dir: C_.storage_directory, logger: Logger.new(C_.conversi
# @return the converted file name
def convert doc_key, version, filename, action
doc = Document.load @storage_dir, doc_key
ignore, orig_content = doc.get_file( version, filename)
ignore, orig_content = doc.get_file(version, filename)
language = 'en' # TODO - add to spec and upload
new_content = convert_file action, orig_content, language
# TODO - handling for variant formats with the same extension
Expand All @@ -36,12 +36,12 @@ def convert doc_key, version, filename, action
# @param orig_content [String] the body of the file to convert
# @param language [String] the file's language
# @return [String] the converted file body
def convert_file action, orig_content, language='en'
Heathen::Converter.new(logger:@logger).convert(action, orig_content, language)
def convert_file action, orig_content, language = 'en'
Heathen::Converter.new(logger: @logger).convert(action, orig_content, language)
rescue Heathen::TaskNotFound => e
raise InvalidAction.new( e.message )
raise InvalidAction.new(e.message)
rescue Heathen::Error => e
raise ConversionError.new( e )
raise ConversionError.new(e)
end
end
end
2 changes: 1 addition & 1 deletion lib/doc_key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def to_s
# subdirectory names into which the docs can be distributed, rather than clumping
# them into a single base directory (which will not scale well)
def subdirectory
Digest::MD5.hexdigest(@doc_id)[0..(STORAGE_SUBDIRECTORY_LENGTH-1)]
Digest::MD5.hexdigest(@doc_id)[0..(STORAGE_SUBDIRECTORY_LENGTH - 1)]
end

private
Expand Down
Loading

0 comments on commit 1bcc850

Please sign in to comment.