start breaking out classes for testing

This commit is contained in:
John Bintz 2009-05-22 09:36:08 -04:00
parent 545a665926
commit dc5b186c7b
3 changed files with 152 additions and 96 deletions

100
classes/Filter.rb Normal file
View File

@ -0,0 +1,100 @@
require 'singleton'
class Filter
include Singleton
@config = {}
@cleanup = []
attr_accessor :config, :cleanup
def initialize
@config = {}
@cleanup = []
end
def cleanup
@cleanup.each do |f|
if File.exists? f; File.unlink(f); end
end
end
def recalc_pixels
if @config['print']
if !@config['dpi']; raise "DPI not defined"; end
if (@config['dpi'].to_i.to_s) != @config['dpi'].to_s; raise "DPI not integer"; end
if @config['width_inches'] && (@config['width_inches'].to_f != 0)
@config['width'] = (@config['width_inches'].to_f * @config['dpi'].to_f).to_i
else
@config.delete('width')
end
if @config['height_inches'] && (@config['height_inches'].to_f != 0)
@config['height'] = (@config['height_inches'].to_f * @config['dpi'].to_f).to_i
else
@config.delete('height')
end
if (!@config['width'] && !@config['height'])
raise "No dimensions defined!"
end
end
end
#
# Set the config
#
def config=(c)
@config = c
recalc_pixels
end
#
# Build a temporary PNG from an SVG file
#
def inkscape(input, target)
params = []
width = @config['width']
height = @config['height']
inkscape_target = target
if @config['rotate']
case @config['rotate']
when 90, -90
t = width; width = height; height = t
inkscape_target = target + "-pre.png"
end
end
if width && (width.to_i != 0); params << "-w #{width} "; end
if height && (height.to_i != 0); params << "-h #{height} "; end
system("inkscape -e \"#{inkscape_target}\" -y 1.0 #{params.join(" ")} \"#{input}\"")
if @config['rotate']
command = [
"\"#{inkscape_target}\"",
"-rotate #{@config['rotate']}",
"\"#{target}\""
]
convert(command)
File.unlink(inkscape_target)
end
end
def convert(command, verbose = false)
system("convert " + (verbose ? "-verbose " : "" ) + [ command ].flatten.join(" "))
end
#
# Get the dimensions of a file
#
def get_dimensions(input)
dimensions = nil
IO.popen("identify -format '%w,%h' \"#{input}\"") do |fh|
dimensions = fh.readlines.first.split(",").collect { |d| d.to_i }
end
dimensions
end
end

View File

@ -2,101 +2,8 @@
require 'yaml' require 'yaml'
require 'time' require 'time'
require 'singleton'
class Filter require File.dirname(__FILE__) + '/classes/Filter.rb'
include Singleton
@config = {}
@cleanup = []
attr_accessor :config, :cleanup
def initialize
@config = {}
@cleanup = []
end
def cleanup
@cleanup.each do |f|
if File.exists? f; File.unlink(f); end
end
end
def recalc_pixels
if @config['print']
if !@config['dpi']; raise "DPI not defined"; end
if @config['width_inches'] && (@config['width_inches'].to_f != 0)
@config['width'] = (@config['width_inches'].to_f * @config['dpi'].to_f).to_i
else
@config.delete('width')
end
if @config['height_inches'] && (@config['height_inches'].to_f != 0)
@config['height'] = (@config['height_inches'].to_f * @config['dpi'].to_f).to_i
else
@config.delete('height')
end
end
end
#
# Set the config
#
def config=(c)
@config = c
recalc_pixels
end
#
# Build a temporary PNG from an SVG file
#
def inkscape(input, target)
params = []
width = @config['width']
height = @config['height']
inkscape_target = target
if @config['rotate']
case @config['rotate']
when 90, -90
t = width; width = height; height = t
inkscape_target = target + "-pre.png"
end
end
if width && (width.to_i != 0); params << "-w #{width} "; end
if height && (height.to_i != 0); params << "-h #{height} "; end
system("inkscape -e \"#{inkscape_target}\" -y 1.0 #{params.join(" ")} \"#{input}\"")
if @config['rotate']
command = [
"\"#{inkscape_target}\"",
"-rotate #{@config['rotate']}",
"\"#{target}\""
]
convert(command)
File.unlink(inkscape_target)
end
end
def convert(command, verbose = false)
system("convert " + (verbose ? "-verbose " : "" ) + [ command ].flatten.join(" "))
end
#
# Get the dimensions of a file
#
def get_dimensions(input)
dimensions = nil
IO.popen("identify -format '%w,%h' \"#{input}\"") do |fh|
dimensions = fh.readlines.first.split(",").collect { |d| d.to_i }
end
dimensions
end
end
class InputFilter < Filter class InputFilter < Filter
OutputFilename = "tmp.png" OutputFilename = "tmp.png"
@ -378,7 +285,7 @@ class TempBitmapToPaginatedPrint < OutputFilter
end end
def targets(info) def targets(info)
(@config['spread'] == true) ? [ filename(info) + "-left.png", "tmp-right.png" ] : filename(info) (@config['spread'] == true) ? [ filename(info) + "-left.png", filename(info) + "-right.png" ] : filename(info)
end end
def process_pagination(output, face, total_faces, left, right) def process_pagination(output, face, total_faces, left, right)
@ -574,6 +481,8 @@ files.each do |filename|
any_rebuilt = true any_rebuilt = true
puts "Rebuilding #{filename_display} (#{type})..." puts "Rebuilding #{filename_display} (#{type})..."
puts " Using #{filename} as a source"
puts " and writing to #{targets.inspect}"
tmp_files = input_obj.build(filename) tmp_files = input_obj.build(filename)
@ -588,7 +497,7 @@ files.each do |filename|
output_files << targets[i] output_files << targets[i]
end end
end end
input_obj.cleanup input_obj.cleanup
end end
if info['is_paginated'] if info['is_paginated']

47
tests/TestFilter.rb Normal file
View File

@ -0,0 +1,47 @@
#!/usr/bin/ruby
require 'rubygems'
require 'test/unit'
require 'mockfs/override'
require File.dirname(__FILE__) + '/../classes/Filter.rb'
class TestFilter < Test::Unit::TestCase
def setup
@filter = Filter.instance
end
def test_recalc_pixels
@filter.config = {}
assert_raise RuntimeError do
@filter.config = {'print' => true}
end
assert_raise RuntimeError do
@filter.config = {'print' => true,
'dpi' => 'test'}
end
assert_raise RuntimeError do
@filter.config = {
'print' => true,
'dpi' => 10
}
end
[ 'width', 'height' ].each do |dim|
assert_raise RuntimeError do
@filter.config = {
'print' => true,
'dpi' => 10,
'width' => 10
}
end
@filter.config = {
'width' => 10
}
assert_equal(10, @filter.config['width'])
end
end
end