OLD | NEW |
1 # Copyright (c) 2017 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2017 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Generic generator for configuration files in JSON5 format. | 5 """Generic generator for configuration files in JSON5 format. |
6 | 6 |
7 The configuration file is expected to contain either a data array or a data map, | 7 The configuration file is expected to contain either a data array or a data map, |
8 an optional parameters validation map, and an optional metdata map. Examples: | 8 an optional parameters validation map, and an optional metdata map. Examples: |
9 { | 9 { |
10 data: [ | 10 data: [ |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 if key not in self.parameters: | 157 if key not in self.parameters: |
158 raise Exception( | 158 raise Exception( |
159 "Unknown parameter: '%s'\nKnown params: %s" % | 159 "Unknown parameter: '%s'\nKnown params: %s" % |
160 (key, self.parameters.keys())) | 160 (key, self.parameters.keys())) |
161 if self.parameters[key]: | 161 if self.parameters[key]: |
162 self._validate_parameter(self.parameters[key], value) | 162 self._validate_parameter(self.parameters[key], value) |
163 entry[key] = value | 163 entry[key] = value |
164 return entry | 164 return entry |
165 | 165 |
166 def _validate_parameter(self, parameter, value): | 166 def _validate_parameter(self, parameter, value): |
167 valid_values = parameter.get("valid_values") | |
168 if valid_values and value not in valid_values: | |
169 raise Exception("Unknown value: '%s'\nKnown values: %s" % | |
170 (value, valid_values)) | |
171 valid_type = parameter.get("valid_type") | 167 valid_type = parameter.get("valid_type") |
172 if valid_type and type(value).__name__ != valid_type: | 168 if valid_type and type(value).__name__ != valid_type: |
173 raise Exception("Incorrect type: '%s'\nExpected type: %s" % | 169 raise Exception("Incorrect type: '%s'\nExpected type: %s" % |
174 (type(value).__name__, valid_type)) | 170 (type(value).__name__, valid_type)) |
| 171 valid_values = parameter.get("valid_values") |
| 172 if not valid_values: |
| 173 return |
| 174 # If valid_values is a list of simple items and not list of list, then |
| 175 # validate each item in the value list against valid_values. |
| 176 if valid_type == "list" and type(valid_values[0]) is not list: |
| 177 for item in value: |
| 178 if item not in valid_values: |
| 179 raise Exception("Unknown value: '%s'\nKnown values: %s" % |
| 180 (item, valid_values)) |
| 181 elif value not in valid_values: |
| 182 raise Exception("Unknown value: '%s'\nKnown values: %s" % |
| 183 (value, valid_values)) |
175 | 184 |
176 | 185 |
177 class Writer(object): | 186 class Writer(object): |
178 # Subclasses should override. | 187 # Subclasses should override. |
179 class_name = None | 188 class_name = None |
180 default_metadata = None | 189 default_metadata = None |
181 default_parameters = None | 190 default_parameters = None |
182 | 191 |
183 def __init__(self, json5_files): | 192 def __init__(self, json5_files): |
184 self._outputs = {} # file_name -> generator | 193 self._outputs = {} # file_name -> generator |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
228 parser.add_argument("--developer_dir", help="Path to Xcode.") | 237 parser.add_argument("--developer_dir", help="Path to Xcode.") |
229 parser.add_argument("--output_dir", default=os.getcwd()) | 238 parser.add_argument("--output_dir", default=os.getcwd()) |
230 args = parser.parse_args() | 239 args = parser.parse_args() |
231 | 240 |
232 if args.developer_dir: | 241 if args.developer_dir: |
233 os.environ["DEVELOPER_DIR"] = args.developer_dir | 242 os.environ["DEVELOPER_DIR"] = args.developer_dir |
234 | 243 |
235 writer = self._writer_class(args.files) | 244 writer = self._writer_class(args.files) |
236 writer.set_gperf_path(args.gperf) | 245 writer.set_gperf_path(args.gperf) |
237 writer.write_files(args.output_dir) | 246 writer.write_files(args.output_dir) |
OLD | NEW |