summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--README.rst3
-rwxr-xr-xscripts/xq5
-rwxr-xr-xscripts/yq2
-rwxr-xr-xsetup.py6
-rwxr-xr-xyq/__init__.py21
5 files changed, 30 insertions, 7 deletions
diff --git a/README.rst b/README.rst
index 827e624..28995f7 100644
--- a/README.rst
+++ b/README.rst
@@ -1,3 +1,6 @@
+FIXME: use https://github.com/martinblech/xmltodict
+
+
yq: Command-line YAML processor - jq wrapper for YAML documents
===============================================================
diff --git a/scripts/xq b/scripts/xq
new file mode 100755
index 0000000..3435723
--- /dev/null
+++ b/scripts/xq
@@ -0,0 +1,5 @@
+#!/usr/bin/env python
+
+from yq import main
+
+main(input_format="xml")
diff --git a/scripts/yq b/scripts/yq
index b6672ba..054acf3 100755
--- a/scripts/yq
+++ b/scripts/yq
@@ -2,4 +2,4 @@
from yq import main
-main()
+main(input_format="yaml")
diff --git a/setup.py b/setup.py
index 8f44242..5fe8156 100755
--- a/setup.py
+++ b/setup.py
@@ -12,11 +12,13 @@ setup(
license="Apache Software License",
author="Andrey Kislyuk",
author_email="kislyuk@gmail.com",
- description="Command-line YAML processor - jq wrapper for YAML documents",
+ description="Command-line YAML and XML processor - jq wrapper for YAML and XML documents",
long_description=open("README.rst").read(),
install_requires=[
"setuptools",
- "PyYAML >= 3.11"
+ "PyYAML >= 3.11",
+ "xmljson >= 0.1.9",
+ "defusedxml >= 0.5.0"
],
tests_require=tests_require,
extras_require={"test": tests_require},
diff --git a/yq/__init__.py b/yq/__init__.py
index af765d2..bfea92a 100755
--- a/yq/__init__.py
+++ b/yq/__init__.py
@@ -10,7 +10,9 @@ from __future__ import absolute_import, division, print_function, unicode_litera
import os, sys, argparse, subprocess, json
from collections import OrderedDict
from datetime import datetime, date, time
+
import yaml
+
from .version import __version__
class Parser(argparse.ArgumentParser):
@@ -54,6 +56,9 @@ parser = Parser(description=__doc__, formatter_class=argparse.RawTextHelpFormatt
parser.add_argument("--yaml-output", "--yml-output", "-y", help="Transcode jq JSON output back into YAML and emit it",
action="store_true")
parser.add_argument("--width", "-w", type=int, help="When using --yaml-output, specify string wrap width")
+parser.add_argument("--xml-converter", nargs="?", choices=["abdera", "badgerfish", "cobra", "gdata", "parker", "yahoo"],
+ default="badgerfish")
+parser.add_argument("--xml-output", "-x", action="store_true")
parser.add_argument("--version", action="version", version="%(prog)s {version}".format(version=__version__))
# jq arguments that consume positionals must be listed here to avoid our parser mistaking them for our positionals
@@ -65,7 +70,7 @@ for arg in jq_arg_spec:
parser.add_argument("jq_filter")
parser.add_argument("files", nargs="*", type=argparse.FileType())
-def main(args=None):
+def main(args=None, input_format="yaml"):
args, jq_args = parser.parse_known_args(args=args)
for arg in jq_arg_spec:
values = getattr(args, arg, None)
@@ -102,9 +107,17 @@ def main(args=None):
yaml.dump_all(decode_docs(jq_out, json_decoder), stream=sys.stdout, Dumper=OrderedDumper, width=args.width,
allow_unicode=True, default_flow_style=False)
else:
- for input_stream in input_streams:
- for doc in yaml.load_all(input_stream, Loader=OrderedLoader):
- json.dump(doc, jq.stdin, cls=JSONDateTimeEncoder)
+ if input_format == "yaml":
+ for input_stream in input_streams:
+ for doc in yaml.load_all(input_stream, Loader=OrderedLoader):
+ json.dump(doc, jq.stdin, cls=JSONDateTimeEncoder)
+ jq.stdin.write("\n")
+ elif input_format == "xml":
+ from defusedxml.ElementTree import parse
+ import xmljson
+ for input_stream in input_streams:
+ data = getattr(xmljson, args.xml_converter).data(parse(input_stream).getroot())
+ json.dump(data, jq.stdin)
jq.stdin.write("\n")
jq.stdin.close()
jq.wait()