comparison sat_frontends/jp/cmd_ping.py @ 3040:fee60f17ebac

jp: jp asyncio port: /!\ this commit is huge. Jp is temporarily not working with `dbus` bridge /!\ This patch implements the port of jp to asyncio, so it is now correctly using the bridge asynchronously, and it can be used with bridges like `pb`. This also simplify the code, notably for things which were previously implemented with many callbacks (like pagination with RSM). During the process, some behaviours have been modified/fixed, in jp and backends, check diff for details.
author Goffi <goffi@goffi.org>
date Wed, 25 Sep 2019 08:56:41 +0200
parents ab2696e34d29
children 9d0df638c8b4
comparison
equal deleted inserted replaced
3039:a1bc34f90fa5 3040:fee60f17ebac
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 from . import base 20 from . import base
21 from sat.core.i18n import _ 21 from sat.core.i18n import _
22 from sat_frontends.jp.constants import Const as C
22 23
23 __commands__ = ["Ping"] 24 __commands__ = ["Ping"]
24 25
25 26
26 class Ping(base.CommandBase): 27 class Ping(base.CommandBase):
27 28
28 def __init__(self, host): 29 def __init__(self, host):
29 super(Ping, self).__init__(host, 'ping', help=_('ping XMPP entity')) 30 super(Ping, self).__init__(host, 'ping', help=_('ping XMPP entity'))
30 self.need_loop=True
31 31
32 def add_parser_options(self): 32 def add_parser_options(self):
33 self.parser.add_argument( 33 self.parser.add_argument(
34 "jid", help=_("jid to ping") 34 "jid", help=_("jid to ping")
35 ) 35 )
36 self.parser.add_argument( 36 self.parser.add_argument(
37 "-d", "--delay-only", action="store_true", help=_("output delay only (in s)") 37 "-d", "--delay-only", action="store_true", help=_("output delay only (in s)")
38 ) 38 )
39 39
40 def _pingCb(self, pong_time): 40 async def start(self):
41 fmt = "{time}" if self.args.delay_only else "PONG ({time} s)" 41 try:
42 self.disp(fmt.format(time=pong_time)) 42 pong_time = await self.host.bridge.ping(self.args.jid, self.profile)
43 self.host.quit() 43 except Exception as e:
44 44 self.disp(msg=_(f"can't do the ping: {e}"), error=True)
45 def start(self): 45 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
46 self.host.bridge.ping(self.args.jid, self.profile, 46 else:
47 callback=self._pingCb, errback=self.errback) 47 msg = pong_time if self.args.delay_only else f"PONG ({pong_time} s)"
48 self.disp(msg)
49 self.host.quit()