()
Logic for inspecting an object given at command line
Logic for inspecting an object given at command line
Logic for inspecting an object given at command line
def _main():
    """ Logic for inspecting an object given at command line """
    import argparse
    import importlib

    parser = argparse.ArgumentParser()
    parser.add_argument(
        'object',
         help="The object to be analysed. "
              "It supports the 'module:qualname' syntax")
    parser.add_argument(
        '-d', '--details', action='store_true',
        help='Display info about the module rather than its source code')

    args = parser.parse_args()

    target = args.object
    mod_name, has_attrs, attrs = target.partition(":")
    try:
        obj = module = importlib.import_module(mod_name)
    except Exception as exc:
        msg = "Failed to import {} ({}: {})".format(mod_name,
                                                    type(exc).__name__,
                                                    exc)
        print(msg, file=sys.stderr)
        sys.exit(2)

    if has_attrs:
        parts = attrs.split(".")
        obj = module
        for part in parts:
            obj = getattr(obj, part)

    if module.__name__ in sys.builtin_module_names:
        print("Can't get info for builtin modules.", file=sys.stderr)
        sys.exit(1)

    if args.details:
        print('Target: {}'.format(target))
        print('Origin: {}'.format(getsourcefile(module)))
        print('Cached: {}'.format(module.__cached__))
        if obj is module:
            print('Loader: {}'.format(repr(module.__loader__)))
            if hasattr(module, '__path__'):
                print('Submodule search path: {}'.format(module.__path__))
        else:
            try:
                __, lineno = findsource(obj)
            except Exception:
                pass
            else:
                print('Line: {}'.format(lineno))

        print('\n')
    else:
        print(getsource(obj))
def _main ( ) : import argparse import importlib parser = argparse . ArgumentParser ( ) parser . add_argument ( 'object' , help = "The object to be analysed. " "It supports the 'module:qualname' syntax" ) parser . add_argument ( '-d' , '--details' , action = 'store_true' , help = 'Display info about the module rather than its source code' ) args = parser . parse_args ( ) target = args . object mod_name , has_attrs , attrs = target . partition ( ":" ) try : obj = module = importlib . import_module ( mod_name ) except Exception as exc : msg = "Failed to import {} ({}: {})" . format ( mod_name , type ( exc ) . __name__ , exc ) print ( msg , file = sys . stderr ) sys . exit ( 2 ) if has_attrs : parts = attrs . split ( "." ) obj = module for part in parts : obj = getattr ( obj , part ) if module . __name__ in sys . builtin_module_names : print ( "Can't get info for builtin modules." , file = sys . stderr ) sys . exit ( 1 ) if args . details : print ( 'Target: {}' . format ( target ) ) print ( 'Origin: {}' . format ( getsourcefile ( module ) ) ) print ( 'Cached: {}' . format ( module . __cached__ ) ) if obj is module : print ( 'Loader: {}' . format ( repr ( module . __loader__ ) ) ) if hasattr ( module , '__path__' ) : print ( 'Submodule search path: {}' . format ( module . __path__ ) ) else : try : __ , lineno = findsource ( obj ) except Exception : pass else : print ( 'Line: {}' . format ( lineno ) ) print ( '\n' ) else : print ( getsource ( obj ) )