diff frontends/src/jp/cmd_info.py @ 2346:c903c259402a

jp (info/disco): type selection + output improvments: - type of discovery can now be specified (can be "infos", "items", or "both" which is the default) - a complex output is now used - default output has been improved by the use of colors an tables
author Goffi <goffi@goffi.org>
date Wed, 23 Aug 2017 00:14:15 +0200
parents ca1ab42c7ae9
children b6dee1f66478
line wrap: on
line diff
--- a/frontends/src/jp/cmd_info.py	Wed Aug 23 00:10:09 2017 +0200
+++ b/frontends/src/jp/cmd_info.py	Wed Aug 23 00:14:15 2017 +0200
@@ -19,7 +19,9 @@
 
 import base
 from sat.core.i18n import _
+from sat.tools.common.ansi import ANSI as A
 from sat_frontends.jp.constants import Const as C
+from sat_frontends.jp import common
 
 __commands__ = ["Info"]
 
@@ -27,50 +29,71 @@
 class Disco(base.CommandBase):
 
     def __init__(self, host):
-        super(Disco, self).__init__(host, 'disco', help=_('service discovery'))
+        extra_outputs = {'default': self.default_output}
+        super(Disco, self).__init__(host, 'disco', use_output='complex', extra_outputs=extra_outputs, help=_('service discovery'))
         self.need_loop=True
 
     def add_parser_options(self):
         self.parser.add_argument(u"jid", type=base.unicode_decoder, help=_(u"entity to discover"))
+        self.parser.add_argument(u"-t", u"--type", type=str, choices=('infos', 'items', 'both'), default='both', help=_(u"type of data to discover"))
         self.parser.add_argument(u"-n", u"--node", type=base.unicode_decoder, default=u'', help=_(u"node to use"))
         self.parser.add_argument(u"-C", u"--no-cache", dest='use_cache', action="store_false", help=_(u"ignore cache"))
 
     def start(self):
+        self.get_infos = self.args.type in ('infos', 'both')
+        self.get_items = self.args.type in ('items', 'both')
         jids = self.host.check_jids([self.args.jid])
         jid = jids[0]
-        self.host.bridge.discoInfos(jid, node=self.args.node, use_cache=self.args.use_cache, profile_key=self.host.profile, callback=lambda infos: self.gotInfos(infos, jid), errback=self.error)
+        if not self.get_infos:
+            self.gotInfos(None, jid)
+        else:
+            self.host.bridge.discoInfos(jid, node=self.args.node, use_cache=self.args.use_cache, profile_key=self.host.profile, callback=lambda infos: self.gotInfos(infos, jid), errback=self.error)
 
     def error(self, failure):
         print (_("Error while doing discovery [%s]") % failure)
         self.host.quit(1)
 
     def gotInfos(self, infos, jid):
-        self.host.bridge.discoItems(jid, node=self.args.node, use_cache=self.args.use_cache, profile_key=self.host.profile, callback=lambda items: self.gotItems(infos, items), errback=self.error)
+        if not self.get_items:
+            self.gotItems(infos, None)
+        else:
+            self.host.bridge.discoItems(jid, node=self.args.node, use_cache=self.args.use_cache, profile_key=self.host.profile, callback=lambda items: self.gotItems(infos, items), errback=self.error)
 
     def gotItems(self, infos, items):
-        features, identities, extensions = infos
-        features.sort()
-        identities.sort(key=lambda identity: identity[2])
-        items.sort(key=lambda item: item[2])
-        identities_lst = []
-        items_lst = []
-        for identity in identities:
-            category, type_, name = identity
-            identities_lst.append(u"{name}({cat}/{type_})".format(
-                                 name = (name+' ') if name else '',
-                                 cat = category,
-                                 type_ = type_))
-        for item in items:
-            entity, node_id, name = item
-            items_lst.append(u"{name}[{entity}{node_id}]".format(
-                                 name = (name+' ') if name else '',
-                                 entity = entity,
-                                 node_id = (', ' + node_id) if node_id else ''
-                                 ))
+        data = {}
+
+        if self.get_infos:
+            features, identities, extensions = infos
+            features.sort()
+            identities.sort(key=lambda identity: identity[2])
+            data.update({
+                u'features': features,
+                u'identities': identities,
+                u'extensions': extensions})
+
+        if self.get_items:
+            items.sort(key=lambda item: item[2])
+            data[u'items'] = items
+
+        self.output(data)
+        self.host.quit()
+
+    def default_output(self, data):
+        features = data.get(u'features', [])
+        identities = data.get(u'identities', [])
+        extensions = data.get(u'extensions', {})
+        items = data.get(u'items', [])
+
+        identities_table = common.Table(self.host,
+                                        identities,
+                                        headers=(_(u'category'),
+                                                 _(u'type'),
+                                                 _(u'name')),
+                                        use_buffer=True)
+
+        extensions_tpl = []
         extensions_types = extensions.keys()
         extensions_types.sort()
-
-        extensions_tpl = []
         for type_ in extensions_types:
             fields = []
             for field in extensions[type_]:
@@ -79,29 +102,35 @@
                 data_keys = data.keys()
                 data_keys.sort()
                 for key in data_keys:
-                    field_lines.append(u'\t{key} = {value}'.format(key=key, value=data[key]))
+                    field_lines.append(A.color(u'\t', C.A_SUBHEADER, key, data[key]))
                 for value in values:
-                    field_lines.append(u'\tvalue = {value}'.format(value=value))
+                    field_lines.append(A.color(u'\t', A.BOLD, value))
                 fields.append(u'\n'.join(field_lines))
             extensions_tpl.append(u'{type_}\n{fields}'.format(type_=type_,
                                                               fields='\n\n'.join(fields)))
 
+        items_table = common.Table(self.host,
+                                   items,
+                                   headers=(_(u'name'),
+                                            _(u'entity'),
+                                            _(u'node')),
+                                   use_buffer=True)
+
         template = []
         if features:
-            template.append(_(u"Features:\n\n{features}"))
+            template.append(A.color(C.A_HEADER, _(u"Features")) + u"\n\n{features}")
         if identities:
-            template.append(_(u"Identities:\n\n{identities}"))
-        if extensions_tpl:
-            template.append(_(u'Extensions:\n\n{extensions}'))
+            template.append(A.color(C.A_HEADER, _(u"Identities")) + u"\n\n{identities}")
+        if extensions:
+            template.append(A.color(C.A_HEADER, _(u"Extensions")) + u"\n\n{extensions}")
         if items:
-            template.append(_(u"Items:\n\n{items}"))
+            template.append(A.color(C.A_HEADER, _(u"Items")) + u"\n\n{items}")
 
-        print "\n--\n".join(template).format ( features = '\n'.join(features),
-                                               identities = '\n'.join(identities_lst),
-                                               extensions = '\n'.join(extensions_tpl),
-                                               items = '\n'.join(items_lst),
-                                             )
-        self.host.quit()
+        print u"\n\n".join(template).format(features = u'\n'.join(features),
+                                            identities = identities_table.display().string,
+                                            extensions = u'\n'.join(extensions_tpl),
+                                            items = items_table.display().string,
+                                           )
 
 
 class Version(base.CommandBase):