Logic for inspecting an object given at command line
()
| 3354 | |
| 3355 | |
| 3356 | def _main(): |
| 3357 | """ Logic for inspecting an object given at command line """ |
| 3358 | import argparse |
| 3359 | import importlib |
| 3360 | |
| 3361 | parser = argparse.ArgumentParser(color=True) |
| 3362 | parser.add_argument( |
| 3363 | 'object', |
| 3364 | help="The object to be analysed. " |
| 3365 | "It supports the 'module:qualname' syntax") |
| 3366 | parser.add_argument( |
| 3367 | '-d', '--details', action='store_true', |
| 3368 | help='Display info about the module rather than its source code') |
| 3369 | |
| 3370 | args = parser.parse_args() |
| 3371 | |
| 3372 | target = args.object |
| 3373 | mod_name, has_attrs, attrs = target.partition(":") |
| 3374 | try: |
| 3375 | obj = module = importlib.import_module(mod_name) |
| 3376 | except Exception as exc: |
| 3377 | msg = "Failed to import {} ({}: {})".format(mod_name, |
| 3378 | type(exc).__name__, |
| 3379 | exc) |
| 3380 | print(msg, file=sys.stderr) |
| 3381 | sys.exit(2) |
| 3382 | |
| 3383 | if has_attrs: |
| 3384 | parts = attrs.split(".") |
| 3385 | obj = module |
| 3386 | for part in parts: |
| 3387 | obj = getattr(obj, part) |
| 3388 | |
| 3389 | if module.__name__ in sys.builtin_module_names: |
| 3390 | print("Can't get info for builtin modules.", file=sys.stderr) |
| 3391 | sys.exit(1) |
| 3392 | |
| 3393 | if args.details: |
| 3394 | print(f'Target: {target}') |
| 3395 | print(f'Origin: {getsourcefile(module)}') |
| 3396 | print(f'Cached: {module.__spec__.cached}') |
| 3397 | if obj is module: |
| 3398 | print(f'Loader: {module.__loader__!r}') |
| 3399 | if hasattr(module, '__path__'): |
| 3400 | print(f'Submodule search path: {module.__path__}') |
| 3401 | else: |
| 3402 | try: |
| 3403 | __, lineno = findsource(obj) |
| 3404 | except Exception: |
| 3405 | pass |
| 3406 | else: |
| 3407 | print(f'Line: {lineno}') |
| 3408 | |
| 3409 | print() |
| 3410 | else: |
| 3411 | print(getsource(obj)) |
| 3412 | |
| 3413 |
no test coverage detected
searching dependent graphs…