Repository: unnonouno/jqp

Issue:
Make import option
I need an option to import modules which I use in commands.

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

Coverage increased (+1.4%) to 83.721% when pulling **df0ac543dc33b5ad23fb4dd94e5f512793a1f5f0 on import** into **0977f3585147190adec127722e940783209b6ab6 on master**.

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

Coverage increased (+1.4%) to 83.721% when pulling **df0ac543dc33b5ad23fb4dd94e5f512793a1f5f0 on import** into **0977f3585147190adec127722e940783209b6ab6 on master**.


Patch:
diff --git a/jqp/__init__.py b/jqp/__init__.py
index cdcc02e..90fd8de 100644
--- a/jqp/__init__.py
+++ b/jqp/__init__.py
@@ -14,7 +14,15 @@ def _exit(error, return_code, message):
     sys.exit(return_code)
 
 
-def run(in_io, out_io, cmd):
+def run(in_io, out_io, cmd, imports=[]):
+    environment = {}
+    for mod_name in imports:
+        try:
+            mod = __import__(mod_name)
+        except Exception as e:
+            _exit(e, 5, 'Cannot import module: %s' % mod_name)
+        environment[mod_name] = mod
+
     for i, line in enumerate(in_io):
         if line.strip() == '':
             continue
@@ -26,7 +34,8 @@ def run(in_io, out_io, cmd):
             _exit(e, 4, 'Parse error: line %d' % line_no)
 
         try:
-            out = eval(cmd, {'j': js})
+            environment['j'] = js
+            out = eval(cmd, environment)
         except Exception as e:
             _exit(e, 3, 'Cannot execute command: line %d' % line_no)
 
@@ -44,6 +53,10 @@ def main():
     parser.add_argument(
         '--version', action='version', version='jqp %s' % __version__,
         help='show version and exit')
+    parser.add_argument(
+        '--import', action='append',
+        help='modules to import')
+
     args = parser.parse_args()
 
-    run(sys.stdin, sys.stdout, args.cmd)
+    run(sys.stdin, sys.stdout, args.cmd, imports=getattr(args, 'import'))


Test patch:
diff --git a/tests/run_test.py b/tests/run_test.py
index 31ceb87..8e64641 100644
--- a/tests/run_test.py
+++ b/tests/run_test.py
@@ -16,6 +16,13 @@ class RunTest(unittest.TestCase):
         jqp.run(inputs, outputs, 'j["name"]')
         self.assertEqual(outputs.getvalue(), '"Taro"\n')
 
+    def test_import(self):
+        inputs = StringIO('''{"name": "Taro", "age": 10}
+''')
+        outputs = StringIO()
+        jqp.run(inputs, outputs, 're.sub("a", "A", j["name"])', imports=['re'])
+        self.assertEqual(outputs.getvalue(), '"TAro"\n')
+
     def test_parse_error(self):
         inputs = StringIO('invalid\n')
         outputs = StringIO()
@@ -36,3 +43,10 @@ class RunTest(unittest.TestCase):
         with self.assertRaises(SystemExit) as e:
             jqp.run(inputs, outputs, 'lambda: 0')
         self.assertEqual(e.exception.code, 3)
+
+    def test_import_error(self):
+        inputs = StringIO('1\n')
+        outputs = StringIO()
+        with self.assertRaises(SystemExit) as e:
+            jqp.run(inputs, outputs, 'j', imports=['unknown_module'])
+        self.assertEqual(e.exception.code, 5)