comparison sat/plugins/plugin_misc_identity.py @ 2624:56f94936df1e

code style reformatting using black
author Goffi <goffi@goffi.org>
date Wed, 27 Jun 2018 20:14:46 +0200
parents 26edcf3a30eb
children 466c9690c43a
comparison
equal deleted inserted replaced
2623:49533de4540b 2624:56f94936df1e
20 20
21 from sat.core.i18n import _ 21 from sat.core.i18n import _
22 from sat.core.constants import Const as C 22 from sat.core.constants import Const as C
23 from sat.core import exceptions 23 from sat.core import exceptions
24 from sat.core.log import getLogger 24 from sat.core.log import getLogger
25
25 log = getLogger(__name__) 26 log = getLogger(__name__)
26 from twisted.internet import defer 27 from twisted.internet import defer
27 from twisted.words.protocols.jabber import jid 28 from twisted.words.protocols.jabber import jid
28 import os.path 29 import os.path
29 30
30 31
31 PLUGIN_INFO = { 32 PLUGIN_INFO = {
32 C.PI_NAME: "Identity Plugin", 33 C.PI_NAME: "Identity Plugin",
33 C.PI_IMPORT_NAME: "IDENTITY", 34 C.PI_IMPORT_NAME: "IDENTITY",
34 C.PI_TYPE: C.PLUG_TYPE_MISC , 35 C.PI_TYPE: C.PLUG_TYPE_MISC,
35 C.PI_PROTOCOLS: [], 36 C.PI_PROTOCOLS: [],
36 C.PI_DEPENDENCIES: ["XEP-0054"], 37 C.PI_DEPENDENCIES: ["XEP-0054"],
37 C.PI_RECOMMENDATIONS: [], 38 C.PI_RECOMMENDATIONS: [],
38 C.PI_MAIN: "Identity", 39 C.PI_MAIN: "Identity",
39 C.PI_HANDLER: "no", 40 C.PI_HANDLER: "no",
40 C.PI_DESCRIPTION: _("""Identity manager""") 41 C.PI_DESCRIPTION: _("""Identity manager"""),
41 } 42 }
42 43
43 44
44 class Identity(object): 45 class Identity(object):
45
46 def __init__(self, host): 46 def __init__(self, host):
47 log.info(_(u"Plugin Identity initialization")) 47 log.info(_(u"Plugin Identity initialization"))
48 self.host = host 48 self.host = host
49 self._v = host.plugins[u'XEP-0054'] 49 self._v = host.plugins[u"XEP-0054"]
50 host.bridge.addMethod(u"identityGet", u".plugin", in_sign=u'ss', out_sign=u'a{ss}', method=self._getIdentity, async=True) 50 host.bridge.addMethod(
51 host.bridge.addMethod(u"identitySet", u".plugin", in_sign=u'a{ss}s', out_sign=u'', method=self._setIdentity, async=True) 51 u"identityGet",
52 u".plugin",
53 in_sign=u"ss",
54 out_sign=u"a{ss}",
55 method=self._getIdentity,
56 async=True,
57 )
58 host.bridge.addMethod(
59 u"identitySet",
60 u".plugin",
61 in_sign=u"a{ss}s",
62 out_sign=u"",
63 method=self._setIdentity,
64 async=True,
65 )
52 66
53 def _getIdentity(self, jid_str, profile): 67 def _getIdentity(self, jid_str, profile):
54 jid_ = jid.JID(jid_str) 68 jid_ = jid.JID(jid_str)
55 client = self.host.getClient(profile) 69 client = self.host.getClient(profile)
56 return self.getIdentity(client, jid_) 70 return self.getIdentity(client, jid_)
68 """ 82 """
69 id_data = {} 83 id_data = {}
70 # we first check roster 84 # we first check roster
71 roster_item = yield client.roster.getItem(jid_.userhostJID()) 85 roster_item = yield client.roster.getItem(jid_.userhostJID())
72 if roster_item is not None and roster_item.name: 86 if roster_item is not None and roster_item.name:
73 id_data[u'nick'] = roster_item.name 87 id_data[u"nick"] = roster_item.name
74 elif jid_.resource and self._v.isRoom(client, jid_): 88 elif jid_.resource and self._v.isRoom(client, jid_):
75 id_data[u'nick'] = jid_.resource 89 id_data[u"nick"] = jid_.resource
76 else: 90 else:
77 # and finally then vcard 91 #  and finally then vcard
78 nick = yield self._v.getNick(client, jid_) 92 nick = yield self._v.getNick(client, jid_)
79 id_data[u'nick'] = nick if nick else jid_.user.capitalize() 93 id_data[u"nick"] = nick if nick else jid_.user.capitalize()
80 94
81 try: 95 try:
82 avatar_path = id_data[u'avatar'] = yield self._v.getAvatar(client, jid_, cache_only=False) 96 avatar_path = id_data[u"avatar"] = yield self._v.getAvatar(
97 client, jid_, cache_only=False
98 )
83 except exceptions.NotFound: 99 except exceptions.NotFound:
84 pass 100 pass
85 else: 101 else:
86 if avatar_path: 102 if avatar_path:
87 id_data[u'avatar_basename'] = os.path.basename(avatar_path) 103 id_data[u"avatar_basename"] = os.path.basename(avatar_path)
88 else: 104 else:
89 del id_data[u'avatar'] 105 del id_data[u"avatar"]
90 106
91 defer.returnValue(id_data) 107 defer.returnValue(id_data)
92 108
93 def _setIdentity(self, id_data, profile): 109 def _setIdentity(self, id_data, profile):
94 client = self.host.getClient(profile) 110 client = self.host.getClient(profile)
99 115
100 @param id_data(dict[unicode, unicode]): data to update, key can be: 116 @param id_data(dict[unicode, unicode]): data to update, key can be:
101 - nick: nickname 117 - nick: nickname
102 the vCard will be updated 118 the vCard will be updated
103 """ 119 """
104 if id_data.keys() != [u'nick']: 120 if id_data.keys() != [u"nick"]:
105 raise NotImplementedError(u'Only nick can be updated for now') 121 raise NotImplementedError(u"Only nick can be updated for now")
106 if u'nick' in id_data: 122 if u"nick" in id_data:
107 return self._v.setNick(client, id_data[u'nick']) 123 return self._v.setNick(client, id_data[u"nick"])
108