PageRenderTime 64ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/NU_JAE_dnaComplete_optimized/pymodules/python2.7/lib/python/ipython-2.2.0-py2.7.egg/IPython/html/services/kernels/kernelmanager.py

https://gitlab.com/pooja043/Globus_Docker_4
Python | 128 lines | 74 code | 17 blank | 37 comment | 5 complexity | 0918bc6271b9aa99cbb5a07af6831094 MD5 | raw file
  1. """A kernel manager relating notebooks and kernels
  2. Authors:
  3. * Brian Granger
  4. """
  5. #-----------------------------------------------------------------------------
  6. # Copyright (C) 2013 The IPython Development Team
  7. #
  8. # Distributed under the terms of the BSD License. The full license is in
  9. # the file COPYING, distributed as part of this software.
  10. #-----------------------------------------------------------------------------
  11. #-----------------------------------------------------------------------------
  12. # Imports
  13. #-----------------------------------------------------------------------------
  14. import os
  15. from tornado import web
  16. from IPython.kernel.multikernelmanager import MultiKernelManager
  17. from IPython.utils.traitlets import (
  18. Dict, List, Unicode,
  19. )
  20. from IPython.html.utils import to_os_path
  21. from IPython.utils.py3compat import getcwd
  22. #-----------------------------------------------------------------------------
  23. # Classes
  24. #-----------------------------------------------------------------------------
  25. class MappingKernelManager(MultiKernelManager):
  26. """A KernelManager that handles notebook mapping and HTTP error handling"""
  27. def _kernel_manager_class_default(self):
  28. return "IPython.kernel.ioloop.IOLoopKernelManager"
  29. kernel_argv = List(Unicode)
  30. root_dir = Unicode(getcwd(), config=True)
  31. def _root_dir_changed(self, name, old, new):
  32. """Do a bit of validation of the root dir."""
  33. if not os.path.isabs(new):
  34. # If we receive a non-absolute path, make it absolute.
  35. self.root_dir = os.path.abspath(new)
  36. return
  37. if not os.path.exists(new) or not os.path.isdir(new):
  38. raise TraitError("kernel root dir %r is not a directory" % new)
  39. #-------------------------------------------------------------------------
  40. # Methods for managing kernels and sessions
  41. #-------------------------------------------------------------------------
  42. def _handle_kernel_died(self, kernel_id):
  43. """notice that a kernel died"""
  44. self.log.warn("Kernel %s died, removing from map.", kernel_id)
  45. self.remove_kernel(kernel_id)
  46. def cwd_for_path(self, path):
  47. """Turn API path into absolute OS path."""
  48. os_path = to_os_path(path, self.root_dir)
  49. # in the case of notebooks and kernels not being on the same filesystem,
  50. # walk up to root_dir if the paths don't exist
  51. while not os.path.exists(os_path) and os_path != self.root_dir:
  52. os_path = os.path.dirname(os_path)
  53. return os_path
  54. def start_kernel(self, kernel_id=None, path=None, **kwargs):
  55. """Start a kernel for a session an return its kernel_id.
  56. Parameters
  57. ----------
  58. kernel_id : uuid
  59. The uuid to associate the new kernel with. If this
  60. is not None, this kernel will be persistent whenever it is
  61. requested.
  62. path : API path
  63. The API path (unicode, '/' delimited) for the cwd.
  64. Will be transformed to an OS path relative to root_dir.
  65. """
  66. if kernel_id is None:
  67. kwargs['extra_arguments'] = self.kernel_argv
  68. if path is not None:
  69. kwargs['cwd'] = self.cwd_for_path(path)
  70. kernel_id = super(MappingKernelManager, self).start_kernel(**kwargs)
  71. self.log.info("Kernel started: %s" % kernel_id)
  72. self.log.debug("Kernel args: %r" % kwargs)
  73. # register callback for failed auto-restart
  74. self.add_restart_callback(kernel_id,
  75. lambda : self._handle_kernel_died(kernel_id),
  76. 'dead',
  77. )
  78. else:
  79. self._check_kernel_id(kernel_id)
  80. self.log.info("Using existing kernel: %s" % kernel_id)
  81. return kernel_id
  82. def shutdown_kernel(self, kernel_id, now=False):
  83. """Shutdown a kernel by kernel_id"""
  84. self._check_kernel_id(kernel_id)
  85. super(MappingKernelManager, self).shutdown_kernel(kernel_id, now=now)
  86. def kernel_model(self, kernel_id):
  87. """Return a dictionary of kernel information described in the
  88. JSON standard model."""
  89. self._check_kernel_id(kernel_id)
  90. model = {"id":kernel_id}
  91. return model
  92. def list_kernels(self):
  93. """Returns a list of kernel_id's of kernels running."""
  94. kernels = []
  95. kernel_ids = super(MappingKernelManager, self).list_kernel_ids()
  96. for kernel_id in kernel_ids:
  97. model = self.kernel_model(kernel_id)
  98. kernels.append(model)
  99. return kernels
  100. # override _check_kernel_id to raise 404 instead of KeyError
  101. def _check_kernel_id(self, kernel_id):
  102. """Check a that a kernel_id exists and raise 404 if not."""
  103. if kernel_id not in self:
  104. raise web.HTTPError(404, u'Kernel does not exist: %s' % kernel_id)