# Path: nbx/nbmanager/bundle/bundle.py
# class NotebookBundle(Bundle):
# 
#     @property
#     def notebook_content(self):
#         filepath = os.path.join(self.path, self.name)
#         with io.open(filepath, 'r', encoding='utf-8') as f:
#             try:
#                 nb = nbformat.read(f, as_version=4)
#             except Exception as e:
#                 nb = None
#             return nb
# 
#     @property
#     def files(self):
#         files = super(NotebookBundle, self).files
#         assert self.name in files
#         files.remove(self.name)
#         assert self.name not in files
#         return files
# 
#     def get_model(self, content=True, file_content=True):
#         os_path = os.path.join(self.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.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


import os
import io
import shutil
import nbformat
from .bundle import NotebookBundle
from nbformat import sign


current = nbformat.v4


def is_notebook(path):
    # checks if path follows bundle format
    if not path.endswith('.ipynb'):
        return False
    # if that we have same named ipynb file in directory
    name = path.rsplit('/', 1)[-1]
    file_path = os.path.join(path, name)
    has_file = os.path.isfile(file_path)
    return has_file


def _list_bundles(path):
    root, dirs, files = next(os.walk(path))
    bundles = (os.path.join(root, d) for d in dirs)
    bundles = filter(lambda path: is_notebook(path), bundles)
    bundles = list(bundles)
    return bundles


class BundleManager(object):

# Path: nbx/nbmanager/bundle/bundle.py
# class NotebookBundle(Bundle):
# 
#     @property
#     def notebook_content(self):
#         filepath = os.path.join(self.path, self.name)
#         with io.open(filepath, 'r', encoding='utf-8') as f:
#             try:
#                 nb = nbformat.read(f, as_version=4)
#             except Exception as e:
#                 nb = None
#             return nb
# 
#     @property
#     def files(self):
#         files = super(NotebookBundle, self).files
#         assert self.name in files
#         files.remove(self.name)
#         assert self.name not in files
#         return files
# 
#     def get_model(self, content=True, file_content=True):
#         os_path = os.path.join(self.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.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


# Path: nbx/nbmanager/bundle/manager.py
import os
import io
import shutil
import nbformat
from .bundle import NotebookBundle
from nbformat import sign



current = nbformat.v4


def is_notebook(path):
    # checks if path follows bundle format
    if not path.endswith('.ipynb'):
        return False
    # if that we have same named ipynb file in directory
    name = path.rsplit('/', 1)[-1]
    file_path = os.path.join(path, name)
    has_file = os.path.isfile(file_path)
    return has_file


def _list_bundles(path):
    root, dirs, files = next(os.walk(path))
    bundles = (os.path.join(root, d) for d in dirs)
    bundles = filter(lambda path: is_notebook(path), bundles)
    bundles = list(bundles)
    return bundles


class BundleManager(object):

    bundle_class = NotebookBundle