瀏覽代碼

Add initial skeleton for json support

Gabriel Cruz 4 年之前
父節點
當前提交
83a54d8f0d
共有 2 個文件被更改,包括 27 次插入3 次删除
  1. 15 3
      howdoi/howdoi.py
  2. 12 0
      test_howdoi.py

+ 15 - 3
howdoi/howdoi.py

@@ -18,6 +18,7 @@ import re
 from cachelib import FileSystemCache, NullCache
 import requests
 import sys
+import json
 from . import __version__
 
 from pygments import highlight
@@ -409,13 +410,16 @@ def howdoi(raw_query):
         args = vars(parser.parse_args(raw_query.split(' ')))
 
     args['query'] = ' '.join(args['query']).replace('?', '')
-    cache_key = str(args)
+    cache_key = json.dumps(args)
 
     if _is_help_query(args['query']):
         return _get_help_instructions() + '\n'
 
     res = cache.get(cache_key)
     if res:
+        if not args['json_output']:
+            # TODO: parse json
+            pass
         return res
 
     try:
@@ -424,6 +428,10 @@ def howdoi(raw_query):
             res = 'Sorry, couldn\'t find any help with that topic\n'
         cache.set(cache_key, res)
 
+        if not args['json_output']:
+            # parse json
+            pass
+
         return res
     except (ConnectionError, SSLError):
         return 'Failed to establish network connection\n'
@@ -437,8 +445,12 @@ def get_parser():
     parser.add_argument('-l', '--link', help='display only the answer link', action='store_true')
     parser.add_argument('-c', '--color', help='enable colorized output', action='store_true')
     parser.add_argument('-n', '--num-answers', help='number of answers to return', default=1, type=int)
-    parser.add_argument('-C', '--clear-cache', help='clear the cache', action='store_true')
-    parser.add_argument('-v', '--version', help='displays the current version of howdoi', action='store_true')
+    parser.add_argument('-C', '--clear-cache', help='clear the cache',
+                        action='store_true')
+    parser.add_argument('-j', '--json-output', help='return answers in json format',
+                        action='store_false'
+    parser.add_argument('-v', '--version', help='displays the current version of howdoi',
+                        action='store_true')
     parser.add_argument('-e', '--engine', help='change search engine for this query only (google, bing, duckduckgo)',
                         dest='search_engine', nargs="?", default='google')
     return parser

+ 12 - 0
test_howdoi.py

@@ -101,6 +101,18 @@ class HowdoiTestCase(unittest.TestCase):
         self.assertNotEqual(first_answer, second_answer)
         self.assertNotEqual(re.match('.*Answer from http.?://.*', second_answer, re.DOTALL), None)
 
+    def test_json_output(self):
+        query = self.queries[0]
+        txt_answers = self.call_howdoi(query + ' -n2')
+        json_answers = self.call_howdoi(query + ' -j -n2')
+        expected_json = {
+            "answers": [
+               txt_answers[0],
+               txt_answers[1],
+            ],
+        }
+        self.assertEqual(json_answers, expected_json)
+
     def test_multiple_answers(self):
         query = self.queries[0]
         first_answer = self.call_howdoi(query)