comparison sat_frontends/jp/cmd_info.py @ 3028:ab2696e34d29

Python 3 port: /!\ this is a huge commit /!\ starting from this commit, SàT is needs Python 3.6+ /!\ SàT maybe be instable or some feature may not work anymore, this will improve with time This patch port backend, bridge and frontends to Python 3. Roughly this has been done this way: - 2to3 tools has been applied (with python 3.7) - all references to python2 have been replaced with python3 (notably shebangs) - fixed files not handled by 2to3 (notably the shell script) - several manual fixes - fixed issues reported by Python 3 that where not handled in Python 2 - replaced "async" with "async_" when needed (it's a reserved word from Python 3.7) - replaced zope's "implements" with @implementer decorator - temporary hack to handle data pickled in database, as str or bytes may be returned, to be checked later - fixed hash comparison for password - removed some code which is not needed anymore with Python 3 - deactivated some code which needs to be checked (notably certificate validation) - tested with jp, fixed reported issues until some basic commands worked - ported Primitivus (after porting dependencies like urwid satext) - more manual fixes
author Goffi <goffi@goffi.org>
date Tue, 13 Aug 2019 19:08:41 +0200
parents 003b8b4b56a7
children fee60f17ebac
comparison
equal deleted inserted replaced
3027:ff5bcb12ae60 3028:ab2696e34d29
15 # GNU Affero General Public License for more details. 15 # GNU Affero General Public License for more details.
16 16
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 from . 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.tools.common.ansi import ANSI as A
23 from sat.tools.common import date_utils 23 from sat.tools.common import date_utils
24 from sat_frontends.jp.constants import Const as C 24 from sat_frontends.jp.constants import Const as C
25 from sat_frontends.jp import common 25 from sat_frontends.jp import common
33 extra_outputs = {'default': self.default_output} 33 extra_outputs = {'default': self.default_output}
34 super(Disco, self).__init__(host, 'disco', use_output='complex', extra_outputs=extra_outputs, help=_('service discovery')) 34 super(Disco, self).__init__(host, 'disco', use_output='complex', extra_outputs=extra_outputs, help=_('service discovery'))
35 self.need_loop=True 35 self.need_loop=True
36 36
37 def add_parser_options(self): 37 def add_parser_options(self):
38 self.parser.add_argument(u"jid", type=base.unicode_decoder, help=_(u"entity to discover")) 38 self.parser.add_argument("jid", help=_("entity to discover"))
39 self.parser.add_argument(u"-t", u"--type", type=str, choices=('infos', 'items', 'both'), default='both', help=_(u"type of data to discover")) 39 self.parser.add_argument("-t", "--type", type=str, choices=('infos', 'items', 'both'), default='both', help=_("type of data to discover"))
40 self.parser.add_argument(u"-n", u"--node", type=base.unicode_decoder, default=u'', help=_(u"node to use")) 40 self.parser.add_argument("-n", "--node", default='', help=_("node to use"))
41 self.parser.add_argument(u"-C", u"--no-cache", dest='use_cache', action="store_false", help=_(u"ignore cache")) 41 self.parser.add_argument("-C", "--no-cache", dest='use_cache', action="store_false", help=_("ignore cache"))
42 42
43 def start(self): 43 def start(self):
44 self.get_infos = self.args.type in ('infos', 'both') 44 self.get_infos = self.args.type in ('infos', 'both')
45 self.get_items = self.args.type in ('items', 'both') 45 self.get_items = self.args.type in ('items', 'both')
46 jids = self.host.check_jids([self.args.jid]) 46 jids = self.host.check_jids([self.args.jid])
49 self.gotInfos(None, jid) 49 self.gotInfos(None, jid)
50 else: 50 else:
51 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) 51 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)
52 52
53 def error(self, failure): 53 def error(self, failure):
54 print (_("Error while doing discovery [%s]") % failure) 54 print((_("Error while doing discovery [%s]") % failure))
55 self.host.quit(1) 55 self.host.quit(1)
56 56
57 def gotInfos(self, infos, jid): 57 def gotInfos(self, infos, jid):
58 if not self.get_items: 58 if not self.get_items:
59 self.gotItems(infos, None) 59 self.gotItems(infos, None)
66 if self.get_infos: 66 if self.get_infos:
67 features, identities, extensions = infos 67 features, identities, extensions = infos
68 features.sort() 68 features.sort()
69 identities.sort(key=lambda identity: identity[2]) 69 identities.sort(key=lambda identity: identity[2])
70 data.update({ 70 data.update({
71 u'features': features, 71 'features': features,
72 u'identities': identities, 72 'identities': identities,
73 u'extensions': extensions}) 73 'extensions': extensions})
74 74
75 if self.get_items: 75 if self.get_items:
76 items.sort(key=lambda item: item[2]) 76 items.sort(key=lambda item: item[2])
77 data[u'items'] = items 77 data['items'] = items
78 78
79 self.output(data) 79 self.output(data)
80 self.host.quit() 80 self.host.quit()
81 81
82 def default_output(self, data): 82 def default_output(self, data):
83 features = data.get(u'features', []) 83 features = data.get('features', [])
84 identities = data.get(u'identities', []) 84 identities = data.get('identities', [])
85 extensions = data.get(u'extensions', {}) 85 extensions = data.get('extensions', {})
86 items = data.get(u'items', []) 86 items = data.get('items', [])
87 87
88 identities_table = common.Table(self.host, 88 identities_table = common.Table(self.host,
89 identities, 89 identities,
90 headers=(_(u'category'), 90 headers=(_('category'),
91 _(u'type'), 91 _('type'),
92 _(u'name')), 92 _('name')),
93 use_buffer=True) 93 use_buffer=True)
94 94
95 extensions_tpl = [] 95 extensions_tpl = []
96 extensions_types = extensions.keys() 96 extensions_types = list(extensions.keys())
97 extensions_types.sort() 97 extensions_types.sort()
98 for type_ in extensions_types: 98 for type_ in extensions_types:
99 fields = [] 99 fields = []
100 for field in extensions[type_]: 100 for field in extensions[type_]:
101 field_lines = [] 101 field_lines = []
102 data, values = field 102 data, values = field
103 data_keys = data.keys() 103 data_keys = list(data.keys())
104 data_keys.sort() 104 data_keys.sort()
105 for key in data_keys: 105 for key in data_keys:
106 field_lines.append(A.color(u'\t', C.A_SUBHEADER, key, A.RESET, u': ', 106 field_lines.append(A.color('\t', C.A_SUBHEADER, key, A.RESET, ': ',
107 data[key])) 107 data[key]))
108 if len(values) == 1: 108 if len(values) == 1:
109 field_lines.append(A.color(u'\t', C.A_SUBHEADER, u"value", A.RESET, 109 field_lines.append(A.color('\t', C.A_SUBHEADER, "value", A.RESET,
110 u': ', values[0] or (A.BOLD + u"UNSET"))) 110 ': ', values[0] or (A.BOLD + "UNSET")))
111 elif len(values) > 1: 111 elif len(values) > 1:
112 field_lines.append(A.color(u'\t', C.A_SUBHEADER, u"values", A.RESET, 112 field_lines.append(A.color('\t', C.A_SUBHEADER, "values", A.RESET,
113 u': ')) 113 ': '))
114 114
115 for value in values: 115 for value in values:
116 field_lines.append(A.color(u'\t - ', A.BOLD, value)) 116 field_lines.append(A.color('\t - ', A.BOLD, value))
117 fields.append(u'\n'.join(field_lines)) 117 fields.append('\n'.join(field_lines))
118 extensions_tpl.append(u'{type_}\n{fields}'.format(type_=type_, 118 extensions_tpl.append('{type_}\n{fields}'.format(type_=type_,
119 fields='\n\n'.join(fields))) 119 fields='\n\n'.join(fields)))
120 120
121 items_table = common.Table(self.host, 121 items_table = common.Table(self.host,
122 items, 122 items,
123 headers=(_(u'entity'), 123 headers=(_('entity'),
124 _(u'node'), 124 _('node'),
125 _(u'name')), 125 _('name')),
126 use_buffer=True) 126 use_buffer=True)
127 127
128 template = [] 128 template = []
129 if features: 129 if features:
130 template.append(A.color(C.A_HEADER, _(u"Features")) + u"\n\n{features}") 130 template.append(A.color(C.A_HEADER, _("Features")) + "\n\n{features}")
131 if identities: 131 if identities:
132 template.append(A.color(C.A_HEADER, _(u"Identities")) + u"\n\n{identities}") 132 template.append(A.color(C.A_HEADER, _("Identities")) + "\n\n{identities}")
133 if extensions: 133 if extensions:
134 template.append(A.color(C.A_HEADER, _(u"Extensions")) + u"\n\n{extensions}") 134 template.append(A.color(C.A_HEADER, _("Extensions")) + "\n\n{extensions}")
135 if items: 135 if items:
136 template.append(A.color(C.A_HEADER, _(u"Items")) + u"\n\n{items}") 136 template.append(A.color(C.A_HEADER, _("Items")) + "\n\n{items}")
137 137
138 print u"\n\n".join(template).format(features = u'\n'.join(features), 138 print("\n\n".join(template).format(features = '\n'.join(features),
139 identities = identities_table.display().string, 139 identities = identities_table.display().string,
140 extensions = u'\n'.join(extensions_tpl), 140 extensions = '\n'.join(extensions_tpl),
141 items = items_table.display().string, 141 items = items_table.display().string,
142 ) 142 ))
143 143
144 144
145 class Version(base.CommandBase): 145 class Version(base.CommandBase):
146 146
147 def __init__(self, host): 147 def __init__(self, host):
155 jids = self.host.check_jids([self.args.jid]) 155 jids = self.host.check_jids([self.args.jid])
156 jid = jids[0] 156 jid = jids[0]
157 self.host.bridge.getSoftwareVersion(jid, self.host.profile, callback=self.gotVersion, errback=self.error) 157 self.host.bridge.getSoftwareVersion(jid, self.host.profile, callback=self.gotVersion, errback=self.error)
158 158
159 def error(self, failure): 159 def error(self, failure):
160 print (_("Error while trying to get version [%s]") % failure) 160 print((_("Error while trying to get version [%s]") % failure))
161 self.host.quit(1) 161 self.host.quit(1)
162 162
163 def gotVersion(self, data): 163 def gotVersion(self, data):
164 infos = [] 164 infos = []
165 name, version, os = data 165 name, version, os = data
168 if version: 168 if version:
169 infos.append(_("Software version: %s") % version) 169 infos.append(_("Software version: %s") % version)
170 if os: 170 if os:
171 infos.append(_("Operating System: %s") % os) 171 infos.append(_("Operating System: %s") % os)
172 172
173 print "\n".join(infos) 173 print("\n".join(infos))
174 self.host.quit() 174 self.host.quit()
175 175
176 176
177 class Session(base.CommandBase): 177 class Session(base.CommandBase):
178 178
181 super(Session, self).__init__(host, 'session', use_output='dict', extra_outputs=extra_outputs, help=_('running session')) 181 super(Session, self).__init__(host, 'session', use_output='dict', extra_outputs=extra_outputs, help=_('running session'))
182 self.need_loop=True 182 self.need_loop=True
183 183
184 def default_output(self, data): 184 def default_output(self, data):
185 started = data['started'] 185 started = data['started']
186 data['started'] = u'{short} (UTC, {relative})'.format( 186 data['started'] = '{short} (UTC, {relative})'.format(
187 short=date_utils.date_fmt(started), 187 short=date_utils.date_fmt(started),
188 relative=date_utils.date_fmt(started, 'relative')) 188 relative=date_utils.date_fmt(started, 'relative'))
189 self.host.output(C.OUTPUT_DICT, 'simple', {}, data) 189 self.host.output(C.OUTPUT_DICT, 'simple', {}, data)
190 190
191 def add_parser_options(self): 191 def add_parser_options(self):
197 def _sessionInfoGetCb(self, data): 197 def _sessionInfoGetCb(self, data):
198 self.output(data) 198 self.output(data)
199 self.host.quit() 199 self.host.quit()
200 200
201 def _sessionInfoGetEb(self, error_data): 201 def _sessionInfoGetEb(self, error_data):
202 self.disp(_(u'Error getting session infos: {}').format(error_data), error=True) 202 self.disp(_('Error getting session infos: {}').format(error_data), error=True)
203 self.host.quit(C.EXIT_BRIDGE_ERRBACK) 203 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
204 204
205 205
206 class Info(base.CommandBase): 206 class Info(base.CommandBase):
207 subcommands = (Disco, Version, Session) 207 subcommands = (Disco, Version, Session)