开发者

Parsing a .cfg file using Ruby?

开发者 https://www.devze.com 2023-02-03 14:03 出处:网络
I have a .cfg file with the following data: *.*.key_val = { key1= \"value1\"; key2 = \"va开发者_运维百科lue2\";

I have a .cfg file with the following data:

*.*.key_val = {
  key1= "value1";
  key2 = "va开发者_运维百科lue2";
  key3 = "value3";
};

I want to read this file and store the key value pairs in a hash @var[key][val].

How it can be done?


THIS cfg you may parse in such way:

  1. read file using File#read
  2. convert text into 2-dimentional array using String#scan and regex
  3. convert array into hash using Hash[]

text = File.read('your.cfg')
# => "*.*.key_val = {\n  key1= \"value1\";\n  key2 = \"value2\";\n  key3 = \"value3\";\n};"

data = text.scan(/(\S+)\s*=\s*"([^"]+)/)
# => [["key1", "value1"], ["key2", "value2"], ["key3", "value3"]]

@var = Hash[data]
# => {"key1"=>"value1", "key2"=>"value2", "key3"=>"value3"}

Or just:

@var = Hash[File.read('your.cfg').scan(/(\S+)\s*=\s*"([^"]+)/)]


I'd strongly recommend transferring the configuration to something like YAML. It's made to be easy to understand, flexible, universally implemented, well documented, both as a standard and as a part of the core library, and easy to understand. (Yes, I said it twice on purpose.)

My YAML files load into Ruby as a Hash when I do something like:

require 'yaml'
config = YAML.load_file('/path/to/config/file')

I'll create the initial template for the configuration file in Ruby, as a Hash, then serialize it and write it to disk. That way I know what's on the disk is exactly the way YAML wants it to be and helps me avoid that "it won't load because either the data is wrong or the code is wrong" quandary.

# A simple round-trip (load and dump) of an object.

require 'yaml'

test_obj = {
  'foo'           => 'bar',
  'one_two_three' => [1, 2, 3],
  'hash'          => {'another' => 'hash'}
} #=> {"foo"=>"bar", "one_two_three"=>[1, 2, 3], "hash"=>{"another"=>"hash"}}

File.open('./config.yaml', 'w') { |fo| fo.puts YAML::dump( test_obj ) } #=> nil
ruby_obj = YAML::load_file( './config.yaml' ) #=> {"foo"=>"bar", "one_two_three"=>[1, 2, 3], "hash"=>{"another"=>"hash"}}
ruby_obj == test_obj #=> true

require 'pp'
pp ruby_obj
{"foo"=>"bar", "one_two_three"=>[1, 2, 3], "hash"=>{"another"=>"hash"}}

pp test_obj
{"foo"=>"bar", "one_two_three"=>[1, 2, 3], "hash"=>{"another"=>"hash"}}


You should try out the 'parseconfig' gem: https://rubygems.org/gems/parseconfig/

gem install parseconfig

Here's a sample how to use this gem:

require 'rubygems'
require 'parseconfig'
my_config = ParseConfig.new('your_file.cfg')
puts my_config.get_value('key_val')

Good luck and have fun learning Ruby. :)

EDIT

As Glenux said this is only for simple configuration files. I'll check if I can find anything else.

EDIT 2

I can't find a gem or something to parse a cfg file like in your example. I guess your only option is to write a parser yourself (like Nakilon did) or use something like YAML instead. Good luck anyway. :)


The parseconfig class is only intended for simple configuration files !

It accepts files of the format "param = value" (cf http://www.5dollarwhitebox.org/drupal/projects#rb-parseconfig ) but it will not parse the *.*.key_val = { and } thing.

Is the configuration file yours or generated/used by a third-party software? If it is yours, it may be wiser to use an other configuration file format (JSON, Ini, YAML, etc).


Wanted to mention this ruby library that can help you transfer configuration between JSON, YAML or Windows Ini file formats:

https://github.com/kigster/dupervisor

The use case is to move the configuration to YAML, but be able to generate whatever the format is needed by the software – in the case of this gem – it's supervisord's INI file format.

0

精彩评论

暂无评论...
验证码 换一张
取 消