From afafd9d71e2976c4ce5623c99cb153208d08b386 Mon Sep 17 00:00:00 2001 From: AnoBug Date: Mon, 17 Jul 2023 09:04:55 +0200 Subject: [PATCH] Allow the config-namespace variable in autoscript --- doc/sphinx_source/using/autoscripts.rst | 3 +++ scripts/autoscripts.tcl | 24 ++++++++++++++++++++---- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/doc/sphinx_source/using/autoscripts.rst b/doc/sphinx_source/using/autoscripts.rst index 02b53c0cfe..fbbc0c746f 100644 --- a/doc/sphinx_source/using/autoscripts.rst +++ b/doc/sphinx_source/using/autoscripts.rst @@ -71,6 +71,7 @@ Every autoscripts package must have a manifest.json file. This file contains met "long_description": "This is an example script to help understand the autoscript system. Yeah, it doesn't really do anything, but that's besides the point. It could, and that should be enough for anyone" "config": { "loaded": 0, + "namespace": "", "udef": { "myflag": { "type": "flag", @@ -129,6 +130,8 @@ Every autoscripts package must have a manifest.json file. This file contains met +--------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | config-loaded | Whether this script is currently loaded or not. It should be default set to 0. | +--------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| config-namespace | If the script uses a namespace which is not the same as the file name, fill this variable with the full namespace, as "::myns::mysubns" | ++--------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | config-udef--type | Type of the user-defined channel setting, could be flag, str or int. | +--------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | config-udef--description | Description of user-defined channel setting used by the script. The description is appended with " .chanset value" in case of int or str, and with " .channel +" when flag | diff --git a/scripts/autoscripts.tcl b/scripts/autoscripts.tcl index 33fb35bba6..200577f8ea 100644 --- a/scripts/autoscripts.tcl +++ b/scripts/autoscripts.tcl @@ -95,11 +95,11 @@ proc loadscripts {} { global jsondict global eggdir foreach scriptentry $jsondict { - namespace eval ::[dict get $scriptentry name] {} + set tns [makens $scriptentry [dict get $scriptentry]] if [dict get $scriptentry config loaded] { if {[dict exists $scriptentry config vars]} { foreach configvar [dict keys [dict get $scriptentry config vars] *] { - set ::[dict get $scriptentry name]::$configvar [dict get $scriptentry config vars $configvar value] + set ${tns}::${configvar} [dict get $scriptentry config vars $configvar value] } } if {[catch {uplevel #0 source $eggdir/[dict get $scriptentry name]/[dict get $scriptentry name].tcl} err]} { @@ -189,10 +189,10 @@ proc egg_load {idx script loadme} { if [string match $script [dict get $scriptentry name]] { set found 1 if {$loadme} { - namespace eval ::[dict get $scriptentry name] {} + set tns [makens $scriptentry [dict get $scriptentry]] if {[dict exists $scriptentry config vars]} { foreach configvar [dict keys [dict get $scriptentry config vars] *] { - set ::[dict get $scriptentry name]::$configvar [dict get $scriptentry config vars $configvar value] + set ${tns}::${configvar} [dict get $scriptentry config vars $configvar value] } } if {[catch {uplevel #0 source $eggdir/${script}/${script}.tcl} err]} { @@ -557,6 +557,22 @@ proc compile_json {spec data} { } } +# Create namespaces for a loaded script +proc makens {script {ns ""}} { + if {$ns eq "" || ![dict exists $ns config namespace]} { + namespace eval ::$script {} + set tns "::$script" + } else { + set tns "" + foreach sns [split [dict get $ns config namespace] "::"] { + if {$sns eq ""} continue; + append tns "::$sns" + namespace eval $tns {} + } + } + return $tns +} + if {![file exists autoscripts]} {file mkdir autoscripts} readjsonfile loadscripts