##// END OF EJS Templates
%env completer
vivainio -
Show More

The requested changes are too big and content was truncated. Show full diff

@@ -1,89 +1,90 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """ %env magic command for storing environment variables persistently
2 """ %env magic command for storing environment variables persistently
3 """
3 """
4
4
5 import IPython.ipapi
5 import IPython.ipapi
6 ip = IPython.ipapi.get()
6 ip = IPython.ipapi.get()
7
7
8 import os,sys
8 import os,sys
9
9
10 def restore_env(self):
10 def restore_env(self):
11 ip = self.getapi()
11 ip = self.getapi()
12 env = ip.db.get('stored_env', {'set' : {}, 'add' : [], 'pre' : []})
12 env = ip.db.get('stored_env', {'set' : {}, 'add' : [], 'pre' : []})
13 for k,v in env['set'].items():
13 for k,v in env['set'].items():
14 os.environ[k] = v
14 os.environ[k] = v
15 for k,v in env['add']:
15 for k,v in env['add']:
16 os.environ[k] = os.environ.get(k,"") + v
16 os.environ[k] = os.environ.get(k,"") + v
17 for k,v in env['pre']:
17 for k,v in env['pre']:
18 os.environ[k] = v + os.environ.get(k,"")
18 os.environ[k] = v + os.environ.get(k,"")
19
19
20
20
21 ip.set_hook('late_startup_hook', restore_env)
21 ip.set_hook('late_startup_hook', restore_env)
22
22
23 def persist_env(self, parameter_s=''):
23 def persist_env(self, parameter_s=''):
24 """ Store environment variables persistently
24 """ Store environment variables persistently
25
25
26 IPython remembers the values across sessions, which is handy to avoid
26 IPython remembers the values across sessions, which is handy to avoid
27 editing startup files.
27 editing startup files.
28
28
29 %env - Show all environment variables
29 %env - Show all environment variables
30 %env VISUAL=jed - set VISUAL to jed
30 %env VISUAL=jed - set VISUAL to jed
31 %env PATH+=;/foo - append ;foo to PATH
31 %env PATH+=;/foo - append ;foo to PATH
32 %env PATH+=;/bar - also append ;bar to PATH
32 %env PATH+=;/bar - also append ;bar to PATH
33 %env PATH-=/wbin; - prepend /wbin; to PATH
33 %env PATH-=/wbin; - prepend /wbin; to PATH
34 %env -d VISUAL - forget VISUAL persistent val
34 %env -d VISUAL - forget VISUAL persistent val
35 %env -p - print all persistent env modifications
35 %env -p - print all persistent env modifications
36 """
36 """
37
37
38
39
40 if not parameter_s.strip():
38 if not parameter_s.strip():
41 return os.environ.data
39 return os.environ.data
42
40
43 ip = self.getapi()
41 ip = self.getapi()
44 db = ip.db
42 db = ip.db
45 env = ip.db.get('stored_env', {'set' : {}, 'add' : [], 'pre' : []})
43 env = ip.db.get('stored_env', {'set' : {}, 'add' : [], 'pre' : []})
46
44
47 if parameter_s.startswith('-p'):
45 if parameter_s.startswith('-p'):
48 return env
46 return env
49
47
50 elif parameter_s.startswith('-d'):
48 elif parameter_s.startswith('-d'):
51 parts = (parameter_s.split()[1], '<del>')
49 parts = (parameter_s.split()[1], '<del>')
52
50
53 else:
51 else:
54 parts = parameter_s.strip().split('=')
52 parts = parameter_s.strip().split('=')
55
53
56 if len(parts) == 2:
54 if len(parts) == 2:
57 k,v = [p.strip() for p in parts]
55 k,v = [p.strip() for p in parts]
58
56
59 if v == '<del>':
57 if v == '<del>':
60 if k in env['set']:
58 if k in env['set']:
61 del env['set'][k]
59 del env['set'][k]
62 env['add'] = [el for el in env['add'] if el[0] != k]
60 env['add'] = [el for el in env['add'] if el[0] != k]
63 env['pre'] = [el for el in env['pre'] if el[0] != k]
61 env['pre'] = [el for el in env['pre'] if el[0] != k]
64
62
65 print "Forgot '%s' (for next session)" % k
63 print "Forgot '%s' (for next session)" % k
66
64
67 elif k.endswith('+'):
65 elif k.endswith('+'):
68 k = k[:-1]
66 k = k[:-1]
69 env['add'].append((k,v))
67 env['add'].append((k,v))
70 os.environ[k] += v
68 os.environ[k] += v
71 print k,"after append =",os.environ[k]
69 print k,"after append =",os.environ[k]
72 elif k.endswith('-'):
70 elif k.endswith('-'):
73 k = k[:-1]
71 k = k[:-1]
74 env['pre'].append((k,v))
72 env['pre'].append((k,v))
75 os.environ[k] = v + os.environ.get(k,"")
73 os.environ[k] = v + os.environ.get(k,"")
76 print k,"after prepend =",os.environ[k]
74 print k,"after prepend =",os.environ[k]
77
75
78
76
79 else:
77 else:
80 env['set'][k] = v
78 env['set'][k] = v
81 print "Setting",k,"to",v
79 print "Setting",k,"to",v
82 os.environ[k] = v
80 os.environ[k] = v
83
81
84 db['stored_env'] = env
82 db['stored_env'] = env
85
86
83
87
84 def env_completer(self,event):
85 """ Custom completer that lists all env vars """
86 return os.environ.keys()
88
87
89 ip.expose_magic('env', persist_env)
88 ip.expose_magic('env', persist_env)
89 ip.set_hook('complete_command',env_completer, str_key = '%env')
90
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
General Comments 0
You need to be logged in to leave comments. Login now