#! python: #w#VIC variables.py module # # Copyright 2002, 2003 by Timothy Rue <3seas@threeseas.net> # # VIC variables.py module: version 0.5.1.python (BETA) # # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation version 2 # of the License. http://www.gnu.org/copyleft/gpl.html # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # Or access http://www.gnu.org/copyleft/gpl.html ########################################################################## #August 29, 2003 made file IQ parseable 0.5.1 #July 24, 2003 initial release 0.5 # ########################################################################## #s#system module import string #s#local module import files ### #w# Module for doing variable substitution in a command line ### or other string. Variables are of the form $foo or $Bar ### They are case sesnsetive. '\$varname' is not a variable ### name, and to embed them use a notation like ${bar}bar ### where the variable name is encased in '{}' (otherwise ### It's not possible to tell if the var is 'bar' or 'barbar') ### Variables have a default value of empty-string, that is ### if the given variable dictionary has no entry for a ### particular name, then it is replaced with an empty string ### #s#def varVal ### ### It's in the dictionary, or it's an empty string ### First check the focused VIC's variable table, and if ### not found then check the global variables. ### def varVal(var_dict,global_dict,key): content = "" if (var_dict.has_key(key)): content = var_dict[key] elif (global_dict.has_key(key)): content = global_dict[key] ### if the variable content is stored in a file, read & return it if (len(content) > 0 and content[0] == '@'): content = files.gulpFile(content[1:len(content)]) return content #s#def replaceVars ### ### Given a command line (or any string really), subsititute variables ### with either a known content or an empty string. Variables are ### defined as $name or ${name} but NOT \$anything ### def replaceVars(command,var_dict,global_dict): output = "" varchars = "_1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" var_start_index = -1 cursor = 0 while (1): if (var_start_index >= 0): ### inside a variable if (cursor == len(command)): ### Ran out of string, so close off variable var_name = command[var_start_index+1:cursor] output = output + varVal(var_dict,global_dict,var_name) elif (not command[cursor] in varchars): if (command[cursor] == '{' and command[cursor-1] == '$'): var_start_index = cursor else: ### Var has ended - output it #print "VAR: ["+command[var_start_index+1:cursor]+"]" var_name = command[var_start_index+1:cursor] output = output + varVal(var_dict,global_dict,var_name) var_start_index = -1 if (command[cursor] != '}'): cursor = cursor - 1 ### Skip back a space to re-process without var processing elif (cursor < len(command)): ### not inside a variable if (command[cursor] == '$'): if (cursor == 0 or (cursor > 0 and command[cursor-1] != '\\')): ### new variable starts var_start_index = cursor elif (cursor > 0 and command[cursor-1] == '\\'): ### Handle escaped dollar '\$' output = output[0:len(output)-1] output = output + '$' else: output = output + command[cursor] ### Step to the next chaaracter in the input cursor = cursor + 1 ### Game over if (cursor == len(command)+1): break return output