changeset 2156:8f96c242fa89

jp (outputs): added OUTPUT_COMPLEX type for data which are more complex than key/value dictionaries
author Goffi <goffi@goffi.org>
date Thu, 16 Feb 2017 00:51:33 +0100
parents cee6561ad086
children b4a515e36631
files frontends/src/jp/constants.py frontends/src/jp/output_std.py
diffstat 2 files changed, 20 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/src/jp/constants.py	Thu Feb 16 00:49:46 2017 +0100
+++ b/frontends/src/jp/constants.py	Thu Feb 16 00:51:33 2017 +0100
@@ -26,9 +26,10 @@
     PLUGIN_CMD = "commands"
     PLUGIN_OUTPUT = "outputs"
     OUTPUT_TEXT = 'text' # blob of unicode text
-    OUTPUT_DICT = 'dict'
+    OUTPUT_DICT = 'dict' # simple key/value dictionary
     OUTPUT_LIST = 'list'
-    OUTPUT_TYPES = (OUTPUT_TEXT, OUTPUT_DICT, OUTPUT_LIST)
+    OUTPUT_COMPLEX = 'complex' # complex data (e.g. multi-level dictionary)
+    OUTPUT_TYPES = (OUTPUT_TEXT, OUTPUT_DICT, OUTPUT_LIST, OUTPUT_COMPLEX)
 
     # exit codes
     EXIT_OK = 0
--- a/frontends/src/jp/output_std.py	Thu Feb 16 00:49:46 2017 +0100
+++ b/frontends/src/jp/output_std.py	Thu Feb 16 00:51:33 2017 +0100
@@ -22,37 +22,46 @@
 from sat_frontends.jp.constants import Const as C
 import json
 
-__outputs__ = ["Default", "Json"]
-DEFAULT = u'default'
+__outputs__ = ["Simple", "Json"]
+SIMPLE = u'simple'
 JSON = u'json'
 JSON_RAW = u'json_raw'
 
 
-class Default(object):
+class Simple(object):
     """Default outputs"""
 
     def __init__(self, jp):
-        jp.register_output(C.OUTPUT_TEXT, DEFAULT, self.text)
-        jp.register_output(C.OUTPUT_LIST, DEFAULT, self.list)
+        jp.register_output(C.OUTPUT_TEXT, SIMPLE, self.simple_print)
+        jp.register_output(C.OUTPUT_LIST, SIMPLE, self.list)
+        jp.register_output(C.OUTPUT_DICT, SIMPLE, self.dict)
+        jp.register_output(C.OUTPUT_COMPLEX, SIMPLE, self.simple_print)
 
-    def text(self, data):
+    def simple_print(self, data):
         print data
 
     def list(self, data):
         print u'\n'.join(data)
 
+    def dict(self, data):
+        for k, v in data:
+            print(u'{key}: {value}'.format(key=k, value=v))
+
 
 class Json(object):
     """outputs in json format"""
 
     def __init__(self, jp):
+        jp.register_output(C.OUTPUT_TEXT, JSON, self.dump)
         jp.register_output(C.OUTPUT_LIST, JSON, self.dump_pretty)
         jp.register_output(C.OUTPUT_LIST, JSON_RAW, self.dump)
         jp.register_output(C.OUTPUT_DICT, JSON, self.dump_pretty)
         jp.register_output(C.OUTPUT_DICT, JSON_RAW, self.dump)
+        jp.register_output(C.OUTPUT_COMPLEX, JSON, self.dump_pretty)
+        jp.register_output(C.OUTPUT_COMPLEX, JSON_RAW, self.dump)
 
     def dump(self, data):
-        print json.dumps(data)
+        print json.dumps(data, default=str)
 
     def dump_pretty(self, data):
-        print json.dumps(data, indent=4)
+        print json.dumps(data, indent=4, default=str)