I am trying to figure out how to code the following problem using python. Suppose we have the following data set in a .txt file:
package autoload
config core 'main'
option Enabled 'no'
option StartTimer '120'
option RetryTimer '30'
option BackoffTimer '15'
option BootUsingConfig 'altconfig'
option BootUsingImage 'altimage'
config entry
option Configured 'yes'
option SegmentName 'altconfig'
option RemoteFilename '$$.ini'
package cwatch
config watch '3g_watch'
option enabled 'yes'
option test_ifaces 'wan1 wan2'
option failure_time_1 '30m'
option failure_action_1 'reboot'
Expecting result as such:
{"autoload":{"core 'main'":{"Enabled": "no", "StartTimer": "120", ...},
"entry":{"Configure": "yes", "SegmentName": "altconfig", ...},
...},
"cwatch": {"watch '3g_watch'":{"Enabled": "yes", "test_ifaces":"wan1 wan2", ...}}
}
I'm stuck at here and not sure what to do next.
numRegex = re.compile(r'^package (\w*)\s*^config (\w*.*\w*)', re.M)
with open(file) as f:
data = {m.groups() for m in numRegex.finditer(f.read())}