Execute REPL config file. :param repl: `PythonInput` instance. :param config_file: Path of the configuration file.
(repl: PythonInput, config_file: str | None = None)
| 451 | |
| 452 | |
| 453 | def run_config(repl: PythonInput, config_file: str | None = None) -> None: |
| 454 | """ |
| 455 | Execute REPL config file. |
| 456 | |
| 457 | :param repl: `PythonInput` instance. |
| 458 | :param config_file: Path of the configuration file. |
| 459 | """ |
| 460 | explicit_config_file = config_file is not None |
| 461 | |
| 462 | # Expand tildes. |
| 463 | config_file = os.path.expanduser( |
| 464 | config_file if config_file is not None else DEFAULT_CONFIG_FILE |
| 465 | ) |
| 466 | |
| 467 | def enter_to_continue() -> None: |
| 468 | input("\nPress ENTER to continue...") |
| 469 | |
| 470 | # Check whether this file exists. |
| 471 | if not os.path.exists(config_file): |
| 472 | if explicit_config_file: |
| 473 | print(f"Impossible to read {config_file}") |
| 474 | enter_to_continue() |
| 475 | return |
| 476 | |
| 477 | # Run the config file in an empty namespace. |
| 478 | try: |
| 479 | namespace: dict[str, Any] = {} |
| 480 | |
| 481 | with open(config_file, "rb") as f: |
| 482 | code = compile(f.read(), config_file, "exec") |
| 483 | exec(code, namespace, namespace) |
| 484 | |
| 485 | # Now we should have a 'configure' method in this namespace. We call this |
| 486 | # method with the repl as an argument. |
| 487 | if "configure" in namespace: |
| 488 | namespace["configure"](repl) |
| 489 | |
| 490 | except Exception: |
| 491 | traceback.print_exc() |
| 492 | enter_to_continue() |
| 493 | |
| 494 | |
| 495 | class exit: |
no test coverage detected