comparison sat/plugins/plugin_misc_identity.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 9d0df638c8b4
comparison
equal deleted inserted replaced
3027:ff5bcb12ae60 3028:ab2696e34d29
1 #!/usr/bin/env python2 1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*- 2 # -*- coding: utf-8 -*-
3 3
4 # SAT plugin for managing xep-0054 4 # SAT plugin for managing xep-0054
5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org) 5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org)
6 # Copyright (C) 2014 Emmanuel Gil Peyrot (linkmauve@linkmauve.fr) 6 # Copyright (C) 2014 Emmanuel Gil Peyrot (linkmauve@linkmauve.fr)
42 } 42 }
43 43
44 44
45 class Identity(object): 45 class Identity(object):
46 def __init__(self, host): 46 def __init__(self, host):
47 log.info(_(u"Plugin Identity initialization")) 47 log.info(_("Plugin Identity initialization"))
48 self.host = host 48 self.host = host
49 self._v = host.plugins[u"XEP-0054"] 49 self._v = host.plugins["XEP-0054"]
50 host.bridge.addMethod( 50 host.bridge.addMethod(
51 u"identityGet", 51 "identityGet",
52 u".plugin", 52 ".plugin",
53 in_sign=u"ss", 53 in_sign="ss",
54 out_sign=u"a{ss}", 54 out_sign="a{ss}",
55 method=self._getIdentity, 55 method=self._getIdentity,
56 async=True, 56 async_=True,
57 ) 57 )
58 host.bridge.addMethod( 58 host.bridge.addMethod(
59 u"identitySet", 59 "identitySet",
60 u".plugin", 60 ".plugin",
61 in_sign=u"a{ss}s", 61 in_sign="a{ss}s",
62 out_sign=u"", 62 out_sign="",
63 method=self._setIdentity, 63 method=self._setIdentity,
64 async=True, 64 async_=True,
65 ) 65 )
66 66
67 def _getIdentity(self, jid_str, profile): 67 def _getIdentity(self, jid_str, profile):
68 jid_ = jid.JID(jid_str) 68 jid_ = jid.JID(jid_str)
69 client = self.host.getClient(profile) 69 client = self.host.getClient(profile)
82 """ 82 """
83 id_data = {} 83 id_data = {}
84 # we first check roster 84 # we first check roster
85 roster_item = yield client.roster.getItem(jid_.userhostJID()) 85 roster_item = yield client.roster.getItem(jid_.userhostJID())
86 if roster_item is not None and roster_item.name: 86 if roster_item is not None and roster_item.name:
87 id_data[u"nick"] = roster_item.name 87 id_data["nick"] = roster_item.name
88 elif jid_.resource and self._v.isRoom(client, jid_): 88 elif jid_.resource and self._v.isRoom(client, jid_):
89 id_data[u"nick"] = jid_.resource 89 id_data["nick"] = jid_.resource
90 else: 90 else:
91 #  and finally then vcard 91 #  and finally then vcard
92 nick = yield self._v.getNick(client, jid_) 92 nick = yield self._v.getNick(client, jid_)
93 if nick: 93 if nick:
94 id_data[u"nick"] = nick 94 id_data["nick"] = nick
95 elif jid_.user: 95 elif jid_.user:
96 id_data[u"nick"] = jid_.user.capitalize() 96 id_data["nick"] = jid_.user.capitalize()
97 else: 97 else:
98 id_data[u"nick"] = jid_.userhost() 98 id_data["nick"] = jid_.userhost()
99 99
100 try: 100 try:
101 avatar_path = id_data[u"avatar"] = yield self._v.getAvatar( 101 avatar_path = id_data["avatar"] = yield self._v.getAvatar(
102 client, jid_, cache_only=False 102 client, jid_, cache_only=False
103 ) 103 )
104 except exceptions.NotFound: 104 except exceptions.NotFound:
105 pass 105 pass
106 else: 106 else:
107 if avatar_path: 107 if avatar_path:
108 id_data[u"avatar_basename"] = os.path.basename(avatar_path) 108 id_data["avatar_basename"] = os.path.basename(avatar_path)
109 else: 109 else:
110 del id_data[u"avatar"] 110 del id_data["avatar"]
111 111
112 defer.returnValue(id_data) 112 defer.returnValue(id_data)
113 113
114 def _setIdentity(self, id_data, profile): 114 def _setIdentity(self, id_data, profile):
115 client = self.host.getClient(profile) 115 client = self.host.getClient(profile)
120 120
121 @param id_data(dict[unicode, unicode]): data to update, key can be: 121 @param id_data(dict[unicode, unicode]): data to update, key can be:
122 - nick: nickname 122 - nick: nickname
123 the vCard will be updated 123 the vCard will be updated
124 """ 124 """
125 if id_data.keys() != [u"nick"]: 125 if list(id_data.keys()) != ["nick"]:
126 raise NotImplementedError(u"Only nick can be updated for now") 126 raise NotImplementedError("Only nick can be updated for now")
127 if u"nick" in id_data: 127 if "nick" in id_data:
128 return self._v.setNick(client, id_data[u"nick"]) 128 return self._v.setNick(client, id_data["nick"])