Commit b98cc29a authored by Scott Lembcke's avatar Scott Lembcke Committed by Scott Lembcke

Added plug-in/test.rb.

2006-07-08  Scott Lembcke <lemb0029@gnome.org>

        * Added plug-in/test.rb.

        * gimp-lib/gimp_oo.rb: Complete re-implementation of gimp OO types.

        * gimp-lib/pdb.rb: Changed Exception handling to more closely mirror
          GIMP procedure errors. Added a verbose mode that echoes procedure
          calls.

        * Removed plug-ins/hello.rb.

        * plug-ins/Makefile.am: Removed plug-ins/hello.rb.

        * gimp-lib/gimp.rb: Changes in error handling to reflect
          gimp-lib/pdb.rb.
parent 36516d1d
2006-07-08 Scott Lembcke <lemb0029@gnome.org>
* Added plug-in/test.rb.
* gimp-lib/gimp_oo.rb: Complete re-implementation of gimp OO types.
* gimp-lib/pdb.rb: Changed Exception handling to more closely mirror
GIMP procedure errors. Added a verbose mode that echoes procedure
calls.
* Removed plug-ins/hello.rb.
* plug-ins/Makefile.am: Removed plug-ins/hello.rb.
* gimp-lib/gimp.rb: Changes in error handling to reflect
gimp-lib/pdb.rb.
2006-07-05 Scott Lembcke <lemb0029@gnome.org>
* ext/conversion.c: Finished conversion for parasite parameters.
* gimp-lib/gimp.rb: Changed error checking to accept parasite types.
* ext/gimp.h: Added function prototypes for parasite conversion
functions.
* gimp-lib/Makefile.am: Added test.rb.
2006-07-01 Kevin Cozens <kcozens@cvs.gnome.org>
* configure.in
......
require 'gimpext'
require 'pdb'
#require 'gimp_oo.rb'
require 'rubyfu.rb'
module Gimp
......@@ -137,7 +136,7 @@ module Gimp
begin
bytes, data = PDB.gimp_procedural_db_get_data(key)
return Marshal.load(data)
rescue PDB::CallError
rescue PDB::ExecutionError
return nil
end
end
......
def module_template(base, methods)
mod = Module.new
methods.each do|name, args|
args = args.join(',')
mod.instance_eval """
def self.#{name.gsub('-', '_')}(#{args})
PDB['#{base}#{name}'].call(#{args})
module Gimp
module GimpOO
module ProcList
module_function
def each_proc(prefix)
prefix_range = prefix.length..-1
get_proc_list(prefix).each do|proc_name|
sym = proc_name[prefix_range].gsub('-','_').to_sym
yield(sym, proc_name)
end
end
"""
end
return mod
end
def get_proc_list(prefix)
num, list =PDB['gimp-procedural-db-query'].call(prefix, *(['']*6))
return list
end
end
def class_template(base, methods)
klass = Class.new
methods.each do|name, args|
args_a = args.join(',')
args_b = args.unshift('self').join(',')
class ClassTemplate
class << self
def add_constructor(name, proc_name)
class_eval """
def self.#{name}(*args)
create PDB['#{proc_name}'].call(*args)
end
"""
end
def add_method(name, proc_name)
class_eval """
def #{name}(*args)
PDB['#{proc_name}'].call(self, *args)
end
"""
end
def add_class_method(name, proc_name)
class_eval """
def self.#{name}(*args)
PDB['#{proc_name}'].call(*args)
end
"""
end
def add_methods(prefix, class_prefix)
ProcList.each_proc(prefix) do|sym, proc_name|
if proc_name =~ /new/
add_constructor(sym, proc_name)
else
add_method(sym, proc_name)
end
end
if class_prefix
ProcList.each_proc(class_prefix) do|sym, proc_name|
add_class_method(sym, proc_name)
end
end
end
def template(prefix, class_prefix)
klass = Class.new(self)
klass.add_methods(prefix, class_prefix)
return klass
end
klass.instance_eval """
def #{name.gsub('-', '_')}(#{args_a})
PDB['#{base}#{name}'].call(#{args_b})
alias_method :create, :new
end
def initialize(value)
@self = value
end
def to_int
Integer(@self)
end
"""
end
klass.instance_eval """
def initialize(value)
@value = value
def to_str
String(@self)
end
end
module ModuleTemplate
def self.template(prefix)
mod = Module.new
ProcList.each_proc(prefix) do|sym, proc_name|
mod.module_eval """
def self.#{sym}(*args)
PDB['#{proc_name}'].call(*args)
end
"""
end
def to_int
@value
return mod
end
end
def to_str
@value
end
"""
return klass
end
Gimp::Brush = ClassTemplate.template('gimp-brush-', 'gimp-brushes-')
Gimp::Channel = ClassTemplate.template('gimp-channel-', nil)
Gimp::Display = ClassTemplate.template('gimp-display-', nil)
Gimp::Gradient = ClassTemplate.template('gimp-gradient-', 'gimp-gradients-')
Gimp::Image = ClassTemplate.template('gimp-image-', nil)
Gimp::Layer = ClassTemplate.template('gimp-layer-', nil)
Gimp::Palette = ClassTemplate.template('gimp-palette-', 'gimp-palettes-')
Gimp::Vectors = ClassTemplate.template('gimp-vectors-', nil)
module Gimp
Buffer = class_template('gimp-buffer-',
[
['delete', []],
['get-bytes', []],
['get-height', []],
['get-image-type', []],
['get-width', []],
['rename', ['new_name']],
]
)
def Buffer.get_list(filter)
PDB['gimp-buffer-get-list'].call(filter)
Gimp::Context = ModuleTemplate.template('gimp-context-')
Gimp::Edit = ModuleTemplate.template('gimp-edit-')
Gimp::Fonts = ModuleTemplate.template('gimp-fonts-')
Gimp::ProceduralDB = ModuleTemplate.template('gimp-procedural-db-')
Gimp::Progress = ModuleTemplate.template('gimp-progress-')
Gimp::Selection = ModuleTemplate.template('gimp-selection-')
end
Context = module_template('gimp-context-',
[
['get-background', []],
['get-brush', []],
['get-font', []],
['get-foreground', []],
['get-gradient', []],
['get-opacity', []],
['get-paint-method', []],
['get-paint-mode', []],
['get-palette', []],
['get-pattern', []],
['pop', []],
['push', []],
['set-background', ['bg']],
['set-brush', ['brush']],
['set-default-colors', []],
['set-font', ['font']],
['set-foreground', ['fg']],
['set-gradient', ['gradient']],
['set-opacity', ['opacity']],
['set-paint-method', ['method']],
['set-paint-mode', ['mode']],
['set-palette', ['palette']],
['set-pattern', ['pattern']],
['swap-colors', []],
]
)
#TODO
#PDB
#Progress
Channel = class_template('gimp-channel-',
[
['combine-masks', ['other', 'op', 'offx', 'offy']],
['copy', []],
['get-color', []],
['get-opacity', []],
['get-show-masked', []],
['set-color', ['color']],
['set-opacity', ['opacity']],
['set-show-masked', ['show']],
]
)
class Channel
def self.new(image, w, h, name, opacity, color)
super PDB['gimp-channel-new'].call(image, w, h, name, opacity, color)
class Image
alias_method :old_undo_group_start, :undo_group_start
def undo_group
old_undo_group_start
if block_given?
yield
undo_group_end
end
end
def self.new_from_component(image, component, name)
#TODO FIXME
alias_method :old_undo_disable, :undo_disable
def undo_disable
old_undo_disable
if block_given?
yield
undo_enable
end
end
end
Display = class_template('gimp-display-',
[
['delete', []],
['get-window-handle', []],
]
)
class Display
def self.new(image)
super PDB['gimp-display-new'].call(image)
end
def self.flush
PDB['gimp-displays-flush'].call
end
def self.reconnect(old_image, new_image)
PDB['gimp-displays-reconnect'].call(old_image, new_image)
module Context
class << self
alias_method :old_push, :push
def push
puts 'yo'
old_push
if block_given?
yield
pop
end
end
end
end
Drawable = class_template('gimp-drawable-',
[
['bpp', []],
['bytes', []],
['delete', []],
['fill', ['fill_type']],
['foreground-extract', ['mode', 'mask']],
['get-image', []],
['get-linked', []],
['get-name', []],
['get-pixel', ['x', 'y']],
['get-tattoo', []],
['get-visible', []],
['has-alpha', []],
['height', []],
['image', []],
['is-channel', []],
['is-gray', []],
['is-indexed', []],
['is-layer', []],
['is-layer-mask', []],
['is-rgb', []],
['mask-bounds', []],
['mask-intersect', []],
['merge-shadow', ['undo']],
['offset', ['wrap', 'fill_type', 'x', 'y']],
['offsets', []],
['parasite-attach', ['parasite']],
['parasite-detach', ['name']],
['parasite-find', ['name']],
['parasite-list', []],
['set-linked', ['linked']],
['set-name', ['name']],
['set-pixel', ['x', 'y', 'num_channels', 'pixels']],
['set-tattoo', ['tattoo']],
['set-visible', ['visible']],
['sub-thumbnail', ['x', 'y', 'w', 'h', 'dest_h']],
['thumbnail', ['w', 'h']],
#TODO transform functions
['type', []],
['type_with_alpha', []],
['update', ['x', 'y', 'w', 'h']],
['width', []],
]
)
Image = class_template('gimp-image-',
[
['add-channel', ['channel', 'pos']],
['add-hguide', ['ypos']],
['add-layer', ['layer', 'pos']],
['add-layer-mask', ['layer', 'mask']],
['add-vectors', ['vectors', 'pos']],
['add-vguide', ['xpos']],
['base-type', []],
['clean-all', []],
['crop', ['w', 'h', 'x', 'y']],
['delete', []],
['delete-guide', ['guide']],
['duplicate', []],
['find-next-guide', ['guide']],
['flatten', []],
['flip', ['flip_type']],
#TODO finish
]
)
end
\ No newline at end of file
end
......@@ -12,14 +12,17 @@ module PDB
end
end
class CallError < PDBException
def initialize(name, status)
@name = name
@status = Gimp::EnumNames::PDBStatusType[status]
class ExecutionError < PDBException
attr_reader :message
def initialize(proc_name)
@message = "#{proc_name} returned an execution error."
end
def message
"#@name exited with status #@status"
end
class CallingError < PDBException
attr_reader :message
def initialize(proc_name)
@message = "#{proc_name} returned a calling error."
end
end
......@@ -70,11 +73,15 @@ module PDB
end
def call(*args)
puts "PDB call: #@name" if PDB.verbose
params = convert_args(args, @args)
values = Gimp.run_procedure(@name, params)
status = values.shift.data
raise CallError.new(@name, status) unless status == Gimp::PDB_SUCCESS
case values.shift.data
when Gimp::PDB_CALLING_ERROR: raise(CallingError, @name)
when Gimp::PDB_EXECUTION_ERROR: raise(ExecutionError, @name)
end
return *values.collect{|param| param.data}
end
......@@ -100,8 +107,13 @@ module PDB
end
end
def self.[](name)
Procedure.new(name)
class << self
attr_accessor :verbose
@verbose = false
def [](name)
Procedure.new(name)
end
end
module Access
......
......@@ -7,7 +7,6 @@ scripts = \
sphere.rb
test_scripts = \
hello.rb \
test.rb
......
#!/usr/bin/env ruby
require 'gimp'
#include RubyFu
#include PDB::Access
RubyFu.register(
'ruby-fu-hello',
'Hello, world!',
'Displays "Hello, world!" in a dialog',
'Scott Lembcke',
'Copyright Scott Lembcke',
'2006',
'<Toolbox>/Xtns/Languages/Ruby-Fu/Hello World!',
'',
[
RubyFu::ParamDef.STRING('stuff', 'test', 'test string'),
],
nil
) do|run_mode, stuff|
run_mode = Gimp::EnumNames::RunMode[run_mode]
Gimp.message "Run mode: #{run_mode}\nStuff: #{stuff.inspect}"
PDB.ruby_fu_hello2
nil
end
RubyFu.register(
'ruby-fu-hello2',
'Hello, world!',
'Displays "Hello, world!" in a dialog',
'Scott Lembcke',
'Copyright Scott Lembcke',
'2006',
nil,
'',
nil,
nil
) do
Gimp.message "Hello, world 2!"
nil
end
RubyFu.main
#!/usr/bin/env ruby
require 'gimp'
RubyFu.register(
'ruby-fu-test1', #procedure name
nil, #blurb
nil, #help
nil, #author
nil, #copyright
nil, #date
nil, #menupath
nil, #image types
nil, #params
nil #results
) do
#nothing
end
RubyFu.register(
'ruby-fu-test2', #procedure name
nil, #blurb
nil, #help
nil, #author
nil, #copyright
nil, #date
nil, #menupath
nil, #image types
[], #params
[] #results
) do
#nothing
end
RubyFu.register(
'ruby-fu-test-echo', #procedure name
nil, #blurb
nil, #help
nil, #author
nil, #copyright
nil, #date
nil, #menupath
nil, #image types
[
RubyFu::ParamDef.INT32('int32', 'int32', 1),
RubyFu::ParamDef.INT16('int16', 'int16', 1),
RubyFu::ParamDef.INT8('int8', 'int8', 1),
RubyFu::ParamDef.FLOAT('float', 'float', 1.1),
RubyFu::ParamDef.STRING('string', 'string', 'hello'),
RubyFu::ParamDef.INT32('length1', 'length1', 1),
RubyFu::ParamDef.INT32ARRAY('int32array', 'int32array', [1, 2, 3]),
RubyFu::ParamDef.INT32('length2', 'length2', 1),
RubyFu::ParamDef.INT16ARRAY('int16array', 'int16array', [1, 2, 3]),
RubyFu::ParamDef.INT32('length3', 'length3', 1),
RubyFu::ParamDef.INT8ARRAY('int8array', 'int8array', 'abc'),
RubyFu::ParamDef.INT32('length4', 'length4', 1),
RubyFu::ParamDef.FLOATARRAY('floatarray', 'floatarray', [1.1, 2.2, 3.3]),
RubyFu::ParamDef.INT32('length5', 'length5', 1),
RubyFu::ParamDef.STRINGARRAY('stringarray', 'stringarray', ['one', 'two', 'three']),
RubyFu::ParamDef.COLOR('color', 'color', Gimp::Rgb.new(1.0, 1.0, 1.0)),
RubyFu::ParamDef.PARASITE('parasite', 'parasite', Gimp::Parasite.new('name', 999, 'data')),
], #params
[
Gimp::ParamDef.INT32('int32', 'int32'),
Gimp::ParamDef.INT16('int16', 'int16'),
Gimp::ParamDef.INT8('int8', 'int8'),
Gimp::ParamDef.FLOAT('float', 'float'),
Gimp::ParamDef.STRING('string', 'string'),
Gimp::ParamDef.INT32('length1', 'length1'),
Gimp::ParamDef.INT32ARRAY('int32array', 'int32array'),
Gimp::ParamDef.INT32('length2', 'length2'),
Gimp::ParamDef.INT16ARRAY('int16array', 'int16array'),
Gimp::ParamDef.INT32('length3', 'length3'),
Gimp::ParamDef.INT8ARRAY('int8array', 'int8array'),
Gimp::ParamDef.INT32('length4', 'length4'),
Gimp::ParamDef.FLOATARRAY('floatarray', 'floatarray'),
Gimp::ParamDef.INT32('length5', 'length5'),
Gimp::ParamDef.STRINGARRAY('stringarray', 'stringarray'),
Gimp::ParamDef.COLOR('color', 'color'),
Gimp::ParamDef.PARASITE('parasite', 'parasite'),
] #results
) do|int32, int16, int8, float, string,
l1, int32array, l2, int16array, l3, int8array, l4, floatarray, l5, stringarray,
color, parasite|
puts parasite
[int32, int16, int8, float, string,
l1, int32array,
l2, int16array,
l3, int8array,
l4, floatarray,
l5, stringarray,
color,
parasite
]
end
RubyFu.register(
'ruby-fu-test-call-echo', #procedure name
'Check that parameter conversion is safe.', #blurb
nil, #help
nil, #author
nil, #copyright
nil, #date
'<Toolbox>/Xtns/Languages/Ruby-Fu/Test/Echo', #menupath
nil, #image types
[], #params
[] #results
) do|run_mode|
args = [
123,
-1,
129,
123.123,
'stringing it',
3,
[1, 2, 3],
3,
[1, 2, 3],
3,
'abc',
3,
[1.1, 2.2, 3.3],
3,
['one', 'two', 'three'],
Gimp::Rgb.new(1.0, 1.0, 1.0),
Gimp::Parasite.new("a rose", 12, "some data")
]
values = PDB.ruby_fu_test_echo(*args)
if args == values
Gimp.message "Success!"
else
Gimp.message "Failure"
end
nil
end
RubyFu.register(
'ruby-fu-test-shelf', #procedure name
'Test that the shelf works correctly.', #blurb
nil, #help
nil, #author
nil, #copyright
nil, #date
'<Toolbox>/Xtns/Languages/Ruby-Fu/Test/Shelf', #menupath
nil, #image types
[], #params
[] #results
) do|run_mode|
def test_method
return false if Gimp::Shelf['badstring'] != nil
Gimp.message "Previous value = #{Gimp::Shelf['goodstring'].inspect}"
obj = ['wow', 1]
Gimp::Shelf['goodstring'] = obj
return false if Gimp::Shelf['goodstring'] != obj
return true
end
if test_method
Gimp.message "Success!"
else
Gimp.message "Failure"
end
nil
end
RubyFu.register(
'ruby-fu-test-crash', #procedure name
nil, #blurb
nil, #help
nil, #author
nil, #copyright
nil, #date
nil, #menupath
nil, #image types
[], #params
[] #results
) do||
raise "This is a test exception"
nil
end
def test_exception(e)
begin
yield
rescue e
puts 'Success!'
return
end
puts 'Failure'
$failure = true
end
RubyFu.register(
'ruby-fu-test-call', #procedure name
'Check proper errors are raised when calling methods improperly.', #blurb
nil, #help
nil, #author
nil, #copyright
nil, #date
'<Toolbox>/Xtns/Languages/Ruby-Fu/Test/Call', #menupath
nil, #image types
[], #params
[] #results
) do|run_mode|
require 'stringio'
$stdout = StringIO.new
print 'Calling with bad params ... '
test_exception(TypeError){PDB.gimp_message(123)}
print 'Calling with too many params ... '
test_exception(ArgumentError){PDB.gimp_message(123, 123)}
##THIS CRASHES HORRIBLY. NOT SURE IF IT'S MY FAULT OR NOT.
# printf 'Calling error ... '
# test_exception(PDB::CallingError) do
# Gimp.run_procedure('ruby-fu-test-crash', [Gimp::Param.INT32(123)])
# end
printf 'Exucution error ... '
test_exception(PDB::ExecutionError){PDB.ruby_fu_test_crash}
Gimp.message('Tests ' + ($failure ? 'failed' : 'successful') + ":\n" + $stdout.string)
$stdout = STDOUT
nil
end
RubyFu.register(
'ruby-fu-test-OO', #procedure name
'Test that the ', #blurb
nil, #help
nil, #author
nil, #copyright
nil, #date
'<Toolbox>/Xtns/Languages/Ruby-Fu/Test/OO', #menupath
nil, #image types
[], #params
[Gimp::ParamDef.IMAGE('image', 'Image')] #results
) do|run_mode|
PDB.verbose = true