#-*- coding: utf-8 -*-
"""
@brief      test log(time=12s)
"""

import sys
import os
import unittest


try:
    import pyquickhelper as skip_
except ImportError:
    path = os.path.normpath(
        os.path.abspath(
            os.path.join(
                os.path.split(__file__)[0],
                "..",
                "..",
                "..",
                "pyquickhelper",
                "src")))
    if path not in sys.path:
        sys.path.append(path)
    import pyquickhelper as skip_


try:
    import src
except ImportError:
    path = os.path.normpath(
        os.path.abspath(
            os.path.join(
                os.path.split(__file__)[0],
                "..",
                "..")))
    if path not in sys.path:
        sys.path.append(path)
    import src

from pyquickhelper.loghelper import fLOG
from pyquickhelper.pycode import get_temp_folder, is_travis_or_appveyor
from pyquickhelper.ipythonhelper import execute_notebook_list
from pyquickhelper.pycode import compare_module_version
from pyquickhelper.ipythonhelper import install_python_kernel_for_unittest
import IPython


class TestRunNotebooksPyData2016_pyjs(unittest.TestCase):

    def test_run_notebook_pyjs(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        if sys.version_info[0] == 2:
            # notebooks are not converted into python 2.7, so not tested
            return

        if compare_module_version(IPython.__version__, "4.0.0") < 0:
            warnings.warn("IPython is not recent enough")
            return

        kernel_name = None if "travis" in sys.executable else install_python_kernel_for_unittest(
            "python3_module_template")

        temp = get_temp_folder(__file__, "temp_run_notebooks_pyjs")

        # selection of notebooks
        fnb = os.path.normpath(os.path.join(
            os.path.abspath(os.path.dirname(__file__)), "..", "..", "_doc", "notebooks", "2016", "pydata"))
        keepnote = []
        for f in os.listdir(fnb):
            if is_travis_or_appveyor() == "travis":
                if "pyjs_brython" in f:
                    continue
            if os.path.splitext(f)[-1] == ".ipynb" and "pyjs_" in f:
                keepnote.append(os.path.join(fnb, f))
        assert len(keepnote) > 0

        # function to tell that a can be run
        def valid(cell):
            if "open_html_form" in cell:
                return False
            if "open_window_params" in cell:
                return False
            if '<div style="position:absolute' in cell:
                return False
            return True

        # additionnal path to add
        addpaths = [os.path.normpath(os.path.join(
            os.path.abspath(os.path.dirname(__file__)), "..", "..", "src")),
            os.path.normpath(os.path.join(
                os.path.abspath(os.path.dirname(__file__)), "..", "..", "..", "pyquickhelper", "src"))
        ]

        # creation of a kernel
        kernel_name = None if "travis" in sys.executable else install_python_kernel_for_unittest(
            "python3_module_template")

        # run the notebooks
        res = execute_notebook_list(
            temp, keepnote, fLOG=fLOG, valid=valid, additional_path=addpaths, kernel_name=kernel_name)

        # final checkings
        assert len(res) > 0
        fails = [(os.path.split(k)[-1], v)
                 for k, v in sorted(res.items()) if not v[0]]
        for f in fails:
            fLOG(f)
        for k, v in sorted(res.items()):
            name = os.path.split(k)[-1]
            fLOG(name, v[0], v[1])
        if len(fails) > 0:
            raise fails[0][1][-1]

if __name__ == "__main__":
    unittest.main()

@@ -93,16 +93,31 @@
 nittest%0A
+import warnings
 %0A%0Atry:%0A 
@@ -2062,16 +2062,89 @@
 r(fnb):%0A
+            if os.path.splitext(f)%5B-1%5D == %22.ipynb%22 and %22pyjs_%22 in f:%0A    
         
@@ -2187,16 +2187,20 @@
 ravis%22:%0A
+    
         
@@ -2255,85 +2255,83 @@
     
-continue%0A            if os.path.splitext(f)%5B-1%5D == %22.ipynb%22 and %22pyjs_%22 in f:
+    warnings.warn(%22Travis, skipping %22 + f)%0A                        continue
 %0A   

update unit test for travis