Mercurial > libervia-backend
comparison frontends/src/jp/cmd_info.py @ 971:8ca5c990ed92
jp: "disco" subcommand, moved into a new "info" subcommand + added "version" subcommand which get software version
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 02 Apr 2014 12:31:15 +0200 |
parents | frontends/src/jp/cmd_disco.py@9cae72da1b95 |
children | 54cd05f68c7c |
comparison
equal
deleted
inserted
replaced
970:2e052998c7eb | 971:8ca5c990ed92 |
---|---|
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__ = ["Info"] | |
26 | |
27 | |
28 class Disco(base.CommandBase): | |
29 | |
30 def __init__(self, host): | |
31 super(Disco, self).__init__(host, 'disco', help=_('Service discovery')) | |
32 | |
33 def add_parser_options(self): | |
34 self.parser.add_argument("jid", type=str, help=_("Entity to discover")) | |
35 | |
36 def connected(self): | |
37 super(Disco, self).connected() | |
38 self.need_loop=True | |
39 jids = self.host.check_jids([self.args.jid]) | |
40 jid = jids[0] | |
41 self.host.bridge.discoInfos(jid, profile_key=self.host.profile, callback=lambda infos: self.gotInfos(infos, jid), errback=self.error) | |
42 | |
43 def error(self, failure): | |
44 print (_("Error while doing discovery")) | |
45 self.host.quit(1) | |
46 | |
47 def gotInfos(self, infos, jid): | |
48 self.host.bridge.discoItems(jid, profile_key=self.host.profile, callback=lambda items: self.gotItems(infos, items), errback=self.error) | |
49 | |
50 def gotItems(self, infos, items): | |
51 features, identities = infos | |
52 features.sort() | |
53 identities.sort(key=lambda identity: identity[2]) | |
54 items.sort(key=lambda item: item[2]) | |
55 identities_lst = [] | |
56 items_lst = [] | |
57 for identity in identities: | |
58 category, type_, name = identity | |
59 identities_lst.append(u"%(name)s(%(cat)s/%(type)s)" % | |
60 {'name': (name+' ') if name else '', | |
61 'cat': category, | |
62 'type': type_}) | |
63 for item in items: | |
64 entity, node_id, name = item | |
65 items_lst.append(u"%(name)s[%(entity)s%(node_id)s]" % | |
66 {'name': (name+' ') if name else '', | |
67 'entity': entity, | |
68 'node_id': (', ' + node_id) if node_id else '' | |
69 }) | |
70 | |
71 template = [] | |
72 if features: | |
73 template.append(_(u"Features:\n\n%(features)s")) | |
74 if identities: | |
75 template.append(_(u"Identities:\n\n%(identities)s")) | |
76 if items: | |
77 template.append(_(u"Items:\n\n%(items)s")) | |
78 | |
79 print "\n--\n".join(template) % { 'features': '\n'.join(features), | |
80 'identities': '\n'.join(identities_lst), | |
81 'items': '\n'.join(items_lst), | |
82 } | |
83 self.host.quit() | |
84 | |
85 | |
86 class Version(base.CommandBase): | |
87 | |
88 def __init__(self, host): | |
89 super(Version, self).__init__(host, 'version', help=_('Client version')) | |
90 | |
91 def add_parser_options(self): | |
92 self.parser.add_argument("jid", type=str, help=_("Entity to request")) | |
93 | |
94 def connected(self): | |
95 self.need_loop=True | |
96 super(Version, self).connected() | |
97 jids = self.host.check_jids([self.args.jid]) | |
98 jid = jids[0] | |
99 self.host.bridge.getSoftwareVersion(jid, self.host.profile, callback=self.gotVersion, errback=self.error) | |
100 | |
101 def error(self, failure): | |
102 print (_("Error while trying to get version [%s]") % failure) | |
103 self.host.quit(1) | |
104 | |
105 def gotVersion(self, data): | |
106 infos = [] | |
107 name, version, os = data | |
108 if name: | |
109 infos.append(_("Client name: %s") % name) | |
110 if version: | |
111 infos.append(_("Client version: %s") % version) | |
112 if os: | |
113 infos.append(_("Operating System: %s") % os) | |
114 | |
115 print "\n".join(infos) | |
116 self.host.quit() | |
117 | |
118 | |
119 class Info(base.CommandBase): | |
120 subcommands = (Disco, Version) | |
121 | |
122 def __init__(self, host): | |
123 super(Info, self).__init__(host, 'info', use_profile=False, help=_('Get various informations on entities')) |