From 34c29fee135803154c6bb494d908b98067f028e2 Mon Sep 17 00:00:00 2001 From: Matěj Cepl Date: Tue, 24 Apr 2012 01:43:03 +0200 Subject: Added -o parameter for output to the specified file. --- json_diff.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'json_diff.py') diff --git a/json_diff.py b/json_diff.py index 2f0d0de..0d191d1 100755 --- a/json_diff.py +++ b/json_diff.py @@ -23,6 +23,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ +from __future__ import print_function try: import json except ImportError: @@ -32,7 +33,7 @@ import logging from optparse import OptionParser __author__ = "Matěj Cepl" -__version__ = "1.2.9" +__version__ = "1.3.0" logging.basicConfig(format='%(levelname)s:%(funcName)s:%(message)s', level=logging.INFO) @@ -337,6 +338,9 @@ def main(sys_args): parser.add_option("-i", "--include", action="append", dest="include", metavar="ATTR", default=[], help="attributes which should be exclusively used when comparing") + parser.add_option("-o", "--output", + action="append", dest="output", metavar="FILE", default=[], + help="name of the output file (default is stdout)") parser.add_option("-a", "--ignore-append", action="store_true", dest="ignore_append", metavar="BOOL", default=False, help="ignore appended keys") @@ -345,6 +349,11 @@ def main(sys_args): help="program should output to HTML report") (options, args) = parser.parse_args(sys_args[1:]) + if options.output: + outf = open(options.output[0], "w") + else: + outf = sys.stdout + if len(args) != 2: parser.error("Script requires two positional arguments, " + \ "names for old and new JSON file.") @@ -353,10 +362,10 @@ def main(sys_args): if options.HTMLoutput: # we want to hardcode UTF-8 here, because that's what's # in element of the generated HTML - print(unicode(HTMLFormatter(diff_res)).encode("utf-8")) + print(unicode(HTMLFormatter(diff_res)).encode("utf-8"), file=outf) else: outs = json.dumps(diff_res, indent=4, ensure_ascii=False) - print(outs.encode("utf-8")) + print(outs.encode("utf-8"), file=outf) if len(diff_res) > 0: return 1 -- cgit