##// END OF EJS Templates
use new assumptions for named_notebook_path
use new assumptions for named_notebook_path

File last commit:

r13015:acf4b32e
r13030:660db8a2
Show More
contentmanager.py
103 lines | 3.7 KiB | text/x-python | PythonLexer
Zachary Sailer
add contents web service api
r13006 """A base class contents manager.
Authors:
* Zach Sailer
"""
#-----------------------------------------------------------------------------
Zachary Sailer
standard model changes
r13011 # Copyright (C) 2013 The IPython Development Team
Zachary Sailer
add contents web service api
r13006 #
# 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 io
import os
import glob
import shutil
import ast
import base64
from tornado import web
from IPython.config.configurable import LoggingConfigurable
from IPython.nbformat import current
from IPython.utils.traitlets import List, Dict, Unicode, TraitError
from IPython.utils import tz
#-----------------------------------------------------------------------------
# Classes
#-----------------------------------------------------------------------------
class ContentManager(LoggingConfigurable):
content_dir = Unicode(os.getcwdu(), config=True, help="""
The directory to use for contents.
""")
contents = List()
def get_content_names(self, content_path):
Zachary Sailer
standard model changes
r13011 """List of dicts of files in content_path"""
Zachary Sailer
add contents web service api
r13006 names = glob.glob(os.path.join(self.content_dir, content_path,'*'))
Zachary Sailer
standard model changes
r13011 contents = list()
dirs = list()
notebooks = list()
Zachary Sailer
add contents web service api
r13006 for name in names:
if os.path.isdir(name) == True:
Zachary Sailer
standard model changes
r13011 dirs.append(os.path.split(name)[1])
elif os.path.splitext(name)[1] == '.ipynb':
notebooks.append(os.path.split(name)[1])
else:
contents.append(os.path.split(name)[1])
return dirs, notebooks, contents
Zachary Sailer
add contents web service api
r13006 def list_contents(self, content_path):
"""List all contents in the named path."""
Zachary Sailer
standard model changes
r13011 dir_names, notebook_names, content_names = self.get_content_names(content_path)
Zachary Sailer
add contents web service api
r13006 content_mapping = []
for name in dir_names:
Zachary Sailer
standard model changes
r13011 model = self.content_model(name, content_path, type='dir')
Zachary Sailer
add contents web service api
r13006 content_mapping.append(model)
for name in content_names:
Zachary Sailer
standard model changes
r13011 model = self.content_model(name, content_path, type='file')
content_mapping.append(model)
for name in notebook_names:
model = self.content_model(name, content_path, type='notebook')
Zachary Sailer
add contents web service api
r13006 content_mapping.append(model)
return content_mapping
def get_path_by_name(self, name, content_path):
"""Return a full path to content"""
path = os.path.join(self.content_dir, content_path, name)
return path
Zachary Sailer
standard model changes
r13011 def content_info(self, name, content_path):
"""Read the content of a named file"""
Zachary Sailer
add contents web service api
r13006 file_type = os.path.splitext(os.path.basename(name))[1]
full_path = self.get_path_by_name(name, content_path)
info = os.stat(full_path)
size = info.st_size
last_modified = tz.utcfromtimestamp(info.st_mtime)
Zachary Sailer
standard model changes
r13011 return last_modified, file_type, size
Zachary Sailer
add contents web service api
r13006
Zachary Sailer
standard model changes
r13011 def content_model(self, name, content_path, type=None):
"""Create a dict standard model for any file (other than notebooks)"""
last_modified, file_type, size = self.content_info(name, content_path)
Zachary Sailer
add contents web service api
r13006 model = {"name": name,
"path": content_path,
Zachary Sailer
standard model changes
r13011 "type": type,
"MIME-type": "",
Zachary Sailer
change standard money keys
r13015 "last_modified (UTC)": last_modified.ctime(),
Zachary Sailer
add contents web service api
r13006 "size": size}
return model
Zachary Sailer
fixed delete_content in contentmanager
r13007 def delete_content(self, content_path):
Zachary Sailer
standard model changes
r13011 """Delete a file"""
Zachary Sailer
fixed delete_content in contentmanager
r13007 os.unlink(os.path.join(self.content_dir, content_path))
Zachary Sailer
add contents web service api
r13006