Show More
@@ -0,0 +1,103 b'' | |||||
|
1 | """A base class contents manager. | |||
|
2 | ||||
|
3 | Authors: | |||
|
4 | ||||
|
5 | * Zach Sailer | |||
|
6 | """ | |||
|
7 | ||||
|
8 | #----------------------------------------------------------------------------- | |||
|
9 | # Copyright (C) 2011 The IPython Development Team | |||
|
10 | # | |||
|
11 | # Distributed under the terms of the BSD License. The full license is in | |||
|
12 | # the file COPYING, distributed as part of this software. | |||
|
13 | #----------------------------------------------------------------------------- | |||
|
14 | ||||
|
15 | #----------------------------------------------------------------------------- | |||
|
16 | # Imports | |||
|
17 | #----------------------------------------------------------------------------- | |||
|
18 | ||||
|
19 | import datetime | |||
|
20 | import io | |||
|
21 | import os | |||
|
22 | import glob | |||
|
23 | import shutil | |||
|
24 | import ast | |||
|
25 | import base64 | |||
|
26 | ||||
|
27 | from tornado import web | |||
|
28 | ||||
|
29 | from IPython.config.configurable import LoggingConfigurable | |||
|
30 | from IPython.nbformat import current | |||
|
31 | from IPython.utils.traitlets import List, Dict, Unicode, TraitError | |||
|
32 | from IPython.utils import tz | |||
|
33 | ||||
|
34 | #----------------------------------------------------------------------------- | |||
|
35 | # Classes | |||
|
36 | #----------------------------------------------------------------------------- | |||
|
37 | ||||
|
38 | class ContentManager(LoggingConfigurable): | |||
|
39 | ||||
|
40 | content_dir = Unicode(os.getcwdu(), config=True, help=""" | |||
|
41 | The directory to use for contents. | |||
|
42 | """) | |||
|
43 | ||||
|
44 | contents = List() | |||
|
45 | ||||
|
46 | def get_content_names(self, content_path): | |||
|
47 | names = glob.glob(os.path.join(self.content_dir, content_path,'*')) | |||
|
48 | content_names = list() | |||
|
49 | dir_names = list() | |||
|
50 | for name in names: | |||
|
51 | if os.path.isdir(name) == True: | |||
|
52 | dir_names.append(os.path.split(name)[1]) | |||
|
53 | elif os.path.splitext(os.path.basename(name))[1] != '.ipynb': | |||
|
54 | content_names.append(os.path.split(name)[1]) | |||
|
55 | return dir_names, content_names | |||
|
56 | ||||
|
57 | def list_contents(self, content_path): | |||
|
58 | """List all contents in the named path.""" | |||
|
59 | dir_names, content_names = self.get_content_names(content_path) | |||
|
60 | content_mapping = [] | |||
|
61 | for name in dir_names: | |||
|
62 | model = self.directory_model(name, content_path) | |||
|
63 | content_mapping.append(model) | |||
|
64 | for name in content_names: | |||
|
65 | model = self.content_model(name, content_path) | |||
|
66 | content_mapping.append(model) | |||
|
67 | return content_mapping | |||
|
68 | ||||
|
69 | def get_path_by_name(self, name, content_path): | |||
|
70 | """Return a full path to content""" | |||
|
71 | path = os.path.join(self.content_dir, content_path, name) | |||
|
72 | return path | |||
|
73 | ||||
|
74 | def read_content(self, name, content_path): | |||
|
75 | file_type = os.path.splitext(os.path.basename(name))[1] | |||
|
76 | #Collect contents of file | |||
|
77 | with open(name, 'rb') as file_content: | |||
|
78 | contents = file_content.read() | |||
|
79 | full_path = self.get_path_by_name(name, content_path) | |||
|
80 | info = os.stat(full_path) | |||
|
81 | size = info.st_size | |||
|
82 | last_modified = tz.utcfromtimestamp(info.st_mtime) | |||
|
83 | return last_modified, file_type, contents, size | |||
|
84 | ||||
|
85 | def directory_model(self, name, content_path): | |||
|
86 | model = {"name": name, | |||
|
87 | "path": content_path, | |||
|
88 | "type": 'tree'} | |||
|
89 | return model | |||
|
90 | ||||
|
91 | def content_model(self, name, content_path): | |||
|
92 | last_modified, file_type, contents, size = self.read_content(name, content_path) | |||
|
93 | model = {"name": name, | |||
|
94 | "path": content_path, | |||
|
95 | "type": file_type, | |||
|
96 | "last_modified": last_modified.ctime(), | |||
|
97 | "size": size} | |||
|
98 | return model | |||
|
99 | ||||
|
100 | def delete_content(self, name, content_path): | |||
|
101 | full_path = self.get_path_by_name(name, content_path) | |||
|
102 | os.unlink(full_path) | |||
|
103 | No newline at end of file |
@@ -0,0 +1,69 b'' | |||||
|
1 | """Tornado handlers for the contents web service. | |||
|
2 | ||||
|
3 | Authors: | |||
|
4 | ||||
|
5 | * Zach Sailer | |||
|
6 | """ | |||
|
7 | ||||
|
8 | #----------------------------------------------------------------------------- | |||
|
9 | # Copyright (C) 2008-2011 The IPython Development Team | |||
|
10 | # | |||
|
11 | # Distributed under the terms of the BSD License. The full license is in | |||
|
12 | # the file COPYING, distributed as part of this software. | |||
|
13 | #----------------------------------------------------------------------------- | |||
|
14 | ||||
|
15 | #----------------------------------------------------------------------------- | |||
|
16 | # Imports | |||
|
17 | #----------------------------------------------------------------------------- | |||
|
18 | ||||
|
19 | from tornado import web | |||
|
20 | ||||
|
21 | from zmq.utils import jsonapi | |||
|
22 | ||||
|
23 | from IPython.utils.jsonutil import date_default | |||
|
24 | ||||
|
25 | from ...base.handlers import IPythonHandler, authenticate_unless_readonly | |||
|
26 | ||||
|
27 | #----------------------------------------------------------------------------- | |||
|
28 | # Contents web service handlers | |||
|
29 | #----------------------------------------------------------------------------- | |||
|
30 | ||||
|
31 | ||||
|
32 | class ContentRootHandler(IPythonHandler): | |||
|
33 | ||||
|
34 | @authenticate_unless_readonly | |||
|
35 | def get(self): | |||
|
36 | nbm = self.notebook_manager | |||
|
37 | cm = self.content_manager | |||
|
38 | contents = cm.list_contents("") | |||
|
39 | self.finish(jsonapi.dumps(contents)) | |||
|
40 | ||||
|
41 | ||||
|
42 | class ContentHandler(IPythonHandler): | |||
|
43 | ||||
|
44 | @web.authenticated | |||
|
45 | def get(self, content_path): | |||
|
46 | cm = self.content_manager | |||
|
47 | nbm = self.notebook_manager | |||
|
48 | contents = cm.list_contents(content_path) | |||
|
49 | self.finish(jsonapi.dumps(contents)) | |||
|
50 | ||||
|
51 | @web.authenticated | |||
|
52 | def delete(self, content_path): | |||
|
53 | ||||
|
54 | self.set_status(204) | |||
|
55 | self.finish() | |||
|
56 | ||||
|
57 | ||||
|
58 | #----------------------------------------------------------------------------- | |||
|
59 | # URL to handler mappings | |||
|
60 | #----------------------------------------------------------------------------- | |||
|
61 | ||||
|
62 | _content_path_regex = r"(?P<content_path>.+)" | |||
|
63 | ||||
|
64 | default_handlers = [ | |||
|
65 | (r"api/contents/%s" % _content_path_regex, ContentHandler), | |||
|
66 | (r"api/contents", ContentRootHandler) | |||
|
67 | ] | |||
|
68 | ||||
|
69 |
@@ -218,6 +218,10 b' class IPythonHandler(AuthenticatedHandler):' | |||||
218 | return self.settings['session_manager'] |
|
218 | return self.settings['session_manager'] | |
219 |
|
219 | |||
220 | @property |
|
220 | @property | |
|
221 | def content_manager(self): | |||
|
222 | return self.settings['content_manager'] | |||
|
223 | ||||
|
224 | @property | |||
221 | def project(self): |
|
225 | def project(self): | |
222 | return self.notebook_manager.notebook_dir |
|
226 | return self.notebook_manager.notebook_dir | |
223 |
|
227 |
@@ -54,8 +54,8 b' class NamedNotebookHandler(IPythonHandler):' | |||||
54 | project = self.project + '/' + name |
|
54 | project = self.project + '/' + name | |
55 | else: |
|
55 | else: | |
56 | project = self.project + '/' + path +'/'+ name |
|
56 | project = self.project + '/' + path +'/'+ name | |
57 |
|
|
57 | if not nbm.notebook_exists(notebook_path): | |
58 |
|
|
58 | raise web.HTTPError(404, u'Notebook does not exist: %s' % name) | |
59 | self.write(self.render_template('notebook.html', |
|
59 | self.write(self.render_template('notebook.html', | |
60 | project=project, |
|
60 | project=project, | |
61 | notebook_path=path, |
|
61 | notebook_path=path, |
@@ -66,6 +66,7 b' from .services.notebooks.nbmanager import NotebookManager' | |||||
66 | from .services.notebooks.filenbmanager import FileNotebookManager |
|
66 | from .services.notebooks.filenbmanager import FileNotebookManager | |
67 | from .services.clusters.clustermanager import ClusterManager |
|
67 | from .services.clusters.clustermanager import ClusterManager | |
68 | from .services.sessions.sessionmanager import SessionManager |
|
68 | from .services.sessions.sessionmanager import SessionManager | |
|
69 | from .services.contents.contentmanager import ContentManager | |||
69 |
|
70 | |||
70 | from .base.handlers import AuthenticatedFileHandler, FileFindHandler |
|
71 | from .base.handlers import AuthenticatedFileHandler, FileFindHandler | |
71 |
|
72 | |||
@@ -128,18 +129,18 b' def load_handlers(name):' | |||||
128 | class NotebookWebApplication(web.Application): |
|
129 | class NotebookWebApplication(web.Application): | |
129 |
|
130 | |||
130 | def __init__(self, ipython_app, kernel_manager, notebook_manager, |
|
131 | def __init__(self, ipython_app, kernel_manager, notebook_manager, | |
131 | cluster_manager, session_manager, log, |
|
132 | cluster_manager, session_manager, content_manager, log, | |
132 | base_project_url, settings_overrides): |
|
133 | base_project_url, settings_overrides): | |
133 |
|
134 | |||
134 | settings = self.init_settings( |
|
135 | settings = self.init_settings( | |
135 | ipython_app, kernel_manager, notebook_manager, cluster_manager, |
|
136 | ipython_app, kernel_manager, notebook_manager, cluster_manager, | |
136 | session_manager, log, base_project_url, settings_overrides) |
|
137 | session_manager, content_manager, log, base_project_url, settings_overrides) | |
137 | handlers = self.init_handlers(settings) |
|
138 | handlers = self.init_handlers(settings) | |
138 |
|
139 | |||
139 | super(NotebookWebApplication, self).__init__(handlers, **settings) |
|
140 | super(NotebookWebApplication, self).__init__(handlers, **settings) | |
140 |
|
141 | |||
141 | def init_settings(self, ipython_app, kernel_manager, notebook_manager, |
|
142 | def init_settings(self, ipython_app, kernel_manager, notebook_manager, | |
142 | cluster_manager, session_manager, log, |
|
143 | cluster_manager, session_manager, content_manager, log, | |
143 | base_project_url, settings_overrides): |
|
144 | base_project_url, settings_overrides): | |
144 | # Python < 2.6.5 doesn't accept unicode keys in f(**kwargs), and |
|
145 | # Python < 2.6.5 doesn't accept unicode keys in f(**kwargs), and | |
145 | # base_project_url will always be unicode, which will in turn |
|
146 | # base_project_url will always be unicode, which will in turn | |
@@ -170,6 +171,7 b' class NotebookWebApplication(web.Application):' | |||||
170 | notebook_manager=notebook_manager, |
|
171 | notebook_manager=notebook_manager, | |
171 | cluster_manager=cluster_manager, |
|
172 | cluster_manager=cluster_manager, | |
172 | session_manager=session_manager, |
|
173 | session_manager=session_manager, | |
|
174 | content_manager=content_manager, | |||
173 |
|
175 | |||
174 | # IPython stuff |
|
176 | # IPython stuff | |
175 | nbextensions_path = ipython_app.nbextensions_path, |
|
177 | nbextensions_path = ipython_app.nbextensions_path, | |
@@ -195,6 +197,7 b' class NotebookWebApplication(web.Application):' | |||||
195 | handlers.extend(load_handlers('services.notebooks.handlers')) |
|
197 | handlers.extend(load_handlers('services.notebooks.handlers')) | |
196 | handlers.extend(load_handlers('services.clusters.handlers')) |
|
198 | handlers.extend(load_handlers('services.clusters.handlers')) | |
197 | handlers.extend(load_handlers('services.sessions.handlers')) |
|
199 | handlers.extend(load_handlers('services.sessions.handlers')) | |
|
200 | handlers.extend(load_handlers('services.contents.handlers')) | |||
198 | handlers.extend([ |
|
201 | handlers.extend([ | |
199 | (r"/files/(.*)", AuthenticatedFileHandler, {'path' : settings['notebook_manager'].notebook_dir}), |
|
202 | (r"/files/(.*)", AuthenticatedFileHandler, {'path' : settings['notebook_manager'].notebook_dir}), | |
200 | (r"/nbextensions/(.*)", FileFindHandler, {'path' : settings['nbextensions_path']}), |
|
203 | (r"/nbextensions/(.*)", FileFindHandler, {'path' : settings['nbextensions_path']}), | |
@@ -528,6 +531,7 b' class NotebookApp(BaseIPythonApplication):' | |||||
528 | self.notebook_manager = kls(parent=self, log=self.log) |
|
531 | self.notebook_manager = kls(parent=self, log=self.log) | |
529 | self.notebook_manager.load_notebook_names('') |
|
532 | self.notebook_manager.load_notebook_names('') | |
530 | self.session_manager = SessionManager(parent=self, log=self.log) |
|
533 | self.session_manager = SessionManager(parent=self, log=self.log) | |
|
534 | self.content_manager = ContentManager(parent=self, log=self.log) | |||
531 | self.cluster_manager = ClusterManager(parent=self, log=self.log) |
|
535 | self.cluster_manager = ClusterManager(parent=self, log=self.log) | |
532 | self.cluster_manager.update_profiles() |
|
536 | self.cluster_manager.update_profiles() | |
533 |
|
537 | |||
@@ -545,8 +549,8 b' class NotebookApp(BaseIPythonApplication):' | |||||
545 | """initialize tornado webapp and httpserver""" |
|
549 | """initialize tornado webapp and httpserver""" | |
546 | self.web_app = NotebookWebApplication( |
|
550 | self.web_app = NotebookWebApplication( | |
547 | self, self.kernel_manager, self.notebook_manager, |
|
551 | self, self.kernel_manager, self.notebook_manager, | |
548 |
self.cluster_manager, self.session_manager, self. |
|
552 | self.cluster_manager, self.session_manager, self.content_manager, | |
549 | self.base_project_url, self.webapp_settings |
|
553 | self.log, self.base_project_url, self.webapp_settings | |
550 | ) |
|
554 | ) | |
551 | if self.certfile: |
|
555 | if self.certfile: | |
552 | ssl_options = dict(certfile=self.certfile) |
|
556 | ssl_options = dict(certfile=self.certfile) |
@@ -80,7 +80,6 b' class FileNotebookManager(NotebookManager):' | |||||
80 | """List all notebook names in the notebook dir.""" |
|
80 | """List all notebook names in the notebook dir.""" | |
81 | names = glob.glob(os.path.join(self.notebook_dir, path, |
|
81 | names = glob.glob(os.path.join(self.notebook_dir, path, | |
82 | '*' + self.filename_ext)) |
|
82 | '*' + self.filename_ext)) | |
83 | #names = [os.path.splitext(os.path.basename(name))[0] |
|
|||
84 | names = [os.path.basename(name) |
|
83 | names = [os.path.basename(name) | |
85 | for name in names] |
|
84 | for name in names] | |
86 | return names |
|
85 | return names | |
@@ -112,13 +111,9 b' class FileNotebookManager(NotebookManager):' | |||||
112 | model = self.notebook_model(notebook_name, notebook_path) |
|
111 | model = self.notebook_model(notebook_name, notebook_path) | |
113 | return model |
|
112 | return model | |
114 |
|
113 | |||
115 |
def notebook_exists(self, notebook_ |
|
114 | def notebook_exists(self, notebook_path): | |
116 | """Does a notebook exist?""" |
|
115 | """Does a notebook exist?""" | |
117 | exists = super(FileNotebookManager, self).notebook_exists(notebook_name) |
|
116 | return os.path.isfile(notebook_path) | |
118 | if not exists: |
|
|||
119 | return False |
|
|||
120 | path = self.get_path_by_name(self.mapping[notebook_name]) |
|
|||
121 | return os.path.isfile(path) |
|
|||
122 |
|
117 | |||
123 | def get_path(self, notebook_name, notebook_path=None): |
|
118 | def get_path(self, notebook_name, notebook_path=None): | |
124 | """Return a full path to a notebook given its notebook_name.""" |
|
119 | """Return a full path to a notebook given its notebook_name.""" |
@@ -35,7 +35,6 b' class NotebookRootHandler(IPythonHandler):' | |||||
35 | @web.authenticated |
|
35 | @web.authenticated | |
36 | def get(self): |
|
36 | def get(self): | |
37 | nbm = self.notebook_manager |
|
37 | nbm = self.notebook_manager | |
38 | km = self.kernel_manager |
|
|||
39 | notebooks = nbm.list_notebooks("") |
|
38 | notebooks = nbm.list_notebooks("") | |
40 | self.finish(jsonapi.dumps(notebooks)) |
|
39 | self.finish(jsonapi.dumps(notebooks)) | |
41 |
|
40 |
@@ -111,9 +111,8 b' class NotebookManager(LoggingConfigurable):' | |||||
111 | raise NotImplementedError('must be implemented in a subclass') |
|
111 | raise NotImplementedError('must be implemented in a subclass') | |
112 |
|
112 | |||
113 |
|
113 | |||
114 |
def notebook_exists(self, notebook_ |
|
114 | def notebook_exists(self, notebook_path): | |
115 | """Does a notebook exist?""" |
|
115 | """Does a notebook exist?""" | |
116 | return notebook_name in self.mapping |
|
|||
117 |
|
116 | |||
118 | def notebook_model(self, notebook_name, notebook_path=None): |
|
117 | def notebook_model(self, notebook_name, notebook_path=None): | |
119 | """ Creates the standard notebook model """ |
|
118 | """ Creates the standard notebook model """ |
General Comments 0
You need to be logged in to leave comments.
Login now