Exemple #1
0
def create_config(sample_ini='', defaults=None):
  """Creates a ``Config`` from the ``sample_ini`` file contents.

  :param string sample_ini: The contents of the ini file containing the config values.
  :param dict defaults: An optional dict of global default ini values to seed.
  """
  if not isinstance(sample_ini, Compatibility.string):
    raise ValueError('The sample_ini supplied must be a string, given: %s' % sample_ini)

  parser = Config.create_parser(defaults)
  with io.BytesIO(sample_ini) as ini:
    parser.readfp(ini)
  return Config(parser)
Exemple #2
0
def create_config(sample_ini='', defaults=None):
    """Creates a ``Config`` from the ``sample_ini`` file contents.

  :param string sample_ini: The contents of the ini file containing the config values.
  :param dict defaults: An optional dict of global default ini values to seed.
  """
    if not isinstance(sample_ini, Compatibility.string):
        raise ValueError(
            'The sample_ini supplied must be a string, given: %s' % sample_ini)

    parser = Config.create_parser(defaults)
    with io.BytesIO(sample_ini) as ini:
        parser.readfp(ini)
    return Config(parser)
Exemple #3
0
    def apply_defaults(self, commands, args):
        """
      Augments the given list of arguments with any default options found for the given commands.
      The returned argments will be a new copy of the given args with possibly extra augmented
      arguments.

      Default options are applied from the following keys under a section with the name of the
      subcommand the default options apply to:
      'options': These options are either prepended or appended to the command line args as
                 specified in the constructor with default_prepend.
      'prepend-options': These options are prepended to the command line args.
      'append-options': These options are appended to the command line args.
    """

        args = args[:]

        if RcFile._DISABLE_PANTS_RC_OPTION in args:
            return args

        config = Config.create_parser()
        read_from = config.read(self.paths)
        if not read_from:
            log.debug('no rcfile found')
            return args

        log.debug('using rcfiles: %s to modify args' % ','.join(read_from))

        def get_rcopts(command, key):
            return config.get(command, key).split() if config.has_option(
                command, key) else []

        commands = list(commands)
        if self.process_default:
            commands.insert(0, Config.DEFAULT_SECTION)

        for cmd in commands:
            opts = get_rcopts(cmd, 'options')
            args = (opts + args) if self.default_prepend else (args + opts)
            args = get_rcopts(cmd, 'prepend-options') + args + get_rcopts(
                cmd, 'append-options')
        return args
Exemple #4
0
  def apply_defaults(self, commands, args):
    """Augment arguments with defaults found for the given commands.

    The returned arguments will be a new copy of the given args with possibly extra augmented
    arguments.

    Default options are applied from the following keys under a section with the name of the
    sub-command the default options apply to:

    * `options` - These options are either prepended or appended to the command line args as
      specified in the constructor with default_prepend.
    * `prepend-options` - These options are prepended to the command line args.
    * `append-options` - These options are appended to the command line args.
    """

    args = args[:]

    if RcFile._DISABLE_PANTS_RC_OPTION in args:
      return args

    config = Config.create_parser()
    read_from = config.read(self.paths)
    if not read_from:
      log.debug('no rcfile found')
      return args

    log.debug('using rcfiles: %s to modify args' % ','.join(read_from))

    def get_rcopts(command, key):
      return config.get(command, key).split() if config.has_option(command, key) else []

    commands = list(commands)
    if self.process_default:
      commands.insert(0, Config.DEFAULT_SECTION)

    for cmd in commands:
      opts = get_rcopts(cmd, 'options')
      args = (opts + args) if self.default_prepend else (args + opts)
      args = get_rcopts(cmd, 'prepend-options') + args + get_rcopts(cmd, 'append-options')
    return args