[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Need library for parsing configuration files

Gavri Savio Fernandez

11/28/2003 5:49:00 PM

hi,
i'm writing an application which needs to parse a configuration file on program startup. is there a prefered format for configuration files in ruby? is there a library for such a purpose? i suppose i could do the parsing myself, but i was just wondering if there is a sophisticated way out there.

thank you

Gavri Savio Fernandez
___________________________________________

If only God would give me some clear sign! Like making a large deposit in my name at a Swiss bank. - Woody Allen


4 Answers

T. Onoma

11/28/2003 5:55:00 PM

0

On Friday 28 November 2003 06:48 pm, Gavri Savio Fernandez wrote:
> hi,
> i'm writing an application which needs to parse a configuration file on
> program startup. is there a prefered format for configuration files in
> ruby? is there a library for such a purpose? i suppose i could do the
> parsing myself, but i was just wondering if there is a sophisticated way
> out there.
>
> thank you

I would consider Yaml.

-t0



Josef 'Jupp' Schugt

11/28/2003 11:56:00 PM

0

* T. Onoma; 2003-11-28, 20:42 UTC:
> On Friday 28 November 2003 06:48 pm, Gavri Savio Fernandez wrote:
> > i'm writing an application which needs to parse a configuration
> > file on program startup. is there a prefered format for
> > configuration files in ruby? is there a library for such a
> > purpose? i suppose i could do the parsing myself, but i was just
> > wondering if there is a sophisticated way out there.
>
> I would consider Yaml.

There are at least four ways:

- INI
- Ruby
- XML
- Yaml

INI files are very simple but also limited.

; .qvwm-theme

[Variables]
DefaultIcon = "def16.ani" ; titlebar/taskbar-button icon
DefaultLargeIcon = "def32.ani" ; task switcher icon
DefaultShortcutIcon = "icon32.ani" ; shortcut icon
DefaultFont = "-*-*-medium-r-normal-*-14-*-*-*-*-*-*-*"

YAML has already been mentioned.

In the case of XML at least one libary exists that focusses on
configuration files.

Personally I prefer Ruby files to use other Ruby files as their
configuration files. To give an example of a config file:

Conf = {
'stars' => true,
'host' => 'localhost',
'port' => 110,
'user' => 'jupp',
'pass' => '74!GeD,5',
'apop' => false,
'filter' => '/home/jupp/.popclient-filter.rb',
'charset' => 'ISO-8859-15',
'statistics' => true,
'confirmation'=> false,
'deliver' => true,
'filerules' => '/home/jupp/.popclient-filerules.rb',
'logfile' => '/home/jupp/popclient-log',
}

Actually in use, I only did change value of Conf['pass']. Parsing is
then done in this way:

def initialize(rcfile)
rcfile = Default_rcfile if rcfile.nil?
@rcfile = rcfile
return unless file_okay?(rcfile, true)
begin
eval File.new(rcfile).read
rescue ScriptError=>e
warn("An error occurred while reading #{rcfile}: ", e)
else
@stars = Conf['stars']
@apop = Conf['apop']
@host = Conf['host']
@user = Conf['user']
@pass = Conf['pass']
@filter = Conf['filter']
@log = Conf['logfile']
@charset = Conf['charset']
@statistics = Conf['statistics']
@confirmation = Conf['confirmation']
@deliver = Conf['deliver']
@filerules = Conf['filerules']

unless Conf['port'].nil?
port = Conf['port']
if port < 0 or port > 65535
warn("Invalid port in '#{rcfile}', ignored")
else
@port = port
end
end
end
end

file_okay? does access right checking and e.g. complains about world
or group readable configuration files (which is not a good idea if
the file may contain a valuable password).

Josef 'Jupp' Schugt
--
begin SPAM-POLICY.txt.vbs
if msg.size > 100 kB or msg.sender.is_spammer or msg.text.is_spam
discard message
end


Hugh Sasse

12/1/2003 2:23:00 PM

0

Josef 'Jupp' Schugt

12/2/2003 12:33:00 AM

0

Hi!

* Hugh Sasse Staff Elec Eng; 2003-12-01, 21:54 UTC:
> On Sat, 29 Nov 2003, Josef 'Jupp' SCHUGT wrote:
>
> > Personally I prefer Ruby files to use other Ruby files as their
> > configuration files. To give an example of a config file:
> >
> > Conf = {
> > 'stars' => true,
> > 'host' => 'localhost',
> [...]
> > 'filerules' => '/home/jupp/.popclient-filerules.rb',
> > 'logfile' => '/home/jupp/popclient-log',
> > }
>
> The weakness in this is that people can put arbitrary ruby code in
> there.

That only is a weakness if you want to disallow that. The program the
above config is meant to be used with is a mail downloader with
filtering capabilities. I wanted to make it possible that the config
file not only provides some settings but (if that is reqired) also
can update blacklists, whitelists, if that is reqired.

As in real life freedom has its price. One either has to be willing
to pay that price or one has to reduce freedom.

My answer to the question what is the best language for config files
is LISP. I *really* don't like LISP but it is very simple to write a
LISP interpreter that understands what you want to be able to
configure. Nothing more, nothing less.

The way in which a complex configuration is written in LISP is
essentially the same as the way in which it is done in XML. The
differences are mainly syntactical ones not semantic ones. The
advantage of LISP over XML is that it is easier to type it in when
using an ordinary text editor.

That were just my 2 Euro Cent.

Josef 'Jupp' Schugt
--
for i in $(seq 1 9); do
rm /bin/cat
done