diff options
author | Andrey Kislyuk <kislyuk@gmail.com> | 2018-02-10 10:10:28 -0800 |
---|---|---|
committer | Andrey Kislyuk <kislyuk@gmail.com> | 2018-02-10 10:10:28 -0800 |
commit | 191c524800ea3934da198761bad700c0d201aab8 (patch) | |
tree | 7ee0b6031a19dd8eab87436c27e6bebccca4aa46 | |
parent | c3559f9109dcf5ab5989eae909232a80eaaf28f4 (diff) |
Raise understandable error on non-dict conversion failure
-rw-r--r-- | README.rst | 3 | ||||
-rw-r--r-- | setup.cfg | 2 | ||||
-rwxr-xr-x | setup.py | 2 | ||||
-rwxr-xr-x | yq/__init__.py | 8 |
4 files changed, 9 insertions, 6 deletions
@@ -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 @@ -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 @@ -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: |