diff --git a/site_ansto/instrument/util/config_reader.tcl b/site_ansto/instrument/util/config_reader.tcl index ad72e992..482f6203 100644 --- a/site_ansto/instrument/util/config_reader.tcl +++ b/site_ansto/instrument/util/config_reader.tcl @@ -4,16 +4,19 @@ namespace eval config_reader { variable version 2.0 } +# return a list of section names from the dict proc config_reader::get_sections {the_dict} { upvar 1 $the_dict db return [dict keys $db] } +# return a list of variables in the named section of the dict proc config_reader::get_variables {the_dict section} { upvar 1 $the_dict db return [dict keys [dict get $db $section]] } +# return the value of the named variable in the named section proc config_reader::get_var {the_dict section varname} { upvar 1 $the_dict db set varname [string tolower $varname] @@ -26,6 +29,7 @@ proc config_reader::get_var {the_dict section varname} { return [dict get $db $section $varname] } +# add a new section to the dictionary proc config_reader::add_section {the_dict section} { upvar 1 $the_dict db @@ -34,6 +38,7 @@ proc config_reader::add_section {the_dict section} { } } +# add a new section/variable and set its value proc config_reader::set_var {the_dict section varname value} { upvar 1 $the_dict db if {![dict exists $db $section]} { @@ -43,6 +48,7 @@ proc config_reader::set_var {the_dict section varname value} { dict set db $section $varname $value } +# Load the named configuration file and return the dict proc config_reader::parse_file {filename} { variable dictionary [dict create] variable cursection @@ -58,11 +64,20 @@ proc config_reader::parse_file {filename} { config_reader::add_section dictionary $cursection } .*=.* { + # split on the '=' and trim leading and trailing spaces set pair [split $line =] set name [string trim [lindex $pair 0] " "] set value [string trim [lindex $pair 1] " "] + # Remove matching quotes on both ends (single, double and braces) + if { [string index $value 0] == "'" && [string index $value end] == "'" } { + set value [string range 1 end-1] + } elseif { [string index $value 0] == "\"" && [string index $value end] == "\"" } { + set value [string range 1 end-1] + } elseif { [string index $value 0] == "{" && [string index $value end] == "}" } { + set value [string range 1 end-1] + } config_reader::set_var dictionary $cursection $name $value - } + } default { error "Error parsing $filename (line: $line_no): $line" } @@ -73,21 +88,23 @@ proc config_reader::parse_file {filename} { return $dictionary } - proc config_reader::dump {{the_config startup.ini}} { - set my_dict [config_reader::parse_file $the_config] - puts "Dict: $my_dict" - foreach s [config_reader::get_sections my_dict] { - set section "$s" - puts "\n\[$section\]" - foreach n [config_reader::get_variables my_dict $s] { - set name "$n" - set value "[config_reader::get_var my_dict $section $n]" - puts " $name = $value" - } +# Load the named file with parse_file print and return the dict for testing +proc config_reader::dump {{the_config startup.ini}} { + set my_dict [config_reader::parse_file $the_config] + puts "Dict: $my_dict" + foreach s [config_reader::get_sections my_dict] { + set section "$s" + puts "\n\[$section\]" + foreach n [config_reader::get_variables my_dict $s] { + set name "$n" + set value "[config_reader::get_var my_dict $section $n]" + puts " $name = $value" } - return $my_dict } + return $my_dict +} +# This code is for i=unit testing in the tclsh executable if { "[lindex [split [info nameofexecutable] "/"] end]" == "tclsh"} { set filename "/tmp/test_[pid].ini" set sects [list]