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