If a local variable is to be referenced with a system command the variable will be escaped with a $. This function replaces the variable with it's value. The $ character can be escaped anywhere with a \ that will be removed. (a literal '\$' then becomes '\\$')
(line)
| 11 | from traceback import print_exc |
| 12 | |
| 13 | def findlocals(line): |
| 14 | """If a local variable is to be referenced with a system command the variable will be escaped with a $. |
| 15 | This function replaces the variable with it's value. |
| 16 | The $ character can be escaped anywhere with a \ that will be removed. |
| 17 | (a literal '\$' then becomes '\\$') |
| 18 | """ |
| 19 | lpos = line.find(' ') |
| 20 | if lpos == -1: return line # if there's no space (no command) then it's not a variable name |
| 21 | command = line[:lpos] |
| 22 | params = line[lpos:].strip() |
| 23 | escapelist = ['$'] # currently just the $ character |
| 24 | variable = '' |
| 25 | varon = 0 |
| 26 | newparams = '' |
| 27 | skip = 0 |
| 28 | params += ' ' # so that a variable at the end of a line is terminated |
| 29 | for char in params: |
| 30 | if skip: # was the last characetr a '\' |
| 31 | if char in escapelist: # if the '\' is followed by a '$' it's not a variable - so jsut move on |
| 32 | newparams += char |
| 33 | else: |
| 34 | newparams += '\\' + char |
| 35 | skip = 0 |
| 36 | continue |
| 37 | |
| 38 | if char == '\\': # is this an escape character ? |
| 39 | skip = 1 |
| 40 | continue |
| 41 | if varon: # are we in the process of replacing a variable name |
| 42 | if char == ' ' or char == '\"': # is this the end of the name ? |
| 43 | if globals().has_key(variable): |
| 44 | newparams += str(globals()[variable]) + ' ' # str or repr ?? |
| 45 | else: |
| 46 | print 'No such variable as ' + variable |
| 47 | return |
| 48 | varon = 0 |
| 49 | variable = '' |
| 50 | else: |
| 51 | variable += char |
| 52 | continue |
| 53 | if char == '$': # have we found the start of a variable name ? |
| 54 | varon = 1 |
| 55 | continue |
| 56 | newparams += char |
| 57 | newline = command + ' ' + newparams # rebuild the command line |
| 58 | return newline |
| 59 | |
| 60 | def chdir(l1): |
| 61 | """A simple function to change the directory. |
no test coverage detected