mirror of
https://github.com/xonsh/xonsh.git
synced 2025-03-06 17:30:59 +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 warnings
|
||||
|
||||
from xonsh.xontribs import update_context
|
||||
from xonsh.xontribs import update_context, prompt_xontrib_install
|
||||
from xonsh.environ import xonshrc_context
|
||||
from xonsh.execer import Execer
|
||||
from xonsh.platform import (best_shell_type, has_prompt_toolkit,
|
||||
|
@ -112,6 +112,9 @@ class Shell(object):
|
|||
names = builtins.__xonsh_config__.get('xontribs', ())
|
||||
for name in names:
|
||||
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
|
||||
env = builtins.__xonsh_env__
|
||||
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."""
|
||||
spec = find_xontrib(name)
|
||||
if spec is None:
|
||||
return {}
|
||||
return None
|
||||
m = importlib.import_module(spec.name)
|
||||
pubnames = getattr(m, '__all__', None)
|
||||
if pubnames is not None:
|
||||
|
@ -49,20 +49,12 @@ def prompt_xontrib_install(names):
|
|||
if xontrib['name'] == name:
|
||||
packages.append(xontrib['package'])
|
||||
|
||||
warn_str = ('The following xontribs are enabled but not installed: \n'
|
||||
' {xontribs}\n'
|
||||
'To install them run \n'
|
||||
' pip install {packages}').format(xontribs=' '.join(names),
|
||||
packages=' '.join(packages))
|
||||
print('The following xontribs are enabled but not installed: \n'
|
||||
' {xontribs}\n'
|
||||
'To install them run \n'
|
||||
' pip install {packages}'.format(xontribs=' '.join(names),
|
||||
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):
|
||||
"""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:
|
||||
ctx = builtins.__xonsh_ctx__
|
||||
if not hasattr(update_context, 'bad_imports'):
|
||||
update_context.bad_imports = []
|
||||
modctx = xontrib_context(name)
|
||||
if modctx == {}:
|
||||
return False
|
||||
ctx.update(modctx)
|
||||
return True
|
||||
if modctx is None:
|
||||
update_context.bad_imports.append(name)
|
||||
return ctx
|
||||
return ctx.update(modctx)
|
||||
|
||||
|
||||
@functools.lru_cache()
|
||||
|
@ -88,16 +82,13 @@ def xontrib_metadata():
|
|||
def _load(ns):
|
||||
"""load xontribs"""
|
||||
ctx = builtins.__xonsh_ctx__
|
||||
missing_specs = []
|
||||
for name in ns.names:
|
||||
if ns.verbose:
|
||||
print('loading xontrib {0!r}'.format(name))
|
||||
res = update_context(name, ctx=ctx)
|
||||
if not res:
|
||||
missing_specs.append(name)
|
||||
print(missing_specs)
|
||||
if missing_specs:
|
||||
prompt_xontrib_install(missing_specs)
|
||||
update_context(name, ctx=ctx)
|
||||
if update_context.bad_imports:
|
||||
prompt_xontrib_install(update_context.bad_imports)
|
||||
del update_context.bad_imports
|
||||
|
||||
|
||||
def _list(ns):
|
||||
|
|
Loading…
Add table
Reference in a new issue