##// END OF EJS Templates
typofix
vivainio -
Show More
@@ -1,96 +1,96 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """
2 """
3 %store magic for lightweight persistence.
3 %store magic for lightweight persistence.
4
4
5 Stores variables, aliases etc. in PickleShare database.
5 Stores variables, aliases etc. in PickleShare database.
6
6
7 $Id: iplib.py 1107 2006-01-30 19:02:20Z vivainio $
7 $Id: iplib.py 1107 2006-01-30 19:02:20Z vivainio $
8 """
8 """
9
9
10 import IPython.ipapi
10 import IPython.ipapi
11 ip = IPython.ipapi.get()
11 ip = IPython.ipapi.get()
12
12
13 import os,sys
13 import os,sys
14
14
15 def restore_env(self):
15 def restore_env(self):
16 ip = self.getapi()
16 ip = self.getapi()
17 env = ip.db.get('stored_env', {'set' : {}, 'add' : [], 'pre' : []})
17 env = ip.db.get('stored_env', {'set' : {}, 'add' : [], 'pre' : []})
18 for k,v in env['set'].items():
18 for k,v in env['set'].items():
19 #print "restore alias",k,v # dbg
19 #print "restore alias",k,v # dbg
20 os.environ[k] = v
20 os.environ[k] = v
21 self.alias_table[k] = v
21 self.alias_table[k] = v
22 for k,v in env['add']:
22 for k,v in env['add']:
23 os.environ[k] = os.environ.get(k,"") + v
23 os.environ[k] = os.environ.get(k,"") + v
24 for k,v in env['pre']:
24 for k,v in env['pre']:
25 os.environ[k] = v + os.environ.get(k,"")
25 os.environ[k] = v + os.environ.get(k,"")
26
26
27
27
28 ip.set_hook('late_startup_hook', restore_env)
28 ip.set_hook('late_startup_hook', restore_env)
29
29
30 def persist_env(self, parameter_s=''):
30 def persist_env(self, parameter_s=''):
31 """ Store environment variables persistently
31 """ Store environment variables persistently
32
32
33 IPython remembers the values across sessions, which is handy to avoid
33 IPython remembers the values across sessions, which is handy to avoid
34 editing startup files.
34 editing startup files.
35
35
36 %env - Show all environment variables
36 %env - Show all environment variables
37 %env VISUAL=jed - set VISUAL to jed
37 %env VISUAL=jed - set VISUAL to jed
38 %env PATH+=;/foo - append ;foo to PATH
38 %env PATH+=;/foo - append ;foo to PATH
39 %env PATH+=;/bar - also append ;bar to PATH
39 %env PATH+=;/bar - also append ;bar to PATH
40 %env PATH-=/wbin; - prepend /wbin; ts PATH
40 %env PATH-=/wbin; - prepend /wbin; to PATH
41 %env -d VISUAL - forget VISUAL persistent val
41 %env -d VISUAL - forget VISUAL persistent val
42 %env -p - print all persistent env modifications
42 %env -p - print all persistent env modifications
43 """
43 """
44
44
45
45
46
46
47 if not parameter_s.strip():
47 if not parameter_s.strip():
48 return os.environ.data
48 return os.environ.data
49
49
50 ip = self.getapi()
50 ip = self.getapi()
51 db = ip.db
51 db = ip.db
52 env = ip.db.get('stored_env', {'set' : {}, 'add' : [], 'pre' : []})
52 env = ip.db.get('stored_env', {'set' : {}, 'add' : [], 'pre' : []})
53
53
54 if parameter_s.startswith('-p'):
54 if parameter_s.startswith('-p'):
55 return env
55 return env
56
56
57 elif parameter_s.startswith('-d'):
57 elif parameter_s.startswith('-d'):
58 parts = (parameter_s.split()[1], '<del>')
58 parts = (parameter_s.split()[1], '<del>')
59
59
60 else:
60 else:
61 parts = parameter_s.strip().split('=')
61 parts = parameter_s.strip().split('=')
62
62
63 if len(parts) == 2:
63 if len(parts) == 2:
64 k,v = parts
64 k,v = parts
65
65
66 if v == '<del>':
66 if v == '<del>':
67 if k in env['set']:
67 if k in env['set']:
68 del env['set'][k]
68 del env['set'][k]
69 env['add'] = [el for el in env['add'] if el[0] != k]
69 env['add'] = [el for el in env['add'] if el[0] != k]
70 env['pre'] = [el for el in env['pre'] if el[0] != k]
70 env['pre'] = [el for el in env['pre'] if el[0] != k]
71
71
72 print "Forgot '%s' (for next session)" % k
72 print "Forgot '%s' (for next session)" % k
73
73
74 elif k.endswith('+'):
74 elif k.endswith('+'):
75 k = k[:-1]
75 k = k[:-1]
76 env['add'].append((k,v))
76 env['add'].append((k,v))
77 os.environ[k] += v
77 os.environ[k] += v
78 print k,"after append =",os.environ[k]
78 print k,"after append =",os.environ[k]
79 elif k.endswith('-'):
79 elif k.endswith('-'):
80 k = k[:-1]
80 k = k[:-1]
81 env['pre'].append((k,v))
81 env['pre'].append((k,v))
82 os.environ[k] = v + os.environ.get(k,"")
82 os.environ[k] = v + os.environ.get(k,"")
83 print k,"after prepend =",os.environ[k]
83 print k,"after prepend =",os.environ[k]
84
84
85
85
86 else:
86 else:
87 env['set'][k] = v
87 env['set'][k] = v
88 print "Setting",k,"to",v
88 print "Setting",k,"to",v
89 os.environ[k] = os.environ.get(k,"") + v
89 os.environ[k] = os.environ.get(k,"") + v
90
90
91 db['stored_env'] = env
91 db['stored_env'] = env
92
92
93
93
94
94
95
95
96 ip.expose_magic('env', persist_env)
96 ip.expose_magic('env', persist_env)
General Comments 0
You need to be logged in to leave comments. Login now