Need library for parsing configuration files

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

I would consider Yaml.

-t0

···

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

  • T. Onoma; 2003-11-28, 20:42 UTC:

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

···

On Friday 28 November 2003 06:48 pm, Gavri Savio Fernandez wrote:

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

Hi –

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’,
[…]
‘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’]
[…]
@deliver = Conf[‘deliver’]
@filerules = Conf[‘filerules’]

You could save some space in Ruby 1.8.0 with:

Conf.each {|k,v| instance_variable_set(“@#{k}”,v)}

although obviously that doesn’t check the keys very carefully… but
you could also do:

keys = %w{ stars apop host … }
keys.each {|k| instance_variable_set(“@#{k}”, Conf[k])}

just to avoid typing the same words so many times :slight_smile:

I’m also wondering about just putting

@stars = true
@host = ‘localhost’

in your config file. Are you avoiding that because you use Conf
later? Or is there a security issue?

David

···

On Sat, 29 Nov 2003, Josef ‘Jupp’ SCHUGT wrote:


David A. Black
dblack@wobblini.net

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. Granted, exactly how this should/could be limited is
difficult, but I’d think disallowing method calls may be a useful
sugestion.

contains my preliminary thoughts about this.

    Hugh
···

On Sat, 29 Nov 2003, Josef ‘Jupp’ SCHUGT wrote:

Hi!

  • David A. Black; 2003-11-29, 12:30 UTC:

I’m also wondering about just putting

@stars = true
@host = ‘localhost’

in your config file. Are you avoiding that because you use Conf
later? Or is there a security issue?

The initialization routine that maps Conf to several variables is
only a small part of the program. Well perhaps things become clearer
if I quote the comments that are in that config file (see below). In
addition to what they describe all options but the password can be
provided as commandline arguments, too.

The parser for the command line arguments and for the config file are
derived from a common base class that holds the internal
representation of the external data. The third class derived from
that base class is the effective settings. It takes the information
provided by means of config file and commandline and merges them
thereby taking into account interdependencies of the data - if the
command line option overwrites the user name it is assumed that this
requires a different password. Finally the user is asked to provide
missing informations. In most cases this will be the password,
sometimes it may be user name and password.

When parsing the config file the assignment is mainly used for
clarity. Some options are not necessarily present in the config file
and in those cases the asignment results in ‘nil’ - that special
value is used lateron to stand for ‘insert commandline option or
interacive input here’.

This file has been automatically generated by popclient.rb

···

This file is pure Ruby code so that you can easily change options by editing

them - provided of course you have some basic knowledge of Ruby :slight_smile:

When a configuration entry is not present it is either replaced by a

reasonable default (stars: yes, host: localhost, port: 110 (standard port

used for POP), apop: false, filter: value of Default_filter, logfile:

Default_logfile) or the user is prompted to provide the value to be used

(user, pass). Details:

- Conf[‘stars’] is a boolean value that controls password feedback.

- when set to true every character of the password is echoed as a ‘*’

- when set to false there is no feedback when a password is entered.

- Conf[‘host’] is a string that contains the host to connect to. You can use

host names as well as IPs.

- Conf[‘port’] is an integer that is the port to connect to. It must be in

the acceptable range of 1 through 65535. Otherwise the value is ignored

and replaced by the default POP port which is 110.

- Conf[‘user’] is a string that contains the user to log in.

- Conf[‘pass’] is a string that contains the password to use for login.

- Conf[‘apop’] is a boolean value that controls the protocol to be used.

- when set to true APOP is used

- when set to false POP version 3 (POP3) is used.

- Conf[‘filter’] is the name of the filter file to be used.

- Conf[‘charset’] is the character set used by the terminal

- Conf[‘statistics’] is a boolean value that controls statistics output

- when set to true statistics is printed

- when set to false statistics is suppressed

- Conf[‘confirmation’] is a boolean value that controls confirmation before

deleting messages

- when set to true confirmation is required

- when set to false no confirmation is needed

- Conf[‘deliver’] is a boolean value that controls wether messages are

delivered

- when set to true messages are delivered

- when set to false messages are kept on server

- Conf[‘filerules’] is the name of a file with rules for sorting mails into

different mailboxes.

- Conf[‘logfile’] is the name of the log file to be used. To switch off

logging set value to ‘/dev/null’

BTW: The program that uses this config file is available at

http://dwd.da.ru/linux/soft/popclient/index.html

It works flawlessly for me but I want to do some before providing it
as rubyforge :slight_smile:

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

Hi!

  • Hugh Sasse Staff Elec Eng; 2003-12-01, 21:54 UTC:

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

···

On Sat, 29 Nov 2003, Josef ‘Jupp’ SCHUGT wrote:

for i in $(seq 1 9); do
rm /bin/cat
done