diff 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
line wrap: on
line diff
--- a/sat_frontends/jp/cmd_info.py	Wed Jul 31 11:31:22 2019 +0200
+++ b/sat_frontends/jp/cmd_info.py	Tue Aug 13 19:08:41 2019 +0200
@@ -17,7 +17,7 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-import base
+from . import base
 from sat.core.i18n import _
 from sat.tools.common.ansi import ANSI as A
 from sat.tools.common import date_utils
@@ -35,10 +35,10 @@
         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"))
+        self.parser.add_argument("jid", help=_("entity to discover"))
+        self.parser.add_argument("-t", "--type", type=str, choices=('infos', 'items', 'both'), default='both', help=_("type of data to discover"))
+        self.parser.add_argument("-n", "--node", default='', help=_("node to use"))
+        self.parser.add_argument("-C", "--no-cache", dest='use_cache', action="store_false", help=_("ignore cache"))
 
     def start(self):
         self.get_infos = self.args.type in ('infos', 'both')
@@ -51,7 +51,7 @@
             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)
+        print((_("Error while doing discovery [%s]") % failure))
         self.host.quit(1)
 
     def gotInfos(self, infos, jid):
@@ -68,78 +68,78 @@
             features.sort()
             identities.sort(key=lambda identity: identity[2])
             data.update({
-                u'features': features,
-                u'identities': identities,
-                u'extensions': extensions})
+                'features': features,
+                'identities': identities,
+                'extensions': extensions})
 
         if self.get_items:
             items.sort(key=lambda item: item[2])
-            data[u'items'] = items
+            data['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', [])
+        features = data.get('features', [])
+        identities = data.get('identities', [])
+        extensions = data.get('extensions', {})
+        items = data.get('items', [])
 
         identities_table = common.Table(self.host,
                                         identities,
-                                        headers=(_(u'category'),
-                                                 _(u'type'),
-                                                 _(u'name')),
+                                        headers=(_('category'),
+                                                 _('type'),
+                                                 _('name')),
                                         use_buffer=True)
 
         extensions_tpl = []
-        extensions_types = extensions.keys()
+        extensions_types = list(extensions.keys())
         extensions_types.sort()
         for type_ in extensions_types:
             fields = []
             for field in extensions[type_]:
                 field_lines = []
                 data, values = field
-                data_keys = data.keys()
+                data_keys = list(data.keys())
                 data_keys.sort()
                 for key in data_keys:
-                    field_lines.append(A.color(u'\t', C.A_SUBHEADER, key, A.RESET, u': ',
+                    field_lines.append(A.color('\t', C.A_SUBHEADER, key, A.RESET, ': ',
                                                data[key]))
                 if len(values) == 1:
-                    field_lines.append(A.color(u'\t', C.A_SUBHEADER, u"value", A.RESET,
-                                               u': ', values[0] or (A.BOLD + u"UNSET")))
+                    field_lines.append(A.color('\t', C.A_SUBHEADER, "value", A.RESET,
+                                               ': ', values[0] or (A.BOLD + "UNSET")))
                 elif len(values) > 1:
-                    field_lines.append(A.color(u'\t', C.A_SUBHEADER, u"values", A.RESET,
-                                               u': '))
+                    field_lines.append(A.color('\t', C.A_SUBHEADER, "values", A.RESET,
+                                               ': '))
 
                     for value in values:
-                        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_,
+                        field_lines.append(A.color('\t  - ', A.BOLD, value))
+                fields.append('\n'.join(field_lines))
+            extensions_tpl.append('{type_}\n{fields}'.format(type_=type_,
                                                               fields='\n\n'.join(fields)))
 
         items_table = common.Table(self.host,
                                    items,
-                                   headers=(_(u'entity'),
-                                            _(u'node'),
-                                            _(u'name')),
+                                   headers=(_('entity'),
+                                            _('node'),
+                                            _('name')),
                                    use_buffer=True)
 
         template = []
         if features:
-            template.append(A.color(C.A_HEADER, _(u"Features")) + u"\n\n{features}")
+            template.append(A.color(C.A_HEADER, _("Features")) + "\n\n{features}")
         if identities:
-            template.append(A.color(C.A_HEADER, _(u"Identities")) + u"\n\n{identities}")
+            template.append(A.color(C.A_HEADER, _("Identities")) + "\n\n{identities}")
         if extensions:
-            template.append(A.color(C.A_HEADER, _(u"Extensions")) + u"\n\n{extensions}")
+            template.append(A.color(C.A_HEADER, _("Extensions")) + "\n\n{extensions}")
         if items:
-            template.append(A.color(C.A_HEADER, _(u"Items")) + u"\n\n{items}")
+            template.append(A.color(C.A_HEADER, _("Items")) + "\n\n{items}")
 
-        print u"\n\n".join(template).format(features = u'\n'.join(features),
+        print("\n\n".join(template).format(features = '\n'.join(features),
                                             identities = identities_table.display().string,
-                                            extensions = u'\n'.join(extensions_tpl),
+                                            extensions = '\n'.join(extensions_tpl),
                                             items = items_table.display().string,
-                                           )
+                                           ))
 
 
 class Version(base.CommandBase):
@@ -157,7 +157,7 @@
         self.host.bridge.getSoftwareVersion(jid, self.host.profile, callback=self.gotVersion, errback=self.error)
 
     def error(self, failure):
-        print (_("Error while trying to get version [%s]") % failure)
+        print((_("Error while trying to get version [%s]") % failure))
         self.host.quit(1)
 
     def gotVersion(self, data):
@@ -170,7 +170,7 @@
         if os:
             infos.append(_("Operating System: %s") %  os)
 
-        print "\n".join(infos)
+        print("\n".join(infos))
         self.host.quit()
 
 
@@ -183,7 +183,7 @@
 
     def default_output(self, data):
         started = data['started']
-        data['started'] = u'{short} (UTC, {relative})'.format(
+        data['started'] = '{short} (UTC, {relative})'.format(
             short=date_utils.date_fmt(started),
             relative=date_utils.date_fmt(started, 'relative'))
         self.host.output(C.OUTPUT_DICT, 'simple', {}, data)
@@ -199,7 +199,7 @@
         self.host.quit()
 
     def _sessionInfoGetEb(self, error_data):
-        self.disp(_(u'Error getting session infos: {}').format(error_data), error=True)
+        self.disp(_('Error getting session infos: {}').format(error_data), error=True)
         self.host.quit(C.EXIT_BRIDGE_ERRBACK)