def get_model(self, content=True, file_content=True): os_path = os.path.join(self.bundle_path, self.name) info = os.stat(os_path) last_modified = tz.utcfromtimestamp(info.st_mtime) created = tz.utcfromtimestamp(info.st_ctime) # Create the notebook model. model = {} model['name'] = self.name model['path'] = self.path model['last_modified'] = last_modified model['created'] = created model['type'] = 'notebook' model['is_bundle'] = True model['content'] = None if content: model['content'] = self.notebook_content files = {} for fn in self.files: with open(os.path.join(self.bundle_path, fn), 'rb') as f: data = None if file_content: try: data = f.read().decode('utf-8') except UnicodeDecodeError: # TODO how to deal with binary data? # right now we skip continue files[fn] = data model['__files'] = files return model
def _base_model(self, path): """Build the common base of a contents model""" os_path = self._get_os_path(path) info = os.stat(os_path) last_modified = tz.utcfromtimestamp(info.st_mtime) created = tz.utcfromtimestamp(info.st_ctime) # Create the base model. model = {} model["name"] = path.rsplit("/", 1)[-1] model["path"] = path model["last_modified"] = last_modified model["created"] = created model["content"] = None model["format"] = None return model
def _base_model(self, name, path=''): """Build the common base of a contents model""" os_path = self._get_os_path(name, path) info = os.stat(os_path) last_modified = tz.utcfromtimestamp(info.st_mtime) created = tz.utcfromtimestamp(info.st_ctime) # Create the base model. model = {} model['name'] = name model['path'] = path model['last_modified'] = last_modified model['created'] = created model['content'] = None model['format'] = None return model
def get_dir_model(self, name, path=''): """Get the directory model given a directory name and its API style path""" path = path.strip('/') os_path = self._get_os_path(name, path) if not os.path.isdir(os_path): raise IOError('directory does not exist: %r' % os_path) info = os.stat(os_path) last_modified = tz.utcfromtimestamp(info.st_mtime) created = tz.utcfromtimestamp(info.st_ctime) # Create the notebook model. return {'name': name, 'path': path, 'last_modified': last_modified, 'created': created, 'type': 'directory'}
def get(self, format, path='', name=None): exporter = get_exporter(format, config=self.config) path = path.strip('/') os_path = self.notebook_manager.get_os_path(name, path) if not os.path.isfile(os_path): raise web.HTTPError(404, u'Notebook does not exist: %s' % name) info = os.stat(os_path) self.set_header('Last-Modified', tz.utcfromtimestamp(info.st_mtime)) try: output, resources = exporter.from_filename(os_path) except Exception as e: raise web.HTTPError(500, "nbconvert failed: %s" % e) if respond_zip(self, name, output, resources): return # Force download if requested if self.get_argument('download', 'false').lower() == 'true': filename = os.path.splitext( name)[0] + '.' + resources['output_extension'] self.set_header('Content-Disposition', 'attachment; filename="%s"' % filename) # MIME type if exporter.output_mimetype: self.set_header('Content-Type', '%s; charset=utf-8' % exporter.output_mimetype) self.finish(output)
def get(self, format, path='', name=None): exporter = exporter_map[format](config=self.config) path = path.strip('/') os_path = self.notebook_manager.get_os_path(name, path) if not os.path.isfile(os_path): raise web.HTTPError(404, u'Notebook does not exist: %s' % name) info = os.stat(os_path) self.set_header('Last-Modified', tz.utcfromtimestamp(info.st_mtime)) output, resources = exporter.from_filename(os_path) if respond_zip(self, name, output, resources): return # Force download if requested if self.get_argument('download', 'false').lower() == 'true': filename = os.path.splitext(name)[0] + '.' + resources['output_extension'] self.set_header('Content-Disposition', 'attachment; filename="%s"' % filename) # MIME type if exporter.output_mimetype: self.set_header('Content-Type', '%s; charset=utf-8' % exporter.output_mimetype) self.finish(output)
def get_dir_model(self, name, path=''): """Get the directory model given a directory name and its API style path""" path = path.strip('/') os_path = self.get_os_path(name, path) if not os.path.isdir(os_path): raise IOError('directory does not exist: %r' % os_path) info = os.stat(os_path) last_modified = tz.utcfromtimestamp(info.st_mtime) created = tz.utcfromtimestamp(info.st_ctime) # Create the notebook model. model ={} model['name'] = name model['path'] = path model['last_modified'] = last_modified model['created'] = created model['type'] = 'directory' return model
def get_dir_model(self, name, path=''): """Get the directory model given a directory name and its API style path""" path = path.strip('/') os_path = self._get_os_path(name, path) if not os.path.isdir(os_path): raise IOError('directory does not exist: %r' % os_path) info = os.stat(os_path) last_modified = tz.utcfromtimestamp(info.st_mtime) created = tz.utcfromtimestamp(info.st_ctime) # Create the notebook model. model = {} model['name'] = name model['path'] = path model['last_modified'] = last_modified model['created'] = created model['type'] = 'directory' return model
def get_checkpoint_model(self, checkpoint_id, path): """construct the info dict for a given checkpoint""" path = path.strip("/") cp_path = self.get_checkpoint_path(checkpoint_id, path) stats = os.stat(cp_path) last_modified = tz.utcfromtimestamp(stats.st_mtime) info = dict(id=checkpoint_id, last_modified=last_modified) return info
def get_dir_model(self, name, path=""): """Get the directory model given a directory name and its API style path""" path = path.strip("/") os_path = self._get_os_path(name, path) if not os.path.isdir(os_path): raise IOError("directory does not exist: %r" % os_path) info = os.stat(os_path) last_modified = tz.utcfromtimestamp(info.st_mtime) created = tz.utcfromtimestamp(info.st_ctime) # Create the notebook model. model = {} model["name"] = name model["path"] = path model["last_modified"] = last_modified model["created"] = created model["type"] = "directory" return model
def checkpoint_model(self, checkpoint_id, os_path): """construct the info dict for a given checkpoint""" stats = os.stat(os_path) last_modified = tz.utcfromtimestamp(stats.st_mtime) info = dict( id=checkpoint_id, last_modified=last_modified, ) return info
def get_checkpoint_info(self, notebook_id, checkpoint_id): """construct the info dict for a given checkpoint""" path = self.get_checkpoint_path(notebook_id, checkpoint_id) stats = os.stat(path) last_modified = tz.utcfromtimestamp(stats.st_mtime) info = dict( checkpoint_id=checkpoint_id, last_modified=last_modified, ) return info
def get_checkpoint_model(self, checkpoint_id, name, path=''): """construct the info dict for a given checkpoint""" path = path.strip('/') cp_path = self.get_checkpoint_path(checkpoint_id, name, path) stats = os.stat(cp_path) last_modified = tz.utcfromtimestamp(stats.st_mtime) info = dict( id=checkpoint_id, last_modified=last_modified, ) return info
def get_checkpoint_info(self, notebook_id, checkpoint_id): """construct the info dict for a given checkpoint""" path = self.get_checkpoint_path(notebook_id, checkpoint_id) stats = os.stat(path) last_modified = tz.utcfromtimestamp(stats.st_mtime) info = dict( checkpoint_id = checkpoint_id, last_modified = last_modified, ) return info
def _base_model(self, path): """Build the common base of a contents model""" os_path = self._get_os_path(path) info = os.stat(os_path) last_modified = tz.utcfromtimestamp(info.st_mtime) created = tz.utcfromtimestamp(info.st_ctime) # Create the base model. model = {} model['name'] = path.rsplit('/', 1)[-1] model['path'] = path model['last_modified'] = last_modified model['created'] = created model['content'] = None model['format'] = None try: model['writable'] = os.access(os_path, os.W_OK) except OSError: self.log.error("Failed to check write permissions on %s", os_path) model['writable'] = False return model
def get_notebook(self, name, path='', content=True): """ Takes a path and name for a notebook and returns its model Parameters ---------- name : str the name of the notebook path : str the URL path that describes the relative path for the notebook Returns ------- model : dict the notebook model. If contents=True, returns the 'contents' dict in the model as well. """ path = path.strip('/') if not self.notebook_exists(name=name, path=path): raise web.HTTPError(404, u'Notebook does not exist: %s' % name) os_path = self._get_os_path(name, path) info = os.stat(os_path) last_modified = tz.utcfromtimestamp(info.st_mtime) created = tz.utcfromtimestamp(info.st_ctime) # Create the notebook model. model = {} model['name'] = name model['path'] = path model['last_modified'] = last_modified model['created'] = created model['type'] = 'notebook' if content: with io.open(os_path, 'r', encoding='utf-8') as f: try: nb = current.read(f, u'json') except Exception as e: raise web.HTTPError( 400, u"Unreadable Notebook: %s %s" % (os_path, e)) self.mark_trusted_cells(nb, name, path) model['content'] = nb return model
def read_notebook_object_from_path(self, path): """read a notebook object from a path""" info = os.stat(path) last_modified = tz.utcfromtimestamp(info.st_mtime) with open(path,'r') as f: s = f.read() try: # v1 and v2 and json in the .ipynb files. nb = current.reads(s, u'json') except Exception as e: raise web.HTTPError(500, u'Unreadable JSON notebook: %s' % e) return last_modified, nb
def read_notebook_object_from_path(self, path): """read a notebook object from a path""" info = os.stat(path) last_modified = tz.utcfromtimestamp(info.st_mtime) with open(path, 'r') as f: s = f.read() try: # v1 and v2 and json in the .ipynb files. nb = current.reads(s, u'json') except ValueError as e: msg = u"Unreadable Notebook: %s" % e raise RuntimeError(msg) return last_modified, nb
def read_notebook_object_from_path(self, path): """read a notebook object from a path""" info = os.stat(path) last_modified = tz.utcfromtimestamp(info.st_mtime) with open(path,'r') as f: s = f.read() try: # v1 and v2 and json in the .ipynb files. nb = current.reads(s, 'json') except ValueError as e: msg = "Unreadable Notebook: %s" % e raise web.HTTPError(400, msg, reason=msg) return last_modified, nb
def get_notebook_model(self, name, path="", content=True): """ Takes a path and name for a notebook and returns its model Parameters ---------- name : str the name of the notebook path : str the URL path that describes the relative path for the notebook Returns ------- model : dict the notebook model. If contents=True, returns the 'contents' dict in the model as well. """ path = path.strip("/") if not self.notebook_exists(name=name, path=path): raise web.HTTPError(404, u"Notebook does not exist: %s" % name) os_path = self.get_os_path(name, path) info = os.stat(os_path) last_modified = tz.utcfromtimestamp(info.st_mtime) created = tz.utcfromtimestamp(info.st_ctime) # Create the notebook model. model = {} model["name"] = name model["path"] = path model["last_modified"] = last_modified model["created"] = created if content is True: with io.open(os_path, "r", encoding="utf-8") as f: try: nb = current.read(f, u"json") except Exception as e: raise web.HTTPError(400, u"Unreadable Notebook: %s %s" % (os_path, e)) model["content"] = nb return model