Filters unknown options and converts some values from string to their corresponding python types (booleans and None). 'option_list' contains the list of valid options, and 'defaults' is used to deduce the type of some options (only bool at the moment). Returns a dictionary with opti
(options, defaults, option_list)
| 63 | |
| 64 | |
| 65 | def parse_shell_options(options, defaults, option_list): |
| 66 | """Filters unknown options and converts some values from string to their corresponding |
| 67 | python types (booleans and None). 'option_list' contains the list of valid options, |
| 68 | and 'defaults' is used to deduce the type of some options (only bool at the moment). |
| 69 | |
| 70 | Returns a dictionary with option names as keys and option values as values. |
| 71 | """ |
| 72 | # Build a dictionary that maps short and long option name to option for a quick lookup. |
| 73 | option_dests = dict() |
| 74 | for option in option_list: |
| 75 | if len(option._short_opts) > 0: |
| 76 | option_dests[option._short_opts[0][1:]] = option |
| 77 | if len(option._long_opts) > 0: |
| 78 | option_dests[option._long_opts[0][2:]] = option |
| 79 | if option.dest not in option_dests: |
| 80 | # Allowing dest name for backward compatibility. |
| 81 | option_dests[option.dest] = option |
| 82 | |
| 83 | result = {} |
| 84 | for option, value in options: |
| 85 | opt = option_dests.get(option) |
| 86 | if opt is None: |
| 87 | warn_msg = ( |
| 88 | "WARNING: Unable to read configuration file correctly. " |
| 89 | "Ignoring unrecognized config option: '%s'" % option |
| 90 | ) |
| 91 | print('\n{0}'.format(warn_msg), file=sys.stderr) |
| 92 | elif isinstance(defaults.get(option), bool) or \ |
| 93 | opt.action == "store_true" or opt.action == "store_false": |
| 94 | result[option] = parse_bool_option(value) |
| 95 | elif opt.action == "append": |
| 96 | result[option] = value.split(",%s=" % option) |
| 97 | elif value.lower() == "none": |
| 98 | result[option] = None |
| 99 | else: |
| 100 | result[option] = value |
| 101 | return result |
| 102 | |
| 103 | |
| 104 | def get_config_from_file(config_filename, option_list): |
no test coverage detected