summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Kögl <stefan@skoegl.net>2013-10-11 20:43:20 +0200
committerStefan Kögl <stefan@skoegl.net>2013-10-11 20:43:20 +0200
commit9290130d30fbd4ef8ecc4c313a10dbfc37276b93 (patch)
tree0f8a78a4198d6c928602c43623810c6a09b16a10
parent3b841b3ffc54f4e1ea65320c80dcc4ce0b139a36 (diff)
downloadpython-json-patch-9290130d30fbd4ef8ecc4c313a10dbfc37276b93.tar.gz
add "jsonpatch" commandline utility
-rwxr-xr-xbin/jsonpatch42
-rw-r--r--setup.py3
2 files changed, 44 insertions, 1 deletions
diff --git a/bin/jsonpatch b/bin/jsonpatch
new file mode 100755
index 0000000..20d5e35
--- /dev/null
+++ b/bin/jsonpatch
@@ -0,0 +1,42 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+from __future__ import print_function
+
+import sys
+import os.path
+import json
+import jsonpatch
+import argparse
+
+
+parser = argparse.ArgumentParser(
+ description='Apply a JSON patch on a JSON files')
+parser.add_argument('ORIGINAL', type=argparse.FileType('r'),
+ help='Original file')
+parser.add_argument('PATCH', type=argparse.FileType('r'),
+ help='Patch file')
+parser.add_argument('--indent', type=int, default=None,
+ help='Indent output by n spaces')
+parser.add_argument('-v', '--version', action='version',
+ version='%(prog)s ' + jsonpatch.__version__)
+
+
+def main():
+ try:
+ patch_files()
+ except KeyboardInterrupt:
+ sys.exit(1)
+
+
+def patch_files():
+ """ Diffs two JSON files and prints a patch """
+ args = parser.parse_args()
+ doc = json.load(args.ORIGINAL)
+ patch = json.load(args.PATCH)
+ result = jsonpatch.apply_patch(doc, patch)
+ print(json.dumps(result, indent=args.indent))
+
+
+if __name__ == "__main__":
+ main()
diff --git a/setup.py b/setup.py
index 0295377..20fa4b7 100644
--- a/setup.py
+++ b/setup.py
@@ -52,10 +52,11 @@ setup(name=PACKAGE,
url=WEBSITE,
py_modules=MODULES,
package_data={'': ['requirements.txt']},
- scripts=['bin/jsondiff'],
+ scripts=['bin/jsondiff', 'bin/jsonpatch'],
entry_poimts = {
'console_scripts': [
'jsondiff = jsondiff:main',
+ 'jsonpatch = jsonpatch:main',
]},
**OPTIONS
)