comparison sat_frontends/jp/cmd_identity.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
19 19
20 20
21 from . import base 21 from . import base
22 from sat.core.i18n import _ 22 from sat.core.i18n import _
23 from sat_frontends.jp.constants import Const as C 23 from sat_frontends.jp.constants import Const as C
24 from functools import partial
25 24
26 __commands__ = ["Identity"] 25 __commands__ = ["Identity"]
27
28 # TODO: move date parsing to base, it may be useful for other commands
29 26
30 27
31 class Get(base.CommandBase): 28 class Get(base.CommandBase):
32 def __init__(self, host): 29 def __init__(self, host):
33 base.CommandBase.__init__( 30 base.CommandBase.__init__(
36 "get", 33 "get",
37 use_output=C.OUTPUT_DICT, 34 use_output=C.OUTPUT_DICT,
38 use_verbose=True, 35 use_verbose=True,
39 help=_("get identity data"), 36 help=_("get identity data"),
40 ) 37 )
41 self.need_loop = True
42 38
43 def add_parser_options(self): 39 def add_parser_options(self):
44 self.parser.add_argument( 40 self.parser.add_argument(
45 "jid", help=_("entity to check") 41 "jid", help=_("entity to check")
46 ) 42 )
47 43
48 def identityGetCb(self, data): 44 async def start(self):
49 self.output(data) 45 jid_ = (await self.host.check_jids([self.args.jid]))[0]
50 self.host.quit() 46 try:
51 47 data = await self.host.bridge.identityGet(
52 def start(self): 48 jid_,
53 jid_ = self.host.check_jids([self.args.jid])[0] 49 self.profile,
54 self.host.bridge.identityGet( 50 )
55 jid_, 51 except Exception as e:
56 self.profile, 52 self.disp(f"can't get identity data: {e}", error=True)
57 callback=self.identityGetCb, 53 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
58 errback=partial( 54 else:
59 self.errback, 55 await self.output(data)
60 msg=_("can't get identity data: {}"), 56 self.host.quit()
61 exit_code=C.EXIT_BRIDGE_ERRBACK,
62 ),
63 )
64 57
65 58
66 class Set(base.CommandBase): 59 class Set(base.CommandBase):
67 def __init__(self, host): 60 def __init__(self, host):
68 super(Set, self).__init__(host, "set", help=_("modify an existing event")) 61 super(Set, self).__init__(host, "set", help=_("modify an existing event"))
76 dest="fields", 69 dest="fields",
77 metavar=("KEY", "VALUE"), 70 metavar=("KEY", "VALUE"),
78 required=True, 71 required=True,
79 help=_("identity field(s) to set"), 72 help=_("identity field(s) to set"),
80 ) 73 )
81 self.need_loop = True
82 74
83 def start(self): 75 async def start(self):
84 fields = dict(self.args.fields) 76 fields = dict(self.args.fields)
85 self.host.bridge.identitySet( 77 try:
86 fields, 78 self.host.bridge.identitySet(
87 self.profile, 79 fields,
88 callback=self.host.quit, 80 self.profile,
89 errback=partial( 81 )
90 self.errback, 82 except Exception as e:
91 msg=_("can't set identity data data: {}"), 83 self.disp(f"can't set identity data: {e}", error=True)
92 exit_code=C.EXIT_BRIDGE_ERRBACK, 84 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
93 ), 85 else:
94 ) 86 self.host.quit()
95 87
96 88
97 class Identity(base.CommandBase): 89 class Identity(base.CommandBase):
98 subcommands = (Get, Set) 90 subcommands = (Get, Set)
99 91