> > Thank you for your ideas. I've adapted it somewhat for parts of my
> > application, but I'm running into problems.
> >
> > I've got the following configuration file:
> >
> > option :display do
> > display :text, :size => 20, :title => "DISPLAY"
> > value :DISPLAY, :default => ENV['DISPLAY'] || 'localhost:0'
> > end
> >
> > argument :xterm_title do
> > display :text, :size => 20, :title => "Xterm Title"
> > value "-T", :default => "You are on a xterm!"
> > end
> >
> > argument :xterm_text_color do
> > display :text, :size => 20, :title => "Xterm Font Color"
> > value "-fg", :default => "red"
> > end
> >
> >
> > application :xterm do
> > executable "/usr/X11R6/bin/xterm"
> > title :Xterm
> > node :fatire
> > options :display
> > arguments :xterm_text_color, :xterm_title
> > end
> >
> > application :xeyes do
> > executable "/usr/X11R6/bin/xeyes"
> > title :Xeyes
> > node :fatire
> > options :display
> > end
> >
> >
> > Should be self-explanatory. I have some applications that take env
> > options and command-line arguments, and I want to have a very readable
> > and configurable file for defining those applications, options, and
> > arguments (and some other things). The 'node' option for the
> > application is the machine that the application should be started on.
> > The 'display' option for the options/arguments state how the GUI
> > should display the option/argument.
> >
> > My problem is trying to adapt your solution to fit something like
> > this. There would be a lot of duplication if I had Builders for
> > applications, nodes, options, and arguments. And I'm also having some
> > difficulties getting each Application object to know what options and
> > arguments it should have.
> >
> > I considered using YAML for the configuration file format, but I'm
> > still leaning towards having a pure Ruby file.
> >
> > Thoughts are greatly appreciated! This is my first time trying to do
> > this type of programming, so it's a little weird.
> >
>
>
> I would also like to be able to 'group' things together, like
>
> group :xterm_options_arguments do
> arguments :xterm_title, :xterm_text_color
> options :display
> end
>
> application :xterm do
> group :xterm_options_arguments
> title :Xterm
> ...
> end
>
> So that applications with common options and arguments can specify a grouping.
>
> (btw, an 'argument' is a command-line argument, like 'ls -F'... the -F
> is an argument. An option is something like "DISPLAY=my_machine:1
> xeyes".. the DISPLAY is an option).
And grouping of applications would also be nice:
application_group :all_x_apps
applications :xterm, :xeyes
display :checkbox, :title => "Start all X applications"
end
So, in the GUI, there would be a checkbox titled "Start all
applications" that would start the xterm and xeyes applications.
In other words, I'd like to be able to do grouping of various things
in this configuration file.
Here's the start of what I have so far. How's it look?
require 'test/unit'
class ConfigLoader
attr_accessor :configuration, :applications, :options
def initialize
@applications = Hash.new
@options = Hash.new
end
def process configuration
instance_eval configuration
end
def get_application_options application_id
@options.values.find_all do |option|
if option.option_type == :env_option
@applications[application_id].options.include? option.option_id
end
end
end
def get_application_arguments application_id
@options.values.find_all do |option|
if option.option_type == :command_line_argument
@applications[application_id].options.include? option.option_id
end
end
end
def application application_id, &block
application = Application.new application_id
@applications[application_id] = application
application.instance_eval &block
end
def option option_id, &block
option = Option.new option_id, :env_option
@options[option_id] = option
option.instance_eval &block
end
def argument option_id, &block
option = Option.new option_id, :command_line_argument
@options[option_id] = option
option.instance_eval &block
end
class Option
attr_accessor :name, :value, :default, :option_id, :option_type
def initialize option_id, option_type
@option_id = option_id
@option_type = option_type
@name = nil
@value = nil
end
def set name, args
@name = name
@value = @default = args[:default]
end
end
class Application
attr_accessor :executable, :application_id, :title, :node, :options
def initialize application_id
@application_id = application_id
@executable = nil
@title = nil
@node = nil
@options =
end
def executable value=nil
@executable = value if value
@executable
end
def title value=nil
@title = value if value
@title
end
def node value=nil
@node = value if value
@node
end
def options *args
@options = args if args.size > 0
@options
end
end
end
class TestConfigLoader < Test::Unit::TestCase
def test_process
a = ConfigLoader.new
a.process sample_configuration
# Test to see that the application stuff was set ok.
assert_equal "/usr/X11R6/bin/xeyes", a.applications[:xeyes].executable
assert_equal [:display], a.applications[:xeyes].options
assert_equal :fatire, a.applications[:xeyes].node
assert_equal :Xeyes, a.applications[:xeyes].title
# Test to see that the options were set ok.
assert_equal :DISPLAY, a.options[:display].name
# Not sure how to properly test these... they depend on your env
# and if your DISPLAY is not set, then it should be something else
# ('localhost:0'). So I'd be duplicating logic in the tests.
#assert_equal "vandyk-j:0", a.options[:display].default
#assert_equal "vandyk-j:0", a.options[:display].value
# Test to see that the env options are ok.
assert_equal [a.options[:display]], a.get_application_options(:xeyes)
assert_equal [a.options[:display]], a.get_application_options(:xterm)
# Test to see that the command line arguments are ok.
assert_equal [a.options[:xterm_font_color], a.options[:xterm_title]],
a.get_application_arguments(:xterm)
end
# A sample configuration file
def sample_configuration
<<-EOF
option :display do
#display :text, :size => 20, :title => "DISPLAY"
set :DISPLAY, :default => ENV['DISPLAY'] || 'localhost:0'
end
argument :xterm_title do
#display :text, :size => 20, :title => "Xterm Title"
set "-T", :default => "You are on a xterm!"
end
argument :xterm_font_color do
#display :text, :size => 20, :title => "Xterm Font Color"
set "-fg", :default => "red"
end
application :xterm do
executable "/usr/X11R6/bin/xterm"
title :Xterm
node :fatire
options :display, :xterm_font_color, :xterm_title
end
application :xeyes do
executable "/usr/X11R6/bin/xeyes"
title :Xeyes
node :fatire
options :display
end
EOF
end
end