comparison libervia/cli/cmd_blocking.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_blocking.py@26b7ed2817da
children
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 import json
22 import os
23 from libervia.backend.core.i18n import _
24 from libervia.backend.tools.common import data_format
25 from libervia.cli import common
26 from libervia.cli.constants import Const as C
27 from . import base
28
29 __commands__ = ["Blocking"]
30
31
32 class List(base.CommandBase):
33 def __init__(self, host):
34 super().__init__(
35 host,
36 "list",
37 use_output=C.OUTPUT_LIST,
38 help=_("list blocked entities"),
39 )
40
41 def add_parser_options(self):
42 pass
43
44 async def start(self):
45 try:
46 blocked_jids = await self.host.bridge.blocking_list(
47 self.profile,
48 )
49 except Exception as e:
50 self.disp(f"can't get blocked entities: {e}", error=True)
51 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
52 else:
53 await self.output(blocked_jids)
54 self.host.quit(C.EXIT_OK)
55
56
57 class Block(base.CommandBase):
58 def __init__(self, host):
59 super().__init__(
60 host,
61 "block",
62 help=_("block one or more entities"),
63 )
64
65 def add_parser_options(self):
66 self.parser.add_argument(
67 "entities",
68 nargs="+",
69 metavar="JID",
70 help=_("JIDs of entities to block"),
71 )
72
73 async def start(self):
74 try:
75 await self.host.bridge.blocking_block(
76 self.args.entities,
77 self.profile
78 )
79 except Exception as e:
80 self.disp(f"can't block entities: {e}", error=True)
81 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
82 else:
83 self.host.quit(C.EXIT_OK)
84
85
86 class Unblock(base.CommandBase):
87 def __init__(self, host):
88 super().__init__(
89 host,
90 "unblock",
91 help=_("unblock one or more entities"),
92 )
93
94 def add_parser_options(self):
95 self.parser.add_argument(
96 "entities",
97 nargs="+",
98 metavar="JID",
99 help=_("JIDs of entities to unblock"),
100 )
101 self.parser.add_argument(
102 "-f",
103 "--force",
104 action="store_true",
105 help=_('when "all" is used, unblock all entities without confirmation'),
106 )
107
108 async def start(self):
109 if self.args.entities == ["all"]:
110 if not self.args.force:
111 await self.host.confirm_or_quit(
112 _("All entities will be unblocked, are you sure"),
113 _("unblock cancelled")
114 )
115 self.args.entities.clear()
116 elif self.args.force:
117 self.parser.error(_('--force is only allowed when "all" is used as target'))
118
119 try:
120 await self.host.bridge.blocking_unblock(
121 self.args.entities,
122 self.profile
123 )
124 except Exception as e:
125 self.disp(f"can't unblock entities: {e}", error=True)
126 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
127 else:
128 self.host.quit(C.EXIT_OK)
129
130
131 class Blocking(base.CommandBase):
132 subcommands = (List, Block, Unblock)
133
134 def __init__(self, host):
135 super().__init__(
136 host, "blocking", use_profile=False, help=_("entities blocking")
137 )