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