Logic for inspecting an object given at command line
()
| 3265 | |
| 3266 | |
| 3267 | def _main(): |
| 3268 | """ Logic for inspecting an object given at command line """ |
| 3269 | import argparse |
| 3270 | import importlib |
| 3271 | |
| 3272 | parser = argparse.ArgumentParser() |
| 3273 | parser.add_argument( |
| 3274 | 'object', |
| 3275 | help="The object to be analysed. " |
| 3276 | "It supports the 'module:qualname' syntax") |
| 3277 | parser.add_argument( |
| 3278 | '-d', '--details', action='store_true', |
| 3279 | help='Display info about the module rather than its source code') |
| 3280 | |
| 3281 | args = parser.parse_args() |
| 3282 | |
| 3283 | target = args.object |
| 3284 | mod_name, has_attrs, attrs = target.partition(":") |
| 3285 | try: |
| 3286 | obj = module = importlib.import_module(mod_name) |
| 3287 | except Exception as exc: |
| 3288 | msg = "Failed to import {} ({}: {})".format(mod_name, |
| 3289 | type(exc).__name__, |
| 3290 | exc) |
| 3291 | print(msg, file=sys.stderr) |
| 3292 | sys.exit(2) |
| 3293 | |
| 3294 | if has_attrs: |
| 3295 | parts = attrs.split(".") |
| 3296 | obj = module |
| 3297 | for part in parts: |
| 3298 | obj = getattr(obj, part) |
| 3299 | |
| 3300 | if module.__name__ in sys.builtin_module_names: |
| 3301 | print("Can't get info for builtin modules.", file=sys.stderr) |
| 3302 | sys.exit(1) |
| 3303 | |
| 3304 | if args.details: |
| 3305 | print('Target: {}'.format(target)) |
| 3306 | print('Origin: {}'.format(getsourcefile(module))) |
| 3307 | print('Cached: {}'.format(module.__cached__)) |
| 3308 | if obj is module: |
| 3309 | print('Loader: {}'.format(repr(module.__loader__))) |
| 3310 | if hasattr(module, '__path__'): |
| 3311 | print('Submodule search path: {}'.format(module.__path__)) |
| 3312 | else: |
| 3313 | try: |
| 3314 | __, lineno = findsource(obj) |
| 3315 | except Exception: |
| 3316 | pass |
| 3317 | else: |
| 3318 | print('Line: {}'.format(lineno)) |
| 3319 | |
| 3320 | print('\n') |
| 3321 | else: |
| 3322 | print(getsource(obj)) |
| 3323 | |
| 3324 |
no test coverage detected