Commit 462eb4aa authored by adam j hartz's avatar adam j hartz

lay some groundwork for plugins

parent c9f004d9
# 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.
class PluginEnv(dict):
def __init__(self, *args, parent=None, **kwargs):
dict.__init__(self, *args, **kwargs)
self.parent = parent
def __getitem__(self, key):
if dict.__contains__(self, key):
return dict.__getitem__(self, key)
else:
if self.parent is None:
raise KeyError
else:
return self.parent[key]
def __contains__(self, key):
if dict.__contains__(self, key):
return True
else:
return self.parent.__contains__(self, key)
......@@ -131,6 +131,7 @@ defaults = {
'/usr/share/bash-completion/completions'),
'HISTORY_SIZE': 8128,
'SHELL_TYPE': 'readline',
'PLUGINS': {},
}
defaults['HISTORY_FILE'] = os.path.join(defaults['DATA_DIR'], 'history')
......
......@@ -18,8 +18,14 @@
# -*- coding: utf-8 -*-
"""The tako shell"""
from takoshell.environ import takorc_context
import os
import builtins
from takoshell.tools import display_error_message
from takoshell.execer import Execer
from takoshell.plugins import PluginEnv
from takoshell.environ import takorc_context
from takoshell.settings import Settings
class Shell(object):
......@@ -46,6 +52,8 @@ class Shell(object):
self._init_environ(ctx,
kwargs.get('scriptcache', True),
kwargs.get('cacheall', False))
if not self.headless:
self._init_plugins(ctx)
def __getattr__(self, attr):
"""Delegates calls to appropriate shell instance."""
......@@ -71,3 +79,26 @@ class Shell(object):
from takoshell.base_shell import BaseShell as shell_class
self.shell = shell_class(execer=self.execer,
ctx=self.ctx, **self.kwargs)
def _init_plugins(self, ctx):
env = builtins.__tako_env__
settings = env['TAKO_SETTINGS']
plugin_dir = os.path.join(settings.config_dir, 'plugins')
if not os.path.isdir(plugin_dir):
return
all_plugins = os.listdir(plugin_dir)
for p in all_plugins:
if not p.endswith('.tako'):
continue
name = p.rsplit('.', 1)[0]
full_path = os.path.join(plugin_dir, p)
if not os.path.isfile(full_path):
continue
env_contents = {'PLUGIN_SETTINGS': Settings({'ALIASES': {}})}
try:
new_env = PluginEnv(env_contents, parent=ctx)
self.execer.exec(open(full_path).read(), glbs=new_env)
settings.plugins[name] = new_env
except:
print('Error loading plugin %s' % name)
display_error_message()
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