MCPcopy Create free account
hub / github.com/ActiveState/code / findlocals

Function findlocals

recipes/Python/279407_Windows_Console/recipe-279407.py:13–58  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

11from traceback import print_exc
12
13def 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
60def chdir(l1):
61 """A simple function to change the directory.

Callers 1

recipe-279407.pyFile · 0.85

Calls 4

strFunction · 0.85
findMethod · 0.45
stripMethod · 0.45
has_keyMethod · 0.45

Tested by

no test coverage detected