Example #1
0
def ibeis_user_profile():
    import utool as ut
    import sys
    addpath = True
    module_fpath = ut.truepath('~/local/init/REPOS1.py')
    if addpath:
        module_dpath = dirname(module_fpath)
        sys.path.append(module_dpath)
    REPOS1 = ut.import_module_from_fpath(module_fpath)
    self = UserProfile()
    #self.project_dpaths = REPOS1.PROJECT_REPOS
    self.project_dpaths = REPOS1.IBEIS_REPOS
    self.project_dpaths += [ut.truepath('~/latex/crall-candidacy-2015/')]
    self.project_include_patterns = [
        '*.py', '*.cxx', '*.cpp', '*.hxx', '*.hpp', '*.c', '*.h', '*.vim'
        #'*.py',  # '*.cxx', '*.cpp', '*.hxx', '*.hpp', '*.c', '*.h', '*.vim'
    ]
    self.project_exclude_dirs = [
        '_graveyard',
        '_broken',
        'CompilerIdCXX',
        'CompilerIdC',
        'build',
        'old',
        '_old_qt_hs_matcher',
    ]
    self.project_exclude_patterns = [
        '_grave*', '_autogen_explicit_controller*'
    ]
    return self
Example #2
0
    def find_module_callers():
        """
        TODO:
        attempt to build a call graph between module functions to make it easy to see
        what can be removed and what cannot.
        """
        import utool as ut
        from os.path import normpath
        mod_fpath = ut.truepath('~/code/ibeis/ibeis/expt/results_analyzer.py')
        mod_fpath = ut.truepath('~/code/ibeis/ibeis/expt/results_all.py')
        mod_fpath = ut.truepath('~/code/ibeis/ibeis/expt/results_organizer.py')
        module = ut.import_module_from_fpath(mod_fpath)
        user_profile = ut.ensure_user_profile()
        doctestables = list(
            ut.iter_module_doctestable(module, include_builtin=False))
        grepkw = {}
        grepkw['exclude_dirs'] = user_profile.project_exclude_dirs
        grepkw['dpath_list'] = user_profile.project_dpaths
        grepkw['verbose'] = True

        usage_map = {}
        for funcname, func in doctestables:
            print('Searching for funcname = %r' % (funcname, ))
            found_fpath_list, found_lines_list, found_lxs_list = ut.grep(
                [funcname], **grepkw)
            used_in = (found_fpath_list, found_lines_list, found_lxs_list)
            usage_map[funcname] = used_in

        external_usage_map = {}
        for funcname, used_in in usage_map.items():
            (found_fpath_list, found_lines_list, found_lxs_list) = used_in
            isexternal_flag = [
                normpath(fpath) != normpath(mod_fpath)
                for fpath in found_fpath_list
            ]
            ext_used_in = (ut.compress(found_fpath_list, isexternal_flag),
                           ut.compress(found_lines_list, isexternal_flag),
                           ut.compress(found_lxs_list, isexternal_flag))
            external_usage_map[funcname] = ext_used_in

        for funcname, used_in in external_usage_map.items():
            (found_fpath_list, found_lines_list, found_lxs_list) = used_in

        print('Calling modules: \n' + ut.repr2(ut.unique_ordered(
            ut.flatten([used_in[0]
                        for used_in in external_usage_map.values()])),
                                               nl=True))
Example #3
0
    def find_module_callers():
        """
        TODO:
        attempt to build a call graph between module functions to make it easy to see
        what can be removed and what cannot.
        """
        import utool as ut
        from os.path import normpath
        mod_fpath = ut.truepath('~/code/ibeis/ibeis/expt/results_analyzer.py')
        mod_fpath = ut.truepath('~/code/ibeis/ibeis/expt/results_all.py')
        mod_fpath = ut.truepath('~/code/ibeis/ibeis/expt/results_organizer.py')
        module = ut.import_module_from_fpath(mod_fpath)
        user_profile = ut.ensure_user_profile()
        doctestables = list(ut.iter_module_doctestable(module, include_builtin=False))
        grepkw = {}
        grepkw['exclude_dirs'] = user_profile.project_exclude_dirs
        grepkw['dpath_list'] = user_profile.project_dpaths
        grepkw['verbose'] = True

        usage_map = {}
        for funcname, func in doctestables:
            print('Searching for funcname = %r' % (funcname,))
            found_fpath_list, found_lines_list, found_lxs_list = ut.grep([funcname], **grepkw)
            used_in = (found_fpath_list, found_lines_list, found_lxs_list)
            usage_map[funcname] = used_in

        external_usage_map = {}
        for funcname, used_in in usage_map.items():
            (found_fpath_list, found_lines_list, found_lxs_list) = used_in
            isexternal_flag = [normpath(fpath) != normpath(mod_fpath) for fpath in found_fpath_list]
            ext_used_in = (ut.compress(found_fpath_list, isexternal_flag),
                           ut.compress(found_lines_list, isexternal_flag),
                           ut.compress(found_lxs_list, isexternal_flag))
            external_usage_map[funcname] = ext_used_in

        for funcname, used_in in external_usage_map.items():
            (found_fpath_list, found_lines_list, found_lxs_list) = used_in

        print('Calling modules: \n' +
              ut.repr2(ut.unique_ordered(ut.flatten([used_in[0] for used_in in  external_usage_map.values()])), nl=True))
Example #4
0
def ibeis_user_profile():
    import utool as ut
    import sys
    addpath = True
    module_fpath = ut.truepath('~/local/init/REPOS1.py')
    if addpath:
        module_dpath = dirname(module_fpath)
        sys.path.append(module_dpath)
    REPOS1 = ut.import_module_from_fpath(module_fpath)
    self = UserProfile(name='ibeis')
    #self.project_dpaths = REPOS1.PROJECT_REPOS
    self.project_dpaths = REPOS1.IBEIS_REPOS
    self.project_dpaths += [ut.truepath('~/latex/crall-candidacy-2015/')]
    self.project_include_patterns = [
        '*.py', '*.cxx', '*.cpp', '*.hxx', '*.hpp', '*.c', '*.h', '*.vim'
        #'*.py',  # '*.cxx', '*.cpp', '*.hxx', '*.hpp', '*.c', '*.h', '*.vim'
    ]
    self.project_exclude_dirs = [
        '_graveyard', '_broken', 'CompilerIdCXX', 'CompilerIdC', 'build',
        'old', '_old_qt_hs_matcher', 'htmlcov'
    ]
    self.project_exclude_patterns = ['_grave*', '_autogen_explicit_controller*']
    return self
Example #5
0
def load_func_from_module(modname, funcname, verbose=True, moddir=None):
    r"""
    Args:
        modname (str):  module name
        funcname (str):  function name
        verbose (bool):  verbosity flag(default = True)
        moddir (None): (default = None)

    CommandLine:
        python -m utool.util_autogen load_func_from_module

    Example:
        >>> # UNSTABLE_DOCTEST
        >>> from utool.util_autogen import *  # NOQA
        >>> import utool as ut
        >>> #modname = 'plottool.plots'
        >>> #funcname = 'multi_plot'
        >>> modname = 'utool.util_path'
        >>> funcname = 'checkpath'
        >>> verbose = True
        >>> moddir = None
        >>> func, module, error_str = load_func_from_module(modname, funcname, verbose, moddir)
        >>> source = ut.get_func_sourcecode(func, strip_docstr=True, strip_comments=True)
        >>> keyname = ut.named_field('keyname', ut.REGEX_VARNAME)
        >>> default = ut.named_field('default', '[\'\"A-Za-z_][A-Za-z0-9_\'\"]*')
        >>> pattern = re.escape('kwargs.get(\'') + keyname + re.escape('\',')
        >>> kwarg_keys = [match.groupdict()['keyname'] for match in re.finditer(pattern, source)]
    """
    import utool as ut
    from os.path import join
    import imp
    func = None
    module = None
    error_str = None
    print('modname = %r' % (modname,))
    print('funcname = %r' % (funcname,))
    print('moddir = %r' % (moddir,))

    if not isinstance(modname, six.string_types):
        error_str = 'modname=%r is not a string. bad input' % (modname,)
    else:
        if False:
            basemodname_ = modname
            basemodname = ''
            parts = modname.split('.')
            for index in range(len(parts)):
                #print('index = %r' % (index,))
                basemodname = '.'.join(parts[0:index + 1])
                #print('basemodname = %r' % (basemodname,))
                basemodule = __import__(basemodname)
                #print('basemodule = %r' % (basemodule,))
                #if hasattr(basemodule, 'rrrr'):
                #    basemodule.rrrr()
                #imp.reload(basemodule)

        try:
            module = __import__(modname)
        except ImportError:
            if moddir is not None:
                #parts =
                # There can be a weird double import error thing happening here
                # Rectify the dots in the filename
                module = ut.import_module_from_fpath(join(moddir, modname.split('.')[-1] + '.py'))
            else:
                raise
        #import inspect
        try:
            imp.reload(module)
        except Exception as ex:
            pass
        if False:
            # Try removing pyc if it exists
            if module.__file__.endswith('.pyc'):
                ut.delete(module.__file__, verbose=False)
                try:
                    module = __import__(modname)
                except ImportError:
                    if moddir is not None:
                        module = ut.import_module_from_fpath(join(moddir, modname.split('.')[-1] + '.py'))
                    else:
                        raise
        try:
            imp.reload(module)
        except Exception as ex:
            pass
        try:
            # FIXME: PYTHON 3
            execstr = ut.codeblock(
                '''
                try:
                    import {modname}
                    module = {modname}
                    #print('Trying to reload module=%r' % (module,))
                    imp.reload(module)
                except Exception:
                    # If it fails maybe the module is not in the path
                    if moddir is not None:
                        try:
                            import imp
                            import os
                            orig_dir = os.getcwd()
                            os.chdir(moddir)
                            modname_str = '{modname}'
                            modinfo = imp.find_module(modname_str, [moddir])
                            module = imp.load_module(modname_str, *modinfo)
                            #print('loaded module=%r' % (module,))
                        except Exception as ex:
                            ut.printex(ex, 'failed to imp.load_module')
                            pass
                        finally:
                            os.chdir(orig_dir)
                import imp
                import utool as ut
                imp.reload(ut.util_autogen)
                imp.reload(ut.util_inspect)
                try:
                    func = module.{funcname}
                except AttributeError:
                    docstr = 'Could not find attribute funcname={funcname} in modname={modname} This might be a reloading issue'
                    imp.reload(module)
                '''
            ).format(**locals())
            exec_locals = locals()
            exec_globals = globals()
            exec(execstr, exec_globals, exec_locals)
            func = exec_locals.get('func', None)
            module = exec_locals.get('module', None)
        except Exception as ex2:
            docstr = 'error ' + str(ex2)
            if verbose:
                import utool as ut
                #ut.printex(ex1, 'ex1')
                ut.printex(ex2, 'ex2', tb=True)
            testcmd = 'python -c "import utool; print(utool.auto_docstr(\'%s\', \'%s\'))"' % (modname, funcname)
            error_str = ut.formatex(ex2, 'ex2', tb=True, keys=['modname', 'funcname', 'testcmd'])
            error_str += '---' + execstr
    return func, module, error_str
Example #6
0
def test_reloading_metaclass():
    r"""
    CommandLine:
        python -m utool.util_class --test-test_reloading_metaclass

    References:
        http://stackoverflow.com/questions/8122734/pythons-imp-reload-function-is-not-working

    Example:
        >>> # ENABLE_DOCTEST
        >>> from utool.util_class import *  # NOQA
        >>> result = test_reloading_metaclass()
        >>> print(result)
    """
    import utool as ut
    testdir = ut.ensure_app_resource_dir('utool', 'metaclass_tests')
    testfoo_fpath = ut.unixjoin(testdir, 'testfoo.py')
    # os.chdir(testdir)
    #with ut.ChdirContext(testdir, stay=ut.inIPython()):
    with ut.ChdirContext(testdir):
        foo_code1 = ut.codeblock(
            r'''
            # STARTBLOCK
            import utool as ut
            import six


            @six.add_metaclass(ut.ReloadingMetaclass)
            class Foo(object):
                def __init__(self):
                    pass

            spamattr = 'version1'
            # ENDBLOCK
            '''
        )
        foo_code2 = ut.codeblock(
            r'''
            # STARTBLOCK
            import utool as ut
            import six


            @six.add_metaclass(ut.ReloadingMetaclass)
            class Foo(object):
                def __init__(self):
                    pass

                def bar(self):
                    return 'spam'

            eggsattr = 'version2'
            # ENDBLOCK
            '''
        )
        # Write a testclass to disk
        ut.delete(testfoo_fpath)
        ut.write_to(testfoo_fpath, foo_code1, verbose=True)
        testfoo = ut.import_module_from_fpath(testfoo_fpath)
        #import testfoo
        foo = testfoo.Foo()
        print('foo = %r' % (foo,))
        assert not hasattr(foo, 'bar'), 'foo should not have a bar attr'
        ut.delete(testfoo_fpath + 'c')  # remove the pyc file because of the identical creation time
        ut.write_to(testfoo_fpath, foo_code2, verbose=True)
        assert not hasattr(foo, 'bar'), 'foo should still not have a bar attr'
        foo.rrr()
        assert foo.bar() == 'spam'
        ut.delete(testfoo_fpath)
        print('Reloading worked nicely')
Example #7
0
def test_reloading_metaclass():
    r"""
    CommandLine:
        python -m utool.util_class --test-test_reloading_metaclass

    References:
        http://stackoverflow.com/questions/8122734/pythons-imp-reload-function-is-not-working

    Example:
        >>> # ENABLE_DOCTEST
        >>> from utool.util_class import *  # NOQA
        >>> result = test_reloading_metaclass()
        >>> print(result)
    """
    import utool as ut
    testdir = ut.ensure_app_resource_dir('utool', 'metaclass_tests')
    testfoo_fpath = ut.unixjoin(testdir, 'testfoo.py')
    # os.chdir(testdir)
    #with ut.ChdirContext(testdir, stay=ut.inIPython()):
    with ut.ChdirContext(testdir):
        foo_code1 = ut.codeblock(
            r'''
            # STARTBLOCK
            import utool as ut
            import six


            @six.add_metaclass(ut.ReloadingMetaclass)
            class Foo(object):
                def __init__(self):
                    pass

            spamattr = 'version1'
            # ENDBLOCK
            '''
        )
        foo_code2 = ut.codeblock(
            r'''
            # STARTBLOCK
            import utool as ut
            import six


            @six.add_metaclass(ut.ReloadingMetaclass)
            class Foo(object):
                def __init__(self):
                    pass

                def bar(self):
                    return 'spam'

            eggsattr = 'version2'
            # ENDBLOCK
            '''
        )
        # Write a testclass to disk
        ut.delete(testfoo_fpath)
        ut.write_to(testfoo_fpath, foo_code1, verbose=True)
        testfoo = ut.import_module_from_fpath(testfoo_fpath)
        #import testfoo
        foo = testfoo.Foo()
        print('foo = %r' % (foo,))
        assert not hasattr(foo, 'bar'), 'foo should not have a bar attr'
        ut.delete(testfoo_fpath + 'c')  # remove the pyc file because of the identical creation time
        ut.write_to(testfoo_fpath, foo_code2, verbose=True)
        assert not hasattr(foo, 'bar'), 'foo should still not have a bar attr'
        foo.rrr()
        assert foo.bar() == 'spam'
        ut.delete(testfoo_fpath)
        print('Reloading worked nicely')