summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrey Kislyuk <kislyuk@gmail.com>2018-02-10 10:10:28 -0800
committerAndrey Kislyuk <kislyuk@gmail.com>2018-02-10 10:10:28 -0800
commit191c524800ea3934da198761bad700c0d201aab8 (patch)
tree7ee0b6031a19dd8eab87436c27e6bebccca4aa46
parentc3559f9109dcf5ab5989eae909232a80eaaf28f4 (diff)
Raise understandable error on non-dict conversion failure
-rw-r--r--README.rst3
-rw-r--r--setup.cfg2
-rwxr-xr-xsetup.py2
-rwxr-xr-xyq/__init__.py8
4 files changed, 9 insertions, 6 deletions
diff --git a/README.rst b/README.rst
index 67be9c7..b9cde75 100644
--- a/README.rst
+++ b/README.rst
@@ -39,7 +39,8 @@ XML support
``yq`` also supports XML. The ``yq`` package installs an executable, ``xq``, which
`transcodes XML to JSON <https://www.xml.com/pub/a/2006/05/31/converting-between-xml-and-json.html>`_ using
`xmltodict <https://github.com/martinblech/xmltodict>`_ and pipes it to ``jq``. Roundtrip transcoding is available with
-the ``xq -x`` option. Multiple XML documents can be passed in separate files/streams as ``xq a.xml b.xml``.
+the ``xq --xml-output``/``xq -x`` option. Multiple XML documents can be passed in separate files/streams as
+``xq a.xml b.xml``.
.. admonition:: Compatibility note
diff --git a/setup.cfg b/setup.cfg
index 46f9d22..47be384 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -2,4 +2,4 @@
universal=1
[flake8]
max-line-length=120
-ignore: E301, E302, E305, E401, E261, E265, E226, F401, E501
+ignore: E302, E305, E401
diff --git a/setup.py b/setup.py
index 2ab5bf3..a0ead87 100755
--- a/setup.py
+++ b/setup.py
@@ -1,6 +1,6 @@
#!/usr/bin/env python
-import os, glob
+import glob
from setuptools import setup, find_packages
tests_require = ["coverage", "flake8", "wheel"]
diff --git a/yq/__init__.py b/yq/__init__.py
index 88e45c5..acc5cdb 100755
--- a/yq/__init__.py
+++ b/yq/__init__.py
@@ -46,7 +46,7 @@ def represent_dict_order(dumper, data):
def decode_docs(jq_output, json_decoder):
while jq_output:
doc, pos = json_decoder.raw_decode(jq_output)
- jq_output = jq_output[pos+1:]
+ jq_output = jq_output[pos + 1:]
yield doc
OrderedLoader.add_constructor(yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, construct_mapping)
@@ -116,11 +116,13 @@ def main(args=None, input_format="yaml"):
jq_out, jq_err = jq.communicate(input_payload)
json_decoder = json.JSONDecoder(object_pairs_hook=OrderedDict)
if args.yaml_output:
- yaml.dump_all(decode_docs(jq_out, json_decoder), stream=sys.stdout, Dumper=OrderedDumper, width=args.width,
- allow_unicode=True, default_flow_style=False)
+ yaml.dump_all(decode_docs(jq_out, json_decoder), stream=sys.stdout, Dumper=OrderedDumper,
+ width=args.width, allow_unicode=True, default_flow_style=False)
elif args.xml_output:
import xmltodict
for doc in decode_docs(jq_out, json_decoder):
+ if not isinstance(doc, OrderedDict):
+ parser.exit("yq: Error converting JSON to XML: cannot represent non-object types at top level")
xmltodict.unparse(doc, output=sys.stdout, full_document=False, pretty=True, indent=" ")
sys.stdout.write(b"\n" if sys.version_info < (3, 0) else "\n")
else: