Show More
contentmanager.py
103 lines
| 3.7 KiB
| text/x-python
|
PythonLexer
|
r13006 | """A base class contents manager. | |
Authors: | |||
* Zach Sailer | |||
""" | |||
#----------------------------------------------------------------------------- | |||
|
r13011 | # Copyright (C) 2013 The IPython Development Team | |
|
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): | |||
|
r13011 | """List of dicts of files in content_path""" | |
|
r13006 | names = glob.glob(os.path.join(self.content_dir, content_path,'*')) | |
|
r13011 | contents = list() | |
dirs = list() | |||
notebooks = list() | |||
|
r13006 | for name in names: | |
if os.path.isdir(name) == True: | |||
|
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 | |||
|
r13006 | def list_contents(self, content_path): | |
"""List all contents in the named path.""" | |||
|
r13011 | dir_names, notebook_names, content_names = self.get_content_names(content_path) | |
|
r13006 | content_mapping = [] | |
for name in dir_names: | |||
|
r13011 | model = self.content_model(name, content_path, type='dir') | |
|
r13006 | content_mapping.append(model) | |
for name in content_names: | |||
|
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') | |||
|
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 | |||
|
r13011 | def content_info(self, name, content_path): | |
"""Read the content of a named file""" | |||
|
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) | |||
|
r13011 | return last_modified, file_type, size | |
|
r13006 | ||
|
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) | |||
|
r13006 | model = {"name": name, | |
"path": content_path, | |||
|
r13011 | "type": type, | |
"MIME-type": "", | |||
|
r13015 | "last_modified (UTC)": last_modified.ctime(), | |
|
r13006 | "size": size} | |
return model | |||
|
r13007 | def delete_content(self, content_path): | |
|
r13011 | """Delete a file""" | |
|
r13007 | os.unlink(os.path.join(self.content_dir, content_path)) | |
|
r13006 |