comparison frontends/src/jp/cmd_disco.py @ 966:9cae72da1b95

jp: added disco subcommand
author Goffi <goffi@goffi.org>
date Tue, 01 Apr 2014 21:16:39 +0200
parents
children
comparison
equal deleted inserted replaced
965:5b95ce21c2d3 966:9cae72da1b95
1 #! /usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # jp: a SAT command line tool
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
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/>.
19
20 from logging import debug, info, error, warning
21
22 import base
23 from sat.core.i18n import _
24
25 __commands__ = ["Disco"]
26
27 class Disco(base.CommandBase):
28
29 def __init__(self, host):
30 super(Disco, self).__init__(host, 'disco', help=_('Service discovery'))
31
32 def add_parser_options(self):
33 self.parser.add_argument("jid", type=str, help=_("Entity to discover"))
34
35 def connected(self):
36 super(Disco, self).connected()
37 self.need_loop=True
38 jids = self.host.check_jids([self.args.jid])
39 jid = jids[0]
40 self.host.bridge.discoInfos(jid, profile_key=self.host.profile, callback=lambda infos: self.gotInfos(infos, jid), errback=lambda ignore: ignore)
41
42 def gotInfos(self, infos, jid):
43 self.host.bridge.discoItems(jid, profile_key=self.host.profile, callback=lambda items: self.gotItems(infos, items), errback=lambda ignore: ignore)
44
45 def gotItems(self, infos, items):
46 features, identities = infos
47 features.sort()
48 identities.sort(key=lambda identity: identity[2])
49 items.sort(key=lambda item: item[2])
50 identities_lst = []
51 items_lst = []
52 for identity in identities:
53 category, type_, name = identity
54 identities_lst.append(u"%(name)s(%(cat)s/%(type)s)" %
55 {'name': (name+' ') if name else '',
56 'cat': category,
57 'type': type_})
58 for item in items:
59 entity, node_id, name = item
60 items_lst.append(u"%(name)s[%(entity)s%(node_id)s]" %
61 {'name': (name+' ') if name else '',
62 'entity': entity,
63 'node_id': (', ' + node_id) if node_id else ''
64 })
65
66 template = []
67 if features:
68 template.append(_(u"Features:\n\n%(features)s"))
69 if identities:
70 template.append(_(u"Identities:\n\n%(identities)s"))
71 if items:
72 template.append(_(u"Items:\n\n%(items)s"))
73
74 print "\n--\n".join(template) % { 'features': '\n'.join(features),
75 'identities': '\n'.join(identities_lst),
76 'items': '\n'.join(items_lst),
77 }
78 self.host.quit()