Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix whitespace related offenses #249

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions bin/autoheathen
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,21 @@ 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
processor = AutoHeathen::EmailProcessor.new(cfg)
case mode
when :return_to_sender
processor.process_rts email
when :email
processor.process email, mail_to
when :return_to_sender
processor.process_rts email
when :email
processor.process email, mail_to
end
24 changes: 12 additions & 12 deletions bin/cvheathen
Original file line number Diff line number Diff line change
Expand Up @@ -22,38 +22,38 @@ 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
75 changes: 39 additions & 36 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,11 +64,12 @@ 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]
doc.set_current version
Expand All @@ -86,18 +87,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,12 +112,14 @@ 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

doc = Document.load @storage_dir, doc_key
raise VersionNotFound.new unless doc.has_version? version

Sidekiq::ConversionWorker.perform_async doc_key, version, filename, action, params[:callback_url]
respond 202, "Conversion initiated"
rescue StandardError => e
Expand All @@ -130,7 +133,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 +146,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 +160,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 +174,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 +199,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 +218,12 @@ class App < Sinatra::Base
post "/#{LegacyConverter::LEGACY}/convert" do
begin
body = if params[:file]
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
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
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,13 +255,13 @@ 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
when StandardError
extra[:backtrace] = status.backtrace if params[:backtrace]
status = 500
when Colore::Error
status = status.http_code
when StandardError
extra[:backtrace] = status.backtrace if params[:backtrace]
status = 500
end
content_type 'application/json'
return status, {
Expand All @@ -281,13 +284,13 @@ 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
when StandardError
extra[:backtrace] = status.backtrace if params[:backtrace]
status = 500
when Error
status = status.http_code
when StandardError
extra[:backtrace] = status.backtrace if params[:backtrace]
status = 500
end
content_type 'application/json'
return status, {
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
Loading