diff options
author | Matt Behrens <matt@zigg.com> | 2013-12-12 11:48:07 -0500 |
---|---|---|
committer | Matt Behrens <matt@zigg.com> | 2013-12-12 11:48:07 -0500 |
commit | c06e7871b849ae7486656b14331eb7fc32d44396 (patch) | |
tree | ea0d5bd13f57b9c6d707e92d58e4e32ad119b536 /bin/jsonpatch | |
parent | 48f9adb8991deb62ba4f327d5e96766c4d0d55e7 (diff) | |
download | python-json-patch-c06e7871b849ae7486656b14331eb7fc32d44396.tar.gz |
add in-place patching and patch read from stdin. enables use of
jsonpatch in a shell script like so:
cat <<EOT | jsonpatch -i a.json
[{"path": "/foo", "value": "baz", "op": "replace"}]
EOT
which will edit a.json in-place and leave a backup in a.json.orig.
Diffstat (limited to 'bin/jsonpatch')
-rwxr-xr-x | bin/jsonpatch | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/bin/jsonpatch b/bin/jsonpatch index 8db17a1..613f703 100755 --- a/bin/jsonpatch +++ b/bin/jsonpatch @@ -1,12 +1,11 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -from __future__ import print_function - import sys import os.path import json import jsonpatch +import tempfile import argparse @@ -15,9 +14,12 @@ parser = argparse.ArgumentParser( parser.add_argument('ORIGINAL', type=argparse.FileType('r'), help='Original file') parser.add_argument('PATCH', type=argparse.FileType('r'), - help='Patch file') + nargs='?', default=sys.stdin, + help='Patch file (read from stdin if omitted)') parser.add_argument('--indent', type=int, default=None, help='Indent output by n spaces') +parser.add_argument('-i', '--in-place', action='store_true', + help='Modify ORIGINAL in-place instead of to stdout') parser.add_argument('-v', '--version', action='version', version='%(prog)s ' + jsonpatch.__version__) @@ -35,7 +37,18 @@ def patch_files(): doc = json.load(args.ORIGINAL) patch = json.load(args.PATCH) result = jsonpatch.apply_patch(doc, patch) - print(json.dumps(result, indent=args.indent)) + if args.in_place: + dirname = os.path.abspath(os.path.dirname(args.ORIGINAL.name)) + fd, pathname = tempfile.mkstemp(dir=dirname) + fp = os.fdopen(fd, 'w') + else: + fp = sys.stdout + json.dump(result, fp, indent=args.indent) + if args.in_place: + fp.close() + os.link(args.ORIGINAL.name, args.ORIGINAL.name + '.orig') + os.chmod(pathname, os.stat(args.ORIGINAL.name).st_mode) + os.rename(pathname, args.ORIGINAL.name) if __name__ == "__main__": |