##// END OF EJS Templates
Add type parameter for contents GET requests
Add type parameter for contents GET requests

File last commit:

r18781:f66a9ec4
r18781:f66a9ec4
Show More
filemanager.py
545 lines | 19.1 KiB | text/x-python | PythonLexer
MinRK
rename notebooks service to contents service...
r17524 """A contents manager that uses the local file system for storage."""
Brian E. Granger
More review changes....
r4609
MinRK
add utils.path.ensure_dir_exists...
r16486 # Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
Brian E. Granger
Massive work on the notebook document format....
r4484
MinRK
teach contents service about non-notebook files
r17525 import base64
Thomas Kluyver
Save notebook as script using unicode file handle....
r6030 import io
Brian E. Granger
Massive work on the notebook document format....
r4484 import os
Stefan van der Walt
Move glob to global level import.
r4624 import glob
MinRK
add checkpoint API to FileNBManager
r10497 import shutil
Zachary Sailer
manual rebase notebooks web services
r12984
Brian E. Granger
Massive work on the notebook document format....
r4484 from tornado import web
MinRK
rename notebooks service to contents service...
r17524 from .manager import ContentsManager
MinRK
don't use nbformat.current in IPython.html...
r18607 from IPython import nbformat
Thomas Kluyver
Implement atomic save...
r17557 from IPython.utils.io import atomic_writing
MinRK
add utils.path.ensure_dir_exists...
r16486 from IPython.utils.path import ensure_dir_exists
Thomas Kluyver
Fix a couple of static analysis warnings
r15526 from IPython.utils.traitlets import Unicode, Bool, TraitError
MinRK
reorganize who knows what about paths...
r15420 from IPython.utils.py3compat import getcwd
MinRK
add timezone info to `last_modified` in notebook managers...
r11145 from IPython.utils import tz
MinRK
update contents per further review...
r17537 from IPython.html.utils import is_hidden, to_os_path, url_path_join
Brian E. Granger
Massive work on the notebook document format....
r4484
MinRK
rename notebooks service to contents service...
r17524 class FileContentsManager(ContentsManager):
MinRK
mv services/notebooks services/contents
r17523
MinRK
rename notebooks service to contents service...
r17524 root_dir = Unicode(getcwd(), config=True)
MinRK
mv services/notebooks services/contents
r17523
MinRK
update contents per further review...
r17537 save_script = Bool(False, config=True, help='DEPRECATED, IGNORED')
def _save_script_changed(self):
self.log.warn("""
Automatically saving notebooks as scripts has been removed.
Use `ipython nbconvert --to python [notebook]` instead.
""")
MinRK
rename notebooks service to contents service...
r17524 def _root_dir_changed(self, name, old, new):
"""Do a bit of validation of the root_dir."""
MinRK
reorganize who knows what about paths...
r15420 if not os.path.isabs(new):
# If we receive a non-absolute path, make it absolute.
MinRK
rename notebooks service to contents service...
r17524 self.root_dir = os.path.abspath(new)
MinRK
reorganize who knows what about paths...
r15420 return
MinRK
update contents per further review...
r17537 if not os.path.isdir(new):
MinRK
rename notebooks service to contents service...
r17524 raise TraitError("%r is not a directory" % new)
MinRK
mv services/notebooks services/contents
r17523
MinRK
create checkpoints dir in notebook subdirectories
r16447 checkpoint_dir = Unicode('.ipynb_checkpoints', config=True,
MinRK
contents service review...
r17529 help="""The directory name in which to keep file checkpoints
MinRK
mv services/notebooks services/contents
r17523
MinRK
contents service review...
r17529 This is a path relative to the file's own directory.
MinRK
mv services/notebooks services/contents
r17523
MinRK
create checkpoints dir in notebook subdirectories
r16447 By default, it is .ipynb_checkpoints
MinRK
add checkpoint API to FileNBManager
r10497 """
)
MinRK
mv services/notebooks services/contents
r17523
MinRK
fallback on copy, copyfile if copy2 fails...
r15827 def _copy(self, src, dest):
"""copy src to dest
MinRK
mv services/notebooks services/contents
r17523
MinRK
only catch errors in copystat
r15865 like shutil.copy2, but log errors in copystat
MinRK
fallback on copy, copyfile if copy2 fails...
r15827 """
MinRK
only catch errors in copystat
r15865 shutil.copyfile(src, dest)
try:
shutil.copystat(src, dest)
except OSError as e:
self.log.debug("copystat on %s failed", dest, exc_info=True)
MinRK
mv services/notebooks services/contents
r17523
Min RK
address review in contents service...
r18758 def _get_os_path(self, path):
"""Given an API path, return its file system path.
MinRK
teach contents service about non-notebook files
r17525
Parameters
----------
path : string
MinRK
updates per review...
r17535 The relative API path to the named file.
MinRK
teach contents service about non-notebook files
r17525
Returns
-------
path : string
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 Native, absolute OS path to for a file.
MinRK
teach contents service about non-notebook files
r17525 """
return to_os_path(path, self.root_dir)
Zachary Sailer
Add 'patch' to session & notebook, rename working
r12997
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 def dir_exists(self, path):
MinRK
teach contents service about non-notebook files
r17525 """Does the API-style path refer to an extant directory?
MinRK
mv services/notebooks services/contents
r17523
MinRK
updates per review...
r17535 API-style wrapper for os.path.isdir
MinRK
move os_path to FileNBMan...
r13070 Parameters
----------
path : string
The path to check. This is an API path (`/` separated,
MinRK
rename notebooks service to contents service...
r17524 relative to root_dir).
MinRK
mv services/notebooks services/contents
r17523
MinRK
move os_path to FileNBMan...
r13070 Returns
-------
exists : bool
Whether the path is indeed a directory.
"""
MinRK
ensure 'path' never has leading or trailing slash in nbmanager...
r13078 path = path.strip('/')
MinRK
reorganize who knows what about paths...
r15420 os_path = self._get_os_path(path=path)
MinRK
move os_path to FileNBMan...
r13070 return os.path.isdir(os_path)
Brian E. Granger
Creating and testing IPython.html.utils.is_hidden.
r15097
def is_hidden(self, path):
"""Does the API style path correspond to a hidden directory or file?
MinRK
mv services/notebooks services/contents
r17523
Brian E. Granger
Creating and testing IPython.html.utils.is_hidden.
r15097 Parameters
----------
path : string
The path to check. This is an API path (`/` separated,
MinRK
rename notebooks service to contents service...
r17524 relative to root_dir).
MinRK
mv services/notebooks services/contents
r17523
Brian E. Granger
Creating and testing IPython.html.utils.is_hidden.
r15097 Returns
-------
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 hidden : bool
Whether the path exists and is hidden.
Brian E. Granger
Creating and testing IPython.html.utils.is_hidden.
r15097 """
path = path.strip('/')
MinRK
reorganize who knows what about paths...
r15420 os_path = self._get_os_path(path=path)
MinRK
rename notebooks service to contents service...
r17524 return is_hidden(os_path, self.root_dir)
Brian E. Granger
Creating and testing IPython.html.utils.is_hidden.
r15097
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 def file_exists(self, path):
MinRK
teach contents service about non-notebook files
r17525 """Returns True if the file exists, else returns False.
MinRK
move os_path to FileNBMan...
r13070
MinRK
updates per review...
r17535 API-style wrapper for os.path.isfile
MinRK
move os_path to FileNBMan...
r13070 Parameters
----------
path : string
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 The relative path to the file (with '/' as separator)
MinRK
move os_path to FileNBMan...
r13070
Returns
-------
MinRK
updates per review...
r17535 exists : bool
Whether the file exists.
MinRK
move os_path to FileNBMan...
r13070 """
MinRK
teach contents service about non-notebook files
r17525 path = path.strip('/')
Min RK
address review in contents service...
r18758 os_path = self._get_os_path(path)
return os.path.isfile(os_path)
MinRK
move os_path to FileNBMan...
r13070
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 def exists(self, path):
"""Returns True if the path exists, else returns False.
Zachary Sailer
refactoring of nbmanager and filenbmanager...
r13046
MinRK
updates per review...
r17535 API-style wrapper for os.path.exists
Zachary Sailer
handle path separators with os.sep and add tests...
r13032 Parameters
----------
path : string
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 The API path to the file (with '/' as separator)
Zachary Sailer
refactoring of nbmanager and filenbmanager...
r13046
Zachary Sailer
handle path separators with os.sep and add tests...
r13032 Returns
-------
MinRK
updates per review...
r17535 exists : bool
Whether the target exists.
Zachary Sailer
handle path separators with os.sep and add tests...
r13032 """
MinRK
ensure 'path' never has leading or trailing slash in nbmanager...
r13078 path = path.strip('/')
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 os_path = self._get_os_path(path=path)
MinRK
teach contents service about non-notebook files
r17525 return os.path.exists(os_path)
Zachary Sailer
manual rebase notebooks web services
r12984
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 def _base_model(self, path):
MinRK
teach contents service about non-notebook files
r17525 """Build the common base of a contents model"""
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 os_path = self._get_os_path(path)
Brian E. Granger
Server side logic for directories.
r15069 info = os.stat(os_path)
last_modified = tz.utcfromtimestamp(info.st_mtime)
created = tz.utcfromtimestamp(info.st_ctime)
MinRK
contents service review...
r17529 # Create the base model.
MinRK
teach contents service about non-notebook files
r17525 model = {}
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 model['name'] = path.rsplit('/', 1)[-1]
Brian E. Granger
Server side logic for directories.
r15069 model['path'] = path
model['last_modified'] = last_modified
model['created'] = created
MinRK
teach contents service about non-notebook files
r17525 model['content'] = None
model['format'] = None
return model
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 def _dir_model(self, path, content=True):
MinRK
teach contents service about non-notebook files
r17525 """Build a model for a directory
if content is requested, will include a listing of the directory
"""
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 os_path = self._get_os_path(path)
MinRK
teach contents service about non-notebook files
r17525
MinRK
update contents per further review...
r17537 four_o_four = u'directory does not exist: %r' % os_path
MinRK
teach contents service about non-notebook files
r17525 if not os.path.isdir(os_path):
MinRK
update contents per further review...
r17537 raise web.HTTPError(404, four_o_four)
MinRK
teach contents service about non-notebook files
r17525 elif is_hidden(os_path, self.root_dir):
MinRK
update contents per further review...
r17537 self.log.info("Refusing to serve hidden directory %r, via 404 Error",
os_path
)
raise web.HTTPError(404, four_o_four)
MinRK
teach contents service about non-notebook files
r17525
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 model = self._base_model(path)
Brian E. Granger
Server side logic for directories.
r15069 model['type'] = 'directory'
MinRK
teach contents service about non-notebook files
r17525 if content:
MinRK
contents service review...
r17529 model['content'] = contents = []
Min RK
address review in contents service...
r18758 os_dir = self._get_os_path(path)
for name in os.listdir(os_dir):
os_path = os.path.join(os_dir, name)
MinRK
fix directory listing with broken symlinks...
r17710 # skip over broken symlinks in listing
if not os.path.exists(os_path):
self.log.warn("%s doesn't exist", os_path)
continue
Min RK
address review in contents service...
r18758 elif not os.path.isfile(os_path) and not os.path.isdir(os_path):
self.log.debug("%s not a regular file", os_path)
continue
MinRK
teach contents service about non-notebook files
r17525 if self.should_list(name) and not is_hidden(os_path, self.root_dir):
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 contents.append(self.get_model(
path='%s/%s' % (path, name),
content=False)
)
MinRK
teach contents service about non-notebook files
r17525
MinRK
add support and tests for uploading and saving regular files
r17527 model['format'] = 'json'
MinRK
teach contents service about non-notebook files
r17525
Brian E. Granger
Server side logic for directories.
r15069 return model
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 def _file_model(self, path, content=True):
MinRK
teach contents service about non-notebook files
r17525 """Build a model for a file
MinRK
mv services/notebooks services/contents
r17523
MinRK
teach contents service about non-notebook files
r17525 if content is requested, include the file contents.
MinRK
contents service review...
r17529 UTF-8 text files will be unicode, binary files will be base64-encoded.
MinRK
teach contents service about non-notebook files
r17525 """
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 model = self._base_model(path)
MinRK
teach contents service about non-notebook files
r17525 model['type'] = 'file'
if content:
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 os_path = self._get_os_path(path)
Min RK
address review in contents service...
r18758 if not os.path.isfile(os_path):
# could be FIFO
raise web.HTTPError(400, "Cannot get content of non-file %s" % os_path)
MinRK
update contents per further review...
r17537 with io.open(os_path, 'rb') as f:
bcontent = f.read()
MinRK
teach contents service about non-notebook files
r17525 try:
MinRK
update contents per further review...
r17537 model['content'] = bcontent.decode('utf8')
MinRK
teach contents service about non-notebook files
r17525 except UnicodeError as e:
MinRK
update contents per further review...
r17537 model['content'] = base64.encodestring(bcontent).decode('ascii')
model['format'] = 'base64'
MinRK
teach contents service about non-notebook files
r17525 else:
model['format'] = 'text'
return model
MinRK
mv services/notebooks services/contents
r17523
MinRK
teach contents service about non-notebook files
r17525
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 def _notebook_model(self, path, content=True):
MinRK
teach contents service about non-notebook files
r17525 """Build a notebook model
if content is requested, the notebook content will be populated
as a JSON structure (not double-serialized)
Zachary Sailer
Code review changes....
r13048 """
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 model = self._base_model(path)
MinRK
teach contents service about non-notebook files
r17525 model['type'] = 'notebook'
if content:
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 os_path = self._get_os_path(path)
MinRK
teach contents service about non-notebook files
r17525 with io.open(os_path, 'r', encoding='utf-8') as f:
try:
MinRK
don't use nbformat.current in IPython.html...
r18607 nb = nbformat.read(f, as_version=4)
MinRK
teach contents service about non-notebook files
r17525 except Exception as e:
MinRK
remove heading cells in v4
r18596 raise web.HTTPError(400, u"Unreadable Notebook: %s %r" % (os_path, e))
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 self.mark_trusted_cells(nb, path)
MinRK
teach contents service about non-notebook files
r17525 model['content'] = nb
model['format'] = 'json'
MinRK
add dialogs for failed save/load...
r18249 self.validate_notebook_model(model)
MinRK
teach contents service about non-notebook files
r17525 return model
Zachary Sailer
refactoring of nbmanager and filenbmanager...
r13046
Thomas Kluyver
Add type parameter for contents GET requests
r18781 def get_model(self, path, content=True, type_=None):
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 """ Takes a path for an entity and returns its model
MinRK
mv services/notebooks services/contents
r17523
Zachary Sailer
Code review changes....
r13048 Parameters
----------
path : str
MinRK
updates per review...
r17535 the API path that describes the relative path for the target
Thomas Kluyver
Add type parameter for contents GET requests
r18781 content : bool
Whether to include the contents in the reply
type_ : str, optional
The requested type - 'file', 'notebook', or 'directory'.
Will raise HTTPError 406 if the content doesn't match.
MinRK
mv services/notebooks services/contents
r17523
Zachary Sailer
Code review changes....
r13048 Returns
-------
model : dict
MinRK
teach contents service about non-notebook files
r17525 the contents model. If content=True, returns the contents
of the file or directory as well.
Zachary Sailer
Code review changes....
r13048 """
MinRK
ensure 'path' never has leading or trailing slash in nbmanager...
r13078 path = path.strip('/')
MinRK
teach contents service about non-notebook files
r17525
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 if not self.exists(path):
raise web.HTTPError(404, u'No such file or directory: %s' % path)
MinRK
teach contents service about non-notebook files
r17525
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 os_path = self._get_os_path(path)
MinRK
teach contents service about non-notebook files
r17525 if os.path.isdir(os_path):
Thomas Kluyver
Add type parameter for contents GET requests
r18781 if type_ not in (None, 'directory'):
raise web.HTTPError(400,
u'%s is a directory, not a %s' % (path, type_))
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 model = self._dir_model(path, content=content)
Thomas Kluyver
Add type parameter for contents GET requests
r18781 elif type_ == 'notebook' or (type_ is None and path.endswith('.ipynb')):
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 model = self._notebook_model(path, content=content)
MinRK
teach contents service about non-notebook files
r17525 else:
Thomas Kluyver
Add type parameter for contents GET requests
r18781 if type_ == 'directory':
raise web.HTTPError(400,
u'%s is not a directory')
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 model = self._file_model(path, content=content)
Zachary Sailer
refactoring of nbmanager and filenbmanager...
r13046 return model
Zachary Sailer
Add 'patch' to session & notebook, rename working
r12997
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 def _save_notebook(self, os_path, model, path=''):
MinRK
contents service review...
r17529 """save a notebook file"""
MinRK
add support and tests for uploading and saving regular files
r17527 # Save the notebook file
MinRK
don't use nbformat.current in IPython.html...
r18607 nb = nbformat.from_dict(model['content'])
MinRK
add support and tests for uploading and saving regular files
r17527
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 self.check_and_sign(nb, path)
MinRK
add support and tests for uploading and saving regular files
r17527
Thomas Kluyver
Implement atomic save...
r17557 with atomic_writing(os_path, encoding='utf-8') as f:
Min RK
fix backward `f, nb` args for nbformat.write
r18613 nbformat.write(nb, f, version=nbformat.NO_CONVERT)
MinRK
add support and tests for uploading and saving regular files
r17527
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 def _save_file(self, os_path, model, path=''):
MinRK
contents service review...
r17529 """save a non-notebook file"""
MinRK
add support and tests for uploading and saving regular files
r17527 fmt = model.get('format', None)
if fmt not in {'text', 'base64'}:
raise web.HTTPError(400, "Must specify format of file contents as 'text' or 'base64'")
try:
content = model['content']
if fmt == 'text':
bcontent = content.encode('utf8')
else:
b64_bytes = content.encode('ascii')
bcontent = base64.decodestring(b64_bytes)
except Exception as e:
raise web.HTTPError(400, u'Encoding error saving %s: %s' % (os_path, e))
Thomas Kluyver
Rework atomic_writing with tests & docstring
r17570 with atomic_writing(os_path, text=False) as f:
MinRK
add support and tests for uploading and saving regular files
r17527 f.write(bcontent)
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 def _save_directory(self, os_path, model, path=''):
MinRK
contents service review...
r17529 """create a directory"""
MinRK
update contents per further review...
r17537 if is_hidden(os_path, self.root_dir):
raise web.HTTPError(400, u'Cannot create hidden directory %r' % os_path)
MinRK
add support and tests for uploading and saving regular files
r17527 if not os.path.exists(os_path):
os.mkdir(os_path)
elif not os.path.isdir(os_path):
raise web.HTTPError(400, u'Not a directory: %s' % (os_path))
MinRK
update contents per further review...
r17537 else:
self.log.debug("Directory %r already exists", os_path)
MinRK
add support and tests for uploading and saving regular files
r17527
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 def save(self, model, path=''):
MinRK
add support and tests for uploading and saving regular files
r17527 """Save the file model and return the model with no content."""
MinRK
ensure 'path' never has leading or trailing slash in nbmanager...
r13078 path = path.strip('/')
Zachary Sailer
refactoring of nbmanager and filenbmanager...
r13046
MinRK
add support and tests for uploading and saving regular files
r17527 if 'type' not in model:
raise web.HTTPError(400, u'No file type provided')
MinRK
updates per review...
r17532 if 'content' not in model and model['type'] != 'directory':
raise web.HTTPError(400, u'No file content provided')
Brian E. Granger
Addressing review comments....
r15093
MinRK
notebooks should always have one checkpoint...
r13245 # One checkpoint should always exist
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 if self.file_exists(path) and not self.list_checkpoints(path):
self.create_checkpoint(path)
Zachary Sailer
refactoring of nbmanager and filenbmanager...
r13046
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 os_path = self._get_os_path(path)
MinRK
add support and tests for uploading and saving regular files
r17527 self.log.debug("Saving %s", os_path)
Brian E. Granger
Massive work on the notebook document format....
r4484 try:
MinRK
add support and tests for uploading and saving regular files
r17527 if model['type'] == 'notebook':
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 self._save_notebook(os_path, model, path)
MinRK
add support and tests for uploading and saving regular files
r17527 elif model['type'] == 'file':
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 self._save_file(os_path, model, path)
MinRK
add support and tests for uploading and saving regular files
r17527 elif model['type'] == 'directory':
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 self._save_directory(os_path, model, path)
MinRK
add support and tests for uploading and saving regular files
r17527 else:
raise web.HTTPError(400, "Unhandled contents type: %s" % model['type'])
except web.HTTPError:
raise
MinRK
include error in 'Unexpected error' message.
r5709 except Exception as e:
MinRK
add support and tests for uploading and saving regular files
r17527 raise web.HTTPError(400, u'Unexpected error while saving file: %s %s' % (os_path, e))
Brian Granger
Refactoring notebook managers and adding Azure backed storage....
r8180
MinRK
add dialogs for failed save/load...
r18249 validation_message = None
if model['type'] == 'notebook':
self.validate_notebook_model(model)
validation_message = model.get('message', None)
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 model = self.get_model(path, content=False)
MinRK
add dialogs for failed save/load...
r18249 if validation_message:
model['message'] = validation_message
Zachary Sailer
refactoring of nbmanager and filenbmanager...
r13046 return model
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 def update(self, model, path):
"""Update the file's path
MinRK
updates per review...
r17535
For use in PATCH requests, to enable renaming a file without
re-uploading its contents. Only used for renaming at the moment.
"""
MinRK
ensure 'path' never has leading or trailing slash in nbmanager...
r13078 path = path.strip('/')
new_path = model.get('path', path).strip('/')
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 if path != new_path:
self.rename(path, new_path)
model = self.get_model(new_path, content=False)
Zachary Sailer
refactoring of nbmanager and filenbmanager...
r13046 return model
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 def delete(self, path):
"""Delete file at path."""
MinRK
ensure 'path' never has leading or trailing slash in nbmanager...
r13078 path = path.strip('/')
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 os_path = self._get_os_path(path)
MinRK
support deleting empty directories...
r17530 rm = os.unlink
if os.path.isdir(os_path):
listing = os.listdir(os_path)
# don't delete non-empty directories (checkpoints dir doesn't count)
MinRK
update contents per further review...
r17537 if listing and listing != [self.checkpoint_dir]:
MinRK
support deleting empty directories...
r17530 raise web.HTTPError(400, u'Directory %s not empty' % os_path)
elif not os.path.isfile(os_path):
MinRK
rename notebooks service to contents service...
r17524 raise web.HTTPError(404, u'File does not exist: %s' % os_path)
MinRK
mv services/notebooks services/contents
r17523
MinRK
add checkpoint API to FileNBManager
r10497 # clear checkpoints
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 for checkpoint in self.list_checkpoints(path):
MinRK
use 'id' for checkpoint ID key...
r13122 checkpoint_id = checkpoint['id']
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 cp_path = self.get_checkpoint_path(checkpoint_id, path)
Zachary Sailer
refactoring of nbmanager and filenbmanager...
r13046 if os.path.isfile(cp_path):
self.log.debug("Unlinking checkpoint %s", cp_path)
os.unlink(cp_path)
MinRK
mv services/notebooks services/contents
r17523
MinRK
support deleting empty directories...
r17530 if os.path.isdir(os_path):
self.log.debug("Removing directory %s", os_path)
shutil.rmtree(os_path)
else:
self.log.debug("Unlinking file %s", os_path)
rm(os_path)
Brian E. Granger
Massive work on the notebook document format....
r4484
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 def rename(self, old_path, new_path):
MinRK
rename notebooks service to contents service...
r17524 """Rename a file."""
MinRK
ensure 'path' never has leading or trailing slash in nbmanager...
r13078 old_path = old_path.strip('/')
new_path = new_path.strip('/')
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 if new_path == old_path:
Zachary Sailer
refactoring of nbmanager and filenbmanager...
r13046 return
MinRK
mv services/notebooks services/contents
r17523
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 new_os_path = self._get_os_path(new_path)
old_os_path = self._get_os_path(old_path)
Zachary Sailer
refactoring of nbmanager and filenbmanager...
r13046
# Should we proceed with the move?
Min RK
address review in contents service...
r18758 if os.path.exists(new_os_path):
raise web.HTTPError(409, u'File already exists: %s' % new_path)
Zachary Sailer
refactoring of nbmanager and filenbmanager...
r13046
MinRK
rename notebooks service to contents service...
r17524 # Move the file
Zachary Sailer
refactoring of nbmanager and filenbmanager...
r13046 try:
Pankaj Pandey
FileNotebookManager: Use shutil.move() instead of os.rename()...
r16212 shutil.move(old_os_path, new_os_path)
Brian E. Granger
Review and refactoring of notebooks web service.
r13051 except Exception as e:
Min RK
address review in contents service...
r18758 raise web.HTTPError(500, u'Unknown error renaming file: %s %s' % (old_path, e))
Zachary Sailer
refactoring of nbmanager and filenbmanager...
r13046
# Move the checkpoints
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 old_checkpoints = self.list_checkpoints(old_path)
Zachary Sailer
refactoring of nbmanager and filenbmanager...
r13046 for cp in old_checkpoints:
MinRK
use 'id' for checkpoint ID key...
r13122 checkpoint_id = cp['id']
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 old_cp_path = self.get_checkpoint_path(checkpoint_id, old_path)
new_cp_path = self.get_checkpoint_path(checkpoint_id, new_path)
Zachary Sailer
refactoring of nbmanager and filenbmanager...
r13046 if os.path.isfile(old_cp_path):
self.log.debug("Renaming checkpoint %s -> %s", old_cp_path, new_cp_path)
Pankaj Pandey
FileNotebookManager: Use shutil.move() instead of os.rename()...
r16212 shutil.move(old_cp_path, new_cp_path)
Zachary Sailer
refactoring of nbmanager and filenbmanager...
r13046
MinRK
add checkpoint API to FileNBManager
r10497 # Checkpoint-related utilities
MinRK
mv services/notebooks services/contents
r17523
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 def get_checkpoint_path(self, checkpoint_id, path):
Zachary Sailer
refactoring of nbmanager and filenbmanager...
r13046 """find the path to a checkpoint"""
MinRK
ensure 'path' never has leading or trailing slash in nbmanager...
r13078 path = path.strip('/')
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 parent, name = ('/' + path).rsplit('/', 1)
parent = parent.strip('/')
MinRK
rename notebooks service to contents service...
r17524 basename, ext = os.path.splitext(name)
MinRK
normalize unicode notebook filenames...
r10777 filename = u"{name}-{checkpoint_id}{ext}".format(
MinRK
fix checkpoint filename
r13244 name=basename,
MinRK
add checkpoint API to FileNBManager
r10497 checkpoint_id=checkpoint_id,
MinRK
rename notebooks service to contents service...
r17524 ext=ext,
MinRK
add checkpoint API to FileNBManager
r10497 )
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 os_path = self._get_os_path(path=parent)
MinRK
create checkpoints dir in notebook subdirectories
r16447 cp_dir = os.path.join(os_path, self.checkpoint_dir)
MinRK
add utils.path.ensure_dir_exists...
r16486 ensure_dir_exists(cp_dir)
MinRK
create checkpoints dir in notebook subdirectories
r16447 cp_path = os.path.join(cp_dir, filename)
Zachary Sailer
refactoring of nbmanager and filenbmanager...
r13046 return cp_path
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 def get_checkpoint_model(self, checkpoint_id, path):
MinRK
checkpoint info is a dict...
r10500 """construct the info dict for a given checkpoint"""
MinRK
ensure 'path' never has leading or trailing slash in nbmanager...
r13078 path = path.strip('/')
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 cp_path = self.get_checkpoint_path(checkpoint_id, path)
Zachary Sailer
refactoring of nbmanager and filenbmanager...
r13046 stats = os.stat(cp_path)
MinRK
add timezone info to `last_modified` in notebook managers...
r11145 last_modified = tz.utcfromtimestamp(stats.st_mtime)
MinRK
checkpoint info is a dict...
r10500 info = dict(
MinRK
use 'id' for checkpoint ID key...
r13122 id = checkpoint_id,
MinRK
checkpoint info is a dict...
r10500 last_modified = last_modified,
)
return info
MinRK
mv services/notebooks services/contents
r17523
MinRK
add checkpoint API to FileNBManager
r10497 # public checkpoint API
MinRK
mv services/notebooks services/contents
r17523
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 def create_checkpoint(self, path):
MinRK
rename notebooks service to contents service...
r17524 """Create a checkpoint from the current state of a file"""
MinRK
ensure 'path' never has leading or trailing slash in nbmanager...
r13078 path = path.strip('/')
Min RK
address review in contents service...
r18758 if not self.file_exists(path):
raise web.HTTPError(404)
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 src_path = self._get_os_path(path)
MinRK
checkpoint info is a dict...
r10500 # only the one checkpoint ID:
MinRK
normalize unicode notebook filenames...
r10777 checkpoint_id = u"checkpoint"
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 cp_path = self.get_checkpoint_path(checkpoint_id, path)
self.log.debug("creating checkpoint for %s", path)
MinRK
rename notebooks service to contents service...
r17524 self._copy(src_path, cp_path)
MinRK
mv services/notebooks services/contents
r17523
MinRK
checkpoint info is a dict...
r10500 # return the checkpoint info
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 return self.get_checkpoint_model(checkpoint_id, path)
MinRK
mv services/notebooks services/contents
r17523
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 def list_checkpoints(self, path):
MinRK
rename notebooks service to contents service...
r17524 """list the checkpoints for a given file
MinRK
mv services/notebooks services/contents
r17523
MinRK
rename notebooks service to contents service...
r17524 This contents manager currently only supports one checkpoint per file.
MinRK
add checkpoint API to FileNBManager
r10497 """
MinRK
ensure 'path' never has leading or trailing slash in nbmanager...
r13078 path = path.strip('/')
Zachary Sailer
manual rebase notebooks web services
r12984 checkpoint_id = "checkpoint"
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 os_path = self.get_checkpoint_path(checkpoint_id, path)
MinRK
create checkpoints dir in notebook subdirectories
r16447 if not os.path.exists(os_path):
MinRK
add checkpoint API to FileNBManager
r10497 return []
MinRK
checkpoint info is a dict...
r10500 else:
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 return [self.get_checkpoint_model(checkpoint_id, path)]
MinRK
mv services/notebooks services/contents
r17523
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 def restore_checkpoint(self, checkpoint_id, path):
MinRK
rename notebooks service to contents service...
r17524 """restore a file to a checkpointed state"""
MinRK
ensure 'path' never has leading or trailing slash in nbmanager...
r13078 path = path.strip('/')
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 self.log.info("restoring %s from checkpoint %s", path, checkpoint_id)
nb_path = self._get_os_path(path)
cp_path = self.get_checkpoint_path(checkpoint_id, path)
MinRK
add checkpoint API to FileNBManager
r10497 if not os.path.isfile(cp_path):
MinRK
checkpoint info is a dict...
r10500 self.log.debug("checkpoint file does not exist: %s", cp_path)
MinRK
add checkpoint API to FileNBManager
r10497 raise web.HTTPError(404,
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 u'checkpoint does not exist: %s@%s' % (path, checkpoint_id)
MinRK
add checkpoint API to FileNBManager
r10497 )
# ensure notebook is readable (never restore from an unreadable notebook)
MinRK
rename notebooks service to contents service...
r17524 if cp_path.endswith('.ipynb'):
with io.open(cp_path, 'r', encoding='utf-8') as f:
MinRK
don't use nbformat.current in IPython.html...
r18607 nbformat.read(f, as_version=4)
MinRK
fallback on copy, copyfile if copy2 fails...
r15827 self._copy(cp_path, nb_path)
MinRK
add checkpoint API to FileNBManager
r10497 self.log.debug("copying %s -> %s", cp_path, nb_path)
MinRK
mv services/notebooks services/contents
r17523
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 def delete_checkpoint(self, checkpoint_id, path):
MinRK
rename notebooks service to contents service...
r17524 """delete a file's checkpoint"""
MinRK
ensure 'path' never has leading or trailing slash in nbmanager...
r13078 path = path.strip('/')
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 cp_path = self.get_checkpoint_path(checkpoint_id, path)
Zachary Sailer
refactoring of nbmanager and filenbmanager...
r13046 if not os.path.isfile(cp_path):
MinRK
add checkpoint API to FileNBManager
r10497 raise web.HTTPError(404,
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 u'Checkpoint does not exist: %s@%s' % (path, checkpoint_id)
MinRK
add checkpoint API to FileNBManager
r10497 )
Zachary Sailer
refactoring of nbmanager and filenbmanager...
r13046 self.log.debug("unlinking %s", cp_path)
os.unlink(cp_path)
MinRK
mv services/notebooks services/contents
r17523
Paul Ivanov
print info string on interrupt, log it on startup
r10019 def info_string(self):
MinRK
rename notebooks service to contents service...
r17524 return "Serving notebooks from local directory: %s" % self.root_dir
Dale Jung
API: Allow NotebookManagers to control kernel startup dir. #5468
r16052
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 def get_kernel_path(self, path, model=None):
MinRK
rename notebooks service to contents service...
r17524 """Return the initial working dir a kernel associated with a given notebook"""
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 if '/' in path:
Min RK
address review in contents service...
r18758 parent_dir = path.rsplit('/', 1)[0]
MinRK
Remove separate 'path', 'name' in Contents API...
r18749 else:
Min RK
address review in contents service...
r18758 parent_dir = ''
return self._get_os_path(parent_dir)