Exemplo n.º 1
0
    def _compute_default(self, dest, kwargs):
        """Compute the default value to use for an option's registration.

    The source of the default value is chosen according to the ranking in RankedValue.
    """
        config_section = 'DEFAULT' if self._scope == GLOBAL_SCOPE else self._scope
        env_var = 'PANTS_{0}_{1}'.format(
            config_section.upper().replace('.', '_'), dest.upper())
        value_type = kwargs.get('type', str)
        env_val_str = self._env.get(env_var) if self._env else None

        env_val = None if env_val_str is None else value_type(env_val_str)
        if kwargs.get('action') == 'append':
            config_val_strs = self._config.getlist(
                config_section, dest) if self._config else None
            config_val = (None if config_val_strs is None else [
                value_type(config_val_str)
                for config_val_str in config_val_strs
            ])
            default = []
        else:
            config_val_str = (self._config.get(
                config_section, dest, default=None) if self._config else None)
            config_val = None if config_val_str is None else value_type(
                config_val_str)
            default = None
        hardcoded_val = kwargs.get('default')
        return RankedValue.choose(None, env_val, config_val, hardcoded_val,
                                  default)
Exemplo n.º 2
0
  def _compute_default(self, dest, kwargs):
    """Compute the default value to use for an option's registration.

    The source of the default value is chosen according to the ranking in RankedValue.
    """
    config_section = 'DEFAULT' if self._scope == GLOBAL_SCOPE else self._scope
    env_var = 'PANTS_{0}_{1}'.format(config_section.upper().replace('.', '_'), dest.upper())
    value_type = kwargs.get('type', str)
    env_val_str = self._env.get(env_var) if self._env else None

    env_val = None if env_val_str is None else value_type(env_val_str)
    config_val = self._config.get(config_section, dest, default=None) if self._config else None
    hardcoded_val = kwargs.get('default')
    return RankedValue.choose(None, env_val, config_val, hardcoded_val)
Exemplo n.º 3
0
    def _compute_default(self, dest, kwargs):
        """Compute the default value to use for an option's registration.

    The source of the default value is chosen according to the ranking in RankedValue.
    """
        config_section = 'DEFAULT' if self._scope == GLOBAL_SCOPE else self._scope
        udest = dest.upper()
        if self._scope == GLOBAL_SCOPE:
            # For convenience, we allow three forms of env var for global scope options.
            # The fully-specified env var is PANTS_DEFAULT_FOO, which is uniform with PANTS_<SCOPE>_FOO
            # for all the other scopes.  However we also allow simply PANTS_FOO. And if the option name
            # itself starts with 'pants-' then we also allow simply FOO. E.g., PANTS_WORKDIR instead of
            # PANTS_PANTS_WORKDIR or PANTS_DEFAULT_PANTS_WORKDIR. We take the first specified value we
            # find, in this order: PANTS_DEFAULT_FOO, PANTS_FOO, FOO.
            env_vars = [
                'PANTS_DEFAULT_{0}'.format(udest), 'PANTS_{0}'.format(udest)
            ]
            if udest.startswith('PANTS_'):
                env_vars.append(udest)
        else:
            sanitized_env_var_scope = self._ENV_SANITIZER_RE.sub(
                '_', config_section.upper())
            env_vars = ['PANTS_{0}_{1}'.format(sanitized_env_var_scope, udest)]
        value_type = self.str_to_bool if is_boolean_flag(
            kwargs) else kwargs.get('type', str)
        env_val_str = None
        if self._env:
            for env_var in env_vars:
                if env_var in self._env:
                    env_val_str = self._env.get(env_var)
                    break
        env_val = None if env_val_str is None else value_type(env_val_str)
        if kwargs.get('action') == 'append':
            config_val_strs = self._config.getlist(
                config_section, dest) if self._config else None
            config_val = (None if config_val_strs is None else [
                value_type(config_val_str)
                for config_val_str in config_val_strs
            ])
            default = []
        else:
            config_val_str = (self._config.get(
                config_section, dest, default=None) if self._config else None)
            config_val = None if config_val_str is None else value_type(
                config_val_str)
            default = None
        hardcoded_val = kwargs.get('default')
        return RankedValue.choose(None, env_val, config_val, hardcoded_val,
                                  default)
Exemplo n.º 4
0
  def _compute_default(self, dest, kwargs):
    """Compute the default value to use for an option's registration.

    The source of the default value is chosen according to the ranking in RankedValue.
    """
    config_section = 'DEFAULT' if self._scope == GLOBAL_SCOPE else self._scope
    udest = dest.upper()
    if self._scope == GLOBAL_SCOPE:
      # For convenience, we allow three forms of env var for global scope options.
      # The fully-specified env var is PANTS_DEFAULT_FOO, which is uniform with PANTS_<SCOPE>_FOO
      # for all the other scopes.  However we also allow simply PANTS_FOO. And if the option name
      # itself starts with 'pants-' then we also allow simply FOO. E.g., PANTS_WORKDIR instead of
      # PANTS_PANTS_WORKDIR or PANTS_DEFAULT_PANTS_WORKDIR. We take the first specified value we
      # find, in this order: PANTS_DEFAULT_FOO, PANTS_FOO, FOO.
      env_vars = ['PANTS_DEFAULT_{0}'.format(udest), 'PANTS_{0}'.format(udest)]
      if udest.startswith('PANTS_'):
        env_vars.append(udest)
    else:
      sanitized_env_var_scope = self._ENV_SANITIZER_RE.sub('_', config_section.upper())
      env_vars = ['PANTS_{0}_{1}'.format(sanitized_env_var_scope, udest)]
    value_type = self.str_to_bool if is_boolean_flag(kwargs) else kwargs.get('type', str)
    env_val_str = None
    if self._env:
      for env_var in env_vars:
        if env_var in self._env:
          env_val_str = self._env.get(env_var)
          break
    env_val = None if env_val_str is None else value_type(env_val_str)
    if kwargs.get('action') == 'append':
      config_val_strs = self._config.getlist(config_section, dest) if self._config else None
      config_val = (None if config_val_strs is None else
                    [value_type(config_val_str) for config_val_str in config_val_strs])
      default = []
    else:
      config_val_str = (self._config.get(config_section, dest, default=None)
                        if self._config else None)
      config_val = None if config_val_str is None else value_type(config_val_str)
      default = None
    hardcoded_val = kwargs.get('default')
    return RankedValue.choose(None, env_val, config_val, hardcoded_val, default)
Exemplo n.º 5
0
    def _compute_default(self, dest, kwargs):
        """Compute the default value to use for an option's registration.

    The source of the default value is chosen according to the ranking in RankedValue.
    """
        is_fromfile = kwargs.get('fromfile', False)
        action = kwargs.get('action')
        if is_fromfile and action and action != 'append':
            raise ParseError(
                'Cannot fromfile {} with an action ({}) in scope {}'.format(
                    dest, action, self._scope))

        config_section = 'DEFAULT' if self._scope == GLOBAL_SCOPE else self._scope
        udest = dest.upper()
        if self._scope == GLOBAL_SCOPE:
            # For convenience, we allow three forms of env var for global scope options.
            # The fully-specified env var is PANTS_DEFAULT_FOO, which is uniform with PANTS_<SCOPE>_FOO
            # for all the other scopes.  However we also allow simply PANTS_FOO. And if the option name
            # itself starts with 'pants-' then we also allow simply FOO. E.g., PANTS_WORKDIR instead of
            # PANTS_PANTS_WORKDIR or PANTS_DEFAULT_PANTS_WORKDIR. We take the first specified value we
            # find, in this order: PANTS_DEFAULT_FOO, PANTS_FOO, FOO.
            env_vars = [
                'PANTS_DEFAULT_{0}'.format(udest), 'PANTS_{0}'.format(udest)
            ]
            if udest.startswith('PANTS_'):
                env_vars.append(udest)
        else:
            sanitized_env_var_scope = self._ENV_SANITIZER_RE.sub(
                '_', config_section.upper())
            env_vars = ['PANTS_{0}_{1}'.format(sanitized_env_var_scope, udest)]

        value_type = self.str_to_bool if is_boolean_flag(
            kwargs) else kwargs.get('type', str)

        env_val_str = None
        if self._env:
            for env_var in env_vars:
                if env_var in self._env:
                    env_val_str = self._env.get(env_var)
                    break

        config_val_str = self._config.get(config_section, dest, default=None)

        def expand(val_str):
            if is_fromfile and val_str and val_str.startswith(
                    '@') and not val_str.startswith('@@'):
                fromfile = val_str[1:]
                try:
                    with open(fromfile) as fp:
                        return fp.read().strip()
                except IOError as e:
                    raise self.FromfileError(
                        'Failed to read {} from file {}: {}'.format(
                            dest, fromfile, e))
            else:
                # Support a literal @ for fromfile values via @@.
                return val_str[1:] if is_fromfile and val_str.startswith(
                    '@@') else val_str

        def parse_typed_list(val_str):
            return None if val_str is None else [
                value_type(x) for x in list_option(expand(val_str))
            ]

        def parse_typed_item(val_str):
            return None if val_str is None else value_type(expand(val_str))

        # Handle the forthcoming conversions argparse will need to do by placing our parse hook - we
        # handle the conversions for env and config ourselves below.  Unlike the env and config
        # handling, `action='append'` does not need to be handled specially since appended flag values
        # come as single items' thus only `parse_typed_item` is ever needed for the flag value type
        # conversions.
        if is_fromfile:
            kwargs['type'] = parse_typed_item

        default, parse = ([], parse_typed_list) if action == 'append' else (
            None, parse_typed_item)
        config_val = parse(config_val_str)
        env_val = parse(env_val_str)
        hardcoded_val = kwargs.get('default')
        return RankedValue.choose(None, env_val, config_val, hardcoded_val,
                                  default)
Exemplo n.º 6
0
  def _compute_default(self, dest, kwargs):
    """Compute the default value to use for an option's registration.

    The source of the default value is chosen according to the ranking in RankedValue.
    """
    is_fromfile = kwargs.get('fromfile', False)
    action = kwargs.get('action')
    if is_fromfile and action:
      raise ParseError('Cannot fromfile {} with an action ({}) in scope {}'
                       .format(dest, action, self._scope))

    config_section = 'DEFAULT' if self._scope == GLOBAL_SCOPE else self._scope
    udest = dest.upper()
    if self._scope == GLOBAL_SCOPE:
      # For convenience, we allow three forms of env var for global scope options.
      # The fully-specified env var is PANTS_DEFAULT_FOO, which is uniform with PANTS_<SCOPE>_FOO
      # for all the other scopes.  However we also allow simply PANTS_FOO. And if the option name
      # itself starts with 'pants-' then we also allow simply FOO. E.g., PANTS_WORKDIR instead of
      # PANTS_PANTS_WORKDIR or PANTS_DEFAULT_PANTS_WORKDIR. We take the first specified value we
      # find, in this order: PANTS_DEFAULT_FOO, PANTS_FOO, FOO.
      env_vars = ['PANTS_DEFAULT_{0}'.format(udest), 'PANTS_{0}'.format(udest)]
      if udest.startswith('PANTS_'):
        env_vars.append(udest)
    else:
      sanitized_env_var_scope = self._ENV_SANITIZER_RE.sub('_', config_section.upper())
      env_vars = ['PANTS_{0}_{1}'.format(sanitized_env_var_scope, udest)]

    value_type = self.str_to_bool if is_boolean_flag(kwargs) else kwargs.get('type', str)

    env_val_str = None
    if self._env:
      for env_var in env_vars:
        if env_var in self._env:
          env_val_str = self._env.get(env_var)
          break

    def expand(val_str):
      if is_fromfile and val_str and val_str.startswith('@') and not val_str.startswith('@@'):
        fromfile = val_str[1:]
        try:
          with open(fromfile) as fp:
            return fp.read().strip()
        except IOError as e:
          raise self.FromfileError('Failed to read {} from file {}: {}'.format(dest, fromfile, e))
      else:
        # Support a literal @ for fromfile values via @@.
        return val_str[1:] if is_fromfile and val_str.startswith('@@') else val_str

    if is_fromfile:
      kwargs['type'] = lambda flag_val_str: value_type(expand(flag_val_str))  # Expand flag values.

    env_val = None if env_val_str is None else value_type(expand(env_val_str))  # Expand env values.

    config_val = None
    if action == 'append':
      config_val_strs = self._config.getlist(config_section, dest) if self._config else None
      if config_val_strs is not None:
        config_val = [value_type(config_val_str) for config_val_str in config_val_strs]
      default = []
    else:
      config_val_str = (self._config.get(config_section, dest, default=None)
                        if self._config else None)
      if config_val_str is not None:
        config_val = value_type(expand(config_val_str))  # Expand config values.
      default = None

    hardcoded_val = kwargs.get('default')  # We don't expand hard coded defaults.
    return RankedValue.choose(None, env_val, config_val, hardcoded_val, default)