mirror of
https://github.com/xonsh/xonsh.git
synced 2025-03-04 08:24:40 +01:00
event on_envvar
This commit is contained in:
parent
86f30316f2
commit
d3227a3f14
3 changed files with 37 additions and 0 deletions
13
news/event-envvar.rst
Normal file
13
news/event-envvar.rst
Normal file
|
@ -0,0 +1,13 @@
|
|||
**Added:**
|
||||
|
||||
* new event ``on_envvar`` that fires after an enviroment variable has been changed.
|
||||
|
||||
**Changed:** None
|
||||
|
||||
**Deprecated:** None
|
||||
|
||||
**Removed:** None
|
||||
|
||||
**Fixed:** None
|
||||
|
||||
**Security:** None
|
|
@ -140,3 +140,19 @@ def test_locate_binary_on_windows(xonsh_builtins):
|
|||
assert locate_binary('file2') == os.path.join(tmpdir, 'FILE2.BAT')
|
||||
assert locate_binary('file2.bat') == os.path.join(tmpdir, 'FILE2.BAT')
|
||||
assert locate_binary('file3') is None
|
||||
|
||||
|
||||
def test_event_on_envvar(xonsh_builtins):
|
||||
env = Env(TEST=0)
|
||||
xonsh_builtins.__xonsh_env__ = env
|
||||
share = []
|
||||
# register
|
||||
@xonsh_builtins.events.on_envvar
|
||||
def handler(name, oldvalue, newvalue, **kwargs):
|
||||
share.extend((name,oldvalue, newvalue))
|
||||
|
||||
# trigger
|
||||
env['TEST'] = 1
|
||||
|
||||
assert share == ['TEST', 0, 1]
|
||||
# assert len(share) > 0
|
||||
|
|
|
@ -18,6 +18,7 @@ from xonsh import __version__ as XONSH_VERSION
|
|||
from xonsh.lazyasd import LazyObject, lazyobject
|
||||
from xonsh.codecache import run_script_with_cache
|
||||
from xonsh.dirstack import _get_cwd
|
||||
from xonsh.events import events
|
||||
from xonsh.foreign_shells import load_foreign_envs
|
||||
from xonsh.platform import (
|
||||
BASH_COMPLETIONS_DEFAULT, DEFAULT_ENCODING, PATH_DEFAULT,
|
||||
|
@ -41,6 +42,11 @@ from xonsh.tools import (
|
|||
)
|
||||
import xonsh.prompt.base as prompt
|
||||
|
||||
events.doc('on_envvar', """
|
||||
on_envvar(name: str, oldvalue, newvalue) -> None
|
||||
|
||||
Fires after an enviromental variable has changed.
|
||||
""")
|
||||
|
||||
@lazyobject
|
||||
def HELP_TEMPLATE():
|
||||
|
@ -913,6 +919,7 @@ class Env(cabc.MutableMapping):
|
|||
ensurer = self.get_ensurer(key)
|
||||
if not ensurer.validate(val):
|
||||
val = ensurer.convert(val)
|
||||
old_val = self._d.get(key)
|
||||
self._d[key] = val
|
||||
if self.detypeable(val):
|
||||
self._detyped = None
|
||||
|
@ -921,6 +928,7 @@ class Env(cabc.MutableMapping):
|
|||
self.replace_env()
|
||||
else:
|
||||
os.environ[key] = ensurer.detype(val)
|
||||
events.on_envvar.fire(name=key, oldvalue=old_val, newvalue=val)
|
||||
|
||||
def __delitem__(self, key):
|
||||
val = self._d.pop(key)
|
||||
|
|
Loading…
Add table
Reference in a new issue