blob: 5132aa8f6ae45f81c14b5e220a336030a77ca91c [file] [log] [blame]
Jooyung Han04329f12019-08-01 23:35:08 +09001#!/usr/bin/env python
2#
3# Copyright (C) 2019 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17
18import argparse
19import collections
20import json
21import sys
22
23def follow_path(obj, path):
24 cur = obj
25 last_key = None
26 for key in path.split('.'):
27 if last_key:
28 if last_key not in cur:
29 return None,None
30 cur = cur[last_key]
31 last_key = key
32 if last_key not in cur:
33 return None,None
34 return cur, last_key
35
36
37def ensure_path(obj, path):
38 cur = obj
39 last_key = None
40 for key in path.split('.'):
41 if last_key:
42 if last_key not in cur:
43 cur[last_key] = dict()
44 cur = cur[last_key]
45 last_key = key
46 return cur, last_key
47
48
49class SetValue(str):
50 def apply(self, obj, val):
51 cur, key = ensure_path(obj, self)
52 cur[key] = val
53
54
55class Replace(str):
56 def apply(self, obj, val):
57 cur, key = follow_path(obj, self)
58 if cur:
59 cur[key] = val
60
61
62class Remove(str):
63 def apply(self, obj):
64 cur, key = follow_path(obj, self)
65 if cur:
66 del cur[key]
67
68
69class AppendList(str):
70 def apply(self, obj, *args):
71 cur, key = ensure_path(obj, self)
72 if key not in cur:
73 cur[key] = list()
74 if not isinstance(cur[key], list):
75 raise ValueError(self + " should be a array.")
76 cur[key].extend(args)
77
Wei Li18b7a2e2022-07-08 19:09:06 -070078# A JSONDecoder that supports line comments start with //
79class JSONWithCommentsDecoder(json.JSONDecoder):
80 def __init__(self, **kw):
81 super().__init__(**kw)
82
83 def decode(self, s: str):
84 s = '\n'.join(l for l in s.split('\n') if not l.lstrip(' ').startswith('//'))
85 return super().decode(s)
Jooyung Han04329f12019-08-01 23:35:08 +090086
87def main():
88 parser = argparse.ArgumentParser()
89 parser.add_argument('-o', '--out',
90 help='write result to a file. If omitted, print to stdout',
91 metavar='output',
92 action='store')
93 parser.add_argument('input', nargs='?', help='JSON file')
94 parser.add_argument("-v", "--value", type=SetValue,
95 help='set value of the key specified by path. If path doesn\'t exist, creates new one.',
96 metavar=('path', 'value'),
97 nargs=2, dest='patch', default=[], action='append')
98 parser.add_argument("-s", "--replace", type=Replace,
99 help='replace value of the key specified by path. If path doesn\'t exist, no op.',
100 metavar=('path', 'value'),
101 nargs=2, dest='patch', action='append')
102 parser.add_argument("-r", "--remove", type=Remove,
103 help='remove the key specified by path. If path doesn\'t exist, no op.',
104 metavar='path',
105 nargs=1, dest='patch', action='append')
106 parser.add_argument("-a", "--append_list", type=AppendList,
107 help='append values to the list specified by path. If path doesn\'t exist, creates new list for it.',
108 metavar=('path', 'value'),
109 nargs='+', dest='patch', default=[], action='append')
110 args = parser.parse_args()
111
112 if args.input:
113 with open(args.input) as f:
Wei Li18b7a2e2022-07-08 19:09:06 -0700114 obj = json.load(f, object_pairs_hook=collections.OrderedDict, cls=JSONWithCommentsDecoder)
Jooyung Han04329f12019-08-01 23:35:08 +0900115 else:
Wei Li18b7a2e2022-07-08 19:09:06 -0700116 obj = json.load(sys.stdin, object_pairs_hook=collections.OrderedDict, cls=JSONWithCommentsDecoder)
Jooyung Han04329f12019-08-01 23:35:08 +0900117
118 for p in args.patch:
119 p[0].apply(obj, *p[1:])
120
121 if args.out:
122 with open(args.out, "w") as f:
Martin Stjernholm37fa32c2020-02-24 15:52:31 +0000123 json.dump(obj, f, indent=2, separators=(',', ': '))
124 f.write('\n')
Jooyung Han04329f12019-08-01 23:35:08 +0900125 else:
Martin Stjernholm37fa32c2020-02-24 15:52:31 +0000126 print(json.dumps(obj, indent=2, separators=(',', ': ')))
Jooyung Han04329f12019-08-01 23:35:08 +0900127
128
129if __name__ == '__main__':
130 main()