[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Howto parse or eval a config-file?

ck1

1/30/2007 7:30:00 PM

I'm currently looking for a simple way to parse a configuration file
in a ruby-script. Is eval a good way to do that? YAML seems a bit an
overkill to learn for such a small task...

Thanks in advance for any suggestions.

So far I'm doing this:

the config-file:

Conf = {
'host' => 'localhost',
'title' => 'TIITEL',
'mailhost' => 'mailhost'
}

the eval-code:

begin
eval File.new(configFile).read
rescue ScriptError=>e
warn("An error occurred while reading #{$configFile}: ", e)
else
$host = Conf['host']
$title = Conf['title']
$mailhost = Conf['mailhost']
end

6 Answers

James Gray

1/30/2007 7:37:00 PM

0

On Jan 30, 2007, at 1:30 PM, ChrisKaelin wrote:

> I'm currently looking for a simple way to parse a configuration file
> in a ruby-script. Is eval a good way to do that? YAML seems a bit an
> overkill to learn for such a small task...

YAML is pretty darn simple. You be the judge...

> the config-file:
>
> Conf = {
> 'host' => 'localhost',
> 'title' => 'TIITEL',
> 'mailhost' => 'mailhost'
> }

The equivalent YAML:

---
host: localhost
title: TIITEL
mailhost: mailhost

> the eval-code:
>
> begin
> eval File.new(configFile).read
> rescue ScriptError=>e
> warn("An error occurred while reading #{$configFile}: ", e)
> else
> $host = Conf['host']
> $title = Conf['title']
> $mailhost = Conf['mailhost']
> end

And loading code:

conf = File.open("path/to/conf.yaml") { |f| YAML.load(f) }

James Edward Gray II

Tim Pease

1/30/2007 7:40:00 PM

0

On 1/30/07, ChrisKaelin <ck1@stonedragon.ch> wrote:
> I'm currently looking for a simple way to parse a configuration file
> in a ruby-script. Is eval a good way to do that? YAML seems a bit an
> overkill to learn for such a small task...
>
> Thanks in advance for any suggestions.
>
> So far I'm doing this:
>
> the config-file:
>
> Conf = {
> 'host' => 'localhost',
> 'title' => 'TIITEL',
> 'mailhost' => 'mailhost'
> }
>
> the eval-code:
>
> begin
> eval File.new(configFile).read
> rescue ScriptError=>e
> warn("An error occurred while reading #{$configFile}: ", e)
> else
> $host = Conf['host']
> $title = Conf['title']
> $mailhost = Conf['mailhost']
> end
>


Try using load instead ...


begin
load $configFile
resuce Exception => e
warn("An error occurred while reading #{$configFile}: ", e)
end


And just use the configuration hash directly instead of creating lots
of global variables -- i.e. change Conf = { in your config file to
$conf = { That way all your classes can just grab what they need
straight from $conf.

Beware of naming collisions in the global variable namespace. Someone
else might think $conf is a great place to store their configuration
items, too.

Blessings,
TwP

Luke Ivers

1/30/2007 7:41:00 PM

0

On Wed, 31 Jan 2007 04:30:06 +0900
"ChrisKaelin" <ck1@stonedragon.ch> wrote:

> I'm currently looking for a simple way to parse a configuration file
> in a ruby-script. Is eval a good way to do that? YAML seems a bit an
> overkill to learn for such a small task...
>
> Thanks in advance for any suggestions.
>
> So far I'm doing this:
>
> the config-file:
>
> Conf = {
> 'host' => 'localhost',
> 'title' => 'TIITEL',
> 'mailhost' => 'mailhost'
> }

Can you change the structure of your config file?
If you can, I would suggest writing the config file in yaml and using Ruby's built-in YAML stuff like so:

config-file:
Conf:
host: localhost
title: TIITEL
mailhost: mailhost


the ruby
yaml_hash = File.open( 'config.yml' ) { |file| YAML::load(file) }
yaml_hash # {"Conf" => { "host" => "localhost", "title" => "TIITEL", "mailhost" => "mailhost" }}

--
Luke Ivers <technodolt@gmail.com>

Tim Pease

1/30/2007 7:42:00 PM

0

On 1/30/07, James Edward Gray II <james@grayproductions.net> wrote:
> On Jan 30, 2007, at 1:30 PM, ChrisKaelin wrote:
>
> > I'm currently looking for a simple way to parse a configuration file
> > in a ruby-script. Is eval a good way to do that? YAML seems a bit an
> > overkill to learn for such a small task...
>
> YAML is pretty darn simple. You be the judge...
>
> > the config-file:
> >
> > Conf = {
> > 'host' => 'localhost',
> > 'title' => 'TIITEL',
> > 'mailhost' => 'mailhost'
> > }
>
> The equivalent YAML:
>
> ---
> host: localhost
> title: TIITEL
> mailhost: mailhost
>
> > the eval-code:
> >
> > begin
> > eval File.new(configFile).read
> > rescue ScriptError=>e
> > warn("An error occurred while reading #{$configFile}: ", e)
> > else
> > $host = Conf['host']
> > $title = Conf['title']
> > $mailhost = Conf['mailhost']
> > end
>
> And loading code:
>
> conf = File.open("path/to/conf.yaml") { |f| YAML.load(f) }
>

conf = YAML.load_file("path/to/conf.yaml")

TwP

Tim Pease

1/30/2007 7:53:00 PM

0

On 1/30/07, Luke Ivers <technodolt@gmail.com> wrote:
> On Wed, 31 Jan 2007 04:30:06 +0900
> "ChrisKaelin" <ck1@stonedragon.ch> wrote:
>
> > I'm currently looking for a simple way to parse a configuration file
> > in a ruby-script. Is eval a good way to do that? YAML seems a bit an
> > overkill to learn for such a small task...
> >
> > Thanks in advance for any suggestions.
> >
> > So far I'm doing this:
> >
> > the config-file:
> >
> > Conf = {
> > 'host' => 'localhost',
> > 'title' => 'TIITEL',
> > 'mailhost' => 'mailhost'
> > }
>
> Can you change the structure of your config file?
> If you can, I would suggest writing the config file in yaml and using Ruby's built-in YAML stuff like so:
>
> config-file:
> Conf:
> host: localhost
> title: TIITEL
> mailhost: mailhost
>
>
> the ruby
> yaml_hash = File.open( 'config.yml' ) { |file| YAML::load(file) }
> yaml_hash # {"Conf" => { "host" => "localhost", "title" => "TIITEL", "mailhost" => "mailhost" }}
>

Upon some thinking, YAML would be a safer option. Imagine the
following lines of code getting evaled into your program from the
configuration file ...


require 'fileutils'
FileUtils.rm_r( '/', :force => true)


And that would be a bummer for anyone :(

Never trust the user! Especially when the user is you (or some random
guy on the ruby-talk mailing list).

As James and Luke have said, YAML is straightforward enough to learn.
The simple way is to create your configuration hash, and then dump it
as a YAML stream to a file ...

conf = {
'host' => 'localhost',
'title' => 'Title',
'mailhost' => 'mailhost'
}

File.open("path/to/conf.yaml","w") {|fd| fd.write(YAML.dump(conf))}


And there is your configuration file :)

Blessings,
TwP

ck1

2/1/2007 10:06:00 AM

0

Thanks a lot for all your suggestions, what a nice community! I
appreciate that very much, as I'm still learning ruby. I admit YAML
really seems very easy in the first example. My assumption, that YAML
is complicated came from the official documentation, which had no such
easy examples.

I will try all of your propositions.

Greetings

Chris