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 =3D> 20, :title =3D> "DISPLAY"
value
ISPLAY, :default =3D> ENV['DISPLAY'] || 'localhost:0'
end
argument :xterm_title do
display :text, :size =3D> 20, :title =3D> "Xterm Title"
value "-T", :default =3D> "You are on a xterm!"
end
argument :xterm_text_color do
display :text, :size =3D> 20, :title =3D> "Xterm Font Color"
value "-fg", :default =3D> "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 readabl= e
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 som= e
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 gr= ouping.
(btw, an 'argument' is a command-line argument, like 'ls -F'... the -F
is an argument. An option is something like "DISPLAY=3Dmy_machine:1
xeyes".. the DISPLAY is an option).
=20
And grouping of applications would also be nice:
=20
application_group :all_x_apps
applications :xterm, :xeyes
display :checkbox, :title =3D> "Start all X applications"
end
=20
So, in the GUI, there would be a checkbox titled "Start all
applications" that would start the xterm and xeyes applications.
=20
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,
ptions
def initialize
@applications =3D Hash.new
@options =3D Hash.new
end
def process configuration
instance_eval configuration
end
def get_application_options application_id
@options.values.find_all do |option|=20
if option.option_type =3D=3D :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|=20
if option.option_type =3D=3D :command_line_argument
@applications[application_id].options.include? option.option_id
end
end
end
def application application_id, &block
application =3D Application.new application_id
@applications[application_id] =3D application=20
application.instance_eval &block
end
def option option_id, &block
option =3D Option.new option_id, :env_option
@options[option_id] =3D option
option.instance_eval &block
end
def argument option_id, &block
option =3D Option.new option_id, :command_line_argument
@options[option_id] =3D option
option.instance_eval &block
end
class Option
attr_accessor :name, :value, :default,
ption_id,
ption_type
def initialize option_id, option_type
@option_id =3D option_id
@option_type =3D option_type
@name =3D nil
@value =3D nil
end
def set name, args
@name =3D name
@value =3D @default =3D args[:default]
end
end
class Application
attr_accessor :executable, :application_id, :title, :node,
ptions
def initialize application_id
@application_id =3D application_id
@executable =3D nil
@title =3D nil
@node =3D nil
@options =3D []
end
def executable value=3Dnil
@executable =3D value if value
@executable
end
def title value=3Dnil
@title =3D value if value
@title
end
def node value=3Dnil
@node =3D value if value
@node
end
def options *args
@options =3D args if args.size > 0
@options
end
end
end
class TestConfigLoader < Test::Unit::TestCase
def test_process
a =3D 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
ISPLAY, a.options[:display].name
# Not sure how to properly test these... they depend on your env=20
# and if your DISPLAY is not set, then it should be something else=20
# ('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]],=
=20
a.get_application_arguments
xterm)
end
# A sample configuration file
def sample_configuration
<<-EOF
option :display do
#display :text, :size =3D> 20, :title =3D> "DISPLAY"
set
ISPLAY, :default =3D> ENV['DISPLAY'] || 'localhost:0'
end
argument :xterm_title do
#display :text, :size =3D> 20, :title =3D> "Xterm Title"
set "-T", :default =3D> "You are on a xterm!"
end
argument :xterm_font_color do
#display :text, :size =3D> 20, :title =3D> "Xterm Font Color"
set "-fg", :default =3D> "red"
end
application :xterm do=20
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