1"""Utilities for installing extensions""" 2 3# Copyright (c) Jupyter Development Team. 4# Distributed under the terms of the Modified BSD License. 5 6import os 7from tornado.log import LogFormatter 8from traitlets import Bool, Any 9from jupyter_core.application import JupyterApp 10from jupyter_core.paths import ( 11 jupyter_config_dir, ENV_CONFIG_PATH, SYSTEM_CONFIG_PATH 12) 13from ._version import __version__ 14 15class ArgumentConflict(ValueError): 16 pass 17 18_base_flags = {} 19_base_flags.update(JupyterApp.flags) 20_base_flags.pop("y", None) 21_base_flags.pop("generate-config", None) 22_base_flags.update({ 23 "user" : ({ 24 "BaseExtensionApp" : { 25 "user" : True, 26 }}, "Apply the operation only for the given user" 27 ), 28 "system" : ({ 29 "BaseExtensionApp" : { 30 "user" : False, 31 "sys_prefix": False, 32 }}, "Apply the operation system-wide" 33 ), 34 "sys-prefix" : ({ 35 "BaseExtensionApp" : { 36 "sys_prefix" : True, 37 }}, "Use sys.prefix as the prefix for installing nbextensions (for environments, packaging)" 38 ), 39 "py" : ({ 40 "BaseExtensionApp" : { 41 "python" : True, 42 }}, "Install from a Python package" 43 ) 44}) 45_base_flags['python'] = _base_flags['py'] 46 47_base_aliases = {} 48_base_aliases.update(JupyterApp.aliases) 49 50 51class BaseExtensionApp(JupyterApp): 52 """Base nbextension installer app""" 53 _log_formatter_cls = LogFormatter 54 flags = _base_flags 55 aliases = _base_aliases 56 version = __version__ 57 58 user = Bool(False, config=True, help="Whether to do a user install") 59 sys_prefix = Bool(False, config=True, help="Use the sys.prefix as the prefix") 60 python = Bool(False, config=True, help="Install from a Python package") 61 62 # Remove for 5.0... 63 verbose = Any(None, config=True, help="DEPRECATED: Verbosity level") 64 65 def _verbose_changed(self): 66 """Warn about verbosity changes""" 67 import warnings 68 warnings.warn("`verbose` traits of `{}` has been deprecated, has no effects and will be removed in notebook 5.0.".format(type(self).__name__), DeprecationWarning) 69 70 def _log_format_default(self): 71 """A default format for messages""" 72 return "%(message)s" 73 74def _get_config_dir(user=False, sys_prefix=False): 75 """Get the location of config files for the current context 76 77 Returns the string to the environment 78 79 Parameters 80 ---------- 81 82 user : bool [default: False] 83 Get the user's .jupyter config directory 84 sys_prefix : bool [default: False] 85 Get sys.prefix, i.e. ~/.envs/my-env/etc/jupyter 86 """ 87 user = False if sys_prefix else user 88 if user and sys_prefix: 89 raise ArgumentConflict("Cannot specify more than one of user or sys_prefix") 90 if user: 91 nbext = jupyter_config_dir() 92 elif sys_prefix: 93 nbext = ENV_CONFIG_PATH[0] 94 else: 95 nbext = SYSTEM_CONFIG_PATH[0] 96 return nbext 97 98# Constants for pretty print extension listing function. 99# Window doesn't support coloring in the commandline 100GREEN_ENABLED = '\033[32m enabled \033[0m' if os.name != 'nt' else 'enabled ' 101RED_DISABLED = '\033[31mdisabled\033[0m' if os.name != 'nt' else 'disabled' 102GREEN_OK = '\033[32mOK\033[0m' if os.name != 'nt' else 'ok' 103RED_X = '\033[31m X\033[0m' if os.name != 'nt' else ' X' 104