hackerkatt
hackerkatt

Reputation: 165

Parse config file and convert array into nested hash

EDITED for clarification

I "had" done a number of searches (more than I can count) on google and apparently I wasn't searching with the right key words. I found numerous sites (rubyforum, personal sites, stackoverflow, etc) none of which really addressed my particular issue and none of which I was able to adapt.

I am parsing a config file that has sections and subsections. That is, it's a file I pull from a device, not one that I can creating. I want to create a hash of that config that has sections and subsections for quick access to a key or a subkey so that if I know the index of a given MAC, I can quickly set its status to disabled instead of having to iterate over the string or array version of config looking for MAC and tracking index.

Config file contents (in part) I want to convert from: i.e. I'll read in the file and covert to hash.

aaa.1.br.devname=br0
aaa.1.devname=ath0
bridge.1.devname=br0
wireless.1.mac_acl.1.comment=testsm
wireless.1.mac_acl.1.mac=DC:9F:DB:32:0A:2E
wireless.1.mac_acl.1.status=enabled
wireless.1.mac_acl.policy=allow
wireless.1.mac_acl.status=enabled

What I want to convert it to:

{
 :aaa => {:"1" => {
                   :br => {:devname => "br0"},
                   :devname => "ath0"
                  }
         },
 :bridge => {:"1" => {:devname => "br0"}},
 :wireless => {:"1" => {
                         :mac_acl => {
                                      :"1" => {:comment => "testsm", :mac => "DC:9F:DB:32:0A:2E", :status => "enabled"}
                                     },
                        :policy => "allow",
                        :status => "enabled"
                       }
              }
}

'Edgars Jekabsons' provided two possible solutions (Thank you!!). Configatron wouldn't work as many of the keys in the config I am parsing are/would be integers, Configatron doesn't like that. It somewhat worked using the .store and .fetch methods, but updating an existing stored value was a problem. Hashquiz was exactly what I was looking for... fantastic!

Sorry I didn't run across it in my many searches, I would like to have! I do feel an assumption was made that I didn't search good enough or at all. And the responses harsh. This should be a friendly place whereby a measure of respect is exhibited and the initial assumption does not lean toward the negative. I understand there should be some research done before posting and that questions should be clear. However, what's clear to one individual is not to another. An appropriate question may have been "I don't understand you question, can you clarify what you mean by...?" or "is the config you are parsing one that you created or are pulling from another source?", vs your question isn't clear. My response, respectfully, is... what isn't clear? Happy to clarify. I, again, thought that the "From" this format (where I had already stated I was parsing a file")... "To" that format (hash) was self explanatory.

Not trying to be a jerk here or ungrateful, but come on guys. I feel the problem was as much not reading the question as it was my not making it clear for some of you.

Best regards

Upvotes: 1

Views: 998

Answers (1)

Edgars Jekabsons
Edgars Jekabsons

Reputation: 2853

Well, If You would have searched a bit more, You would probably end up finding this gist:

https://gist.github.com/potatosalad/760726

Does exactly what You want. Not a bad alternative to consider:

https://github.com/markbates/configatron

Upvotes: 1

Related Questions