# "bokeh is imported and unused as a quick way to check for directory bokeh/bokeh/static/js
# which is required for many (but not all) examples to run properly.
import bokeh
import glob
import os
import sys

# TODO: --no-log option
#       --test-all option (run through tests on every file in a given directory, rather than a small subset)
#           - This is currently the default behavior
#       --clean option (remove .html files created by running examples)


def tester(TestingGround, HomeDir):
    """
    Collect and run all .py files in an examples directory, ignoring __init__.py

    User input is collected to determine a properly or improperly displayed page
    """

    os.chdir(TestingGround)

    if 'notebook' in TestingGround:
        command = "ipython notebook"
        suffix = ".ipynb"
    else:
        command = "python"
        suffix = ".py"

    TestFiles = [
        fileName for fileName in os.listdir('.') if fileName.endswith(suffix) and fileName != '__init__.py'
    ]

    Log = []

    get_input = check_py_ver()

    for fileName in TestFiles:
        try:
            print("\nOpening %s.\n" % fileName)
            os.system("%s %s" % (command, fileName))

            TestStatus = get_input("Did the plot(s) in %s display correctly? (y/n) " % fileName)
            while not TestStatus.startswith(('y', 'n')):
                print()
                TestStatus = get_input("Unexpected answer.  Please type y or n.  ")
            if TestStatus.startswith('n'):
                ErrorReport = get_input("Please describe the problem: ")
                Log.append("\n\n%s: \n %s" % (fileName, ErrorReport))
        except KeyboardInterrupt:
            break

    cleaner()
    os.chdir(HomeDir)

    if Log:
        logger(Log)


def check_py_ver():
    """
    Check the version of Python to find the correct user input function.
    """

    if sys.version_info[0] >= 3:
        get_input = input
    else:
        get_input = raw_input

    return get_input


def cleaner():
    """
    Remove all .html files created by running each of these python files.
    """

    dustbin = glob.glob('*.html')
    for dust in dustbin:
        os.remove(dust)


def logger(ErrorArray):
    """
    Log errors by appending to a log.txt file.
    """

    with open(logfile, 'a') as f:
        print()
        print("\nWriting error log to %s" % logfile)
        f.write("%s\n" % base_dir)
        for error in ErrorArray:
            f.write("%s\n" % error)


if __name__ == '__main__':
    if len(sys.argv) == 2 and sys.argv[1] in ['notebook', 'server', 'file']:
        target = sys.argv[1]

        logfile = "%sExamplesTestlog.txt" % target
        if os.path.exists(logfile):
            os.remove(logfile)

        base_dir = os.getcwd()
        test_dir = os.path.join(base_dir, 'plotting', target)
        tester(test_dir, base_dir)
    else:
        # # This is kept necessarily explicit so that you don't
        # accidentally provide a directory that has .html files
        # you don't want to have deleted.
        print("Please choose an examples directory to test in ('python bokehTest.py <notebook/server/file>)")
        sys.exit(1)

@@ -189,16 +189,44 @@
 port os%0A
+from six.moves import input%0A
 import s
@@ -229,16 +229,17 @@
 rt sys%0A%0A
+%0A
 # TODO: 
@@ -562,16 +562,26 @@
 all .py 
+or .ipynb 
 files in
@@ -749,168 +749,8 @@
 d)%0A%0A
-    if 'notebook' in TestingGround:%0A        command = %22ipython notebook%22%0A        suffix = %22.ipynb%22%0A    else:%0A        command = %22python%22%0A        suffix = %22.py%22%0A%0A
     
@@ -811,16 +811,24 @@
 dir('.')
+%0A       
  if file
@@ -845,14 +845,25 @@
 ith(
-suffix
+('.py', '.ipynb')
 ) an
@@ -915,40 +915,8 @@
 %5B%5D%0A%0A
-    get_input = check_py_ver()%0A%0A
     
@@ -1019,38 +1019,15 @@
     
-os.system(%22%25s %25s%22 %25 (command, 
+runner(
 file
@@ -1031,17 +1031,16 @@
 ileName)
-)
 %0A%0A      
@@ -1054,28 +1054,24 @@
 estStatus = 
-get_
 input(%22Did t
@@ -1236,20 +1236,16 @@
 tatus = 
-get_
 input(%22U
@@ -1360,20 +1360,16 @@
 eport = 
-get_
 input(%22P
@@ -1601,234 +1601,306 @@
 def 
-check_py_ver():%0A    %22%22%22%0A    Check the version of Python to find the correct user input function.%0A    %22%22%22%0A%0A    if sys.version_info%5B0%5D %3E= 3:%0A        get_input = input%0A    else:%0A        get_input = raw_input%0A%0A    return get_input
+runner(someFile):%0A    %22%22%22Determines how to open a file depending%0A    on whether it is a .py or a .ipynb file%0A    %22%22%22%0A%0A    if someFile.endswith('.py'):%0A        command = %22python%22%0A    elif someFile.endswith('.ipynb'):%0A        command = %22ipython notebook%22%0A%0A    os.system(%22%25s %25s%22 %25 (command, someFile))
 %0A%0A%0Ad

Use six for input compat, support for dirs with both .py and .ipynb files.