• proc to get default parameters from json config file

    From Cecil Westerhof@21:1/5 to All on Fri Mar 10 08:42:52 2023
    I want to get default parameters from a json config file. For this I
    wrote the following proc:
    proc getDefaultParams {configDir paramNames} {
    set fileName "~/.config/${configDir}/[file tail ${::argv0}]"
    if { ! [file readable ${fileName}] } {
    puts stderr "Cannot read ${fileName}"
    exit 1
    }
    set fp [open ${fileName} r]
    set jsonData [read $fp]
    close $fp
    if { ! [json::validate ${jsonData}] } {
    puts stderr "${fileName} does not contain correct json data"
    exit 1
    }
    set defaultParams [json::json2dict ${jsonData}]

    foreach name ${paramNames} {
    if { ! [dict exist ${defaultParams} ${name}] } {
    puts stderr "Did not find ${name} in defaultParams"
    puts stderr "Needed: ${paramNames}"
    exit 1
    }
    set ::${name} [dict get ${defaultParams} ${name}]
    }
    }

    One problem is that there is a bug in json::validate, but lets ignore
    that for the moment being. (I started another thread for that.)

    - This works for Linux, but it would be nice if it would work for 'all'
    systems. How would I do that?
    - Is it better tho use unique error codes? If yes, is there a
    'standard' way to do that?
    - Is there a better way to code this? For example all variables are
    defined globally. No problem for where I am going to use it, but
    maybe a problem for others.


    --
    Cecil Westerhof
    Senior Software Engineer
    LinkedIn: http://www.linkedin.com/in/cecilwesterhof

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From heinrichmartin@21:1/5 to Cecil Westerhof on Fri Mar 10 00:05:17 2023
    On Friday, March 10, 2023 at 8:44:07 AM UTC+1, Cecil Westerhof wrote:
    set fileName "~/.config/${configDir}/[file tail ${::argv0}]"
    ...
    - This works for Linux, but it would be nice if it would work for 'all' systems. How would I do that?
    - Is it better tho use unique error codes? If yes, is there a
    'standard' way to do that?
    - Is there a better way to code this? For example all variables are
    defined globally. No problem for where I am going to use it, but
    maybe a problem for others.

    [file nativename [file join ~ .config foo bar]] returns {C:\Users\<user>\.config\foo\bar} here. Is that what you are looking for?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Cecil Westerhof@21:1/5 to heinrichmartin on Fri Mar 10 11:36:57 2023
    heinrichmartin <[email protected]> writes:

    On Friday, March 10, 2023 at 8:44:07 AM UTC+1, Cecil Westerhof wrote:
    set fileName "~/.config/${configDir}/[file tail ${::argv0}]"
    ...
    - This works for Linux, but it would be nice if it would work for 'all'
    systems. How would I do that?
    - Is it better tho use unique error codes? If yes, is there a
    'standard' way to do that?
    - Is there a better way to code this? For example all variables are
    defined globally. No problem for where I am going to use it, but
    maybe a problem for others.

    [file nativename [file join ~ .config foo bar]] returns {C:\Users\<user>\.config\foo\bar} here. Is that what you are looking
    for?

    Could be, but I am not sure. In Linux the place for configuration is
    normally ~/.config. But I think it will be different on Windows and
    Mac. (I expect it to be the same on BSD variants.)
    It looks like on Windows it would be:
    C:\Users\<Username>\AppData

    But I am not missing a library that can be used for this?

    I could use $tcl_platform(os) to check if it is running on a supported
    os and if not exit with an error.

    --
    Cecil Westerhof
    Senior Software Engineer
    LinkedIn: http://www.linkedin.com/in/cecilwesterhof

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Barry Arthur@21:1/5 to Cecil Westerhof on Sat Mar 11 08:24:56 2023
    On 10/3/23 21:36, Cecil Westerhof wrote:
    heinrichmartin <[email protected]> writes:


    [file nativename [file join ~ .config foo bar]] returns
    {C:\Users\<user>\.config\foo\bar} here. Is that what you are looking
    for?

    Could be, but I am not sure. In Linux the place for configuration is
    normally ~/.config. But I think it will be different on Windows and
    Mac. (I expect it to be the same on BSD variants.)
    It looks like on Windows it would be:
    C:\Users\<Username>\AppData

    But I am not missing a library that can be used for this?

    I could use $tcl_platform(os) to check if it is running on a supported
    os and if not exit with an error.


    There is an XDG paths package, https://github.com/LawrenceWoodman/xdgbasedir_tcl , referenced on the
    wiki, https://wiki.tcl-lang.org/page/Where+to+store+application+configuration+files

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)