Ejemplo n.º 1
0
    def register(self, *args, **kwargs):
        """Register an option."""
        if self._frozen:
            raise FrozenRegistration(self.scope, args[0])

        # Prevent further registration in enclosing scopes.
        ancestor = self._parent_parser
        while ancestor:
            ancestor._freeze()
            ancestor = ancestor._parent_parser

        # Boolean options always have an implicit boolean-typed default.  They can never be None.
        # We make that default explicit here.
        if kwargs.get('type') == bool and kwargs.get('default') is None:
            kwargs['default'] = not self._ensure_bool(
                kwargs.get('implicit_value', True))

        # Record the args. We'll do the underlying parsing on-demand.
        self._option_registrations.append((args, kwargs))
        if self._parent_parser:
            for arg in args:
                existing_scope = self._parent_parser._existing_scope(arg)
                if existing_scope is not None:
                    raise Shadowing(
                        self.scope,
                        arg,
                        outer_scope=self._scope_str(existing_scope))
        for arg in args:
            if arg in self._known_args:
                raise OptionAlreadyRegistered(self.scope, arg)
        self._known_args.update(args)
Ejemplo n.º 2
0
  def register(self, *args, **kwargs):
    """Register an option."""
    if self._frozen:
      raise FrozenRegistration(self.scope, args[0])

    # Prevent further registration in enclosing scopes.
    ancestor = self._parent_parser
    while ancestor:
      ancestor._freeze()
      ancestor = ancestor._parent_parser

    if kwargs.get('type') == bool:
      default = kwargs.get('default')
      if default is None:
        # Unless a tri-state bool is explicitly opted into with the `UnsetBool` default value,
        # boolean options always have an implicit boolean-typed default. We make that default
        # explicit here.
        kwargs['default'] = not self._ensure_bool(kwargs.get('implicit_value', True))
      elif default is UnsetBool:
        kwargs['default'] = None

    # Record the args. We'll do the underlying parsing on-demand.
    self._option_registrations.append((args, kwargs))
    if self._parent_parser:
      for arg in args:
        existing_scope = self._parent_parser._existing_scope(arg)
        if existing_scope is not None:
          raise Shadowing(self.scope, arg, outer_scope=self._scope_str(existing_scope))
    for arg in args:
      if arg in self._known_args:
        raise OptionAlreadyRegistered(self.scope, arg)
    self._known_args.update(args)
Ejemplo n.º 3
0
  def register(self, *args, **kwargs):
    """Register an option."""
    if self._frozen:
      raise FrozenRegistration(self.scope, args[0])

    # Prevent further registration in enclosing scopes.
    ancestor = self._parent_parser
    while ancestor:
      ancestor._freeze()
      ancestor = ancestor._parent_parser

    # Record the args. We'll do the underlying parsing on-demand.
    self._option_registrations.append((args, kwargs))
    if self._parent_parser:
      for arg in args:
        existing_scope = self._parent_parser._existing_scope(arg)
        if existing_scope is not None:
          raise Shadowing(self.scope, arg, outer_scope=self._scope_str(existing_scope))
    self._known_args.update(args)
Ejemplo n.º 4
0
 def _check_shadowing(cls, parent_scope, parent_known_args, child_scope,
                      child_known_args):
     for arg in parent_known_args & child_known_args:
         raise Shadowing(child_scope,
                         arg,
                         outer_scope=cls.scope_str(parent_scope))
Ejemplo n.º 5
0
    def register(self, *args, **kwargs):
        """Register an option."""
        if self._frozen:
            raise FrozenRegistration(self.scope, args[0])

        # Prevent further registration in enclosing scopes.
        ancestor = self._parent_parser
        while ancestor:
            ancestor._freeze()
            ancestor = ancestor._parent_parser

        def check_deprecated_types(kwarg_name):
            t = kwargs.get(kwarg_name)
            # First check for deprecated direct use of the internal types.
            if t == list_option:
                deprecated_conditional(
                    lambda: True, '0.0.83',
                    'list_option is deprecated for option {} in scope {}. '
                    'Use type=list.'.format(args[0], self.scope))
            elif t == dict_option:
                deprecated_conditional(
                    lambda: True, '0.0.83',
                    'dict_option is deprecated for option {} in scope {}. '
                    'Use type=dict.'.format(args[0], self.scope))

        check_deprecated_types('type')
        check_deprecated_types('member_type')

        action = kwargs.get('action')
        # Temporary munging to effectively turn type=list options into list options,
        # for uniform handling.  From here on, type=list is an error.
        # TODO: Remove after type=list deprecation.
        if action == 'append':
            if 'type' in kwargs:
                kwargs['member_type'] = kwargs['type']
            kwargs['type'] = list
            del kwargs['action']
            deprecated_conditional(
                lambda: True, '0.0.83',
                "action='append' is deprecated for option {} in scope {}. "
                "Use type=list.".format(args[0], self.scope))

        # Temporary munging to effectively turn type='target_list_option' options into list options,
        # with member type 'target_option', for uniform handling.
        # TODO: Remove after target_list_option deprecation.
        if kwargs.get('type') == target_list_option:
            kwargs['type'] = list
            kwargs['member_type'] = target_option
            deprecated_conditional(
                lambda: True, '0.0.83',
                'target_list_option is deprecated for option {} in scope {}. '
                'Use type=list, member_type=target_option.'.format(
                    args[0], self.scope))

        # Temporary munging to effectively turn action='store_true' into bool-typed options.
        # From here on, action='store_true' is an error.  Ditto for store_false.
        # TODO: Remove after action='store_true'/'store_false' deprecation.
        if action == 'store_true':
            kwargs['type'] = bool
            kwargs['implicit_value'] = True
            del kwargs['action']
            deprecated_conditional(
                lambda: True, '0.0.83',
                "action='store_true' is deprecated for option {} in scope {}. "
                "Use type=bool.".format(args[0], self.scope))
        elif action == 'store_false':
            kwargs['type'] = bool
            kwargs['implicit_value'] = False
            del kwargs['action']
            deprecated_conditional(
                lambda: True, '0.0.83',
                "action='store_false' is deprecated for option {} in scope {}. "
                "Use type=bool, implicit_value=False.".format(
                    args[0], self.scope))

        # Boolean options always have an implicit boolean-typed default.  They can never be None.
        # We make that default explicit here.
        if kwargs.get('type') == bool and kwargs.get('default') is None:
            kwargs['default'] = not self._ensure_bool(
                kwargs.get('implicit_value', True))

        # Record the args. We'll do the underlying parsing on-demand.
        self._option_registrations.append((args, kwargs))
        if self._parent_parser:
            for arg in args:
                existing_scope = self._parent_parser._existing_scope(arg)
                if existing_scope is not None:
                    raise Shadowing(
                        self.scope,
                        arg,
                        outer_scope=self._scope_str(existing_scope))
        for arg in args:
            if arg in self._known_args:
                raise OptionAlreadyRegistered(self.scope, arg)
        self._known_args.update(args)