This file is indexed.

/usr/lib/python2.7/dist-packages/IPython/html/services/notebooks/azurenbmanager.py is in ipython-notebook 1.2.1-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
"""A notebook manager that uses Azure blob storage.

Authors:

* Brian Granger
"""

#-----------------------------------------------------------------------------
#  Copyright (C) 2012  The IPython Development Team
#
#  Distributed under the terms of the BSD License.  The full license is in
#  the file COPYING, distributed as part of this software.
#-----------------------------------------------------------------------------

#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------

import datetime

import azure
from azure.storage import BlobService

from tornado import web

from .nbmanager import NotebookManager
from IPython.nbformat import current
from IPython.utils.traitlets import Unicode, Instance
from IPython.utils import tz

#-----------------------------------------------------------------------------
# Classes
#-----------------------------------------------------------------------------

class AzureNotebookManager(NotebookManager):

    account_name = Unicode('', config=True, help='Azure storage account name.')
    account_key = Unicode('', config=True, help='Azure storage account key.')
    container = Unicode('', config=True, help='Container name for notebooks.')

    blob_service_host_base = Unicode('.blob.core.windows.net', config=True,
        help='The basename for the blob service URL. If running on the preview site this '
             'will be .blob.core.azure-preview.com.')
    def _blob_service_host_base_changed(self, new):
        self._update_service_host_base(new)

    blob_service = Instance('azure.storage.BlobService')
    def _blob_service_default(self):
        return BlobService(account_name=self.account_name, account_key=self.account_key)

    def __init__(self, **kwargs):
        super(AzureNotebookManager, self).__init__(**kwargs)
        self._update_service_host_base(self.blob_service_host_base)
        self._create_container()

    def _update_service_host_base(self, shb):
        azure.BLOB_SERVICE_HOST_BASE = shb

    def _create_container(self):
        self.blob_service.create_container(self.container)

    def load_notebook_names(self):
        """On startup load the notebook ids and names from Azure.

        The blob names are the notebook ids and the notebook names are stored
        as blob metadata.
        """
        self.mapping = {}
        blobs = self.blob_service.list_blobs(self.container)
        ids = [blob.name for blob in blobs]
        
        for id in ids:
            md = self.blob_service.get_blob_metadata(self.container, id)
            name = md['x-ms-meta-nbname']
            self.mapping[id] = name

    def list_notebooks(self):
        """List all notebooks in the container.

        This version uses `self.mapping` as the authoritative notebook list.
        """
        data = [dict(notebook_id=id,name=name) for id, name in self.mapping.items()]
        data = sorted(data, key=lambda item: item['name'])
        return data

    def read_notebook_object(self, notebook_id):
        """Get the object representation of a notebook by notebook_id."""
        if not self.notebook_exists(notebook_id):
            raise web.HTTPError(404, u'Notebook does not exist: %s' % notebook_id)
        try:
            s = self.blob_service.get_blob(self.container, notebook_id)
        except:
            raise web.HTTPError(500, u'Notebook cannot be read.')
        try:
            # v1 and v2 and json in the .ipynb files.
            nb = current.reads(s, u'json')
        except:
            raise web.HTTPError(500, u'Unreadable JSON notebook.')
        # Todo: The last modified should actually be saved in the notebook document.
        # We are just using the current datetime until that is implemented.
        last_modified = tz.utcnow()
        return last_modified, nb

    def write_notebook_object(self, nb, notebook_id=None):
        """Save an existing notebook object by notebook_id."""
        try:
            new_name = nb.metadata.name
        except AttributeError:
            raise web.HTTPError(400, u'Missing notebook name')

        if notebook_id is None:
            notebook_id = self.new_notebook_id(new_name)

        if notebook_id not in self.mapping:
            raise web.HTTPError(404, u'Notebook does not exist: %s' % notebook_id)

        try:
            data = current.writes(nb, u'json')
        except Exception as e:
            raise web.HTTPError(400, u'Unexpected error while saving notebook: %s' % e)

        metadata = {'nbname': new_name}
        try:
            self.blob_service.put_blob(self.container, notebook_id, data, 'BlockBlob', x_ms_meta_name_values=metadata)
        except Exception as e:
            raise web.HTTPError(400, u'Unexpected error while saving notebook: %s' % e)

        self.mapping[notebook_id] = new_name
        return notebook_id

    def delete_notebook(self, notebook_id):
        """Delete notebook by notebook_id."""
        if not self.notebook_exists(notebook_id):
            raise web.HTTPError(404, u'Notebook does not exist: %s' % notebook_id)
        try:
            self.blob_service.delete_blob(self.container, notebook_id)
        except Exception as e:
            raise web.HTTPError(400, u'Unexpected error while deleting notebook: %s' % e)
        else:
            self.delete_notebook_id(notebook_id)

    def info_string(self):
        return "Serving notebooks from Azure storage: %s, %s" % (self.account_name, self.container)