Example #1
0
def get_reprs(*args, **kwargs):
    if 'locals_' in kwargs:
        locals_ = kwargs['locals_']
    else:
        locals_ = locals()
        locals_.update(get_caller_locals())

    msg_list = []
    var_list = list(args) + kwargs.get('var_list', [])
    for key in var_list:
        var = locals_[key]
        msg = util_str.horiz_string(str(key) + ' = ', repr(var))
        msg_list.append(msg)

    reprs = '\n' + util_str.indent('\n##\n'.join(msg_list)) + '\n'
    return reprs
Example #2
0
def get_reprs(*args, **kwargs):
    if 'locals_' in kwargs:
        locals_ = kwargs['locals_']
    else:
        locals_ = locals()
        locals_.update(get_caller_locals())

    msg_list = []
    var_list = list(args) + kwargs.get('var_list', [])
    for key in var_list:
        var = locals_[key]
        msg = util_str.horiz_string(six.text_type(key) + ' = ', repr(var))
        msg_list.append(msg)

    reprs = '\n' + util_str.indent('\n##\n'.join(msg_list)) + '\n'
    return reprs
Example #3
0
def dynamic_import(modname,
                   IMPORT_TUPLES,
                   developing=True,
                   ignore_froms=[],
                   dump=False,
                   ignore_startswith=[],
                   ignore_endswith=[],
                   ignore_list=[],
                   check_not_imported=True,
                   verbose=False):
    """
    MAIN ENTRY POINT

    Dynamically import listed util libraries and their attributes.
    Create reload_subs function.

    Using __import__ like this is typically not considered good style However,
    it is better than import * and this will generate the good file text that
    can be used when the module is 'frozen"
    """
    if verbose:
        print('[UTIL_IMPORT] Running Dynamic Imports for modname=%r ' %
              modname)
    # Get the module that will be imported into
    module = sys.modules[modname]
    # List of modules to be imported
    IMPORTS = __get_imports(IMPORT_TUPLES)
    # Import the modules
    __excecute_imports(module, modname, IMPORTS, verbose=verbose)
    # If developing do explicit import stars
    if developing:
        FROM_IMPORTS = __execute_fromimport_star(
            module,
            modname,
            IMPORT_TUPLES,
            ignore_list=ignore_list,
            ignore_startswith=ignore_startswith,
            ignore_endswith=ignore_endswith,
            check_not_imported=check_not_imported,
            verbose=verbose)
    else:
        FROM_IMPORTS = __execute_fromimport(module,
                                            modname,
                                            IMPORT_TUPLES,
                                            verbose=verbose)

    inject_execstr = _inject_execstr(modname, IMPORT_TUPLES)

    # If requested: print what the __init__ module should look like
    dump_requested = (('--dump-%s-init' % modname) in sys.argv or
                      ('--print-%s-init' % modname) in sys.argv) or dump
    overwrite_requested = ('--update-%s-init' % modname) in sys.argv
    if verbose:
        print('[UTIL_IMPORT] Finished Dynamic Imports for modname=%r ' %
              modname)

    if dump_requested:
        is_main_proc = multiprocessing.current_process().name == 'MainProcess'
        if is_main_proc:
            from utool import util_str
            initstr = _initstr(modname, IMPORTS, FROM_IMPORTS, inject_execstr)
            print(util_str.indent(initstr))
    # Overwrite the __init__.py file with new explicit imports
    if overwrite_requested:
        """
        SeeAlso:
            util_inject.inject_python_code
            util_str.replace_between_tags
        """
        is_main_proc = multiprocessing.current_process().name == 'MainProcess'
        if is_main_proc:
            from utool import util_str
            from os.path import join, exists
            initstr = _initstr(modname,
                               IMPORTS,
                               FROM_IMPORTS,
                               inject_execstr,
                               withheader=False)
            new_else = util_str.indent(initstr)
            #print(new_else)
            # Get path to init file so we can overwrite it
            init_fpath = join(module.__path__[0], '__init__.py')
            print('attempting to update: %r' % init_fpath)
            assert exists(init_fpath)
            new_lines = []
            editing = False
            updated = False
            #start_tag = '# <AUTOGEN_INIT>'
            #end_tag = '# </AUTOGEN_INIT>'
            with open(init_fpath, 'r') as file_:
                #text = file_.read()
                lines = file_.readlines()
                for line in lines:
                    if not editing:
                        new_lines.append(line)
                    if line.strip().startswith('# <AUTOGEN_INIT>'):
                        new_lines.append('\n' + new_else +
                                         '\n    # </AUTOGEN_INIT>\n')
                        editing = True
                        updated = True
                    if line.strip().startswith('# </AUTOGEN_INIT>'):
                        editing = False
            # TODO:
            #new_text = util_str.replace_between_tags(text, new_else, start_tag, end_tag)
            if updated:
                print('writing updated file: %r' % init_fpath)
                new_text = ''.join(new_lines)
                with open(init_fpath, 'w') as file_:
                    file_.write(new_text)
            else:
                print('no write hook for file: %r' % init_fpath)

    return inject_execstr
Example #4
0
def dynamic_import(modname, import_tuples, developing=True, ignore_froms=[],
                   dump=False, ignore_startswith=[], ignore_endswith=[],
                   ignore_list=[], check_not_imported=True, return_initstr=False,
                   verbose=False):
    """
    MAIN ENTRY POINT

    Dynamically import listed util libraries and their attributes.
    Create reload_subs function.

    Using __import__ like this is typically not considered good style However,
    it is better than import * and this will generate the good file text that
    can be used when the module is 'frozen"

    Returns:
        str: init_inject_str - by default all imports are executed in this
            function and only the remainig code needed to be executed is
            returned to define the reload logic.

        str, str: init_inject_str, init_str - if return_initstr is True then
            also returns init_str defining the from imports.

    Ignore:
        ignore_startswith = []
        ignore_endswith = []
        check_not_imported = True
        verbose = True
    """
    if verbose:
        print('[UTIL_IMPORT] Running Dynamic Imports for modname=%r ' % modname)
    # Get the module that will be imported into
    try:
        module = sys.modules[modname]
    except:
        module = __import__(modname)
    # List of modules to be imported
    imports = [tup[0] for tup in import_tuples]
    # Import the modules
    __excecute_imports(module, modname, imports, verbose=verbose)
    # If developing do explicit import stars
    if developing:
        from_imports = __execute_fromimport_star(module, modname, import_tuples,
                                                 ignore_list=ignore_list,
                                                 ignore_startswith=ignore_startswith,
                                                 ignore_endswith=ignore_endswith,
                                                 check_not_imported=check_not_imported,
                                                 verbose=verbose)
    else:
        from_imports = __execute_fromimport(module, modname, import_tuples, verbose=verbose)

    inject_execstr = _inject_execstr(modname, import_tuples)

    # If requested: print what the __init__ module should look like
    dump_requested = (('--dump-%s-init' % modname) in sys.argv or
                      ('--print-%s-init' % modname) in sys.argv) or dump
    overwrite_requested = ('--update-%s-init' % modname) in sys.argv
    if verbose:
        print('[UTIL_IMPORT] Finished Dynamic Imports for modname=%r ' % modname)

    if dump_requested:
        is_main_proc = multiprocessing.current_process().name == 'MainProcess'
        if is_main_proc:
            from utool import util_str
            initstr = _initstr(modname, imports, from_imports, inject_execstr)
            print(util_str.indent(initstr))
    # Overwrite the __init__.py file with new explicit imports
    if overwrite_requested:
        """
        SeeAlso:
            util_inject.inject_python_code
            util_str.replace_between_tags
        """
        is_main_proc = multiprocessing.current_process().name == 'MainProcess'
        if is_main_proc:
            from utool import util_str
            from os.path import join, exists
            initstr = _initstr(modname, imports, from_imports, inject_execstr, withheader=False)
            new_else = util_str.indent(initstr)
            #print(new_else)
            # Get path to init file so we can overwrite it
            init_fpath = join(module.__path__[0], '__init__.py')
            print('attempting to update: %r' % init_fpath)
            assert exists(init_fpath)
            new_lines = []
            editing = False
            updated = False
            #start_tag = '# <AUTOGEN_INIT>'
            #end_tag = '# </AUTOGEN_INIT>'
            with open(init_fpath, 'r') as file_:
                #text = file_.read()
                lines = file_.readlines()
                for line in lines:
                    if not editing:
                        new_lines.append(line)
                    if line.strip().startswith('# <AUTOGEN_INIT>'):
                        new_lines.append('\n' + new_else + '\n    # </AUTOGEN_INIT>\n')
                        editing = True
                        updated = True
                    if line.strip().startswith('# </AUTOGEN_INIT>'):
                        editing = False
            # TODO:
            #new_text = util_str.replace_between_tags(text, new_else, start_tag, end_tag)
            if updated:
                print('writing updated file: %r' % init_fpath)
                new_text = ''.join(new_lines)
                with open(init_fpath, 'w') as file_:
                    file_.write(new_text)
            else:
                print('no write hook for file: %r' % init_fpath)
    if return_initstr:
        initstr = _initstr(modname, imports, from_imports, '', withheader=False)
        return inject_execstr, initstr
    else:
        return inject_execstr
Example #5
0
def dynamic_import(modname, IMPORT_TUPLES, developing=True, ignore_froms=[],
                   dump=False, ignore_startswith=[], ignore_endswith=[],
                   ignore_list=[],
                   verbose=False):
    """
    MAIN ENTRY POINT

    Dynamically import listed util libraries and their attributes.
    Create reload_subs function.

    Using __import__ like this is typically not considered good style However,
    it is better than import * and this will generate the good file text that
    can be used when the module is "frozen"
    """
    if verbose:
        print('[UTIL_IMPORT] Running Dynamic Imports for modname=%r ' % modname)
    # Get the module that will be imported into
    module = sys.modules[modname]
    # List of modules to be imported
    IMPORTS = __get_imports(IMPORT_TUPLES)
    # Import the modules
    __excecute_imports(module, modname, IMPORTS, verbose=verbose)
    # If developing do explicit import stars
    if developing:
        FROM_IMPORTS = __execute_fromimport_star(module, modname, IMPORT_TUPLES,
                                                 ignore_list=ignore_list,
                                                 ignore_startswith=ignore_startswith,
                                                 ignore_endswith=ignore_endswith,
                                                 verbose=verbose)
    else:
        FROM_IMPORTS = __execute_fromimport(module, modname, IMPORT_TUPLES, verbose=verbose)

    inject_execstr = _inject_execstr(modname, IMPORT_TUPLES)

    # If requested: print what the __init__ module should look like
    dump_requested = (('--dump-%s-init' % modname) in sys.argv or
                      ('--print-%s-init' % modname) in sys.argv) or dump
    overwrite_requested = ('--update-%s-init' % modname) in sys.argv
    if verbose:
        print('[UTIL_IMPORT] Finished Dynamic Imports for modname=%r ' % modname)

    if dump_requested:
        is_main_proc = multiprocessing.current_process().name == 'MainProcess'
        if is_main_proc:
            from utool import util_str
            initstr = _initstr(modname, IMPORTS, FROM_IMPORTS, inject_execstr)
            print(util_str.indent(initstr))
    # Overwrite the __init__.py file with new explicit imports
    if overwrite_requested:
        is_main_proc = multiprocessing.current_process().name == 'MainProcess'
        if is_main_proc:
            from utool import util_str
            from os.path import join, exists
            initstr = _initstr(modname, IMPORTS, FROM_IMPORTS, inject_execstr, withheader=False)
            new_else = util_str.indent(initstr)
            #print(new_else)
            # Get path to init file so we can overwrite it
            init_fpath = join(module.__path__[0], '__init__.py')
            print("attempting to update: %r" % init_fpath)
            assert exists(init_fpath)
            new_lines = []
            broken = False
            with open(init_fpath, 'r') as file_:
                lines = file_.readlines()
                for line in lines:
                    new_lines.append(line)
                    if line.strip().startswith('# <AUTOGEN_INIT>'):
                        new_lines.append('\n' + new_else + '\n    # </AUTOGEN_INIT>')
                        broken = True
                        break
            if broken:
                print("writing updated file: %r" % init_fpath)
                new_text = ''.join(new_lines)
                with open(init_fpath, 'w') as file_:
                    file_.write(new_text)
            else:
                print("no write hook for file: %r" % init_fpath)

    return inject_execstr
Example #6
0
def write_explicit(pyth_modname, execstr):
    import multiprocessing
    is_main_proc = multiprocessing.current_process().name == 'MainProcess'
    if is_main_proc:
        from utool import util_str
        from os.path import exists
        new_else = util_str.indent(execstr)
        #print(new_else)
        # Get path to init file so we can overwrite it
        pyth_fpath = pyth_modname
        module = sys.modules[pyth_modname]  # module currently being imported
        modpath, ext = splitext(module.__file__)
        assert ext == '.pyc' or ext == '.py'
        pyth_fpath = modpath + '.py'
        #import IPython
        #IPython.embed()
        print("attempting to update: %r" % pyth_fpath)
        assert exists(pyth_fpath)
        new_lines = []
        broken = False
        closed = False
        start_sentinal = '    # <AUTOGEN_CYTH>'
        end_sentinal   = '    # </AUTOGEN_CYTH>'
        with open(pyth_fpath, 'r') as file_:
            lines = file_.readlines()
            for line in lines:
                if not closed and not broken:
                    # Append lines until you see start_sentinal
                    new_lines.append(line)
                    if line.startswith(start_sentinal):
                        indent = '    '
                        help_line = '# Regen command: python -c "import %s" --cyth-write\n' % pyth_modname
                        new_lines.append(indent + help_line)
                        new_lines.append(new_else + '\n')
                        broken = True
                elif not closed and broken:
                    # Skip lines between sentinals
                    if line.startswith(end_sentinal):
                        new_lines.append(end_sentinal + '\n')
                        closed = True
                elif closed and broken:
                    # Append lines after sentinals
                    new_lines.append(line)
                else:
                    raise AssertionError('closed before opening cyth tags')
        if broken and closed:
            print("writing updated file: %r" % pyth_fpath)
            new_text = ''.join(new_lines)
            #print(new_text)
            with open(pyth_fpath, 'w') as file_:
                file_.write(new_text)
        else:
            default_cyth_block = utool.unindent('''
            import cyth
            if cyth.DYNAMIC:
                exec(cyth.import_cyth_execstr(__name__))
            else:
                # <AUTOGEN_CYTH>
                # </AUTOGEN_CYTH>
                pass
            ''')
            default_cyth_block  # NOQA
            print("no write hook for file: %r" % pyth_fpath)