def get_config_file_paths(env, args): """Get the location of the config files. The locations are specified by the --pants-config-files option. However we need to load the config in order to process the options. This method special-cases --pants-config-files in order to solve this chicken-and-egg problem. Note that, obviously, it's not possible to set the location of config files in a config file. Doing so will have no effect. """ # This exactly mirrors the logic applied in Option to all regular options. Note that we'll # also parse --pants-config as a regular option later, but there's no harm in that. In fact, # it's preferable, so that any code that happens to want to know where we read config from # can inspect the option. flag = '--pants-config-files=' evars = ['PANTS_GLOBAL_PANTS_CONFIG_FILES', 'PANTS_PANTS_CONFIG_FILES', 'PANTS_CONFIG_FILES'] path_list_values = [ListValueComponent.create(get_default_pants_config_file())] for var in evars: if var in env: path_list_values.append(ListValueComponent.create(env[var])) break for arg in args: # Technically this is very slightly incorrect, as we don't check scope. But it's # very unlikely that any task or subsystem will have an option named --pants-config-files. # TODO: Enforce a ban on options with a --pants- prefix outside our global options? if arg.startswith(flag): path_list_values.append(ListValueComponent.create(arg[len(flag):])) return ListValueComponent.merge(path_list_values).val
def to_value_type(self, val_str, type_arg, member_type, dest): """Convert a string to a value of the option's type.""" if val_str is None: return None if type_arg == bool: return self._ensure_bool(val_str) try: if type_arg == list: return ListValueComponent.create(val_str, member_type=member_type) if type_arg == dict: return DictValueComponent.create(val_str) return type_arg(val_str) except (TypeError, ValueError) as e: raise ParseError( f"Error applying type '{type_arg.__name__}' to option value '{val_str}', " f"for option '--{dest}' in {self._scope_str()}: {e}" )
def to_value_type(self, val_str, type_arg, member_type, dest): """Convert a string to a value of the option's type.""" if val_str is None: return None if type_arg == bool: return self.ensure_bool(val_str) try: if type_arg == list: return ListValueComponent.create(val_str, member_type=member_type) if type_arg == dict: return DictValueComponent.create(val_str) return type_arg(val_str) except (TypeError, ValueError) as e: if issubclass(type_arg, Enum): choices = ", ".join(f"{choice.value}" for choice in type_arg) raise ParseError(f"Invalid choice '{val_str}'. Choose from: {choices}") raise ParseError( f"Error applying type '{type_arg.__name__}' to option value '{val_str}': {e}" )
def assert_list_parsed(self, s: str, *, expected: ParsedList) -> None: assert expected == ListValueComponent.create(s).val