comparison 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
comparison
equal deleted inserted replaced
2345:c57bc0fe17d9 2346:c903c259402a
17 # You should have received a copy of the GNU Affero General Public License 17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 import base 20 import base
21 from sat.core.i18n import _ 21 from sat.core.i18n import _
22 from sat.tools.common.ansi import ANSI as A
22 from sat_frontends.jp.constants import Const as C 23 from sat_frontends.jp.constants import Const as C
24 from sat_frontends.jp import common
23 25
24 __commands__ = ["Info"] 26 __commands__ = ["Info"]
25 27
26 28
27 class Disco(base.CommandBase): 29 class Disco(base.CommandBase):
28 30
29 def __init__(self, host): 31 def __init__(self, host):
30 super(Disco, self).__init__(host, 'disco', help=_('service discovery')) 32 extra_outputs = {'default': self.default_output}
33 super(Disco, self).__init__(host, 'disco', use_output='complex', extra_outputs=extra_outputs, help=_('service discovery'))
31 self.need_loop=True 34 self.need_loop=True
32 35
33 def add_parser_options(self): 36 def add_parser_options(self):
34 self.parser.add_argument(u"jid", type=base.unicode_decoder, help=_(u"entity to discover")) 37 self.parser.add_argument(u"jid", type=base.unicode_decoder, help=_(u"entity to discover"))
38 self.parser.add_argument(u"-t", u"--type", type=str, choices=('infos', 'items', 'both'), default='both', help=_(u"type of data to discover"))
35 self.parser.add_argument(u"-n", u"--node", type=base.unicode_decoder, default=u'', help=_(u"node to use")) 39 self.parser.add_argument(u"-n", u"--node", type=base.unicode_decoder, default=u'', help=_(u"node to use"))
36 self.parser.add_argument(u"-C", u"--no-cache", dest='use_cache', action="store_false", help=_(u"ignore cache")) 40 self.parser.add_argument(u"-C", u"--no-cache", dest='use_cache', action="store_false", help=_(u"ignore cache"))
37 41
38 def start(self): 42 def start(self):
43 self.get_infos = self.args.type in ('infos', 'both')
44 self.get_items = self.args.type in ('items', 'both')
39 jids = self.host.check_jids([self.args.jid]) 45 jids = self.host.check_jids([self.args.jid])
40 jid = jids[0] 46 jid = jids[0]
41 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) 47 if not self.get_infos:
48 self.gotInfos(None, jid)
49 else:
50 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)
42 51
43 def error(self, failure): 52 def error(self, failure):
44 print (_("Error while doing discovery [%s]") % failure) 53 print (_("Error while doing discovery [%s]") % failure)
45 self.host.quit(1) 54 self.host.quit(1)
46 55
47 def gotInfos(self, infos, jid): 56 def gotInfos(self, infos, jid):
48 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) 57 if not self.get_items:
58 self.gotItems(infos, None)
59 else:
60 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)
49 61
50 def gotItems(self, infos, items): 62 def gotItems(self, infos, items):
51 features, identities, extensions = infos 63 data = {}
52 features.sort() 64
53 identities.sort(key=lambda identity: identity[2]) 65 if self.get_infos:
54 items.sort(key=lambda item: item[2]) 66 features, identities, extensions = infos
55 identities_lst = [] 67 features.sort()
56 items_lst = [] 68 identities.sort(key=lambda identity: identity[2])
57 for identity in identities: 69 data.update({
58 category, type_, name = identity 70 u'features': features,
59 identities_lst.append(u"{name}({cat}/{type_})".format( 71 u'identities': identities,
60 name = (name+' ') if name else '', 72 u'extensions': extensions})
61 cat = category, 73
62 type_ = type_)) 74 if self.get_items:
63 for item in items: 75 items.sort(key=lambda item: item[2])
64 entity, node_id, name = item 76 data[u'items'] = items
65 items_lst.append(u"{name}[{entity}{node_id}]".format( 77
66 name = (name+' ') if name else '', 78 self.output(data)
67 entity = entity, 79 self.host.quit()
68 node_id = (', ' + node_id) if node_id else '' 80
69 )) 81 def default_output(self, data):
82 features = data.get(u'features', [])
83 identities = data.get(u'identities', [])
84 extensions = data.get(u'extensions', {})
85 items = data.get(u'items', [])
86
87 identities_table = common.Table(self.host,
88 identities,
89 headers=(_(u'category'),
90 _(u'type'),
91 _(u'name')),
92 use_buffer=True)
93
94 extensions_tpl = []
70 extensions_types = extensions.keys() 95 extensions_types = extensions.keys()
71 extensions_types.sort() 96 extensions_types.sort()
72
73 extensions_tpl = []
74 for type_ in extensions_types: 97 for type_ in extensions_types:
75 fields = [] 98 fields = []
76 for field in extensions[type_]: 99 for field in extensions[type_]:
77 field_lines = [] 100 field_lines = []
78 data, values = field 101 data, values = field
79 data_keys = data.keys() 102 data_keys = data.keys()
80 data_keys.sort() 103 data_keys.sort()
81 for key in data_keys: 104 for key in data_keys:
82 field_lines.append(u'\t{key} = {value}'.format(key=key, value=data[key])) 105 field_lines.append(A.color(u'\t', C.A_SUBHEADER, key, data[key]))
83 for value in values: 106 for value in values:
84 field_lines.append(u'\tvalue = {value}'.format(value=value)) 107 field_lines.append(A.color(u'\t', A.BOLD, value))
85 fields.append(u'\n'.join(field_lines)) 108 fields.append(u'\n'.join(field_lines))
86 extensions_tpl.append(u'{type_}\n{fields}'.format(type_=type_, 109 extensions_tpl.append(u'{type_}\n{fields}'.format(type_=type_,
87 fields='\n\n'.join(fields))) 110 fields='\n\n'.join(fields)))
88 111
112 items_table = common.Table(self.host,
113 items,
114 headers=(_(u'name'),
115 _(u'entity'),
116 _(u'node')),
117 use_buffer=True)
118
89 template = [] 119 template = []
90 if features: 120 if features:
91 template.append(_(u"Features:\n\n{features}")) 121 template.append(A.color(C.A_HEADER, _(u"Features")) + u"\n\n{features}")
92 if identities: 122 if identities:
93 template.append(_(u"Identities:\n\n{identities}")) 123 template.append(A.color(C.A_HEADER, _(u"Identities")) + u"\n\n{identities}")
94 if extensions_tpl: 124 if extensions:
95 template.append(_(u'Extensions:\n\n{extensions}')) 125 template.append(A.color(C.A_HEADER, _(u"Extensions")) + u"\n\n{extensions}")
96 if items: 126 if items:
97 template.append(_(u"Items:\n\n{items}")) 127 template.append(A.color(C.A_HEADER, _(u"Items")) + u"\n\n{items}")
98 128
99 print "\n--\n".join(template).format ( features = '\n'.join(features), 129 print u"\n\n".join(template).format(features = u'\n'.join(features),
100 identities = '\n'.join(identities_lst), 130 identities = identities_table.display().string,
101 extensions = '\n'.join(extensions_tpl), 131 extensions = u'\n'.join(extensions_tpl),
102 items = '\n'.join(items_lst), 132 items = items_table.display().string,
103 ) 133 )
104 self.host.quit()
105 134
106 135
107 class Version(base.CommandBase): 136 class Version(base.CommandBase):
108 137
109 def __init__(self, host): 138 def __init__(self, host):