summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrey Kislyuk <kislyuk@gmail.com>2017-01-17 19:05:30 -0800
committerAndrey Kislyuk <kislyuk@gmail.com>2017-01-17 19:05:30 -0800
commit98e95ea449733d0a558ab83788d2fd3abb73f081 (patch)
treef0ce30ea3bb17659f20a2031c7c96c4cf92cfc22
parentf211d1c466ef7b11bff2579a69b621809b6f2099 (diff)
-rw-r--r--README.rst10
-rwxr-xr-xscripts/yq2
-rwxr-xr-xsetup.py3
-rwxr-xr-xyq/__init__.py14
4 files changed, 21 insertions, 8 deletions
diff --git a/README.rst b/README.rst
index 4af39be..56ebfb8 100644
--- a/README.rst
+++ b/README.rst
@@ -1,5 +1,5 @@
-yq: Command-line YAML processor - jq wrapper for YAML documents
-===============================================================
+yq: Command-line YAML processor - jq and JMESPath wrapper for YAML documents
+============================================================================
Installation
------------
@@ -26,6 +26,12 @@ JSON output back into fancier-than-JSON YAML; the transformation is one-way only
unless there was an error in YAML parsing, in which case the exit code is 1. See the `jq manual
<https://stedolan.github.io/jq/manual/>`_ for more details on jq features and options.
+JMESPath mode
+-------------
+
+In addition to the default, jq wrapper mode, yq supports `JMESPath <http://jmespath.org/>`_ expressions via the
+``--jmespath PATH`` option.
+
.. admonition:: Compatibility note
This package's release series available on PyPI begins with version 2.0.0. Versions of ``yq`` prior to 2.0.0 are
diff --git a/scripts/yq b/scripts/yq
index b6672ba..81e501b 100755
--- a/scripts/yq
+++ b/scripts/yq
@@ -2,4 +2,4 @@
from yq import main
-main()
+exit(main())
diff --git a/setup.py b/setup.py
index 0406159..85d315d 100755
--- a/setup.py
+++ b/setup.py
@@ -16,7 +16,8 @@ setup(
long_description=open("README.rst").read(),
install_requires=[
"setuptools",
- "PyYAML >= 3.11"
+ "PyYAML >= 3.11",
+ "jmespath >= 0.9.0"
],
tests_require=tests_require,
extras_require={"test": tests_require},
diff --git a/yq/__init__.py b/yq/__init__.py
index 6c033cf..6657699 100755
--- a/yq/__init__.py
+++ b/yq/__init__.py
@@ -1,7 +1,7 @@
"""
yq: Command-line YAML processor - jq wrapper for YAML documents
-yq transcodes YAML documents to JSON and passes them to jq.
+yq transcodes YAML documents to JSON and passes them to jq or JMESPath.
See https://github.com/kislyuk/yq for more information.
"""
@@ -13,19 +13,25 @@ import yaml
class Parser(argparse.ArgumentParser):
def print_help(self):
yq_help = argparse.ArgumentParser.format_help(self).splitlines()
- print("\n".join(["usage: yq [options] <jq filter> [YAML file...]"] + yq_help[1:] + [""]))
+ print("\n".join(["usage: yq [options] <jq or jmespath filter>"] + yq_help[1:] + [""]))
try:
subprocess.check_call(["jq", "--help"])
except:
pass
parser = Parser(description=__doc__, formatter_class=argparse.RawTextHelpFormatter)
+parser.add_argument("--jmespath", help="Use jmespath with this expression instead of launching the yq subprocess")
parser.add_argument("jq_args", nargs=argparse.REMAINDER)
def main(args=None):
args = parser.parse_args(args=args)
if sys.stdin.isatty():
- return parser.print_help()
+ parser.print_help()
+ return os.EX_OK
+ if args.jmespath is not None:
+ import jmespath
+ print(jmespath.search(args.jmespath, yaml.safe_load(sys.stdin)))
+ return os.EX_OK
try:
# Note: universal_newlines is just a way to induce subprocess to make stdin a text buffer and encode it for us
jq = subprocess.Popen(['jq'] + args.jq_args, stdin=subprocess.PIPE, universal_newlines=True)
@@ -35,6 +41,6 @@ def main(args=None):
json.dump(yaml.safe_load(sys.stdin), jq.stdin)
jq.stdin.close()
jq.wait()
- exit(jq.returncode)
+ return jq.returncode
except Exception as e:
parser.exit("yq: Error while running jq: {}: {}.".format(type(e).__name__, e))