Loop over user prompts for an ENV param until receiving a valid response. For the env param var_name, read from the environment or verify user input until receiving valid input. When done, set var_name in the environ_cp to its new value. Args: environ_cp: (Dict) copy of the os.environ.
(environ_cp,
var_name,
var_default,
ask_for_var,
check_success,
error_msg,
suppress_default_error=False,
n_ask_attempts=_DEFAULT_PROMPT_ASK_ATTEMPTS)
| 619 | |
| 620 | |
| 621 | def prompt_loop_or_load_from_env(environ_cp, |
| 622 | var_name, |
| 623 | var_default, |
| 624 | ask_for_var, |
| 625 | check_success, |
| 626 | error_msg, |
| 627 | suppress_default_error=False, |
| 628 | n_ask_attempts=_DEFAULT_PROMPT_ASK_ATTEMPTS): |
| 629 | """Loop over user prompts for an ENV param until receiving a valid response. |
| 630 | |
| 631 | For the env param var_name, read from the environment or verify user input |
| 632 | until receiving valid input. When done, set var_name in the environ_cp to its |
| 633 | new value. |
| 634 | |
| 635 | Args: |
| 636 | environ_cp: (Dict) copy of the os.environ. |
| 637 | var_name: (String) string for name of environment variable, e.g. "TF_MYVAR". |
| 638 | var_default: (String) default value string. |
| 639 | ask_for_var: (String) string for how to ask for user input. |
| 640 | check_success: (Function) function that takes one argument and returns a |
| 641 | boolean. Should return True if the value provided is considered valid. May |
| 642 | contain a complex error message if error_msg does not provide enough |
| 643 | information. In that case, set suppress_default_error to True. |
| 644 | error_msg: (String) String with one and only one '%s'. Formatted with each |
| 645 | invalid response upon check_success(input) failure. |
| 646 | suppress_default_error: (Bool) Suppress the above error message in favor of |
| 647 | one from the check_success function. |
| 648 | n_ask_attempts: (Integer) Number of times to query for valid input before |
| 649 | raising an error and quitting. |
| 650 | |
| 651 | Returns: |
| 652 | [String] The value of var_name after querying for input. |
| 653 | |
| 654 | Raises: |
| 655 | UserInputError: if a query has been attempted n_ask_attempts times without |
| 656 | success, assume that the user has made a scripting error, and will |
| 657 | continue to provide invalid input. Raise the error to avoid infinitely |
| 658 | looping. |
| 659 | """ |
| 660 | default = environ_cp.get(var_name) or var_default |
| 661 | full_query = '%s [Default is %s]: ' % ( |
| 662 | ask_for_var, |
| 663 | default, |
| 664 | ) |
| 665 | |
| 666 | for _ in range(n_ask_attempts): |
| 667 | val = get_from_env_or_user_or_default(environ_cp, var_name, full_query, |
| 668 | default) |
| 669 | if check_success(val): |
| 670 | break |
| 671 | if not suppress_default_error: |
| 672 | print(error_msg % val) |
| 673 | environ_cp[var_name] = '' |
| 674 | else: |
| 675 | raise UserInputError('Invalid %s setting was provided %d times in a row. ' |
| 676 | 'Assuming to be a scripting mistake.' % |
| 677 | (var_name, n_ask_attempts)) |
| 678 |
no test coverage detected