comparison libervia/cli/cmd_identity.py @ 4075:47401850dec6

refactoring: rename `libervia.frontends.jp` to `libervia.cli`
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 14:54:26 +0200
parents libervia/frontends/jp/cmd_identity.py@26b7ed2817da
children 0d7bb4df2343
comparison
equal deleted inserted replaced
4074:26b7ed2817da 4075:47401850dec6
1 #!/usr/bin/env python3
2
3
4 # Libervia CLI
5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
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/>.
19
20
21 from . import base
22 from libervia.backend.core.i18n import _
23 from libervia.cli.constants import Const as C
24 from libervia.backend.tools.common import data_format
25
26 __commands__ = ["Identity"]
27
28
29 class Get(base.CommandBase):
30 def __init__(self, host):
31 base.CommandBase.__init__(
32 self,
33 host,
34 "get",
35 use_output=C.OUTPUT_DICT,
36 use_verbose=True,
37 help=_("get identity data"),
38 )
39
40 def add_parser_options(self):
41 self.parser.add_argument(
42 "--no-cache", action="store_true", help=_("do no use cached values")
43 )
44 self.parser.add_argument(
45 "jid", help=_("entity to check")
46 )
47
48 async def start(self):
49 jid_ = (await self.host.check_jids([self.args.jid]))[0]
50 try:
51 data = await self.host.bridge.identity_get(
52 jid_,
53 [],
54 not self.args.no_cache,
55 self.profile
56 )
57 except Exception as e:
58 self.disp(f"can't get identity data: {e}", error=True)
59 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
60 else:
61 data = data_format.deserialise(data)
62 await self.output(data)
63 self.host.quit()
64
65
66 class Set(base.CommandBase):
67 def __init__(self, host):
68 super(Set, self).__init__(host, "set", help=_("update identity data"))
69
70 def add_parser_options(self):
71 self.parser.add_argument(
72 "-n",
73 "--nickname",
74 action="append",
75 metavar="NICKNAME",
76 dest="nicknames",
77 help=_("nicknames of the entity"),
78 )
79 self.parser.add_argument(
80 "-d",
81 "--description",
82 help=_("description of the entity"),
83 )
84
85 async def start(self):
86 id_data = {}
87 for field in ("nicknames", "description"):
88 value = getattr(self.args, field)
89 if value is not None:
90 id_data[field] = value
91 if not id_data:
92 self.parser.error("At least one metadata must be set")
93 try:
94 self.host.bridge.identity_set(
95 data_format.serialise(id_data),
96 self.profile,
97 )
98 except Exception as e:
99 self.disp(f"can't set identity data: {e}", error=True)
100 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
101 else:
102 self.host.quit()
103
104
105 class Identity(base.CommandBase):
106 subcommands = (Get, Set)
107
108 def __init__(self, host):
109 super(Identity, self).__init__(
110 host, "identity", use_profile=False, help=_("identity management")
111 )