OLD | NEW |
1 # Copyright (C) 2013 Google Inc. All rights reserved. | 1 # Copyright (C) 2013 Google Inc. All rights reserved. |
2 # | 2 # |
3 # Redistribution and use in source and binary forms, with or without | 3 # Redistribution and use in source and binary forms, with or without |
4 # modification, are permitted provided that the following conditions are | 4 # modification, are permitted provided that the following conditions are |
5 # met: | 5 # met: |
6 # | 6 # |
7 # * Redistributions of source code must retain the above copyright | 7 # * Redistributions of source code must retain the above copyright |
8 # notice, this list of conditions and the following disclaimer. | 8 # notice, this list of conditions and the following disclaimer. |
9 # * Redistributions in binary form must reproduce the above | 9 # * Redistributions in binary form must reproduce the above |
10 # copyright notice, this list of conditions and the following disclaimer | 10 # copyright notice, this list of conditions and the following disclaimer |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 lines += in_file.readlines() | 68 lines += in_file.readlines() |
69 return InFile(lines, defaults, valid_values, default_parameters) | 69 return InFile(lines, defaults, valid_values, default_parameters) |
70 | 70 |
71 def _is_sequence(self, arg): | 71 def _is_sequence(self, arg): |
72 return (not hasattr(arg, "strip") | 72 return (not hasattr(arg, "strip") |
73 and hasattr(arg, "__getitem__") | 73 and hasattr(arg, "__getitem__") |
74 or hasattr(arg, "__iter__")) | 74 or hasattr(arg, "__iter__")) |
75 | 75 |
76 def _parse(self, lines): | 76 def _parse(self, lines): |
77 parsing_parameters = True | 77 parsing_parameters = True |
| 78 indices = {} |
78 for line in lines: | 79 for line in lines: |
79 if _is_comment(line): | 80 if _is_comment(line): |
80 continue | 81 continue |
81 if not line: | 82 if not line: |
82 parsing_parameters = False | 83 parsing_parameters = False |
83 continue | 84 continue |
84 if parsing_parameters: | 85 if parsing_parameters: |
85 self._parse_parameter(line) | 86 self._parse_parameter(line) |
86 else: | 87 else: |
87 self.name_dictionaries.append(self._parse_line(line)) | 88 entry = self._parse_line(line) |
| 89 name = entry['name'] |
| 90 if name in indices: |
| 91 entry = self._merge_entries(entry, self.name_dictionaries[in
dices[name]]) |
| 92 entry['name'] = name |
| 93 self.name_dictionaries[indices[name]] = entry |
| 94 else: |
| 95 indices[name] = len(self.name_dictionaries) |
| 96 self.name_dictionaries.append(entry) |
| 97 |
| 98 |
| 99 def _merge_entries(self, one, two): |
| 100 merged = {} |
| 101 for key in one: |
| 102 if key not in two: |
| 103 self._fatal("Expected key '%s' not found in entry: %s" % (key, t
wo)) |
| 104 if one[key] and two[key]: |
| 105 val_one = one[key] |
| 106 val_two = two[key] |
| 107 if isinstance(val_one, list) and isinstance(val_two, list): |
| 108 val = val_one + val_two |
| 109 elif isinstance(val_one, list): |
| 110 val = val_one + [val_two] |
| 111 elif isinstance(val_two, list): |
| 112 val = [val_one] + val_two |
| 113 else: |
| 114 val = [val_one, val_two] |
| 115 merged[key] = val |
| 116 elif one[key]: |
| 117 merged[key] = one[key] |
| 118 else: |
| 119 merged[key] = two[key] |
| 120 return merged |
| 121 |
88 | 122 |
89 def _parse_parameter(self, line): | 123 def _parse_parameter(self, line): |
90 if '=' in line: | 124 if '=' in line: |
91 name, value = line.split('=') | 125 name, value = line.split('=') |
92 else: | 126 else: |
93 name, value = line, True | 127 name, value = line, True |
94 if not name in self.parameters: | 128 if not name in self.parameters: |
95 self._fatal("Unknown parameter: '%s' in line:\n%s\nKnown parameters:
%s" % (name, line, self.parameters.keys())) | 129 self._fatal("Unknown parameter: '%s' in line:\n%s\nKnown parameters:
%s" % (name, line, self.parameters.keys())) |
96 self.parameters[name] = value | 130 self.parameters[name] = value |
97 | 131 |
(...skipping 19 matching lines...) Expand all Loading... |
117 if self._is_sequence(args[arg_name]): | 151 if self._is_sequence(args[arg_name]): |
118 args[arg_name].append(arg_value) | 152 args[arg_name].append(arg_value) |
119 else: | 153 else: |
120 args[arg_name] = arg_value | 154 args[arg_name] = arg_value |
121 return args | 155 return args |
122 | 156 |
123 def _fatal(self, message): | 157 def _fatal(self, message): |
124 # FIXME: This should probably raise instead of exit(1) | 158 # FIXME: This should probably raise instead of exit(1) |
125 print message | 159 print message |
126 exit(1) | 160 exit(1) |
OLD | NEW |