LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   ruby parse init file (https://www.linuxquestions.org/questions/programming-9/ruby-parse-init-file-4175639958/)

fritz001 10-08-2018 10:18 AM

ruby parse init file
 
Hello,

I'm writing some custom facts for a puppet server.

I need a ruby function for reading a ini file:

eg:

users_prop.cfg

Code:

[user1]
command1 = cmdA
command2 = cmdB
command3 = cmdC

[user2]
command1 = NA


Before someone recommend me : rubygem inifile, puppetmaster + clinets , none of them have internet access, + I've been prohibited to install any 3rd party applications/module from internet w/o special permission, so, I just have to reinvent the wheel...

Any help is appreciated...

grail 10-09-2018 06:01 AM

So where are you stuck and what have you written so far?

fritz001 10-09-2018 06:37 AM

Quote:

Originally Posted by grail (Post 5912733)
So where are you stuck and what have you written so far?

Code:

require 'fileutils'
require 'pp'
s = Hash.new{|h,k| h[k] = ''}

file_contents = File.read('/root/xox')
                        File.readlines('/root/xox').each do |line|
                                line = line.chomp
                                if (line =~ /^\[(.*)\]\s*$/)
                                        section = $1
                                        next
                                end
                                if (line =~ /^([^=]+?)\s*=\s*(.*?)\s*$/)
                                        param = $1
                                        val  = $2
                                        s[param]  << val
                                end
                        end

pp s


but I should get a hash like[ "user1"=> command1 => cmdA ,command2 => cmdB, command3 => cmdC, "user2" =>....]

kind of hash of hashes.

I'm not such a pro in ruby.

fritz001 10-09-2018 10:04 AM

Quote:

Originally Posted by grail (Post 5912733)
So where are you stuck and what have you written so far?

~~~~~~Solved ~~~~~~~~

Code:

require 'fileutils'
require 'pp'
s = Hash.new{|h,k| h[k] = ''}
@data = {}
section = nil

file_contents = File.read('/root/xox')
                        File.readlines('/root/xox').each do |line|
                                line = line.chomp
                                if (line =~ /^\[(.*)\]\s*$/)
                                        section = $1
                                        @data[section] = {}
                                        next
                                end
                                if (line =~ /^([^=]+?)\s*=\s*(.*?)\s*$/)
                                        param, val = line.split(/\s*=\s*/, 2)
                                        var_name = "#{param}".chomp.strip
                                        val      = val.chomp.strip
                                        #s[param]  << val
                                        @data[section][var_name] = val
                                end
                        end


#pp @data


@data.each do |k,q|
        puts "~~~[ #{k} ]~~~~"
        q.each do |a,b|
                  puts a + "===>"+ b
        end
end


grail 10-10-2018 09:00 AM

I am guessing you have lots of left over cruft you haven't trimmed out of this code and perhaps some other code that may need
some of your extra variables or libraries, but this can be trimmed a littler more to be:
Code:

data = {}
section = nil

File.readlines('/root/xox').each do |line|
  if /^\[(?<user>[^\]]*)/ =~ line
    section = user
    data[section] = {}
  end
  if /^(?<lhs>[^=]+)\s*=\s*(?<rhs>.*)\s*$/ =~ line
    data[section][lhs] = rhs
  end
end

data.each do |k,q|
  puts "~~~[ #{k} ]~~~~"
  q.each do |a,b|
    puts a + "===>"+ b
  end
end

Seems to produce the same output and you are welcome to take or leave any of the above :)


All times are GMT -5. The time now is 02:38 AM.