~ipython-contrib/+junk/ipython-zmq

« back to all changes in this revision

Viewing changes to IPython/Extensions/envpersist.py

  • Committer: ville
  • Date: 2008-02-16 09:50:47 UTC
  • mto: (0.12.1 ipython_main)
  • mto: This revision was merged to the branch mainline in revision 990.
  • Revision ID: ville@ville-pc-20080216095047-500x6dluki1iz40o
initialization (no svn history)

Show diffs side-by-side

added added

removed removed

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