mirror of
https://github.com/xonsh/xonsh.git
synced 2025-03-07 01:40:58 +01:00
prompt to install missing xontribs if loaded
This commit is contained in:
parent
e4eeb30782
commit
ec519a8c6e
2 changed files with 20 additions and 26 deletions
|
@ -5,7 +5,7 @@ import random
|
||||||
import builtins
|
import builtins
|
||||||
import warnings
|
import warnings
|
||||||
|
|
||||||
from xonsh.xontribs import update_context
|
from xonsh.xontribs import update_context, prompt_xontrib_install
|
||||||
from xonsh.environ import xonshrc_context
|
from xonsh.environ import xonshrc_context
|
||||||
from xonsh.execer import Execer
|
from xonsh.execer import Execer
|
||||||
from xonsh.platform import (best_shell_type, has_prompt_toolkit,
|
from xonsh.platform import (best_shell_type, has_prompt_toolkit,
|
||||||
|
@ -112,6 +112,9 @@ class Shell(object):
|
||||||
names = builtins.__xonsh_config__.get('xontribs', ())
|
names = builtins.__xonsh_config__.get('xontribs', ())
|
||||||
for name in names:
|
for name in names:
|
||||||
update_context(name, ctx=self.ctx)
|
update_context(name, ctx=self.ctx)
|
||||||
|
if update_context.bad_imports:
|
||||||
|
prompt_xontrib_install(update_context.bad_imports)
|
||||||
|
del update_context.bad_imports
|
||||||
# load run control files
|
# load run control files
|
||||||
env = builtins.__xonsh_env__
|
env = builtins.__xonsh_env__
|
||||||
rc = env.get('XONSHRC') if rc is None else rc
|
rc = env.get('XONSHRC') if rc is None else rc
|
||||||
|
|
|
@ -30,7 +30,7 @@ def xontrib_context(name):
|
||||||
"""Return a context dictionary for a xontrib of a given name."""
|
"""Return a context dictionary for a xontrib of a given name."""
|
||||||
spec = find_xontrib(name)
|
spec = find_xontrib(name)
|
||||||
if spec is None:
|
if spec is None:
|
||||||
return {}
|
return None
|
||||||
m = importlib.import_module(spec.name)
|
m = importlib.import_module(spec.name)
|
||||||
pubnames = getattr(m, '__all__', None)
|
pubnames = getattr(m, '__all__', None)
|
||||||
if pubnames is not None:
|
if pubnames is not None:
|
||||||
|
@ -49,20 +49,12 @@ def prompt_xontrib_install(names):
|
||||||
if xontrib['name'] == name:
|
if xontrib['name'] == name:
|
||||||
packages.append(xontrib['package'])
|
packages.append(xontrib['package'])
|
||||||
|
|
||||||
warn_str = ('The following xontribs are enabled but not installed: \n'
|
print('The following xontribs are enabled but not installed: \n'
|
||||||
' {xontribs}\n'
|
' {xontribs}\n'
|
||||||
'To install them run \n'
|
'To install them run \n'
|
||||||
' pip install {packages}').format(xontribs=' '.join(names),
|
' pip install {packages}'.format(xontribs=' '.join(names),
|
||||||
packages=' '.join(packages))
|
packages=' '.join(packages)))
|
||||||
|
|
||||||
with warnings.catch_warnings():
|
|
||||||
warnings.simplefilter('default', ImportWarning)
|
|
||||||
warnings.warn(warn_str, ImportWarning)
|
|
||||||
# return install_str.format(xontrib=name, package=xontrib['package'])
|
|
||||||
|
|
||||||
# install_str = ('xontrib "{xontrib}" is not installed. \n'
|
|
||||||
# 'To install it run \n'
|
|
||||||
# ' pip install {package}')
|
|
||||||
|
|
||||||
def update_context(name, ctx=None):
|
def update_context(name, ctx=None):
|
||||||
"""Updates a context in place from a xontrib. If ctx is not provided,
|
"""Updates a context in place from a xontrib. If ctx is not provided,
|
||||||
|
@ -70,11 +62,13 @@ def update_context(name, ctx=None):
|
||||||
"""
|
"""
|
||||||
if ctx is None:
|
if ctx is None:
|
||||||
ctx = builtins.__xonsh_ctx__
|
ctx = builtins.__xonsh_ctx__
|
||||||
|
if not hasattr(update_context, 'bad_imports'):
|
||||||
|
update_context.bad_imports = []
|
||||||
modctx = xontrib_context(name)
|
modctx = xontrib_context(name)
|
||||||
if modctx == {}:
|
if modctx is None:
|
||||||
return False
|
update_context.bad_imports.append(name)
|
||||||
ctx.update(modctx)
|
return ctx
|
||||||
return True
|
return ctx.update(modctx)
|
||||||
|
|
||||||
|
|
||||||
@functools.lru_cache()
|
@functools.lru_cache()
|
||||||
|
@ -88,16 +82,13 @@ def xontrib_metadata():
|
||||||
def _load(ns):
|
def _load(ns):
|
||||||
"""load xontribs"""
|
"""load xontribs"""
|
||||||
ctx = builtins.__xonsh_ctx__
|
ctx = builtins.__xonsh_ctx__
|
||||||
missing_specs = []
|
|
||||||
for name in ns.names:
|
for name in ns.names:
|
||||||
if ns.verbose:
|
if ns.verbose:
|
||||||
print('loading xontrib {0!r}'.format(name))
|
print('loading xontrib {0!r}'.format(name))
|
||||||
res = update_context(name, ctx=ctx)
|
update_context(name, ctx=ctx)
|
||||||
if not res:
|
if update_context.bad_imports:
|
||||||
missing_specs.append(name)
|
prompt_xontrib_install(update_context.bad_imports)
|
||||||
print(missing_specs)
|
del update_context.bad_imports
|
||||||
if missing_specs:
|
|
||||||
prompt_xontrib_install(missing_specs)
|
|
||||||
|
|
||||||
|
|
||||||
def _list(ns):
|
def _list(ns):
|
||||||
|
|
Loading…
Add table
Reference in a new issue