summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Kögl <stefan@skoegl.net>2013-10-12 10:24:37 +0200
committerStefan Kögl <stefan@skoegl.net>2013-10-12 10:24:37 +0200
commitb607526f60b5b3bc35309058c6df5ecfc710f413 (patch)
treedf4a56b2a2cea52510d5a96e17476b3d1c8a420c
parent07812c7363de12736e76ba9f376e9eea72b99248 (diff)
downloadpython-json-pointer-b607526f60b5b3bc35309058c6df5ecfc710f413.tar.gz
add "jsonpointer" commandline utility
-rwxr-xr-xbin/jsonpointer46
-rw-r--r--doc/commandline.rst33
-rw-r--r--doc/index.rst1
-rw-r--r--setup.py5
4 files changed, 85 insertions, 0 deletions
diff --git a/bin/jsonpointer b/bin/jsonpointer
new file mode 100755
index 0000000..1d49fae
--- /dev/null
+++ b/bin/jsonpointer
@@ -0,0 +1,46 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+from __future__ import print_function
+
+import sys
+import os.path
+import json
+import jsonpointer
+import argparse
+
+
+parser = argparse.ArgumentParser(
+ description='Resolve a JSON pointer on JSON files')
+parser.add_argument('POINTER', type=argparse.FileType('r'),
+ help='File containing a JSON pointer expression')
+parser.add_argument('FILE', type=argparse.FileType('r'), nargs='+',
+ help='Files for which the pointer should be resolved')
+parser.add_argument('--indent', type=int, default=None,
+ help='Indent output by n spaces')
+parser.add_argument('-v', '--version', action='version',
+ version='%(prog)s ' + jsonpointer.__version__)
+
+
+def main():
+ try:
+ resolve_files()
+ except KeyboardInterrupt:
+ sys.exit(1)
+
+
+def resolve_files():
+ """ Resolve a JSON pointer on JSON files """
+ args = parser.parse_args()
+ ptr = json.load(args.POINTER)
+ for f in args.FILE:
+ doc = json.load(f)
+ try:
+ result = jsonpointer.resolve_pointer(doc, ptr)
+ print(json.dumps(result, indent=args.indent))
+ except jsonpointer.JsonPointerException as e:
+ print('Could not resolve pointer: %s' % str(e), file=sys.stderr)
+
+
+if __name__ == "__main__":
+ main()
diff --git a/doc/commandline.rst b/doc/commandline.rst
new file mode 100644
index 0000000..7678442
--- /dev/null
+++ b/doc/commandline.rst
@@ -0,0 +1,33 @@
+The ``jsonpointer`` commandline utility
+=======================================
+
+The JSON pointer package also installs a ``jsonpointer`` commandline utility
+that can be used to resolve a JSON pointers on JSON files.
+
+The program has the following usage ::
+
+ usage: jsonpointer [-h] [--indent INDENT] [-v] POINTER FILE [FILE ...]
+
+ Resolve a JSON pointer on JSON files
+
+ positional arguments:
+ POINTER File containing a JSON pointer expression
+ FILE Files for which the pointer should be resolved
+
+ optional arguments:
+ -h, --help show this help message and exit
+ --indent INDENT Indent output by n spaces
+ -v, --version show program's version number and exit
+
+
+The following shows example usage ::
+
+ $ cat a.json
+ { "a": [1, 2, 3] }
+ $ cat b.json
+ { "a": {"b": [1, 3, 4]}, "b": 1 }
+ $ cat ptr.json
+ "/a"
+ $ jsonpointer ptr.json a.json b.json
+ [1, 2, 3]
+ {"b": [1, 3, 4]}
diff --git a/doc/index.rst b/doc/index.rst
index 02c30c3..dbdf2c6 100644
--- a/doc/index.rst
+++ b/doc/index.rst
@@ -17,6 +17,7 @@ and PyPy are supported.
tutorial
mod-jsonpointer
+ commandline
RFC 6901 <http://tools.ietf.org/html/rfc6901>
diff --git a/setup.py b/setup.py
index c4b666b..ff95edf 100644
--- a/setup.py
+++ b/setup.py
@@ -33,4 +33,9 @@ setup(name=PACKAGE,
license=LICENSE,
url=WEBSITE,
py_modules=MODULES,
+ scripts=['bin/jsonpointer'],
+ entry_poimts = {
+ 'console_scripts': [
+ 'jsonpointer = jsonpointer:main',
+ ]},
)