[twill] Variable expansion [PATCH]
Titus Brown
titus at caltech.edu
Fri Mar 17 14:11:24 PST 2006
added. Now in the usual egg/-latest.tar.gz location.
thanks!
--titus
On Fri, Mar 03, 2006 at 04:48:39PM +0000, Jeff Martin wrote:
-> Don't know if I've just reinvented the wheel, but here's a patch which
-> implements expansion of variables within command arguments. e.g.
->
-> setlocal hostname example.com
-> setlocal moreurl abitmoreurl
->
-> go http://${hostname}/someurl/${moreurl}
->
-> This uses the python eval method so you can also do things like ${var1 +
-> var2} to concatenate variables etc.
->
-> --
-> jeff martin
-> information technologist
-> mkodo limited
->
-> mobile: 44 (0) 78 55 478 331
-> phone: 44 (0) 20 77 29 45 45
-> email: jeff.martin at mkodo.com
->
-> www.mkodo.com
->
-> 73 Leonard St, London, EC2A 4QS. U.K
->
->
->
-> --- twill/parse.py.orig 2006-02-09 01:11:03.000000000 +0000
-> +++ twill/parse.py 2006-03-01 13:07:16.000000000 +0000
-> @@ -12,6 +12,7 @@
->
-> import twill.commands as commands
-> import namespaces
-> +import re
->
-> ### pyparsing stuff
->
-> @@ -76,14 +77,14 @@
-> newargs.append(val)
->
-> # $variable substitution
-> - elif arg.startswith('$'):
-> + elif arg.startswith('$') and not arg.startswith('${'):
-> try:
-> val = eval(arg[1:], globals_dict, locals_dict)
-> except NameError: # not in dictionary; don't interpret.
-> val = arg
-> newargs.append(val)
-> else:
-> - newargs.append(arg)
-> + newargs.append(variable_substitution(arg, globals_dict, locals_dict))
->
-> return newargs
->
-> @@ -222,3 +223,21 @@
-> global _print_commands
-> _print_commands = bool(flag)
->
-> +
-> +variable_expression = re.compile("\${(.*?)}")
-> +
-> +def variable_substitution(raw_str, globals_dict, locals_dict):
-> + str=''
-> + pos = 0
-> + for m in variable_expression.finditer(raw_str):
-> + str = str+raw_str[pos:m.start()]
-> + try:
-> + str = str + eval(m.group(1), globals_dict, locals_dict)
-> + except NameError:
-> + str = str + m.group()
-> + pos = m.end()
-> +
-> + str = str+raw_str[pos:]
-> +
-> + return str
-> +
-> setlocal foo bar
->
-> echo $foo # bar
-> echo ${foo} # bar
-> echo " ${foo}" # bar
-> echo "stuff ${foo} stuff" # stuff bar stuff
-> echo "stuff ${bar} stuff" # stuff ${bar} stuff
-> echo "stuff ${foo + 'bar'} stuff" # stuff barbar stuff
-> _______________________________________________
-> twill mailing list
-> twill at lists.idyll.org
-> http://lists.idyll.org/listinfo/twill
More information about the twill
mailing list