You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
3.2 KiB

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