def _value_for_option(self, option): implied = {} for implied_option in self._implied_options[:]: if implied_option.name not in (option.name, option.env): continue self._implied_options.remove(implied_option) if (implied_option.when and not self._value_for(implied_option.when)): continue value = self._resolve(implied_option.value, need_help_dependency=False) if value is not None: if isinstance(value, OptionValue): pass elif value is True: value = PositiveOptionValue() elif value is False or value == (): value = NegativeOptionValue() elif isinstance(value, types.StringTypes): value = PositiveOptionValue((value, )) elif isinstance(value, tuple): value = PositiveOptionValue(value) else: raise TypeError("Unexpected type: '%s'" % type(value).__name__) opt = value.format(implied_option.option) self._helper.add(opt, 'implied') implied[opt] = implied_option try: value, option_string = self._helper.handle(option) except ConflictingOptionError as e: reason = implied[e.arg].reason if isinstance(reason, Option): reason = self._raw_options.get(reason) or reason.option reason = reason.split('=', 1)[0] raise InvalidOptionError( "'%s' implied by '%s' conflicts with '%s' from the %s" % (e.arg, reason, e.old_arg, e.old_origin)) if option_string: self._raw_options[option] = option_string when = self._conditions.get(option) if (when and not self._value_for(when, need_help_dependency=True) and value is not None and value.origin != 'default'): if value.origin == 'environment': # The value we return doesn't really matter, because of the # requirement for @depends to have the same when. return None raise InvalidOptionError( '%s is not available in this configuration' % option_string.split('=', 1)[0]) return value
def _value_for_option(self, option): implied = {} for implied_option in self._implied_options[:]: if implied_option.name not in (option.name, option.env): continue self._implied_options.remove(implied_option) if (implied_option.when and not self._value_for(implied_option.when)): continue value = self._resolve(implied_option.value, need_help_dependency=False) if value is not None: if isinstance(value, OptionValue): pass elif value is True: value = PositiveOptionValue() elif value is False or value == (): value = NegativeOptionValue() elif isinstance(value, types.StringTypes): value = PositiveOptionValue((value,)) elif isinstance(value, tuple): value = PositiveOptionValue(value) else: raise TypeError("Unexpected type: '%s'" % type(value).__name__) opt = value.format(implied_option.option) self._helper.add(opt, 'implied') implied[opt] = implied_option try: value, option_string = self._helper.handle(option) except ConflictingOptionError as e: reason = implied[e.arg].reason if isinstance(reason, Option): reason = self._raw_options.get(reason) or reason.option reason = reason.split('=', 1)[0] raise InvalidOptionError( "'%s' implied by '%s' conflicts with '%s' from the %s" % (e.arg, reason, e.old_arg, e.old_origin)) if option_string: self._raw_options[option] = option_string when = self._conditions.get(option) if (when and not self._value_for(when, need_help_dependency=True) and value is not None and value.origin != 'default'): if value.origin == 'environment': # The value we return doesn't really matter, because of the # requirement for @depends to have the same when. return None raise InvalidOptionError( '%s is not available in this configuration' % option_string.split('=', 1)[0]) return value
def _value_for_option(self, option): implied = {} for implied_option in self._implied_options[:]: if implied_option.name not in (option.name, option.env): continue self._implied_options.remove(implied_option) value = self._resolve(implied_option.value, need_help_dependency=False) if value is not None: if isinstance(value, OptionValue): pass elif value is True: value = PositiveOptionValue() elif value is False or value == (): value = NegativeOptionValue() elif isinstance(value, types.StringTypes): value = PositiveOptionValue((value,)) elif isinstance(value, tuple): value = PositiveOptionValue(value) else: raise TypeError("Unexpected type: '%s'" % type(value).__name__) opt = value.format(implied_option.option) self._helper.add(opt, 'implied') implied[opt] = implied_option try: value, option_string = self._helper.handle(option) except ConflictingOptionError as e: reason = implied[e.arg].reason if isinstance(reason, Option): reason = self._raw_options.get(reason) or reason.option reason = reason.split('=', 1)[0] raise InvalidOptionError( "'%s' implied by '%s' conflicts with '%s' from the %s" % (e.arg, reason, e.old_arg, e.old_origin)) if option_string: self._raw_options[option] = option_string return value
def imply_option_impl(self, option, value, reason=None): '''Implementation of imply_option(). Injects additional options as if they had been passed on the command line. The `option` argument is a string as in option()'s `name` or `env`. The option must be declared after `imply_option` references it. The `value` argument indicates the value to pass to the option. It can be: - True. In this case `imply_option` injects the positive option (--enable-foo/--with-foo). imply_option('--enable-foo', True) imply_option('--disable-foo', True) are both equivalent to `--enable-foo` on the command line. - False. In this case `imply_option` injects the negative option (--disable-foo/--without-foo). imply_option('--enable-foo', False) imply_option('--disable-foo', False) are both equivalent to `--disable-foo` on the command line. - None. In this case `imply_option` does nothing. imply_option('--enable-foo', None) imply_option('--disable-foo', None) are both equivalent to not passing any flag on the command line. - a string or a tuple. In this case `imply_option` injects the positive option with the given value(s). imply_option('--enable-foo', 'a') imply_option('--disable-foo', 'a') are both equivalent to `--enable-foo=a` on the command line. imply_option('--enable-foo', ('a', 'b')) imply_option('--disable-foo', ('a', 'b')) are both equivalent to `--enable-foo=a,b` on the command line. Because imply_option('--disable-foo', ...) can be misleading, it is recommended to use the positive form ('--enable' or '--with') for `option`. The `value` argument can also be (and usually is) a reference to a @depends function, in which case the result of that function will be used as per the descripted mapping above. The `reason` argument indicates what caused the option to be implied. It is necessary when it cannot be inferred from the `value`. ''' # Don't do anything when --help was on the command line if self._help: return if not reason and isinstance(value, DummyFunction): deps = self._depends[value][1] possible_reasons = [d for d in deps if d != self._help_option] if len(possible_reasons) == 1: if isinstance(possible_reasons[0], Option): reason = (self._raw_options.get(possible_reasons[0]) or possible_reasons[0].option) if not reason or not isinstance(value, DummyFunction): raise ConfigureError( "Cannot infer what implies '%s'. Please add a `reason` to " "the `imply_option` call." % option) value = self._resolve(value, need_help_dependency=False) if value is not None: if isinstance(value, OptionValue): pass elif value is True: value = PositiveOptionValue() elif value is False or value == (): value = NegativeOptionValue() elif isinstance(value, types.StringTypes): value = PositiveOptionValue((value, )) elif isinstance(value, tuple): value = PositiveOptionValue(value) else: raise TypeError("Unexpected type: '%s'" % type(value)) option = value.format(option) self._helper.add(option, 'implied') self._implied_options[option] = inspect.stack()[1], reason
def test_option_value_format(self): val = PositiveOptionValue() self.assertEquals('--with-value', val.format('--with-value')) self.assertEquals('--with-value', val.format('--without-value')) self.assertEquals('--enable-value', val.format('--enable-value')) self.assertEquals('--enable-value', val.format('--disable-value')) self.assertEquals('--value', val.format('--value')) self.assertEquals('VALUE=1', val.format('VALUE')) val = PositiveOptionValue(('a', )) self.assertEquals('--with-value=a', val.format('--with-value')) self.assertEquals('--with-value=a', val.format('--without-value')) self.assertEquals('--enable-value=a', val.format('--enable-value')) self.assertEquals('--enable-value=a', val.format('--disable-value')) self.assertEquals('--value=a', val.format('--value')) self.assertEquals('VALUE=a', val.format('VALUE')) val = PositiveOptionValue(('a', 'b')) self.assertEquals('--with-value=a,b', val.format('--with-value')) self.assertEquals('--with-value=a,b', val.format('--without-value')) self.assertEquals('--enable-value=a,b', val.format('--enable-value')) self.assertEquals('--enable-value=a,b', val.format('--disable-value')) self.assertEquals('--value=a,b', val.format('--value')) self.assertEquals('VALUE=a,b', val.format('VALUE')) val = NegativeOptionValue() self.assertEquals('--without-value', val.format('--with-value')) self.assertEquals('--without-value', val.format('--without-value')) self.assertEquals('--disable-value', val.format('--enable-value')) self.assertEquals('--disable-value', val.format('--disable-value')) self.assertEquals('', val.format('--value')) self.assertEquals('VALUE=', val.format('VALUE'))
def test_option_value_format(self): val = PositiveOptionValue() self.assertEquals('--with-value', val.format('--with-value')) self.assertEquals('--with-value', val.format('--without-value')) self.assertEquals('--enable-value', val.format('--enable-value')) self.assertEquals('--enable-value', val.format('--disable-value')) self.assertEquals('--value', val.format('--value')) self.assertEquals('VALUE=1', val.format('VALUE')) val = PositiveOptionValue(('a',)) self.assertEquals('--with-value=a', val.format('--with-value')) self.assertEquals('--with-value=a', val.format('--without-value')) self.assertEquals('--enable-value=a', val.format('--enable-value')) self.assertEquals('--enable-value=a', val.format('--disable-value')) self.assertEquals('--value=a', val.format('--value')) self.assertEquals('VALUE=a', val.format('VALUE')) val = PositiveOptionValue(('a', 'b')) self.assertEquals('--with-value=a,b', val.format('--with-value')) self.assertEquals('--with-value=a,b', val.format('--without-value')) self.assertEquals('--enable-value=a,b', val.format('--enable-value')) self.assertEquals('--enable-value=a,b', val.format('--disable-value')) self.assertEquals('--value=a,b', val.format('--value')) self.assertEquals('VALUE=a,b', val.format('VALUE')) val = NegativeOptionValue() self.assertEquals('--without-value', val.format('--with-value')) self.assertEquals('--without-value', val.format('--without-value')) self.assertEquals('--disable-value', val.format('--enable-value')) self.assertEquals('--disable-value', val.format('--disable-value')) self.assertEquals('', val.format('--value')) self.assertEquals('VALUE=', val.format('VALUE'))
def imply_option_impl(self, option, value, reason=None): '''Implementation of imply_option(). Injects additional options as if they had been passed on the command line. The `option` argument is a string as in option()'s `name` or `env`. The option must be declared after `imply_option` references it. The `value` argument indicates the value to pass to the option. It can be: - True. In this case `imply_option` injects the positive option (--enable-foo/--with-foo). imply_option('--enable-foo', True) imply_option('--disable-foo', True) are both equivalent to `--enable-foo` on the command line. - False. In this case `imply_option` injects the negative option (--disable-foo/--without-foo). imply_option('--enable-foo', False) imply_option('--disable-foo', False) are both equivalent to `--disable-foo` on the command line. - None. In this case `imply_option` does nothing. imply_option('--enable-foo', None) imply_option('--disable-foo', None) are both equivalent to not passing any flag on the command line. - a string or a tuple. In this case `imply_option` injects the positive option with the given value(s). imply_option('--enable-foo', 'a') imply_option('--disable-foo', 'a') are both equivalent to `--enable-foo=a` on the command line. imply_option('--enable-foo', ('a', 'b')) imply_option('--disable-foo', ('a', 'b')) are both equivalent to `--enable-foo=a,b` on the command line. Because imply_option('--disable-foo', ...) can be misleading, it is recommended to use the positive form ('--enable' or '--with') for `option`. The `value` argument can also be (and usually is) a reference to a @depends function, in which case the result of that function will be used as per the descripted mapping above. The `reason` argument indicates what caused the option to be implied. It is necessary when it cannot be inferred from the `value`. ''' # Don't do anything when --help was on the command line if self._help: return if not reason and isinstance(value, DummyFunction): deps = self._depends[value][1] possible_reasons = [d for d in deps if d != self._help_option] if len(possible_reasons) == 1: if isinstance(possible_reasons[0], Option): reason = (self._raw_options.get(possible_reasons[0]) or possible_reasons[0].option) if not reason or not isinstance(value, DummyFunction): raise ConfigureError( "Cannot infer what implies '%s'. Please add a `reason` to " "the `imply_option` call." % option) value = self._resolve(value, need_help_dependency=False) if value is not None: if isinstance(value, OptionValue): pass elif value is True: value = PositiveOptionValue() elif value is False or value == (): value = NegativeOptionValue() elif isinstance(value, types.StringTypes): value = PositiveOptionValue((value,)) elif isinstance(value, tuple): value = PositiveOptionValue(value) else: raise TypeError("Unexpected type: '%s'" % type(value)) option = value.format(option) self._helper.add(option, 'implied') self._implied_options[option] = inspect.stack()[1], reason
def _value_for_option(self, option): implied = {} for implied_option in self._implied_options[:]: if implied_option.name not in (option.name, option.env): continue self._implied_options.remove(implied_option) if (implied_option.when and not self._value_for(implied_option.when)): continue value = self._resolve(implied_option.value) if value is not None: if isinstance(value, OptionValue): pass elif value is True: value = PositiveOptionValue() elif value is False or value == (): value = NegativeOptionValue() elif isinstance(value, types.StringTypes): value = PositiveOptionValue((value, )) elif isinstance(value, tuple): value = PositiveOptionValue(value) else: raise TypeError("Unexpected type: '%s'" % type(value).__name__) opt = value.format(implied_option.option) self._helper.add(opt, 'implied') implied[opt] = implied_option try: value, option_string = self._helper.handle(option) except ConflictingOptionError as e: reason = implied[e.arg].reason if isinstance(reason, Option): reason = self._raw_options.get(reason) or reason.option reason = reason.split('=', 1)[0] raise InvalidOptionError( "'%s' implied by '%s' conflicts with '%s' from the %s" % (e.arg, reason, e.old_arg, e.old_origin)) if option_string: self._raw_options[option] = option_string when = self._conditions.get(option) # If `when` resolves to a false-ish value, we always return None. # This makes option(..., when='--foo') equivalent to # option(..., when=depends('--foo')(lambda x: x)). if when and not self._value_for(when) and value is not None: # If the option was passed explicitly, we throw an error that # the option is not available. Except when the option was passed # from the environment, because that would be too cumbersome. if value.origin not in ('default', 'environment'): raise InvalidOptionError( '%s is not available in this configuration' % option_string.split('=', 1)[0]) return None return value
def test_option_value_format(self): val = PositiveOptionValue() self.assertEquals("--with-value", val.format("--with-value")) self.assertEquals("--with-value", val.format("--without-value")) self.assertEquals("--enable-value", val.format("--enable-value")) self.assertEquals("--enable-value", val.format("--disable-value")) self.assertEquals("--value", val.format("--value")) self.assertEquals("VALUE=1", val.format("VALUE")) val = PositiveOptionValue(("a",)) self.assertEquals("--with-value=a", val.format("--with-value")) self.assertEquals("--with-value=a", val.format("--without-value")) self.assertEquals("--enable-value=a", val.format("--enable-value")) self.assertEquals("--enable-value=a", val.format("--disable-value")) self.assertEquals("--value=a", val.format("--value")) self.assertEquals("VALUE=a", val.format("VALUE")) val = PositiveOptionValue(("a", "b")) self.assertEquals("--with-value=a,b", val.format("--with-value")) self.assertEquals("--with-value=a,b", val.format("--without-value")) self.assertEquals("--enable-value=a,b", val.format("--enable-value")) self.assertEquals("--enable-value=a,b", val.format("--disable-value")) self.assertEquals("--value=a,b", val.format("--value")) self.assertEquals("VALUE=a,b", val.format("VALUE")) val = NegativeOptionValue() self.assertEquals("--without-value", val.format("--with-value")) self.assertEquals("--without-value", val.format("--without-value")) self.assertEquals("--disable-value", val.format("--enable-value")) self.assertEquals("--disable-value", val.format("--disable-value")) self.assertEquals("", val.format("--value")) self.assertEquals("VALUE=", val.format("VALUE"))