Repository: unnonouno/jqp

Issue:
Make raw output mode
It shows raw string instead of showing JSON string.

Discussion:
coveralls: 
[![Coverage Status](https://coveralls.io/builds/8411457/badge)](https://coveralls.io/builds/8411457)

Coverage decreased (-1.8%) to 80.0% when pulling **f405db260506c60284215d247f886eef63ad8898 on raw-output** into **c5cc6ce947f65446926749039542004f3c5a2a2b on master**.

coveralls: 
[![Coverage Status](https://coveralls.io/builds/8411508/badge)](https://coveralls.io/builds/8411508)

Coverage increased (+0.2%) to 82.0% when pulling **6296205b798a73cc82ffd3d9223d11c7ff79ad9e on raw-output** into **c5cc6ce947f65446926749039542004f3c5a2a2b on master**.


Patch:
diff --git a/jqp/__init__.py b/jqp/__init__.py
index 19e1ea8..def3f62 100644
--- a/jqp/__init__.py
+++ b/jqp/__init__.py
@@ -6,6 +6,12 @@ import sys
 __version__ = '0.0.0.1'
 
 
+if sys.version_info.major >= 3:
+    _basestring = str
+else:
+    _basestring = basestring  # NOQA
+
+
 def _exit(error, return_code, message):
     sys.stderr.write(message)
     sys.stderr.write('\nOiginal error: ')
@@ -14,7 +20,7 @@ def _exit(error, return_code, message):
     sys.exit(return_code)
 
 
-def run(in_io, out_io, cmd, imports=[], sort_keys=False):
+def run(in_io, out_io, cmd, imports=[], sort_keys=False, raw_output=False):
     environment = {}
     for mod_name in imports:
         try:
@@ -39,10 +45,13 @@ def run(in_io, out_io, cmd, imports=[], sort_keys=False):
         except Exception as e:
             _exit(e, 3, 'Cannot execute command: line %d' % line_no)
 
-        try:
-            json.dump(out, out_io, sort_keys=sort_keys)
-        except Exception as e:
-            _exit(e, 3, 'Cannot dump result: line %d' % line_no)
+        if raw_output and isinstance(out, _basestring):
+            out_io.write(out)
+        else:
+            try:
+                json.dump(out, out_io, sort_keys=sort_keys)
+            except Exception as e:
+                _exit(e, 3, 'Cannot dump result: line %d' % line_no)
 
         out_io.write('\n')
 
@@ -59,8 +68,11 @@ def main():
     parser.add_argument(
         '--sort-keys', '-S', action='store_true',
         help='sort keys in objects when the command print it')
+    parser.add_argument(
+        '--raw-output', '-r', action='store_true',
+        help='when a result is string, the command shows a raw string')
 
     args = parser.parse_args()
 
     run(sys.stdin, sys.stdout, args.cmd, imports=getattr(args, 'import'),
-        sort_keys=args.sort_keys)
+        sort_keys=args.sort_keys, raw_output=args.raw_output)


Test patch:
diff --git a/tests/run_test.py b/tests/run_test.py
index 4f26ab8..f2fb837 100644
--- a/tests/run_test.py
+++ b/tests/run_test.py
@@ -31,6 +31,14 @@ class RunTest(unittest.TestCase):
         jqp.run(inputs, outputs, '{"a": 0, "b": 0, "c": 0}', sort_keys=True)
         self.assertEqual(outputs.getvalue(), '{"a": 0, "b": 0, "c": 0}\n')
 
+    def test_raw_output(self):
+        # This command ignores input
+        inputs = StringIO('''1
+''')
+        outputs = StringIO()
+        jqp.run(inputs, outputs, '"a"', raw_output=True)
+        self.assertEqual(outputs.getvalue(), 'a\n')
+
     def test_parse_error(self):
         inputs = StringIO('invalid\n')
         outputs = StringIO()