envpersist.py
92 lines
| 2.5 KiB
| text/x-python
|
PythonLexer
vivainio
|
r508 | # -*- coding: utf-8 -*- | ||
vivainio
|
r638 | """ %env magic command for storing environment variables persistently | ||
vivainio
|
r508 | """ | ||
Brian Granger
|
r2027 | from IPython.core import ipapi | ||
Brian Granger
|
r2205 | from IPython.core.error import TryNext | ||
Brian Granger
|
r2027 | ip = ipapi.get() | ||
vivainio
|
r508 | |||
import os,sys | ||||
vivainio
|
r784 | def restore_env(self): | ||
vivainio
|
r508 | ip = self.getapi() | ||
vivainio
|
r509 | env = ip.db.get('stored_env', {'set' : {}, 'add' : [], 'pre' : []}) | ||
vivainio
|
r508 | for k,v in env['set'].items(): | ||
os.environ[k] = v | ||||
for k,v in env['add']: | ||||
os.environ[k] = os.environ.get(k,"") + v | ||||
vivainio
|
r509 | for k,v in env['pre']: | ||
os.environ[k] = v + os.environ.get(k,"") | ||||
Brian Granger
|
r2205 | raise TryNext | ||
vivainio
|
r508 | |||
ip.set_hook('late_startup_hook', restore_env) | ||||
def persist_env(self, parameter_s=''): | ||||
""" Store environment variables persistently | ||||
IPython remembers the values across sessions, which is handy to avoid | ||||
editing startup files. | ||||
%env - Show all environment variables | ||||
%env VISUAL=jed - set VISUAL to jed | ||||
%env PATH+=;/foo - append ;foo to PATH | ||||
vivainio
|
r509 | %env PATH+=;/bar - also append ;bar to PATH | ||
vivainio
|
r510 | %env PATH-=/wbin; - prepend /wbin; to PATH | ||
vivainio
|
r509 | %env -d VISUAL - forget VISUAL persistent val | ||
%env -p - print all persistent env modifications | ||||
vivainio
|
r508 | """ | ||
if not parameter_s.strip(): | ||||
return os.environ.data | ||||
ip = self.getapi() | ||||
db = ip.db | ||||
vivainio
|
r509 | env = ip.db.get('stored_env', {'set' : {}, 'add' : [], 'pre' : []}) | ||
vivainio
|
r508 | |||
vivainio
|
r509 | if parameter_s.startswith('-p'): | ||
return env | ||||
elif parameter_s.startswith('-d'): | ||||
parts = (parameter_s.split()[1], '<del>') | ||||
else: | ||||
parts = parameter_s.strip().split('=') | ||||
vivainio
|
r508 | if len(parts) == 2: | ||
vivainio
|
r693 | k,v = [p.strip() for p in parts] | ||
vivainio
|
r508 | |||
vivainio
|
r509 | if v == '<del>': | ||
vivainio
|
r508 | if k in env['set']: | ||
del env['set'][k] | ||||
vivainio
|
r509 | env['add'] = [el for el in env['add'] if el[0] != k] | ||
env['pre'] = [el for el in env['pre'] if el[0] != k] | ||||
print "Forgot '%s' (for next session)" % k | ||||
vivainio
|
r508 | |||
elif k.endswith('+'): | ||||
k = k[:-1] | ||||
env['add'].append((k,v)) | ||||
os.environ[k] += v | ||||
vivainio
|
r509 | print k,"after append =",os.environ[k] | ||
elif k.endswith('-'): | ||||
k = k[:-1] | ||||
env['pre'].append((k,v)) | ||||
os.environ[k] = v + os.environ.get(k,"") | ||||
print k,"after prepend =",os.environ[k] | ||||
vivainio
|
r508 | else: | ||
env['set'][k] = v | ||||
print "Setting",k,"to",v | ||||
vivainio
|
r693 | os.environ[k] = v | ||
vivainio
|
r508 | |||
db['stored_env'] = env | ||||
vivainio
|
r793 | def env_completer(self,event): | ||
""" Custom completer that lists all env vars """ | ||||
return os.environ.keys() | ||||
vivainio
|
r508 | |||
Brian Granger
|
r2205 | ip.define_magic('env', persist_env) | ||
vivainio
|
r793 | ip.set_hook('complete_command',env_completer, str_key = '%env') | ||