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