comparison sat/plugins/plugin_xep_0092.py @ 4037:524856bd7b19

massive refactoring to switch from camelCase to snake_case: historically, Libervia (SàT before) was using camelCase as allowed by PEP8 when using a pre-PEP8 code, to use the same coding style as in Twisted. However, snake_case is more readable and it's better to follow PEP8 best practices, so it has been decided to move on full snake_case. Because Libervia has a huge codebase, this ended with a ugly mix of camelCase and snake_case. To fix that, this patch does a big refactoring by renaming every function and method (including bridge) that are not coming from Twisted or Wokkel, to use fully snake_case. This is a massive change, and may result in some bugs.
author Goffi <goffi@goffi.org>
date Sat, 08 Apr 2023 13:54:42 +0200
parents be6d91572633
children e75827204fe0
comparison
equal deleted inserted replaced
4036:c4464d7ae97b 4037:524856bd7b19
45 45
46 class XEP_0092(object): 46 class XEP_0092(object):
47 def __init__(self, host): 47 def __init__(self, host):
48 log.info(_("Plugin XEP_0092 initialization")) 48 log.info(_("Plugin XEP_0092 initialization"))
49 self.host = host 49 self.host = host
50 host.bridge.addMethod( 50 host.bridge.add_method(
51 "getSoftwareVersion", 51 "software_version_get",
52 ".plugin", 52 ".plugin",
53 in_sign="ss", 53 in_sign="ss",
54 out_sign="(sss)", 54 out_sign="(sss)",
55 method=self._getVersion, 55 method=self._get_version,
56 async_=True, 56 async_=True,
57 ) 57 )
58 try: 58 try:
59 self.host.plugins[C.TEXT_CMDS].addWhoIsCb(self._whois, 50) 59 self.host.plugins[C.TEXT_CMDS].add_who_is_cb(self._whois, 50)
60 except KeyError: 60 except KeyError:
61 log.info(_("Text commands not available")) 61 log.info(_("Text commands not available"))
62 62
63 def _getVersion(self, entity_jid_s, profile_key): 63 def _get_version(self, entity_jid_s, profile_key):
64 def prepareForBridge(data): 64 def prepare_for_bridge(data):
65 name, version, os = data 65 name, version, os = data
66 return (name or "", version or "", os or "") 66 return (name or "", version or "", os or "")
67 67
68 d = self.getVersion(jid.JID(entity_jid_s), profile_key) 68 d = self.version_get(jid.JID(entity_jid_s), profile_key)
69 d.addCallback(prepareForBridge) 69 d.addCallback(prepare_for_bridge)
70 return d 70 return d
71 71
72 def getVersion(self, jid_, profile_key=C.PROF_KEY_NONE): 72 def version_get(self, jid_, profile_key=C.PROF_KEY_NONE):
73 """ Ask version of the client that jid_ is running 73 """ Ask version of the client that jid_ is running
74 @param jid_: jid from who we want to know client's version 74 @param jid_: jid from who we want to know client's version
75 @param profile_key: %(doc_profile_key)s 75 @param profile_key: %(doc_profile_key)s
76 @return (tuple): a defered which fire a tuple with the following data (None if not available): 76 @return (tuple): a defered which fire a tuple with the following data (None if not available):
77 - name: Natural language name of the software 77 - name: Natural language name of the software
78 - version: specific version of the software 78 - version: specific version of the software
79 - os: operating system of the queried entity 79 - os: operating system of the queried entity
80 """ 80 """
81 client = self.host.getClient(profile_key) 81 client = self.host.get_client(profile_key)
82 82
83 def getVersion(__): 83 def version_get(__):
84 iq_elt = compat.IQ(client.xmlstream, "get") 84 iq_elt = compat.IQ(client.xmlstream, "get")
85 iq_elt["to"] = jid_.full() 85 iq_elt["to"] = jid_.full()
86 iq_elt.addElement("query", NS_VERSION) 86 iq_elt.addElement("query", NS_VERSION)
87 d = iq_elt.send() 87 d = iq_elt.send()
88 d.addCallback(self._gotVersion) 88 d.addCallback(self._got_version)
89 return d 89 return d
90 90
91 d = self.host.checkFeature(client, NS_VERSION, jid_) 91 d = self.host.check_feature(client, NS_VERSION, jid_)
92 d.addCallback(getVersion) 92 d.addCallback(version_get)
93 reactor.callLater( 93 reactor.callLater(
94 TIMEOUT, d.cancel 94 TIMEOUT, d.cancel
95 ) # XXX: timeout needed because some clients don't answer the IQ 95 ) # XXX: timeout needed because some clients don't answer the IQ
96 return d 96 return d
97 97
98 def _gotVersion(self, iq_elt): 98 def _got_version(self, iq_elt):
99 try: 99 try:
100 query_elt = next(iq_elt.elements(NS_VERSION, "query")) 100 query_elt = next(iq_elt.elements(NS_VERSION, "query"))
101 except StopIteration: 101 except StopIteration:
102 raise exceptions.DataError 102 raise exceptions.DataError
103 ret = [] 103 ret = []
111 return tuple(ret) 111 return tuple(ret)
112 112
113 def _whois(self, client, whois_msg, mess_data, target_jid): 113 def _whois(self, client, whois_msg, mess_data, target_jid):
114 """ Add software/OS information to whois """ 114 """ Add software/OS information to whois """
115 115
116 def versionCb(version_data): 116 def version_cb(version_data):
117 name, version, os = version_data 117 name, version, os = version_data
118 if name: 118 if name:
119 whois_msg.append(_("Client name: %s") % name) 119 whois_msg.append(_("Client name: %s") % name)
120 if version: 120 if version:
121 whois_msg.append(_("Client version: %s") % version) 121 whois_msg.append(_("Client version: %s") % version)
122 if os: 122 if os:
123 whois_msg.append(_("Operating system: %s") % os) 123 whois_msg.append(_("Operating system: %s") % os)
124 124
125 def versionEb(failure): 125 def version_eb(failure):
126 failure.trap(exceptions.FeatureNotFound, defer.CancelledError) 126 failure.trap(exceptions.FeatureNotFound, defer.CancelledError)
127 if failure.check(failure, exceptions.FeatureNotFound): 127 if failure.check(failure, exceptions.FeatureNotFound):
128 whois_msg.append(_("Software version not available")) 128 whois_msg.append(_("Software version not available"))
129 else: 129 else:
130 whois_msg.append(_("Client software version request timeout")) 130 whois_msg.append(_("Client software version request timeout"))
131 131
132 d = self.getVersion(target_jid, client.profile) 132 d = self.version_get(target_jid, client.profile)
133 d.addCallbacks(versionCb, versionEb) 133 d.addCallbacks(version_cb, version_eb)
134 return d 134 return d