| 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 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 class Json5File(object): | 92 class Json5File(object): |
| 93 def __init__(self, file_paths, doc, default_metadata=None, default_parameter
s=None): | 93 def __init__(self, file_paths, doc, default_metadata=None, default_parameter
s=None): |
| 94 self.file_paths = file_paths | 94 self.file_paths = file_paths |
| 95 self.name_dictionaries = [] | 95 self.name_dictionaries = [] |
| 96 self.metadata = copy.deepcopy(default_metadata if default_metadata else
{}) | 96 self.metadata = copy.deepcopy(default_metadata if default_metadata else
{}) |
| 97 self.parameters = copy.deepcopy(default_parameters if default_parameters
else {}) | 97 self.parameters = copy.deepcopy(default_parameters if default_parameters
else {}) |
| 98 self._defaults = {} | 98 self._defaults = {} |
| 99 self._process(doc) | 99 self._process(doc) |
| 100 | 100 |
| 101 @classmethod | 101 @classmethod |
| 102 def load_from_files(cls, file_paths, default_metadata, default_parameters=No
ne): | 102 def load_from_files(cls, file_paths, default_metadata=None, default_paramete
rs=None): |
| 103 merged_doc = dict() | 103 merged_doc = dict() |
| 104 for path in file_paths: | 104 for path in file_paths: |
| 105 assert path.endswith(".json5") | 105 assert path.endswith(".json5") |
| 106 with open(os.path.abspath(path)) as json5_file: | 106 with open(os.path.abspath(path)) as json5_file: |
| 107 doc = _json5_load(json5_file.read()) | 107 doc = _json5_load(json5_file.read()) |
| 108 if not merged_doc: | 108 if not merged_doc: |
| 109 merged_doc = doc | 109 merged_doc = doc |
| 110 else: | 110 else: |
| 111 _merge_doc(merged_doc, doc) | 111 _merge_doc(merged_doc, doc) |
| 112 return Json5File(file_paths, merged_doc, default_metadata, default_param
eters) | 112 return Json5File(file_paths, merged_doc, default_metadata, default_param
eters) |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 | 191 |
| 192 class Writer(object): | 192 class Writer(object): |
| 193 # Subclasses should override. | 193 # Subclasses should override. |
| 194 class_name = None | 194 class_name = None |
| 195 default_metadata = None | 195 default_metadata = None |
| 196 default_parameters = None | 196 default_parameters = None |
| 197 | 197 |
| 198 def __init__(self, json5_files): | 198 def __init__(self, json5_files): |
| 199 self._outputs = {} # file_name -> generator | 199 self._outputs = {} # file_name -> generator |
| 200 self.gperf_path = None | 200 self.gperf_path = None |
| 201 if isinstance(json5_files, basestring): | |
| 202 json5_files = [json5_files] | |
| 203 if json5_files: | 201 if json5_files: |
| 204 self.json5_file = Json5File.load_from_files(json5_files, | 202 self.json5_file = Json5File.load_from_files(json5_files, |
| 205 self.default_metadata, | 203 self.default_metadata, |
| 206 self.default_parameters) | 204 self.default_parameters) |
| 207 | 205 |
| 208 def _write_file_if_changed(self, output_dir, contents, file_name): | 206 def _write_file_if_changed(self, output_dir, contents, file_name): |
| 209 path = os.path.join(output_dir, file_name) | 207 path = os.path.join(output_dir, file_name) |
| 210 | 208 |
| 211 # The build system should ensure our output directory exists, but just | 209 # The build system should ensure our output directory exists, but just |
| 212 # in case. | 210 # in case. |
| (...skipping 30 matching lines...) Expand all Loading... |
| 243 parser.add_argument("--developer_dir", help="Path to Xcode.") | 241 parser.add_argument("--developer_dir", help="Path to Xcode.") |
| 244 parser.add_argument("--output_dir", default=os.getcwd()) | 242 parser.add_argument("--output_dir", default=os.getcwd()) |
| 245 args = parser.parse_args() | 243 args = parser.parse_args() |
| 246 | 244 |
| 247 if args.developer_dir: | 245 if args.developer_dir: |
| 248 os.environ["DEVELOPER_DIR"] = args.developer_dir | 246 os.environ["DEVELOPER_DIR"] = args.developer_dir |
| 249 | 247 |
| 250 writer = self._writer_class(args.files) | 248 writer = self._writer_class(args.files) |
| 251 writer.set_gperf_path(args.gperf) | 249 writer.set_gperf_path(args.gperf) |
| 252 writer.write_files(args.output_dir) | 250 writer.write_files(args.output_dir) |
| OLD | NEW |