示例#1
0
def test_decorator_module():
    import utool as ut
    import decorator
    ut.rrrr()

    def testdecor(func):
        @ut.on_exception_report_input
        @ut.accepts_scalar_input2([0])
        @ut.ignores_exc_tb
        def testwrp(*args, **kwargs):
            print('was wrapped')
            return func(*args, **kwargs)

        return testwrp

    preserving_testdecor = decorator.decorator(testdecor)

    def myfunction(self, listinput_, arg1, *args, **kwargs):
        " just a test function "
        return [x + 1 for x in listinput_]

    wrapper = testdecor(myfunction)
    orig_func = myfunction

    _wrp_preserve0 = preserving_testdecor(myfunction)
    _wrp_preserve1 = ut.preserve_sig(wrapper, orig_func, True)
    _wrp_preserve2 = ut.preserve_sig(wrapper, orig_func, False)

    print('___')
    print(ut.get_func_sourcecode(_wrp_preserve0))
    print('___')
    print(ut.get_func_sourcecode(_wrp_preserve1))
    print('___')
    print(ut.get_func_sourcecode(_wrp_preserve2))
    print('___')

    print('---')
    print(ut.get_docstr(_wrp_preserve0))
    print('---')
    print(ut.get_docstr(_wrp_preserve1))
    print('---')
    print(ut.get_docstr(_wrp_preserve2))
    print('---')

    print(ut.dict_str(_wrp_preserve2._utinfo))
示例#2
0
def test_decorator_module():
    import utool as ut
    import decorator
    ut.rrrr()
    def testdecor(func):
        @ut.on_exception_report_input
        @ut.accepts_scalar_input2([0])
        @ut.ignores_exc_tb
        def testwrp(*args, **kwargs):
            print('was wrapped')
            return func(*args, **kwargs)
        return testwrp

    preserving_testdecor = decorator.decorator(testdecor)

    def myfunction(self, listinput_, arg1, *args, **kwargs):
        " just a test function "
        return [x + 1 for x in listinput_]

    wrapper = testdecor(myfunction)
    orig_func = myfunction

    _wrp_preserve0 = preserving_testdecor(myfunction)
    _wrp_preserve1 = ut.preserve_sig(wrapper, orig_func, True)
    _wrp_preserve2 = ut.preserve_sig(wrapper, orig_func, False)

    print('___')
    print(ut.get_func_sourcecode(_wrp_preserve0))
    print('___')
    print(ut.get_func_sourcecode(_wrp_preserve1))
    print('___')
    print(ut.get_func_sourcecode(_wrp_preserve2))
    print('___')

    print('---')
    print(ut.get_docstr(_wrp_preserve0))
    print('---')
    print(ut.get_docstr(_wrp_preserve1))
    print('---')
    print(ut.get_docstr(_wrp_preserve2))
    print('---')

    print(ut.dict_str(_wrp_preserve2._utinfo))
示例#3
0
def auto_docstr(**kwargs):
    import imp
    import utool as ut
    ut.util_dbg.COLORED_EXCEPTIONS = False
    ut.ENABLE_COLORS = False
    ut.util_str.ENABLE_COLORS = False
    try:
        print("RELOADING UTOOL via imp")
        imp.reload(ut)
        imp.reload(ut._internal.meta_util_arg)
    except Exception as ex:
        print("... errored")
        pass
    print("RELOADING UTOOL via rrrr")
    ut.rrrr(verbose=0)
    imp.reload(ut)
    import vim

    modname = None
    funcname = None
    flag = False
    dbgtext = ''
    docstr = ''
    dbgmsg = ''

    try:
        funcname, searchlines = find_pyfunc_above_cursor()
        modname, moddir = get_current_modulename()

        if funcname is None:
            funcname = '[vimerr] UNKNOWN_FUNC: funcname is None'
            flag = True
        else:
            # Text to insert into the current buffer
            verbose = True
            autodockw = dict(verbose=verbose)
            autodockw.update(kwargs)
            docstr = ut.auto_docstr(modname, funcname, moddir=moddir, **autodockw)
            #if docstr.find('unexpected indent') > 0:
            #    docstr = funcname + ' ' + docstr
            if docstr[:].strip() == 'error':
                flag = True
    except vim.error as ex:
        dbgmsg = 'vim_error: ' + str(ex)
        flag = False
    except Exception as ex:
        dbgmsg = 'exception(%r): %s' % (type(ex), str(ex))
        ut.printex(ex, tb=True)
        flag = False

    if flag:
        dbgtext += '\n+======================'
        dbgtext += '\n| --- DEBUG OUTPUT --- '
        if len(dbgmsg) > 0:
            dbgtext += '\n| Message: '
            dbgtext += dbgmsg
        dbgtext += '\n+----------------------'
        dbgtext += '\n| InsertDoctstr(modname=%r, funcname=%r' % (modname, funcname)
        pycmd = ('import ut; print(ut.auto_docstr(%r, %r)))' % (modname, funcname))
        pycmd = pycmd.replace('\'', '\\"')
        dbgtext += '\n| python -c "%s"' % (pycmd,)
        dbgtext += '\n+----------------------'
        dbgtext += '\n+searchlines = '
        dbgtext += ut.indentjoin(searchlines, '\n| ')
        dbgtext += '\nL----------------------'
    elif len(dbgmsg) > 0:
        dbgtext += '\n| Message: '
        dbgtext += dbgmsg

    text = '\n'.join([docstr + dbgtext])

    if text == '':
        print('No Text! For some reason flag=%r' % (flag,))
    return text
示例#4
0
def reloading_test1():
    print('TEST1')
    print_ids()
    utool.rrrr()
    print_ids()
示例#5
0
def reloading_test1():
    print('TEST1')
    print_ids()
    utool.rrrr()
    print_ids()
示例#6
0
def auto_docstr(**kwargs):
    import imp
    import utool as ut
    ut.util_dbg.COLORED_EXCEPTIONS = False
    ut.ENABLE_COLORS = False
    ut.util_str.ENABLE_COLORS = False
    try:
        print("RELOADING UTOOL via imp")
        imp.reload(ut)
        imp.reload(ut._internal.meta_util_arg)
    except Exception as ex:
        print("... errored")
        pass
    print("RELOADING UTOOL via rrrr")
    ut.rrrr(verbose=0)
    imp.reload(ut)
    import vim

    modname = None
    funcname = None
    flag = False
    dbgtext = ''
    docstr = ''
    dbgmsg = ''

    try:
        funcname, searchlines, pos, foundline = find_pyfunc_above_cursor()
        modname, moddir = get_current_modulename()
        modpath = vim.current.buffer.name
        print('modpath = {!r}'.format(modpath))

        if funcname is None:
            funcname = '[vimerr] UNKNOWN_FUNC: funcname is None'
            flag = True
        else:
            # Text to insert into the current buffer
            verbose = True
            autodockw = dict(verbose=verbose)
            autodockw.update(kwargs)
            docstr = ut.auto_docstr(modname, funcname, moddir=moddir,
                                    modpath=modpath, **autodockw)
            #if docstr.find('unexpected indent') > 0:
            #    docstr = funcname + ' ' + docstr
            if docstr[:].strip() == 'error':
                flag = True
    except vim.error as ex:
        dbgmsg = 'vim_error: ' + str(ex)
        flag = False
    except Exception as ex:
        dbgmsg = 'exception(%r): %s' % (type(ex), str(ex))
        ut.printex(ex, tb=True)
        flag = False

    if flag:
        dbgtext += '\n+======================'
        dbgtext += '\n| --- DEBUG OUTPUT --- '
        if len(dbgmsg) > 0:
            dbgtext += '\n| Message: '
            dbgtext += dbgmsg
        dbgtext += '\n+----------------------'
        dbgtext += '\n| InsertDoctstr(modname=%r, funcname=%r' % (modname, funcname)
        pycmd = ('import ut; print(ut.auto_docstr(%r, %r, %r)))' % (modname, funcname, modpath))
        pycmd = pycmd.replace('\'', '\\"')
        dbgtext += '\n| python -c "%s"' % (pycmd,)
        dbgtext += '\n+----------------------'
        dbgtext += '\n+searchlines = '
        dbgtext += ut.indentjoin(searchlines, '\n| ')
        dbgtext += '\nL----------------------'
    elif len(dbgmsg) > 0:
        dbgtext += '\n| Message: '
        dbgtext += dbgmsg

    text = '\n'.join([docstr + dbgtext])

    if text == '':
        print('No Text! For some reason flag=%r' % (flag,))
    return text
示例#7
0
文件: rob_old.py 项目: Erotemic/local
def batch_move(r, search, repl, force=False):
    r"""
    This function has not yet been successfully implemented.
    Its a start though.

    rob batch_move train_patchmetric\(.*\) patchmatch\1 False
    ut.named_field('rest', '.*' )
    ut.backref_field('rest')

    search = 'train_patchmetric(?P<rest>.*)'
    repl = 'patchmatch\\g<rest>'
    """
    force = rutil.cast(force, bool)
    # rob batch_move '\(*\)util.py' 'util_\1.py'
    print('Batch Move')
    print('force = %r' % force)
    print('search = %r' % search)
    print('repl = %r' % repl)
    dpath_list = [os.getcwd()]
    spec_open = ['\\(', '\(']
    spec_close = ['\\)', '\)']
    special_repl_strs = ['\1', '\\1']
    print('special_repl_strs = %r' % special_repl_strs)
    print('special_search_strs = %r' % ((spec_open, spec_close,),))

    search_pat = ut.extend_regex(search)
    #for spec in spec_open + spec_close:
    #    search_pat = search_pat.replace(spec, '')
    print('search_pat=%r' % search_pat)

    include_patterns = [search_pat]

    import utool as ut
    import re
    fpath_list = ut.ls('.')
    matching_fpaths = [fpath for fpath in fpath_list if re.search(search_pat, basename(fpath))]
    repl_fpaths = [re.sub(search_pat, repl, fpath) for fpath in matching_fpaths]

    ut.rrrr()
    for fpath1, fpath2 in zip(matching_fpaths, repl_fpaths):
        ut.util_path.copy(fpath1, fpath2, deeplink=False, dryrun=False)
    #for fpath in rob_nav._matching_fnames(dpath_list, include_patterns, recursive=False):
    #    print(fpath)
    return

    parse_str = search
    for spec in spec_open:
        parse_str = parse_str.replace(spec, '{')
    for spec in spec_close:
        parse_str = parse_str.replace(spec, '}')
    parse_str = parse_str.replace('{*}', '{}')
    print('parse_str = %r' % parse_str)

    for fpath in rob_nav._matching_fnames(dpath_list, include_patterns, recursive=False):
        dpath, fname = split(fpath)
        name, ext = splitext(fname)
        # Hard coded parsing
        parsed = parse.parse(parse_str, fname)
        repl1 = parsed[0]
        #print(fname)
        newfname = 'util_' + repl1 + ext
        newfpath = join(dpath, newfname)
        print('move')
        print(fpath)
        print(newfpath)
        if force is True:
            shutil.move(fpath, newfpath)
            print('real run')
        else:
            print('dry run')
        pass