Example #1
0
  def testStripLocations(self):
    lv_data = yaml.load("""\
        null: null
        boolean: true
        integer: 123
        float: 3.14
        string: abc
        dict:
          xyz: 789
          abc: 123
        list:
        - a
        - 2
    """, location_value=True)
    data = yaml.strip_locations(lv_data)

    self.assertEqual(None, data['null'])
    self.assertEqual(True, data['boolean'])
    self.assertEqual(123, data['integer'])
    self.assertEqual(3.14, data['float'])
    self.assertEqual('abc', data['string'])
    self.assertIsInstance(data['dict'], dict)
    self.assertIsInstance(data['list'], list)
Example #2
0
def _AddFlagsFileFlags(inject, flags_file, parent_locations=None):
    """Recursively append the flags file flags to inject."""

    flag = calliope_base.FLAGS_FILE_FLAG.name

    if parent_locations and parent_locations.FileInStack(flags_file):
        raise parser_errors.ArgumentError(
            '{} recursive reference ({}).'.format(flag, parent_locations))

    # Load the YAML flag:value dict or list of dicts. List of dicts allows
    # flags to be specified more than once.

    if flags_file == '-':
        contents = sys.stdin.read()
    elif not os.path.exists(flags_file):
        raise parser_errors.ArgumentError('{} [{}] not found.'.format(
            flag, flags_file))
    else:
        contents = files.ReadFileContents(flags_file)
    data = yaml.load(contents, location_value=True)
    group = data if isinstance(data, list) else [data]

    # Generate the list of args to inject.

    for member in group:

        if not isinstance(member.value, dict):
            raise parser_errors.ArgumentError(
                '{}:{}: {} file must contain a dictionary or list of dictionaries '
                'of flags.'.format(flags_file, member.lc.line + 1, flag))

        for arg, obj in six.iteritems(member.value):

            line_col = obj.lc
            value = yaml.strip_locations(obj)

            if arg == flag:
                # The flags-file YAML arg value can be a path or list of paths.
                file_list = obj.value if isinstance(obj.value,
                                                    list) else [obj.value]
                for path in file_list:
                    locations = _ArgLocations(arg, flags_file, line_col,
                                              parent_locations)
                    _AddFlagsFileFlags(inject, path, locations)
                continue
            if isinstance(value, (type(None), bool)):
                separate_value_arg = False
            elif isinstance(value, (list, dict)):
                separate_value_arg = True
            else:
                separate_value_arg = False
                arg = '{}={}'.format(arg, value)
            inject.append(_FLAG_FILE_LINE_NAME)
            inject.append(
                _ArgLocations(arg, flags_file, line_col, parent_locations))
            inject.append(arg)
            if separate_value_arg:
                # Add the already lexed arg and with one swoop we sidestep all flag
                # value and command line interpreter quoting issues. The ArgList and
                # ArgDict arg parsers have been adjusted to handle this.
                inject.append(value)