##// END OF EJS Templates
sanitize %env options, add prepend and print
vivainio -
Show More
@@ -14,13 +14,15 b' import os,sys'
14 14
15 15 def restore_env(self):
16 16 ip = self.getapi()
17 env = ip.db.get('stored_env', {'set' : {}, 'add' : []})
17 env = ip.db.get('stored_env', {'set' : {}, 'add' : [], 'pre' : []})
18 18 for k,v in env['set'].items():
19 19 #print "restore alias",k,v # dbg
20 20 os.environ[k] = v
21 21 self.alias_table[k] = v
22 22 for k,v in env['add']:
23 23 os.environ[k] = os.environ.get(k,"") + v
24 for k,v in env['pre']:
25 os.environ[k] = v + os.environ.get(k,"")
24 26
25 27
26 28 ip.set_hook('late_startup_hook', restore_env)
@@ -34,9 +36,10 b" def persist_env(self, parameter_s=''):"
34 36 %env - Show all environment variables
35 37 %env VISUAL=jed - set VISUAL to jed
36 38 %env PATH+=;/foo - append ;foo to PATH
37 %env PATH+=;/foo - also append ;bar to PATH
38 %env VISUAL=del - forget VISUAL persistent val
39
39 %env PATH+=;/bar - also append ;bar to PATH
40 %env PATH-=/wbin; - prepend /wbin; ts PATH
41 %env -d VISUAL - forget VISUAL persistent val
42 %env -p - print all persistent env modifications
40 43 """
41 44
42 45
@@ -44,27 +47,42 b" def persist_env(self, parameter_s=''):"
44 47 if not parameter_s.strip():
45 48 return os.environ.data
46 49
47 parts = parameter_s.strip().split('=')
48
49 50 ip = self.getapi()
50 51 db = ip.db
51 env = ip.db.get('stored_env', {'set' : {}, 'add' : []})
52 env = ip.db.get('stored_env', {'set' : {}, 'add' : [], 'pre' : []})
52 53
54 if parameter_s.startswith('-p'):
55 return env
56
57 elif parameter_s.startswith('-d'):
58 parts = (parameter_s.split()[1], '<del>')
59
60 else:
61 parts = parameter_s.strip().split('=')
62
53 63 if len(parts) == 2:
54 64 k,v = parts
55
56 65
57 if v == 'del':
66 if v == '<del>':
58 67 if k in env['set']:
59 68 del env['set'][k]
60 env['add'] = [el for el in env['add'] if el[0] == k]
61 #del os.environ[k]
62 print "Forgot",k,"(for next session)"
69 env['add'] = [el for el in env['add'] if el[0] != k]
70 env['pre'] = [el for el in env['pre'] if el[0] != k]
71
72 print "Forgot '%s' (for next session)" % k
63 73
64 74 elif k.endswith('+'):
65 75 k = k[:-1]
66 76 env['add'].append((k,v))
67 77 os.environ[k] += v
78 print k,"after append =",os.environ[k]
79 elif k.endswith('-'):
80 k = k[:-1]
81 env['pre'].append((k,v))
82 os.environ[k] = v + os.environ.get(k,"")
83 print k,"after prepend =",os.environ[k]
84
85
68 86 else:
69 87 env['set'][k] = v
70 88 print "Setting",k,"to",v
General Comments 0
You need to be logged in to leave comments. Login now