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