コード例 #1
0
ファイル: bundle.py プロジェクト: pjcrosbie/nbx
 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
コード例 #2
0
ファイル: filemanager.py プロジェクト: robertotang/ipython
 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
コード例 #3
0
ファイル: filemanager.py プロジェクト: d10genes/ipython
 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
コード例 #4
0
 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
コード例 #5
0
ファイル: filenbmanager.py プロジェクト: dvska/ipython
 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'}
コード例 #6
0
    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)
コード例 #7
0
ファイル: handlers.py プロジェクト: Web5design/ipython
    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)
コード例 #8
0
ファイル: filenbmanager.py プロジェクト: JohnnyBurst/ipython
 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
コード例 #9
0
ファイル: filenbmanager.py プロジェクト: vindolin/ipython
 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
コード例 #10
0
ファイル: filemanager.py プロジェクト: robertotang/ipython
 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
コード例 #11
0
 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
コード例 #12
0
 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
コード例 #13
0
ファイル: filecheckpoints.py プロジェクト: hack1nt0/CM
 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
コード例 #14
0
    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
コード例 #15
0
ファイル: filenbmanager.py プロジェクト: vindolin/ipython
 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
コード例 #16
0
ファイル: filenbmanager.py プロジェクト: breisfeld/ipython
 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
コード例 #17
0
 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
コード例 #18
0
ファイル: filemanager.py プロジェクト: ChristoSilvia/ipython
 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
コード例 #19
0
ファイル: filenbmanager.py プロジェクト: vindolin/ipython
 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
コード例 #20
0
ファイル: filenbmanager.py プロジェクト: pyarnold/ipython
    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
コード例 #21
0
ファイル: filenbmanager.py プロジェクト: breisfeld/ipython
 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
コード例 #22
0
 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
コード例 #23
0
 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
コード例 #24
0
ファイル: filenbmanager.py プロジェクト: tomspur/ipython
 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