##// END OF EJS Templates
envpersist.py extension - boosts %env to remember env variables
vivainio -
Show More
@@ -0,0 +1,78 b''
1 # -*- coding: utf-8 -*-
2 """
3 %store magic for lightweight persistence.
4
5 Stores variables, aliases etc. in PickleShare database.
6
7 $Id: iplib.py 1107 2006-01-30 19:02:20Z vivainio $
8 """
9
10 import IPython.ipapi
11 ip = IPython.ipapi.get()
12
13 import os,sys
14
15 def restore_env(self):
16 ip = self.getapi()
17 env = ip.db.get('stored_env', {'set' : {}, 'add' : []})
18 for k,v in env['set'].items():
19 #print "restore alias",k,v # dbg
20 os.environ[k] = v
21 self.alias_table[k] = v
22 for k,v in env['add']:
23 os.environ[k] = os.environ.get(k,"") + v
24
25
26 ip.set_hook('late_startup_hook', restore_env)
27
28 def persist_env(self, parameter_s=''):
29 """ Store environment variables persistently
30
31 IPython remembers the values across sessions, which is handy to avoid
32 editing startup files.
33
34 %env - Show all environment variables
35 %env VISUAL=jed - set VISUAL to jed
36 %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
40 """
41
42
43
44 if not parameter_s.strip():
45 return os.environ.data
46
47 parts = parameter_s.strip().split('=')
48
49 ip = self.getapi()
50 db = ip.db
51 env = ip.db.get('stored_env', {'set' : {}, 'add' : []})
52
53 if len(parts) == 2:
54 k,v = parts
55
56
57 if v == 'del':
58 if k in env['set']:
59 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)"
63
64 elif k.endswith('+'):
65 k = k[:-1]
66 env['add'].append((k,v))
67 os.environ[k] += v
68 else:
69 env['set'][k] = v
70 print "Setting",k,"to",v
71 os.environ[k] = os.environ.get(k,"") + v
72
73 db['stored_env'] = env
74
75
76
77
78 ip.expose_magic('env', persist_env)
@@ -1,3 +1,9 b''
1 2007-01-10 Ville Vainio <vivainio@gmail.com>
2
3 * Extensions/envpersist.py: Turbocharged %env that remembers
4 env vars across sessions; e.g. "%env PATH+=;/opt/scripts" or
5 "%env VISUAL=jed".
6
1 7 2007-01-05 Fernando Perez <Fernando.Perez@colorado.edu>
2 8
3 9 * IPython/iplib.py (showtraceback): ensure that we correctly call
General Comments 0
You need to be logged in to leave comments. Login now