"""The basic dict based notebook format.

The Python representation of a notebook is a nested structure of
dictionary subclasses that support attribute access
(ipython_genutils.ipstruct.Struct). The functions in this module are merely
helpers to build the structs in the right form.
"""

# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.

import pprint
import uuid

from ipython_genutils.ipstruct import Struct
from ipython_genutils.py3compat import cast_unicode

#-----------------------------------------------------------------------------
# Code
#-----------------------------------------------------------------------------

# Change this when incrementing the nbformat version
nbformat = 3
nbformat_minor = 0
nbformat_schema = {
    (3, 0): 'nbformat.v3.schema.json'
}

class NotebookNode(Struct):
    pass


def from_dict(d):
    if isinstance(d, dict):
        newd = NotebookNode()
        for k,v in d.items():
            newd[k] = from_dict(v)
        return newd
    elif isinstance(d, (tuple, list)):
        return [from_dict(i) for i in d]
    else:
        return d


def new_output(output_type, output_text=None, output_png=None,
    output_html=None, output_svg=None, output_latex=None, output_json=None,
    output_javascript=None, output_jpeg=None, prompt_number=None,
    ename=None, evalue=None, traceback=None, stream=None, metadata=None):
    """Create a new output, to go in the ``cell.outputs`` list of a code cell.
    """
    output = NotebookNode()
    output.output_type = str(output_type)

    if metadata is None:
        metadata = {}
    if not isinstance(metadata, dict):
        raise TypeError("metadata must be dict")


    if output_type in {u'pyout', 'display_data'}:
        output.metadata = metadata

    if output_type != 'pyerr':
        if output_text is not None:
            output.text = cast_unicode(output_text)
        if output_png is not None:
            output.png = cast_unicode(output_png)
        if output_jpeg is not None:
            output.jpeg = cast_unicode(output_jpeg)