annotate libervia/cli/cmd_blocking.py @ 4180:b86912d3fd33

plugin IP: fix use of legacy URL + coroutine use: An https:/salut-a-toi.org URL was used to retrieve external IP, but it's not valid anymore, resulting in an exception. This feature is currently disabled. Also moved several methods from legacy inline callbacks to coroutines.
author Goffi <goffi@goffi.org>
date Sat, 09 Dec 2023 14:30:54 +0100
parents 47401850dec6
children 0d7bb4df2343
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3788
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python3
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
2
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
4075
47401850dec6 refactoring: rename `libervia.frontends.jp` to `libervia.cli`
Goffi <goffi@goffi.org>
parents: 4074
diff changeset
4 # Libervia CLI
3788
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
6
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
19
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
20
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 import json
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 import os
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
23 from libervia.backend.core.i18n import _
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
24 from libervia.backend.tools.common import data_format
4075
47401850dec6 refactoring: rename `libervia.frontends.jp` to `libervia.cli`
Goffi <goffi@goffi.org>
parents: 4074
diff changeset
25 from libervia.cli import common
47401850dec6 refactoring: rename `libervia.frontends.jp` to `libervia.cli`
Goffi <goffi@goffi.org>
parents: 4074
diff changeset
26 from libervia.cli.constants import Const as C
3788
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from . import base
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
28
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 __commands__ = ["Blocking"]
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
30
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
31
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 class List(base.CommandBase):
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 def __init__(self, host):
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 super().__init__(
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 host,
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 "list",
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 use_output=C.OUTPUT_LIST,
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 help=_("list blocked entities"),
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 )
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
40
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 def add_parser_options(self):
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 pass
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
43
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 async def start(self):
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 try:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3788
diff changeset
46 blocked_jids = await self.host.bridge.blocking_list(
3788
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 self.profile,
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 )
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 except Exception as e:
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 self.disp(f"can't get blocked entities: {e}", error=True)
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 else:
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 await self.output(blocked_jids)
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 self.host.quit(C.EXIT_OK)
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
55
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
56
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 class Block(base.CommandBase):
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 def __init__(self, host):
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 super().__init__(
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 host,
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 "block",
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 help=_("block one or more entities"),
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 )
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
64
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 def add_parser_options(self):
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 self.parser.add_argument(
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 "entities",
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 nargs="+",
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 metavar="JID",
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 help=_("JIDs of entities to block"),
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 )
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
72
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 async def start(self):
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 try:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3788
diff changeset
75 await self.host.bridge.blocking_block(
3788
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 self.args.entities,
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 self.profile
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 )
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 except Exception as e:
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 self.disp(f"can't block entities: {e}", error=True)
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 else:
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 self.host.quit(C.EXIT_OK)
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
84
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
85
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 class Unblock(base.CommandBase):
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 def __init__(self, host):
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 super().__init__(
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 host,
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 "unblock",
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 help=_("unblock one or more entities"),
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 )
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
93
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
94 def add_parser_options(self):
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
95 self.parser.add_argument(
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 "entities",
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 nargs="+",
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 metavar="JID",
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 help=_("JIDs of entities to unblock"),
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 )
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 self.parser.add_argument(
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 "-f",
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 "--force",
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
104 action="store_true",
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 help=_('when "all" is used, unblock all entities without confirmation'),
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
106 )
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
107
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 async def start(self):
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
109 if self.args.entities == ["all"]:
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 if not self.args.force:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3788
diff changeset
111 await self.host.confirm_or_quit(
3788
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 _("All entities will be unblocked, are you sure"),
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
113 _("unblock cancelled")
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 )
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
115 self.args.entities.clear()
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
116 elif self.args.force:
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
117 self.parser.error(_('--force is only allowed when "all" is used as target'))
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
118
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 try:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3788
diff changeset
120 await self.host.bridge.blocking_unblock(
3788
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
121 self.args.entities,
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
122 self.profile
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 )
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 except Exception as e:
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 self.disp(f"can't unblock entities: {e}", error=True)
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
126 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
127 else:
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
128 self.host.quit(C.EXIT_OK)
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
129
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
130
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
131 class Blocking(base.CommandBase):
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 subcommands = (List, Block, Unblock)
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
133
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
134 def __init__(self, host):
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
135 super().__init__(
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
136 host, "blocking", use_profile=False, help=_("entities blocking")
12317ba98d99 cli: new `blocking` commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
137 )