Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[ANN] Configurator v0.2

49 views
Skip to first unread message

Lawrence Woodman

unread,
Jul 1, 2015, 1:30:44 AM7/1/15
to

A Configuration Parsing Module for Tcl

This module provides a simple way to parse a configuration script and
create a dictionary from it. The configuration script is a valid Tcl
script, where the commands are used to set keys within the returned
configuration dictionary. The script is parsed using a safe
interpreter.

Project page:
http://vlifesystems.com/projects/configurator/
Github:
https://github.com/LawrenceWoodman/configurator_tcl


Changes:
Configurator v0.2 is the first proper public release of the module,
the most recent change is supporting slave commands with command
prefixes.


Module Usage
If you don't pass the -keys option then any command that isn't
recognised, is taken to be a command that sets a key with the same
name to the single value given as an argument to it. A configuration
script is a valid Tcl script where the commands are used to set keys
within the returned configuration dictionary. If you don't pass the
-keys option then any command that isn't recognised, is taken to be
a command that sets a key with the same name to the single value
given as an argument to it.

package require configurator
namespace import configurator::*

set script {
device /dev/hda
desc {Main hdd}
write_cache 1
dma 1
options {ro boost}
}

# Outputs a dictionary:
# device /dev/hda desc {Main hdd} write_cache 1 dma 1 options {ro boost}
puts [parseConfig $script]



If you wanted to make the configuration script more resilient you can
specify the accepted keys:

set script {
device /dev/hda
desc {Main hdd}
write_cache 1
!dma 1
options ro boost
}

# Note the use of many, for options in the keys dictionary,
# which allows options to take multiple values in the script.
# You can also see that !dma is used as the command to set the
# dma field.
set keys {
device {device 1 "1|0"}
desc {desc 1 "description"}
write_cache {write_cache 1 "1|0"}
!dma {dma 1 "1|0"}
options {options many "option ?option ...?"}
}

# Outputs the same dictionary as above:
# device /dev/hda desc {Main hdd} write_cache 1 dma 1 options {ro boost}
puts [parseConfig -keys $keys $script]



To make the configuration scripts more flexible you can export
commands that have been hidden:

set script {
set a 5
titles "Title with the number $a in it" \
"Five is [%string length "five"] characters long"
}

set keys {
titles {many "title ?title ...?"}
}

set exposeCmds {
set set
%string string
}

# Outputs a dictionary:
# titles {{Title with the number 5 in it} {Five is 4 characters long}}
puts [parseConfig -keys $keys -exposeCmds $exposeCmds $script]



If you wanted to be able to access commands from the master interpreter
you can use the -masterCmds option:

proc sum {a b} {
expr {$a + $b}
}

set script {
title "The sum of 5 and 6 is [%sum 5 6]"
}

set masterCmds {
%sum sum
}

parseConfig -masterCmds $masterCmds $script



If you wanted to be able to access commands from the master interpreter,
which have access to the slave interpreter you can use the -slaveCmds option:

proc seta {int value} {
$int invokehidden set a $value
}

set script {
%seta 7
title "a is set to: $a"
}

set slaveCmds {
%seta seta
}

parseConfig -slaveCmds $slaveCmds $script



Feedback and contributions:
Both are very welcome and ideally should be made via GitHub or failing
that to me directly: lwoo...@vlifesystems.com


Licence:
The module is released under an MIT licence.


Best Wishes


Lorry


http://vlifesystems.com



--
vLife Systems Ltd
Registered Office: The Apex, 2 Sheriffs Orchard, Coventry, CV1 3PP
Registered in England and Wales No. 06477649
http://vlifesystems.com
0 new messages