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