"""Helper for adding content stored in a file to a jupyter notebook."""
import os
from pkg_resources import resource_string
from IPython.display import display, Javascript, HTML


# Originally I implemented this using regular open() and read(), so it
# could use relative paths from the importing file.
#
# But for distributable packages, pkg_resources.resource_string is the
# best way to load data files, because it works even if the package is
# in an egg or zip file. See:
# http://pythonhosted.org/setuptools/setuptools.html#accessing-data-files-at-runtime

def display_auto(qcodes_path, file_type=None):
    """
    Display some javascript, css, or html content in a jupyter notebook.

    Content comes from a package-relative file path. Will use the file
    extension to determine file type unless overridden by file_type

    Args:
        qcodes_path (str): the path to the target file within the qcodes
            package, like 'widgets/widgets.js'

        file_type (Optional[str]): Override the file extension to determine
            what type of file this is. Case insensitive, supported values
            are 'js', 'css', and 'html'
    """
    contents = resource_string('qcodes', qcodes_path).decode('utf-8')

    if file_type is None:
        ext = os.path.splitext(qcodes_path)[1].lower()
    elif 'js' in file_type.lower():
        ext = '.js'
    elif 'css' in file_type.lower():
        ext = '.css'
    else:
        ext = '.html'

    if ext == '.js':
        display(Javascript(contents))
    elif ext == '.css':
        display(HTML('<style>' + contents + '</style>'))
    else:
        # default to html. Anything else?
        display(HTML(contents))

[((30, 15, 30, 53), 'pkg_resources.resource_string', 'resource_string', ({(30, 31, 30, 39): '"""qcodes"""', (30, 41, 30, 52): 'qcodes_path'}, {}), "('qcodes', qcodes_path)", False, 'from pkg_resources import resource_string\n'), ((42, 16, 42, 36), 'IPython.display.Javascript', 'Javascript', ({(42, 27, 42, 35): 'contents'}, {}), '(contents)', False, 'from IPython.display import display, Javascript, HTML\n'), ((44, 16, 44, 55), 'IPython.display.HTML', 'HTML', ({(44, 21, 44, 54): "('<style>' + contents + '</style>')"}, {}), "('<style>' + contents + '</style>')", False, 'from IPython.display import display, Javascript, HTML\n'), ((47, 16, 47, 30), 'IPython.display.HTML', 'HTML', ({(47, 21, 47, 29): 'contents'}, {}), '(contents)', False, 'from IPython.display import display, Javascript, HTML\n'), ((33, 14, 33, 43), 'os.path.splitext', 'os.path.splitext', ({(33, 31, 33, 42): 'qcodes_path'}, {}), '(qcodes_path)', False, 'import os\n')]