Пример #1
0
def make_run_tests_script_text(test_headers, test_argvs, quick_tests=None,
                               repodir=None, exclude_list=[]):
    """
    Autogeneration function

    TODO move to util_autogen or just depricate

    Examples:
        >>> from utool.util_tests import *  # NOQA
        >>> import utool  # NOQA
        >>> testdirs = ['~/code/ibeis/test_ibs*.py']
    """
    import utool as ut
    from os.path import relpath, join, dirname  # NOQA

    exclude_list += ['__init__.py']

    # General format of the testing script

    script_fmtstr = ut.codeblock(
        r'''
        #!/bin/bash
        # Runs all tests
        # Win32 path hacks
        export CWD=$(pwd)
        export PYMAJOR="$(python -c "import sys; print(sys.version_info[0])")"

        # <CORRECT_PYTHON>
        # GET CORRECT PYTHON ON ALL PLATFORMS
        export SYSNAME="$(expr substr $(uname -s) 1 10)"
        if [ "$SYSNAME" = "MINGW32_NT" ]; then
            export PYEXE=python
        else
            if [ "$PYMAJOR" = "3" ]; then
                # virtual env?
                export PYEXE=python
            else
                export PYEXE=python2.7
            fi
        fi
        # </CORRECT_PYTHON>

        PRINT_DELIMETER()
        {{
            printf "\n#\n#\n#>>>>>>>>>>> next_test\n\n"
        }}

        export TEST_ARGV="{test_argvs} $@"

        {dirdef_block}

        # Default tests to run
        set_test_flags()
        {{
            export DEFAULT=$1
        {testdefault_block}
        }}
        set_test_flags OFF
        {testdefaulton_block}

        # Parse for bash commandline args
        for i in "$@"
        do
        case $i in --testall)
            set_test_flags ON
            ;;
        esac
        {testcmdline_block}
        done

        BEGIN_TESTS()
        {{
        cat <<EOF
        {runtests_bubbletext}
        EOF
            echo "BEGIN: TEST_ARGV=$TEST_ARGV"
            PRINT_DELIMETER
            num_passed=0
            num_ran=0
            export FAILED_TESTS=''
        }}

        RUN_TEST()
        {{
            echo "RUN_TEST: $@"
            export TEST="$PYEXE $@ $TEST_ARGV"
            $TEST
            export RETURN_CODE=$?
            echo "RETURN_CODE=$RETURN_CODE"
            PRINT_DELIMETER
            num_ran=$(($num_ran + 1))
            if [ "$RETURN_CODE" == "0" ] ; then
                num_passed=$(($num_passed + 1))
            fi
            if [ "$RETURN_CODE" != "0" ] ; then
                export FAILED_TESTS="$FAILED_TESTS\n$TEST"
            fi
        }}

        END_TESTS()
        {{
            echo "RUN_TESTS: DONE"
            if [ "$FAILED_TESTS" != "" ] ; then
                echo "-----"
                printf "Failed Tests:"
                printf "$FAILED_TESTS\n"
                printf "$FAILED_TESTS\n" >> failed_shelltests.txt
                echo "-----"
            fi
            echo "$num_passed / $num_ran tests passed"
        }}

        #---------------------------------------------
        # START TESTS
        BEGIN_TESTS

        {quicktest_block}

        {test_block}

        #---------------------------------------------
        # END TESTING
        END_TESTS
        ''')

    testcmdline_fmtstr = ut.codeblock(
        r'''
        case $i in --notest{header_lower})
            export {testflag}=OFF
            ;;
        esac
        case $i in --test{header_lower})
            export {testflag}=ON
            ;;
        esac
        ''')

    header_test_block_fmstr = ut.codeblock(
        r'''

        #---------------------------------------------
        #{header_text}
        if [ "${testflag}" = "ON" ] ; then
        cat <<EOF
        {header_bubble_text}
        EOF
        {testlines_block}
        fi
        ''')

    #specialargv = '--noshow'
    specialargv = ''
    testline_fmtstr = 'RUN_TEST ${dirvar}/{fpath} {specialargv}'
    testline_fmtstr2 = 'RUN_TEST {fpath} {specialargv}'

    def format_testline(fpath, dirvar):
        if dirvar is None:
            return testline_fmtstr2.format(fpath=fpath, specialargv=specialargv)
        else:
            return testline_fmtstr.format(dirvar=dirvar, fpath=fpath, specialargv=specialargv)

    default_flag_line_list = []
    defaulton_flag_line_list = []
    testcmdline_list = []
    dirdef_list = []
    header_test_block_list = []

    known_tests = ut.ddict(list)

    # Tests to always run
    if quick_tests is not None:
        quicktest_block = '\n'.join(
            ['# Quick Tests (always run)'] +
            ['RUN_TEST ' + testline for testline in quick_tests])
    else:
        quicktest_block = '# No quick tests'

    # Loop over different test types
    for testdef_tup in test_headers:
        header, default, modname, dpath, pats, testcmds = testdef_tup
        # Build individual test type information
        header_upper =  header.upper()
        header_lower = header.lower()
        testflag = header_upper + '_TEST'

        if modname is not None:
            dirvar = header_upper + '_DIR'
            dirdef = ''.join([
                'export {dirvar}=$($PYEXE -c "',
                'import os, {modname};',
                'print(str(os.path.dirname(os.path.dirname({modname}.__file__))))',
                '")']).format(dirvar=dirvar, modname=modname)
            dirdef_list.append(dirdef)
        else:
            dirvar = None

        # Build test dir
        #dirvar = header_upper + '_DIR'
        #dirdef = 'export {dirvar}={dirname}'.format(dirvar=dirvar, dirname=dirname)
        #dirdef_list.append(dirdef)

        # Build command line flags
        default_flag_line = 'export {testflag}=$DEFAULT'.format(testflag=testflag)

        if default:
            defaulton_flag_line = 'export {testflag}=ON'.format(testflag=testflag)
            defaulton_flag_line_list.append(defaulton_flag_line)

        testcmdline_fmtdict = dict(header_lower=header_lower,
                                        testflag=testflag,)
        testcmdline = testcmdline_fmtstr.format(**testcmdline_fmtdict)

        #ut.ls(dpath)

        # VERY HACK BIT OF CODE

        # Get list of tests from patterns
        if testcmds is None:
            if modname is not None:
                module = __import__(modname)
                repo_path = dirname(dirname(module.__file__))
            else:
                repo_path = repodir
            dpath_ = ut.unixpath(util_path.unixjoin(repo_path, dpath))

            if header_upper == 'OTHER':
                # Hacky way to grab any other tests not explicitly seen in this directory
                _testfpath_list = list(set(ut.glob(dpath_, '*.py')) - set(known_tests[dpath_]))
                #_testfpath_list = ut.glob(dpath_, '*.py')
                #set(known_tests[dpath_])
            else:
                _testfpath_list = ut.flatten([ut.glob(dpath_, pat) for pat in pats])

            def not_excluded(x):
                return not any([x.find(exclude) > -1 for exclude in exclude_list])

            _testfpath_list = list(filter(not_excluded, _testfpath_list))

            known_tests[dpath_].extend(_testfpath_list)
            #print(_testfpath_list)
            testfpath_list = [util_path.unixjoin(dpath, relpath(fpath, dpath_))
                              for fpath in _testfpath_list]

            testline_list = [format_testline(fpath, dirvar) for fpath in testfpath_list]
        else:
            testline_list = testcmds

        testlines_block = ut.indentjoin(testline_list).strip('\n')

        # Construct test block for this type
        header_text = header_upper + ' TESTS'
        headerfont = 'cybermedium'
        header_bubble_text =  ut.indent(ut.bubbletext(header_text, headerfont).strip())
        header_test_block_dict = dict(
            testflag=testflag,
            header_text=header_text,
            testlines_block=testlines_block,
            header_bubble_text=header_bubble_text,)
        header_test_block = header_test_block_fmstr.format(**header_test_block_dict)

        # Append to script lists
        header_test_block_list.append(header_test_block)
        default_flag_line_list.append(default_flag_line)
        testcmdline_list.append(testcmdline)

    runtests_bubbletext = ut.bubbletext('RUN TESTS', 'cyberlarge')

    test_block = '\n'.join(header_test_block_list)
    dirdef_block = '\n'.join(dirdef_list)
    testdefault_block = ut.indent('\n'.join(default_flag_line_list))
    testdefaulton_block = '\n'.join(defaulton_flag_line_list)
    testcmdline_block = '\n'.join(testcmdline_list)

    script_fmtdict = dict(
        quicktest_block=quicktest_block,
        runtests_bubbletext=runtests_bubbletext,
        test_argvs=test_argvs, dirdef_block=dirdef_block,
        testdefault_block=testdefault_block,
        testdefaulton_block=testdefaulton_block,
        testcmdline_block=testcmdline_block,
        test_block=test_block,)
    script_text = script_fmtstr.format(**script_fmtdict)

    return script_text
Пример #2
0
def devmain():
    """
    The Developer Script
        A command line interface to almost everything

        -w     # wait / show the gui / figures are visible
        --cmd  # ipython shell to play with variables
        -t     # run list of tests

        Examples:
    """

    helpstr = ut.codeblock(
        '''
        Dev is meant to be run as an interactive script.

        The dev.py script runs any test you regiter with @devcmd in any combination
        of configurations specified by a Config object.

        Dev caches information in order to get quicker results.  # FIXME: Provide quicker results  # FIXME: len(line)
        ''')

    INTRO_TITLE = 'The dev.py Script'
    #INTRO_TEXT = ''.join((ut.bubbletext(INTRO_TITLE, font='cybermedium'), helpstr))
    INTRO_TEXT = ut.bubbletext(INTRO_TITLE, font='cybermedium')

    INTRO_STR = ut.msgblock('dev.py Intro',  INTRO_TEXT)

    EXAMPLE_STR = ut.msgblock('dev.py Examples', ut.codeblock(EXAMPLE_TEXT))

    if ut.NOT_QUIET:
        print(INTRO_STR)
    if ut.get_argflag(('--help', '--verbose')):
        print(EXAMPLE_STR)

    CMD   = ut.get_argflag('--cmd')
    NOGUI = not ut.get_argflag('--gui')

    if len(sys.argv) == 1:
        print('Run dev.py with arguments!')
        sys.exit(1)

    # Run Precommands
    run_devprecmds()

    #
    #
    # Run IBEIS Main, create controller, and possibly gui
    print('++dev')
    main_locals = ibeis.main(gui=ut.get_argflag('--gui'))
    #utool.set_process_title('IBEIS_dev')

    #
    #
    # Load snippet variables
    SNIPPITS = True and CMD
    if SNIPPITS:
        snippet_locals = dev_snippets(main_locals)
        snippet_execstr = utool.execstr_dict(snippet_locals, 'snippet_locals')
        exec(snippet_execstr)

    #
    #
    # Development code
    RUN_DEV = True  # RUN_DEV = '__IPYTHON__' in vars()
    if RUN_DEV:
        dev_locals = run_dev(main_locals['ibs'])
        dev_execstr = utool.execstr_dict(dev_locals, 'dev_locals')
        exec(dev_execstr)

    command = ut.get_argval('--eval', type_=str, default=None)
    if command is not None:
        result = eval(command, globals(), locals())
        print('result = %r' % (result,))
        #ibs.search_annot_notes('360')

    #
    #
    # Main Loop (IPython interaction, or some exec loop)
    #if '--nopresent' not in sys.argv or '--noshow' in sys.argv:
    ut.show_if_requested()
    if ut.get_argflag(('--show', '--wshow')):
        pt.present()
    main_execstr = ibeis.main_loop(main_locals, ipy=(NOGUI or CMD))
    exec(main_execstr)

    #
    #
    # Memory profile
    if ut.get_argflag('--memprof'):
        utool.print_resource_usage()
        utool.memory_profile()

    print('exiting dev')
Пример #3
0
def embed(parent_locals=None, parent_globals=None, exec_lines=None,
          remove_pyqt_hook=True, N=0):
    """
    Starts interactive session. Similar to keyboard command in matlab.
    Wrapper around IPython.embed

    Args:
        parent_locals (None):
        parent_globals (None):
        exec_lines (None):
        remove_pyqt_hook (bool):
        N (int):

    CommandLine:
        python -m utool.util_dbg --test-embed

    References:
       http://stackoverflow.com/questions/27911570/can-you-specify-a-command-to-run-after-you-embed-into-ipython/27914204#27914204
       http://stackoverflow.com/questions/15167200/how-do-i-embed-an-ipython-interpreter-into-an-application-running-in-an-ipython

    TODO:
        try:
            get_ipython
        except NameError:
            banner=exit_msg=''
        else:
            banner = '*** Nested interpreter ***'
            exit_msg = '*** Back in main IPython ***'

        # First import the embed function
        from IPython.frontend.terminal.embed import InteractiveShellEmbed
        # Now create the IPython shell instance. Put ipshell() anywhere in your code
        # where you want it to open.
        ipshell = InteractiveShellEmbed(banner1=banner, exit_msg=exit_msg)
        #Then use ipshell() whenever you want to be dropped into an IPython shell. This
        #will allow you to embed (and even nest) IPython interpreters in your code and
        #inspect objects or the state of the program.

    Example:
        >>> # DISABLE_DOCTEST
        >>> from utool.util_dbg import *  # NOQA
        >>> # build test data
        >>> parent_locals = None
        >>> parent_globals = None
        >>> exec_lines = None
        >>> remove_pyqt_hook = True
        >>> N = 0
        >>> # execute function
        >>> result = embed(parent_locals, parent_globals, exec_lines, remove_pyqt_hook, N)
        >>> # verify results
        >>> print(result)
    """
    if parent_globals is None:
        parent_globals = get_parent_globals(N=N)
        #parent_globals1 = get_parent_globals(N=0)
        #exec(execstr_dict(parent_globals1, 'parent_globals1'))
    if parent_locals is None:
        parent_locals = get_parent_locals(N=N)

    stackdepth = N  # NOQA
    import utool as ut
    from functools import partial
    getframe = partial(ut.get_caller_stack_frame, N=N)  # NOQA

    exec(execstr_dict(parent_globals, 'parent_globals'))
    exec(execstr_dict(parent_locals,  'parent_locals'))
    print('')
    print('================')
    print(ut.bubbletext('EMBEDING'))
    print('================')
    print('[util] embedding')
    import IPython
    try:
        if remove_pyqt_hook:
            try:
                import guitool
                guitool.remove_pyqt_input_hook()
            except (ImportError, ValueError) as ex:
                #print(ex)
                printex(ex, iswarning=True)
                pass
            # make qt not loop forever (I had qflag loop forever with this off)
    except ImportError as ex:
        print(ex)
    NEW_METHOD = False
    if NEW_METHOD:
        user_ns = globals()
        user_ns = globals().copy()
        user_ns.update(locals())
        if parent_globals is not None:
            user_ns.update(parent_globals)
        if parent_locals is not None:
            user_ns.update(parent_locals)
        orig_argv = sys.argv  # NOQA
        print('About to start_ipython')
        config = IPython.Config()
        exec_lines_ = [
            '%pylab qt4',
            'print("Entered IPYTHON via utool")',
            'print("Entry Point: %r" % (ut.get_caller_stack_frame(N=11).f_code.co_name,))',
            #'print("Entry Point: %r" % (ut.get_caller_stack_frame(N=10).f_code.co_name,))',
            #'print("Entry Point: %r" % (ut.get_caller_stack_frame(N=9).f_code.co_name,))',
            #'print("Entry Point: %r" % (ut.get_caller_stack_frame(N=8).f_code.co_name,))',
            #'print("Entry Point: %r" % (ut.get_caller_stack_frame(N=7).f_code.co_name,))',
            #'print("Entry Point: %r" % (ut.get_caller_stack_frame(N=6).f_code.co_name,))',
            #'print("Entry Point: %r" % (ut.get_caller_stack_frame(N=5).f_code.co_name,))',
            #execstr_dict(parent_locals)
        ] + ut.ensure_str_list(exec_lines if exec_lines is not None else [])
        config.InteractiveShellApp.exec_lines = exec_lines_
        print('Exec Lines: ')
        print(ut.indentjoin(exec_lines_, '\n    >>> '))
        IPython.start_ipython(config=config, argv=[], user_ns=user_ns)
        # Exit python immediately if specifed
        if user_ns.get('qqq', False) or vars.get('qqq', False) or user_ns.get('EXIT_NOW', False):
            print('[utool.embed] EXIT_NOW or qqq specified')
            sys.exit(1)
    else:
        #from IPython.config.loader import Config
        # cfg = Config()
        #config_dict = {}
        #if exec_lines is not None:
        #    config_dict['exec_lines'] = exec_lines
        #IPython.embed(**config_dict)
        print('[util]  Get stack location with: ')
        print('[util] ut.get_caller_stack_frame(N=8).f_code.co_name')
        print('[util] set EXIT_NOW or qqq to True(ish) to hard exit on unembed')
        #print('set iup to True to draw plottool stuff')
        print('[util] call %pylab qt4 to get plottool stuff working')
        once = True
        # Allow user to set iup and redo the loop
        while once or vars().get('iup', False):
            if not once:
                # SUPER HACKY WAY OF GETTING FIGURES ON THE SCREEN BETWEEN UPDATES
                #vars()['iup'] = False
                # ALL YOU NEED TO DO IS %pylab qt4
                print('re-emebeding')
                #import plottool as pt
                #pt.update()
                #(pt.present())
                for _ in range(100):
                    time.sleep(.01)

            once = False
            #vars().get('iup', False):
            print('[util] calling IPython.embed()')
            """
            Notes:
                /usr/local/lib/python2.7/dist-packages/IPython/terminal/embed.py
                IPython.terminal.embed.InteractiveShellEmbed

                # instance comes from  IPython.config.configurable.SingletonConfigurable.instance
            """
            IPython.embed()
            #config = IPython.terminal.ipapp.load_default_config()
            #config.InteractiveShellEmbed = config.TerminalInteractiveShell
            #module = sys.modules[parent_globals['__name__']]
            #config['module'] = module
            #config['module'] = module
            #embed2(stack_depth=N + 2 + 1)
            #IPython.embed(config=config)
            #IPython.embed(config=config)
            #IPython.embed(module=module)
            # Exit python immediately if specifed
            if vars().get('EXIT_NOW', False) or vars().get('qqq', False):
                print('[utool.embed] EXIT_NOW specified')
                sys.exit(1)
Пример #4
0
def devmain():
    """
    The Developer Script
        A command line interface to almost everything

        -w     # wait / show the gui / figures are visible
        --cmd  # ipython shell to play with variables
        -t     # run list of tests

        Examples:
    """

    helpstr = ut.codeblock('''
        Dev is meant to be run as an interactive script.

        The dev.py script runs any test you regiter with @devcmd in any combination
        of configurations specified by a Config object.

        Dev caches information in order to get quicker results.  # FIXME: Provide quicker results  # FIXME: len(line)
        ''')

    INTRO_TITLE = 'The dev.py Script'
    #INTRO_TEXT = ''.join((ut.bubbletext(INTRO_TITLE, font='cybermedium'), helpstr))
    INTRO_TEXT = ut.bubbletext(INTRO_TITLE, font='cybermedium')

    INTRO_STR = ut.msgblock('dev.py Intro', INTRO_TEXT)

    EXAMPLE_STR = ut.msgblock('dev.py Examples', ut.codeblock(EXAMPLE_TEXT))

    if ut.NOT_QUIET:
        print(INTRO_STR)
    if ut.get_argflag(('--help', '--verbose')):
        print(EXAMPLE_STR)

    CMD = ut.get_argflag('--cmd')
    NOGUI = not ut.get_argflag('--gui')

    if len(sys.argv) == 1:
        print('Run dev.py with arguments!')
        sys.exit(1)

    # Run Precommands
    run_devprecmds()

    #
    #
    # Run IBEIS Main, create controller, and possibly gui
    print('++dev')
    main_locals = ibeis.main(gui=ut.get_argflag('--gui'))
    #utool.set_process_title('IBEIS_dev')

    #
    #
    # Load snippet variables
    SNIPPITS = True and CMD
    if SNIPPITS:
        snippet_locals = dev_snippets(main_locals)
        snippet_execstr = utool.execstr_dict(snippet_locals, 'snippet_locals')
        exec(snippet_execstr)

    #
    #
    # Development code
    RUN_DEV = True  # RUN_DEV = '__IPYTHON__' in vars()
    if RUN_DEV:
        dev_locals = run_dev(main_locals['ibs'])
        dev_execstr = utool.execstr_dict(dev_locals, 'dev_locals')
        exec(dev_execstr)

    command = ut.get_argval('--eval', type_=str, default=None)
    if command is not None:
        result = eval(command, globals(), locals())
        print('result = %r' % (result, ))
        #ibs.search_annot_notes('360')

    #
    #
    # Main Loop (IPython interaction, or some exec loop)
    #if '--nopresent' not in sys.argv or '--noshow' in sys.argv:
    ut.show_if_requested()
    if ut.get_argflag(('--show', '--wshow')):
        pt.present()
    main_execstr = ibeis.main_loop(main_locals, ipy=(NOGUI or CMD))
    exec(main_execstr)

    #
    #
    # Memory profile
    if ut.get_argflag('--memprof'):
        utool.print_resource_usage()
        utool.memory_profile()

    print('exiting dev')
Пример #5
0
def embed(parent_locals=None, parent_globals=None, exec_lines=None,
          remove_pyqt_hook=True, N=0):
    """
    Starts interactive session. Similar to keyboard command in matlab.
    Wrapper around IPython.embed

    Notes:
        #https://github.com/ipython/ipython/wiki/Cookbook%3a-Updating-code-for-use-with-IPython-0.11-and-later

        import IPython
        x = 3
        IPython.embed()
        c = IPython.Config()
        c.InteractiveShellApp.exec_lines = [
            '%pylab qt4',
            "print 'System Ready!'",
        ]

        def foo():
            return x + 3

        a = 3
        def bar():
            return a + 3
        bar()
        #NameError: global name 'a' is not defined


        from IPython.terminal.ipapp import TerminalIPythonApp
        x = 3
        app = TerminalIPythonApp.instance()
        app.initialize(argv=[]) # argv=[] instructs IPython to ignore sys.argv
        app.start()


    Args:
        parent_locals (None):
        parent_globals (None):
        exec_lines (None):
        remove_pyqt_hook (bool):
        N (int):

    CommandLine:
        python -m utool.util_dbg --test-embed

    References:
       http://stackoverflow.com/questions/27911570/can-you-specify-a-command-to-run-after-you-embed-into-ipython/27914204#27914204
       http://stackoverflow.com/questions/15167200/how-do-i-embed-an-ipython-interpreter-into-an-application-running-in-an-ipython

    TODO:
        try:
            get_ipython
        except NameError:
            banner=exit_msg=''
        else:
            banner = '*** Nested interpreter ***'
            exit_msg = '*** Back in main IPython ***'

        # First import the embed function
        from IPython.frontend.terminal.embed import InteractiveShellEmbed
        # Now create the IPython shell instance. Put ipshell() anywhere in your code
        # where you want it to open.
        ipshell = InteractiveShellEmbed(banner1=banner, exit_msg=exit_msg)
        #Then use ipshell() whenever you want to be dropped into an IPython shell. This
        #will allow you to embed (and even nest) IPython interpreters in your code and
        #inspect objects or the state of the program.

    Example:
        >>> # DISABLE_DOCTEST
        >>> from utool.util_dbg import *  # NOQA
        >>> # build test data
        >>> parent_locals = None
        >>> parent_globals = None
        >>> exec_lines = None
        >>> remove_pyqt_hook = True
        >>> N = 0
        >>> # execute function
        >>> result = embed(parent_locals, parent_globals, exec_lines, remove_pyqt_hook, N)
        >>> # verify results
        >>> print(result)
    """
    import utool as ut
    from functools import partial
    import IPython

    if parent_globals is None:
        parent_globals = get_parent_globals(N=N)
        #parent_globals1 = get_parent_globals(N=0)
        #exec(execstr_dict(parent_globals1, 'parent_globals1'))
    if parent_locals is None:
        parent_locals = get_parent_locals(N=N)

    stackdepth = N  # NOQA
    getframe = partial(ut.get_caller_stack_frame, N=N)  # NOQA

    exec(execstr_dict(parent_globals, 'parent_globals'))
    exec(execstr_dict(parent_locals,  'parent_locals'))
    print('')
    print('================')
    print(ut.bubbletext('EMBEDDING'))
    print('================')
    print('[util] embedding')
    try:
        if remove_pyqt_hook:
            try:
                import guitool
                guitool.remove_pyqt_input_hook()
            except (ImportError, ValueError, AttributeError) as ex:
                #print(ex)
                printex(ex, iswarning=True)
                pass
            # make qt not loop forever (I had qflag loop forever with this off)
    except ImportError as ex:
        print(ex)
    NEW_METHOD = False
    if NEW_METHOD:
        user_ns = globals()
        user_ns = globals().copy()
        user_ns.update(locals())
        if parent_globals is not None:
            user_ns.update(parent_globals)
        if parent_locals is not None:
            user_ns.update(parent_locals)
        orig_argv = sys.argv  # NOQA
        print('About to start_ipython')
        config = IPython.Config()
        exec_lines_ = [
            '%pylab qt4',
            'print("Entered IPYTHON via utool")',
            'print("Entry Point: %r" % (ut.get_caller_stack_frame(N=11).f_code.co_name,))',
            #'print("Entry Point: %r" % (ut.get_caller_stack_frame(N=10).f_code.co_name,))',
            #'print("Entry Point: %r" % (ut.get_caller_stack_frame(N=9).f_code.co_name,))',
            #'print("Entry Point: %r" % (ut.get_caller_stack_frame(N=8).f_code.co_name,))',
            #'print("Entry Point: %r" % (ut.get_caller_stack_frame(N=7).f_code.co_name,))',
            #'print("Entry Point: %r" % (ut.get_caller_stack_frame(N=6).f_code.co_name,))',
            #'print("Entry Point: %r" % (ut.get_caller_stack_frame(N=5).f_code.co_name,))',
            #execstr_dict(parent_locals)
        ] + ut.ensure_str_list(exec_lines if exec_lines is not None else [])
        config.InteractiveShellApp.exec_lines = exec_lines_
        print('Exec Lines: ')
        print(ut.indentjoin(exec_lines_, '\n    >>> '))
        IPython.start_ipython(config=config, argv=[], user_ns=user_ns)
        # Exit python immediately if specifed
        if user_ns.get('qqq', False) or vars.get('qqq', False) or user_ns.get('EXIT_NOW', False):
            print('[utool.embed] EXIT_NOW or qqq specified')
            sys.exit(1)
    else:
        #from IPython.config.loader import Config
        # cfg = Config()
        #config_dict = {}
        #if exec_lines is not None:
        #    config_dict['exec_lines'] = exec_lines
        #IPython.embed(**config_dict)
        print('[util]  Get stack location with: ')
        print('[util] ut.get_caller_stack_frame(N=8).f_code.co_name')
        print('[util] set EXIT_NOW or qqq to True(ish) to hard exit on unembed')
        #print('set iup to True to draw plottool stuff')
        print('[util] call %pylab qt4 to get plottool stuff working')
        once = True
        # Allow user to set iup and redo the loop
        while once or vars().get('iup', False):
            if not once:
                # SUPER HACKY WAY OF GETTING FIGURES ON THE SCREEN BETWEEN UPDATES
                #vars()['iup'] = False
                # ALL YOU NEED TO DO IS %pylab qt4
                print('re-emebeding')
                #import plottool as pt
                #pt.update()
                #(pt.present())
                for _ in range(100):
                    time.sleep(.01)

            once = False
            #vars().get('iup', False):
            print('[util] calling IPython.embed()')
            """
            Notes:
                /usr/local/lib/python2.7/dist-packages/IPython/terminal/embed.py
                IPython.terminal.embed.InteractiveShellEmbed

                # instance comes from  IPython.config.configurable.SingletonConfigurable.instance
            """
            #c = IPython.Config()
            #c.InteractiveShellApp.exec_lines = [
            #    '%pylab qt4',
            #    "print 'System Ready!'",
            #]
            #IPython.embed(config=c)
            try:
                IPython.embed()
            except RuntimeError as ex:
                ut.printex(ex, 'Failed to open ipython')
            #config = IPython.terminal.ipapp.load_default_config()
            #config.InteractiveShellEmbed = config.TerminalInteractiveShell
            #module = sys.modules[parent_globals['__name__']]
            #config['module'] = module
            #config['module'] = module
            #embed2(stack_depth=N + 2 + 1)
            #IPython.embed(config=config)
            #IPython.embed(config=config)
            #IPython.embed(module=module)
            # Exit python immediately if specifed
            if vars().get('EXIT_NOW', False) or vars().get('qqq', False):
                print('[utool.embed] EXIT_NOW specified')
                sys.exit(1)
Пример #6
0
def main():
    print(ut.bubbletext('TeX FiX'))
    #dryrun = ut.get_argflag('--dryrun')
    dryrun = not ut.get_argflag('-w')
    if dryrun:
        print('dryrun=True, specify --w to save any changes')
    find_text = ut.get_argval('--grep')
    if find_text is not None:
        tup = ut.grep(find_text, fpath_list=testdata_fpaths(), verbose=True)
        found_fpath_list, found_lines_list, found_lxs_list = tup
    else:
        print('~~~ --grep [text] ~~~')

    findrepl = ut.get_argval('--sed', type_=list, default=None)
    if findrepl is not None:
        assert len(findrepl) == 2, 'must specify a search and replace'
        search, repl = findrepl
        tup = ut.sed(search,
                     repl,
                     fpath_list=testdata_fpaths(),
                     verbose=True,
                     force=not dryrun)

    @ut.argv_flag_dec(indent='    ')
    def fix_common_errors():
        for tex_fpath in testdata_fpaths():
            fix_section_common_errors(tex_fpath, dryrun)

    @ut.argv_flag_dec(indent='    ')
    def fixcap():
        for tex_fpath in testdata_fpaths():
            fix_section_title_capitalization(tex_fpath, dryrun)

    @ut.argv_flag_dec(indent='    ')
    def fixsent():
        fix_sentences()
        # for tex_fpath in testdata_fpaths():
        #     fix_section_title_capitalization(tex_fpath, dryrun)

    @ut.argv_flag_dec(indent='    ')
    def glossterms():
        re_glossterm = ut.named_field('glossterm', '.' + ut.REGEX_NONGREEDY)
        pat = r'\\glossterm{' + re_glossterm + '}'
        tup = ut.grep(pat, fpath_list=testdata_fpaths(), verbose=True)
        found_fpath_list, found_lines_list, found_lxs_list = tup
        glossterm_list = []
        for line in ut.flatten(found_lines_list):
            match = re.search(pat, line)
            glossterm = match.groupdict()['glossterm']
            glossterm_list.append(glossterm)
        print('Glossary Terms: ')
        print(ut.repr2(ut.dict_hist(glossterm_list), nl=True, strvals=True))

    @ut.argv_flag_dec(indent='    ')
    def fix_chktex():
        """
        ./texfix.py --fixcite --fix-chktex
        """
        import parse
        fpaths = testdata_fpaths()
        print('Running chktex')
        output_list = [
            ut.cmd('chktex', fpath, verbose=False)[0] for fpath in fpaths
        ]

        fixcite = ut.get_argflag('--fixcite')
        fixlbl = ut.get_argflag('--fixlbl')
        fixcmdterm = ut.get_argflag('--fixcmdterm')

        for fpath, output in zip(fpaths, output_list):
            text = ut.readfrom(fpath)
            buffer = text.split('\n')
            pat = '\n' + ut.positive_lookahead('Warning')
            warn_list = list(
                filter(lambda x: x.startswith('Warning'),
                       re.split(pat, output)))
            delete_linenos = []

            if not (fixcmdterm or fixlbl or fixcite):
                print(' CHOOSE A FIX ')

            modified_lines = []

            for warn in warn_list:
                warnlines = warn.split('\n')
                pres = parse.parse(
                    'Warning {num} in {fpath} line {lineno}: {warnmsg}',
                    warnlines[0])
                if pres is not None:
                    fpath_ = pres['fpath']
                    lineno = int(pres['lineno']) - 1
                    warnmsg = pres['warnmsg']
                    try:
                        assert fpath == fpath_, ('%r != %r' % (fpath, fpath_))
                    except AssertionError:
                        continue
                    if 'No errors printed' in warn:
                        #print('Cannot fix')
                        continue
                    if lineno in modified_lines:
                        print('Skipping modified line')
                        continue
                    if fixcmdterm and warnmsg == 'Command terminated with space.':
                        print('Fix command termination')
                        errorline = warnlines[1]  # NOQA
                        carrotline = warnlines[2]
                        pos = carrotline.find('^')
                        if 0:
                            print('pos = %r' % (pos, ))
                            print('lineno = %r' % (lineno, ))
                            print('errorline = %r' % (errorline, ))
                        modified_lines.append(lineno)
                        line = buffer[lineno]
                        pre_, post_ = line[:pos], line[pos + 1:]
                        newline = (pre_ + '{} ' + post_).rstrip(' ')
                        #print('newline   = %r' % (newline,))
                        buffer[lineno] = newline
                    elif fixlbl and warnmsg == 'Delete this space to maintain correct pagereferences.':
                        print('Fix label newline')
                        fpath_ = pres['fpath']
                        errorline = warnlines[1]  # NOQA
                        new_prevline = buffer[
                            lineno - 1].rstrip() + errorline.lstrip(' ')
                        buffer[lineno - 1] = new_prevline
                        modified_lines.append(lineno)
                        delete_linenos.append(lineno)
                    elif fixcite and re.match(
                            'Non-breaking space \\(.~.\\) should have been used',
                            warnmsg):
                        #print(warnmsg)
                        #print('\n'.join(warnlines))
                        print('Fix citation space')
                        carrotline = warnlines[2]
                        pos = carrotline.find('^')
                        modified_lines.append(lineno)
                        line = buffer[lineno]
                        if line[pos] == ' ':
                            pre_, post_ = line[:pos], line[pos + 1:]
                            newline = (pre_ + '~' + post_).rstrip(' ')
                        else:
                            pre_, post_ = line[:pos + 1], line[pos + 1:]
                            newline = (pre_ + '~' + post_).rstrip(' ')
                            print(warn)
                            print(line[pos])
                            assert False
                            #assert line[pos] == ' ', '%r' % line[pos]
                            break
                        if len(pre_.strip()) == 0:
                            new_prevline = buffer[
                                lineno - 1].rstrip() + newline.lstrip(' ')
                            buffer[lineno - 1] = new_prevline
                            delete_linenos.append(lineno)
                        else:
                            #print('newline   = %r' % (newline,))
                            buffer[lineno] = newline
                    #print(warn)

            if len(delete_linenos) > 0:
                mask = ut.index_to_boolmask(delete_linenos, len(buffer))
                buffer = ut.compress(buffer, ut.not_list(mask))
            newtext = '\n'.join(buffer)

            #ut.dump_autogen_code(fpath, newtext, 'tex', fullprint=False)
            ut.print_difftext(
                ut.get_textdiff(text, newtext, num_context_lines=4))
            if ut.get_argflag('-w'):
                ut.writeto(fpath, newtext)
            else:
                print('Specify -w to finialize change')

    @ut.argv_flag_dec(indent='    ')
    def reformat():
        """
        ./texfix.py --reformat --fpaths NewParts.tex
        >>> from texfix import *  # NOQA
        """
        fpaths = testdata_fpaths()

        for fpath in fpaths:
            text = ut.readfrom(fpath)
            root = latex_parser.LatexDocPart.parse_text(text, debug=None)

            if ut.get_argflag('--fixcref'):
                root.find(' \\\\cref')
                continue

            #print(root.children)
            #root.children = root.children[0:5]
            #print('Parsed Str Short')
            new_text = '\n'.join(root.reformat_blocks(debug=None))
            # remove trailing spaces
            new_text = re.sub(' *$', '', new_text, flags=re.MULTILINE)
            # remove double newlines
            new_text = re.sub('(\n *)+\n+',
                              '\n\n',
                              new_text,
                              flags=re.MULTILINE)

            if ut.get_argflag('--summary'):
                print('---summary---')
                root.print_summary()
                print('---/summary---')
                # ut.colorprint(root.summary_str(), 'blue')

            numchars1 = len(text.replace(' ', '').replace('\n', ''))
            numchars2 = len(new_text.replace(' ', '').replace('\n', ''))

            print('numchars1 = %r' % (numchars1, ))
            print('numchars2 = %r' % (numchars2, ))
            #assert numchars1 == numchars2, '%r == %r' % (numchars1, numchars2)

            print('old newlines = %r' % (text.count('\n'), ))
            print('new newlines = %r' % (new_text.count('\n'), ))

            #import unicodedata
            #new_text = unicodedata.normalize('NFKD', new_text).encode('ascii','ignore')
            #print('new_text = %r' % (new_text,))

            ut.dump_autogen_code(fpath,
                                 new_text,
                                 codetype='latex',
                                 fullprint=False)

    @ut.argv_flag_dec(indent='    ')
    def outline():
        """
        ./texfix.py --fpaths chapter4-application.tex --outline --asmarkdown --numlines=999 -w --ignoreinputstartswith=def,Crall,header,colordef,figdef
        """
        fpaths = testdata_fpaths()
        print('fpaths = %r' % (fpaths, ))

        for fpath in fpaths:
            text = ut.readfrom(fpath)
            root = latex_parser.LatexDocPart.parse_text(text, debug=None)

            # HACK
            new_text = '\n'.join(root.reformat_blocks(debug=None))
            # remove trailing spaces
            new_text = re.sub(' *$', '', new_text, flags=re.MULTILINE)
            # remove double newlines
            new_text = re.sub('(\n *)+\n+',
                              '\n\n',
                              new_text,
                              flags=re.MULTILINE)

            document = root.find_descendant_type('document')
            #document = root.find_descendant_type('section', pat='Identification')
            print('document = %r' % (document, ))
            if document is not None:
                root = document

            sectionpat = ut.get_argval('--section', default=None)
            if sectionpat is not None:
                root = root.find_descendant_type('section', pat=sectionpat)
                print('root = %r' % (root, ))
                if root is None:
                    # import utool
                    # utool.embed()
                    raise Exception('section %r does not exist' % (sectionpat))
            #print(root.get_debug_tree_text())

            #ut.colorprint(root.summary_str(outline=True), 'yellow')
            print('---outline---')
            outline = True
            # outline = False
            outline_text = root.summary_str(outline=outline, highlight=False)
            summary = root.summary_str(outline=outline, highlight=True)
            if not ut.get_argflag('-w'):
                print(summary)
            print('---/outline---')
            if root._config['asmarkdown']:
                codetype = 'markdown'
                newext = '.md'
            else:
                codetype = 'latex'
                newext = None

            ut.dump_autogen_code(ut.augpath(fpath,
                                            augpref='outline_',
                                            newext=newext),
                                 outline_text,
                                 codetype=codetype,
                                 fullprint=False)

    @ut.argv_flag_dec(indent='    ')
    def tozip():
        re_fpath = ut.named_field('fpath', 'figure.*?[jp][pn]g') + '}'
        patterns = [
            'chapter4-application.tex', 'figdef4*', 'main.tex', 'def.tex',
            'Crall*', 'thesis.cls', 'header*', 'colordef.tex', '*.bib'
        ]
        exclude_dirs = ['guts']
        fpaths = sorted(
            ut.glob('.', patterns, recursive=True, exclude_dirs=exclude_dirs))

        tup = ut.grep(re_fpath, fpath_list=fpaths, verbose=True)
        found_fpath_list, found_lines_list, found_lxs_list = tup
        fig_fpath_list = []
        for line in ut.flatten(found_lines_list):
            if not line.startswith('%'):
                for match in re.finditer(re_fpath, line):
                    fig_fpath = match.groupdict()['fpath']
                    if 'junc' not in fig_fpath and 'markov' not in fig_fpath and 'bayes' not in fig_fpath:
                        fig_fpath_list += [fig_fpath]

        fpath_list = fig_fpath_list + fpaths
        ut.archive_files('chap4.zip', fpath_list)

    fix_common_errors()
    fixcap()
    glossterms()
    fix_chktex()
    reformat()
    outline()
    tozip()
    fixsent()

    ut.argv_flag_dec(check_doublewords, indent='    ')()
    ut.argv_flag_dec(findcite, indent='    ')()

    print('Use --fpaths to specify specific files')
Пример #7
0
def make_run_tests_script_text(test_headers,
                               test_argvs,
                               quick_tests=None,
                               repodir=None,
                               exclude_list=[]):
    """
    Autogeneration function

    TODO move to util_autogen or just depricate

    Examples:
        >>> from utool.util_tests import *  # NOQA
        >>> import utool  # NOQA
        >>> testdirs = ['~/code/ibeis/test_ibs*.py']
    """
    import utool as ut
    from os.path import relpath, join, dirname  # NOQA

    exclude_list += ['__init__.py']

    # General format of the testing script

    script_fmtstr = ut.codeblock(r'''
        #!/bin/bash
        # Runs all tests
        # Win32 path hacks
        export CWD=$(pwd)
        export PYMAJOR="$(python -c "import sys; print(sys.version_info[0])")"

        # <CORRECT_PYTHON>
        # GET CORRECT PYTHON ON ALL PLATFORMS
        export SYSNAME="$(expr substr $(uname -s) 1 10)"
        if [ "$SYSNAME" = "MINGW32_NT" ]; then
            export PYEXE=python
        else
            if [ "$PYMAJOR" = "3" ]; then
                # virtual env?
                export PYEXE=python
            else
                export PYEXE=python2.7
            fi
        fi
        # </CORRECT_PYTHON>

        PRINT_DELIMETER()
        {{
            printf "\n#\n#\n#>>>>>>>>>>> next_test\n\n"
        }}

        export TEST_ARGV="{test_argvs} $@"

        {dirdef_block}

        # Default tests to run
        set_test_flags()
        {{
            export DEFAULT=$1
        {testdefault_block}
        }}
        set_test_flags OFF
        {testdefaulton_block}

        # Parse for bash commandline args
        for i in "$@"
        do
        case $i in --testall)
            set_test_flags ON
            ;;
        esac
        {testcmdline_block}
        done

        BEGIN_TESTS()
        {{
        cat <<EOF
        {runtests_bubbletext}
        EOF
            echo "BEGIN: TEST_ARGV=$TEST_ARGV"
            PRINT_DELIMETER
            num_passed=0
            num_ran=0
            export FAILED_TESTS=''
        }}

        RUN_TEST()
        {{
            echo "RUN_TEST: $@"
            export TEST="$PYEXE $@ $TEST_ARGV"
            $TEST
            export RETURN_CODE=$?
            echo "RETURN_CODE=$RETURN_CODE"
            PRINT_DELIMETER
            num_ran=$(($num_ran + 1))
            if [ "$RETURN_CODE" == "0" ] ; then
                num_passed=$(($num_passed + 1))
            fi
            if [ "$RETURN_CODE" != "0" ] ; then
                export FAILED_TESTS="$FAILED_TESTS\n$TEST"
            fi
        }}

        END_TESTS()
        {{
            echo "RUN_TESTS: DONE"
            if [ "$FAILED_TESTS" != "" ] ; then
                echo "-----"
                printf "Failed Tests:"
                printf "$FAILED_TESTS\n"
                printf "$FAILED_TESTS\n" >> failed_shelltests.txt
                echo "-----"
            fi
            echo "$num_passed / $num_ran tests passed"
        }}

        #---------------------------------------------
        # START TESTS
        BEGIN_TESTS

        {quicktest_block}

        {test_block}

        #---------------------------------------------
        # END TESTING
        END_TESTS
        ''')

    testcmdline_fmtstr = ut.codeblock(r'''
        case $i in --notest{header_lower})
            export {testflag}=OFF
            ;;
        esac
        case $i in --test{header_lower})
            export {testflag}=ON
            ;;
        esac
        ''')

    header_test_block_fmstr = ut.codeblock(r'''

        #---------------------------------------------
        #{header_text}
        if [ "${testflag}" = "ON" ] ; then
        cat <<EOF
        {header_bubble_text}
        EOF
        {testlines_block}
        fi
        ''')

    #specialargv = '--noshow'
    specialargv = ''
    testline_fmtstr = 'RUN_TEST ${dirvar}/{fpath} {specialargv}'
    testline_fmtstr2 = 'RUN_TEST {fpath} {specialargv}'

    def format_testline(fpath, dirvar):
        if dirvar is None:
            return testline_fmtstr2.format(fpath=fpath,
                                           specialargv=specialargv)
        else:
            return testline_fmtstr.format(dirvar=dirvar,
                                          fpath=fpath,
                                          specialargv=specialargv)

    default_flag_line_list = []
    defaulton_flag_line_list = []
    testcmdline_list = []
    dirdef_list = []
    header_test_block_list = []

    known_tests = ut.ddict(list)

    # Tests to always run
    if quick_tests is not None:
        quicktest_block = '\n'.join(
            ['# Quick Tests (always run)'] +
            ['RUN_TEST ' + testline for testline in quick_tests])
    else:
        quicktest_block = '# No quick tests'

    # Loop over different test types
    for testdef_tup in test_headers:
        header, default, modname, dpath, pats, testcmds = testdef_tup
        # Build individual test type information
        header_upper = header.upper()
        header_lower = header.lower()
        testflag = header_upper + '_TEST'

        if modname is not None:
            dirvar = header_upper + '_DIR'
            dirdef = ''.join([
                'export {dirvar}=$($PYEXE -c "', 'import os, {modname};',
                'print(str(os.path.dirname(os.path.dirname({modname}.__file__))))',
                '")'
            ]).format(dirvar=dirvar, modname=modname)
            dirdef_list.append(dirdef)
        else:
            dirvar = None

        # Build test dir
        #dirvar = header_upper + '_DIR'
        #dirdef = 'export {dirvar}={dirname}'.format(dirvar=dirvar, dirname=dirname)
        #dirdef_list.append(dirdef)

        # Build command line flags
        default_flag_line = 'export {testflag}=$DEFAULT'.format(
            testflag=testflag)

        if default:
            defaulton_flag_line = 'export {testflag}=ON'.format(
                testflag=testflag)
            defaulton_flag_line_list.append(defaulton_flag_line)

        testcmdline_fmtdict = dict(
            header_lower=header_lower,
            testflag=testflag,
        )
        testcmdline = testcmdline_fmtstr.format(**testcmdline_fmtdict)

        #ut.ls(dpath)

        # VERY HACK BIT OF CODE

        # Get list of tests from patterns
        if testcmds is None:
            if modname is not None:
                module = __import__(modname)
                repo_path = dirname(dirname(module.__file__))
            else:
                repo_path = repodir
            dpath_ = ut.unixpath(util_path.unixjoin(repo_path, dpath))

            if header_upper == 'OTHER':
                # Hacky way to grab any other tests not explicitly seen in this directory
                _testfpath_list = list(
                    set(ut.glob(dpath_, '*.py')) - set(known_tests[dpath_]))
                #_testfpath_list = ut.glob(dpath_, '*.py')
                #set(known_tests[dpath_])
            else:
                _testfpath_list = ut.flatten(
                    [ut.glob(dpath_, pat) for pat in pats])

            def not_excluded(x):
                return not any(
                    [x.find(exclude) > -1 for exclude in exclude_list])

            _testfpath_list = list(filter(not_excluded, _testfpath_list))

            known_tests[dpath_].extend(_testfpath_list)
            #print(_testfpath_list)
            testfpath_list = [
                util_path.unixjoin(dpath, relpath(fpath, dpath_))
                for fpath in _testfpath_list
            ]

            testline_list = [
                format_testline(fpath, dirvar) for fpath in testfpath_list
            ]
        else:
            testline_list = testcmds

        testlines_block = ut.indentjoin(testline_list).strip('\n')

        # Construct test block for this type
        header_text = header_upper + ' TESTS'
        headerfont = 'cybermedium'
        header_bubble_text = ut.indent(
            ut.bubbletext(header_text, headerfont).strip())
        header_test_block_dict = dict(
            testflag=testflag,
            header_text=header_text,
            testlines_block=testlines_block,
            header_bubble_text=header_bubble_text,
        )
        header_test_block = header_test_block_fmstr.format(
            **header_test_block_dict)

        # Append to script lists
        header_test_block_list.append(header_test_block)
        default_flag_line_list.append(default_flag_line)
        testcmdline_list.append(testcmdline)

    runtests_bubbletext = ut.bubbletext('RUN TESTS', 'cyberlarge')

    test_block = '\n'.join(header_test_block_list)
    dirdef_block = '\n'.join(dirdef_list)
    testdefault_block = ut.indent('\n'.join(default_flag_line_list))
    testdefaulton_block = '\n'.join(defaulton_flag_line_list)
    testcmdline_block = '\n'.join(testcmdline_list)

    script_fmtdict = dict(
        quicktest_block=quicktest_block,
        runtests_bubbletext=runtests_bubbletext,
        test_argvs=test_argvs,
        dirdef_block=dirdef_block,
        testdefault_block=testdefault_block,
        testdefaulton_block=testdefaulton_block,
        testcmdline_block=testcmdline_block,
        test_block=test_block,
    )
    script_text = script_fmtstr.format(**script_fmtdict)

    return script_text
Пример #8
0
def embed(parent_locals=None, parent_globals=None, exec_lines=None,
          remove_pyqt_hook=True, N=0):
    """
    Starts interactive session. Similar to keyboard command in matlab.
    Wrapper around IPython.embed

    """
    import utool as ut
    from functools import partial
    import IPython

    if parent_globals is None:
        parent_globals = get_parent_frame(N=N).f_globals
    if parent_locals is None:
        parent_locals = get_parent_frame(N=N).f_locals

    stackdepth = N  # NOQA
    getframe = partial(ut.get_parent_frame, N=N)  # NOQA

    # exec(execstr_dict(parent_globals, 'parent_globals'))
    # exec(execstr_dict(parent_locals,  'parent_locals'))
    print('')
    print('================')
    print(ut.bubbletext('EMBEDDING'))
    print('================')
    print('[util] embedding')
    try:
        if remove_pyqt_hook:
            try:
                import guitool
                guitool.remove_pyqt_input_hook()
            except (ImportError, ValueError, AttributeError) as ex:
                #print(ex)
                printex(ex, iswarning=True)
                pass
            # make qt not loop forever (I had qflag loop forever with this off)
    except ImportError as ex:
        print(ex)

    #from IPython.config.loader import Config
    # cfg = Config()
    #config_dict = {}
    #if exec_lines is not None:
    #    config_dict['exec_lines'] = exec_lines
    #IPython.embed(**config_dict)
    print('[util]  Get stack location with: ')
    print('[util] ut.get_parent_frame(N=8).f_code.co_name')
    print('[util] set EXIT_NOW or qqq to True(ish) to hard exit on unembed')
    #print('set iup to True to draw plottool stuff')
    print('[util] call %pylab qt4 to get plottool stuff working')
    once = True
    # Allow user to set iup and redo the loop
    while once or vars().get('iup', False):
        if not once:
            # SUPER HACKY WAY OF GETTING FIGURES ON THE SCREEN BETWEEN UPDATES
            #vars()['iup'] = False
            # ALL YOU NEED TO DO IS %pylab qt4
            print('re-emebeding')
            #import plottool as pt
            #pt.update()
            #(pt.present())
            for _ in range(100):
                time.sleep(.01)

        once = False
        #vars().get('iup', False):
        print('[util] calling IPython.embed()')
        """
        Notes:
            /usr/local/lib/python2.7/dist-packages/IPython/terminal/embed.py
            IPython.terminal.embed.InteractiveShellEmbed

            # instance comes from  IPython.config.configurable.SingletonConfigurable.instance
        """
        #c = IPython.Config()
        #c.InteractiveShellApp.exec_lines = [
        #    '%pylab qt4',
        #    '%gui qt4',
        #    "print 'System Ready!'",
        #]
        #IPython.embed(config=c)
        parent_ns = parent_globals.copy()
        parent_ns.update(parent_locals)
        locals().update(parent_ns)
        try:
            IPython.embed()
        except RuntimeError as ex:
            ut.printex(ex, 'Failed to open ipython')
        #config = IPython.terminal.ipapp.load_default_config()
        #config.InteractiveShellEmbed = config.TerminalInteractiveShell
        #module = sys.modules[parent_globals['__name__']]
        #config['module'] = module
        #config['module'] = module
        #embed2(stack_depth=N + 2 + 1)
        #IPython.embed(config=config)
        #IPython.embed(config=config)
        #IPython.embed(module=module)
        # Exit python immediately if specifed
        if vars().get('EXIT_NOW', False) or vars().get('qqq', False):
            print('[utool.embed] EXIT_NOW specified')
            sys.exit(1)
Пример #9
0
def _wip_embed(parent_locals=None, parent_globals=None, exec_lines=None,
               remove_pyqt_hook=True, N=0):
    """
    Starts interactive session. Similar to keyboard command in matlab.
    Wrapper around IPython.embed

    Notes:
        #https://github.com/ipython/ipython/wiki/Cookbook%3a-Updating-code-for-use-with-IPython-0.11-and-later

        import IPython
        x = 3
        IPython.embed()
        c = IPython.Config()
        c.InteractiveShellApp.exec_lines = [
            '%pylab qt4',
            "print 'System Ready!'",
        ]

        def foo():
            return x + 3

        a = 3
        def bar():
            return a + 3
        bar()
        #NameError: global name 'a' is not defined


        from IPython.terminal.ipapp import TerminalIPythonApp
        x = 3
        app = TerminalIPythonApp.instance()
        app.initialize(argv=[]) # argv=[] instructs IPython to ignore sys.argv
        app.start()


    Args:
        parent_locals (None):
        parent_globals (None):
        exec_lines (None):
        remove_pyqt_hook (bool):
        N (int):

    CommandLine:
        python -m utool.util_dbg --test-embed

    References:
       http://stackoverflow.com/questions/27911570/can-you-specify-a-command-to-run-after-you-embed-into-ipython/27914204#27914204
       http://stackoverflow.com/questions/15167200/how-do-i-embed-an-ipython-interpreter-into-an-application-running-in-an-ipython

    Notes:
        Use cases I want to achieve

        1) Simply stop execution and embed in an IPython terminal session
        2) Like case 1, but execute a specific set of command (eg '%gui qt')
           AFTER IPython has started
        3) Embed and pause GUI execution (this is just case 1)
        3) Embed and let GUI execution continue while embeded. (basically just need case 2)

    TODO:
        try:
            get_ipython
        except NameError:
            banner=exit_msg=''
        else:
            banner = '*** Nested interpreter ***'
            exit_msg = '*** Back in main IPython ***'

        # First import the embed function
        from IPython.frontend.terminal.embed import InteractiveShellEmbed
        # Now create the IPython shell instance. Put ipshell() anywhere in your code
        # where you want it to open.
        ipshell = InteractiveShellEmbed(banner1=banner, exit_msg=exit_msg)
        #Then use ipshell() whenever you want to be dropped into an IPython shell. This
        #will allow you to embed (and even nest) IPython interpreters in your code and
        #inspect objects or the state of the program.

    Example:
        >>> # DISABLE_DOCTEST
        >>> from utool.util_dbg import *  # NOQA
        >>> # build test data
        >>> parent_locals = None
        >>> parent_globals = None
        >>> exec_lines = None
        >>> remove_pyqt_hook = True
        >>> N = 0
        >>> # execute function
        >>> result = embed(parent_locals, parent_globals, exec_lines, remove_pyqt_hook, N)
        >>> # verify results
        >>> print(result)
    """
    import utool as ut
    from functools import partial
    import IPython

    if parent_globals is None:
        parent_globals = get_parent_frame(N=N).f_globals
    if parent_locals is None:
        parent_locals = get_parent_frame(N=N).f_locals

    stackdepth = N  # NOQA
    getframe = partial(ut.get_parent_frame, N=N)  # NOQA

    exec(execstr_dict(parent_globals, 'parent_globals'))
    exec(execstr_dict(parent_locals,  'parent_locals'))
    print('')
    print('================')
    print(ut.bubbletext('EMBEDDING'))
    print('================')
    print('[util] embedding')
    try:
        if remove_pyqt_hook:
            try:
                import guitool
                guitool.remove_pyqt_input_hook()
            except (ImportError, ValueError, AttributeError) as ex:
                #print(ex)
                printex(ex, iswarning=True)
                pass
            # make qt not loop forever (I had qflag loop forever with this off)
    except ImportError as ex:
        print(ex)
    user_ns = globals()
    user_ns = globals().copy()
    user_ns.update(locals())
    if parent_globals is not None:
        user_ns.update(parent_globals)
    if parent_locals is not None:
        user_ns.update(parent_locals)
    orig_argv = sys.argv  # NOQA
    print('About to start_ipython')
    config = IPython.Config()
    exec_lines_ = [
        '%pylab qt4',
        'print("Entered IPYTHON via utool")',
        'print("Entry Point: %r" % (ut.get_parent_frame(N=11).f_code.co_name,))',
        #'print("Entry Point: %r" % (ut.get_parent_frame(N=10).f_code.co_name,))',
        #'print("Entry Point: %r" % (ut.get_parent_frame(N=9).f_code.co_name,))',
        #'print("Entry Point: %r" % (ut.get_parent_frame(N=8).f_code.co_name,))',
        #'print("Entry Point: %r" % (ut.get_parent_frame(N=7).f_code.co_name,))',
        #'print("Entry Point: %r" % (ut.get_parent_frame(N=6).f_code.co_name,))',
        #'print("Entry Point: %r" % (ut.get_parent_frame(N=5).f_code.co_name,))',
        #execstr_dict(parent_locals)
    ] + ut.ensure_str_list(exec_lines if exec_lines is not None else [])
    config.InteractiveShellApp.exec_lines = exec_lines_
    print('Exec Lines: ')
    print(ut.indentjoin(exec_lines_, '\n    >>> '))
    IPython.start_ipython(config=config, argv=[], user_ns=user_ns)
    # Exit python immediately if specifed
    if user_ns.get('qqq', False) or vars.get('qqq', False) or user_ns.get('EXIT_NOW', False):
        print('[utool.embed] EXIT_NOW or qqq specified')
        sys.exit(1)