Index: chrome/browser/resources/chromeos/chromevox/tools/merge_manifests.py |
diff --git a/chrome/browser/resources/chromeos/chromevox/tools/merge_manifests.py b/chrome/browser/resources/chromeos/chromevox/tools/merge_manifests.py |
new file mode 100755 |
index 0000000000000000000000000000000000000000..633d3fbda3e40dd762dc37d1359a36ebe322f3a3 |
--- /dev/null |
+++ b/chrome/browser/resources/chromeos/chromevox/tools/merge_manifests.py |
@@ -0,0 +1,53 @@ |
+#!/usr/bin/env python |
+ |
+# Copyright 2014 The Chromium Authors. All rights reserved. |
+# Use of this source code is governed by a BSD-style license that can be |
+# found in the LICENSE file. |
+ |
+import json |
Peter Lundblad
2014/05/22 00:28:25
Care to provide a minimal docstring?
David Tseng
2014/05/22 02:42:04
Done.
|
+import optparse |
+import sys |
+ |
+def MergeManifests(base, override, output): |
+ ''' Takes two extension manifests and merges them into an output manifest. |
Peter Lundblad
2014/05/22 00:28:25
Nit: space after '''
David Tseng
2014/05/22 02:42:04
Remove it?
+ ''' Takes two extension manifests an
|
+ |
+ Args: |
+ base: path to a extension manifest. |
+ override: path to a extension manifest. |
+ output: path to the result of merging base and override. |
+ ''' |
+ base_file = open(base) |
+ override_file = open(override) |
Peter Lundblad
2014/05/22 00:28:25
Consider using with to have these automatically cl
David Tseng
2014/05/22 02:42:04
Done. (Nice construct).
|
+ base_manifest = json.load(base_file) |
+ override_manifest = json.load(override_file) |
+ MergeDicts(base_manifest, override_manifest) |
+ json.dump(base_manifest, open(output, 'w'), sort_keys=True, indent=2) |
Peter Lundblad
2014/05/22 00:28:25
Can you put the open call out of line and close th
David Tseng
2014/05/22 02:42:04
Done.
|
+ |
+ |
+def MergeDicts(base, override): |
+ '''Merges override into base. |
+ |
+ Args: |
+ base: dictionary with base key values. |
+ override: dictionary with key values to insert (and possibly overwrite) in |
+ base. |
+ ''' |
+ for key in override: |
+ if type(override[key]) is dict: |
+ MergeDicts(base, override[key]) |
Peter Lundblad
2014/05/22 00:28:25
Shouldn't you merge override into base[key], creat
David Tseng
2014/05/22 02:42:04
yYup; goofed on this case; also handle the case wh
|
+ else: |
+ base[key] = override[key] |
+ |
+ |
+def main(): |
+ parser = optparse.OptionParser(description=__doc__) |
+ parser.usage = ('%prog [options] <base_manifest_path>' |
+ ' <override_manifest_path> <output_manifest_path>') |
Peter Lundblad
2014/05/22 00:28:25
I recommend making the out file an -o option to be
|
+ _, args = parser.parse_args() |
+ if len(args) != 3: |
+ print >>sys.stderr, 'Expected exactly three arguments' |
+ sys.exit(1) |
+ MergeManifests(args[0], args[1], args[2]) |
+ |
+if __name__ == '__main__': |
+ main() |