Skip to content

Commit

Permalink
Allow the config-namespace variable in autoscript
Browse files Browse the repository at this point in the history
  • Loading branch information
crazycatdevs committed Jul 17, 2023
1 parent f80ed01 commit afafd9d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
3 changes: 3 additions & 0 deletions doc/sphinx_source/using/autoscripts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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-<varname>-type | Type of the user-defined channel setting, could be flag, str or int. |
+--------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| config-udef-<varname>-description | Description of user-defined channel setting used by the script. The description is appended with " .chanset <channel> <varname> value" in case of int or str, and with " .channel <channel> +<varname>" when flag |
Expand Down
24 changes: 20 additions & 4 deletions scripts/autoscripts.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -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]} {
Expand Down Expand Up @@ -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]} {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit afafd9d

Please sign in to comment.