Commit 5fea45ba authored by adam j hartz's avatar adam j hartz

add rudimentary support for events

parent 082f1366
......@@ -30,6 +30,8 @@ from takoshell.codecache import (should_use_cache, code_cache_name,
from takoshell.completer import Completer
from takoshell.environ import multiline_prompt, format_prompt
from takoshell.events import fire_event
class BaseShell(object):
"""The tako shell."""
......@@ -93,6 +95,7 @@ class BaseShell(object):
builtins.__tako_env__['PWD'] = cwd # track it now
if old is not None:
builtins.__tako_env__['OLDPWD'] = old # and update $OLDPWD like dirstack.
fire_event('on_chdir', cwd, old)
def push(self, line):
"""Pushes a line onto the buffer and compiles the code in a way that
......
......@@ -38,6 +38,7 @@ from collections import Sequence
from contextlib import contextmanager
from subprocess import Popen, PIPE, STDOUT, CalledProcessError
from takoshell.events import fire_event
from takoshell.braceexpand import braceexpand
from takoshell.environ import Env, default_env, locate_binary
from takoshell.jobs import add_job, wait_for_active_job
......@@ -45,7 +46,7 @@ from takoshell.proc import (ProcProxy, SimpleProcProxy,
CompletedCommand, HiddenCompletedCommand)
from takoshell.tools import (
suggest_commands, expandvars, CommandsCache, globpath, TakoError,
TakoCalledProcessError
TakoCalledProcessError
)
BUILTINS_LOADED = False
......@@ -379,6 +380,7 @@ def run_subproc(cmds, captured=False):
prev_proc = None
_capture_streams = captured in {'stdout', 'object'}
for ix, cmd in enumerate(cmds):
ocmd = cmd
starttime = time.time()
procinfo['args'] = list(cmd)
stdin = None
......@@ -530,6 +532,7 @@ def run_subproc(cmds, captured=False):
if len(sug.strip()) > 0:
e += '\n' + suggest_commands(cmd, env, aliases)
raise TakoError(e)
fire_event('pre_command', ocmd)
procs.append(proc)
prev_proc = proc
if cls is Popen and _pipeline_group is None:
......@@ -603,6 +606,7 @@ def run_subproc(cmds, captured=False):
settings.raise_subproc_error):
raise CalledProcessError(prev_proc.returncode, aliased_cmd, output=output)
if captured == 'stdout':
fire_event('post_command', output)
return output
elif captured is not False:
procinfo['executed_cmd'] = aliased_cmd
......@@ -615,9 +619,13 @@ def run_subproc(cmds, captured=False):
_stdin_file.seek(0)
procinfo['stdin'] = _stdin_file.read().decode()
_stdin_file.close()
return CompletedCommand(**procinfo)
out = CompletedCommand(**procinfo)
fire_event('post_command', out)
return out
else:
return HiddenCompletedCommand(**procinfo)
out = HiddenCompletedCommand(**procinfo)
fire_event('post_command', out)
return out
def subproc_captured_stdout(*cmds):
......
......@@ -23,6 +23,8 @@ import builtins
from glob import iglob
from argparse import ArgumentParser
from takoshell.events import fire_event
DIRSTACK = []
"""A list containing the currently remembered directories."""
......@@ -50,6 +52,7 @@ def _change_working_directory(newdir):
env['OLDPWD'] = old
if new is not None:
env['PWD'] = os.path.abspath(new)
fire_event('on_chdir', new, old)
def _try_cdpath(apath):
......
# This file is part of tako
# Copyright (c) 2015-2017 Adam Hartz <hartz@mit.edu> and contributors
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the Soopycat License, version 2.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the Soopycat License for more details.
#
# You should have received a copy of the Soopycat License along with this
# program. If not, see <https://smatz.net/soopycat>.
#
#
# tako is a fork of xonsh (http://xon.sh)
# xonsh is Copyright (c) 2015-2016 the xonsh developers and is licensed under
# the 2-Clause BSD license.
import builtins
def fire_event(name, *args):
settings = builtins.__tako_env__['TAKO_SETTINGS']
for f in settings.hooks[name]:
f(*args)
for plugin in settings.plugins:
for f in settings.plugins[plugin].hooks[name]:
f(*args)
......@@ -21,6 +21,8 @@ import sys
import socket
import builtins
from collections import defaultdict
import takoshell.jobs
import takoshell.tools
import takoshell.aliases
......@@ -137,6 +139,7 @@ defaults = {
'HISTORY_SIZE': 8128,
'SHELL_TYPE': 'readline',
'PLUGINS': Settings(),
'HOOKS': defaultdict(list),
}
defaults['HISTORY_FILE'] = os.path.join(defaults['DATA_DIR'], 'history')
......
......@@ -21,6 +21,8 @@
import os
import builtins
from collections import defaultdict
from takoshell.tools import display_error_message
from takoshell.execer import Execer
from takoshell.plugins import PluginEnv
......@@ -94,7 +96,7 @@ class Shell(object):
full_path = os.path.join(plugin_dir, p)
if not os.path.isfile(full_path):
continue
env_contents = {'PLUGIN_SETTINGS': Settings({'ALIASES': {}})}
env_contents = {'PLUGIN_SETTINGS': Settings({'ALIASES': {}, 'HOOKS': defaultdict(list)})}
try:
new_env = PluginEnv(env_contents, parent=ctx)
self.execer.exec(open(full_path).read(), glbs=new_env)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment