comparison sat_frontends/jp/cmd_blocking.py @ 3788:12317ba98d99

cli: new `blocking` commands: commands to list blocked entities, block or unblock one or more entities rel 367
author Goffi <goffi@goffi.org>
date Fri, 27 May 2022 12:15:04 +0200
parents
children 524856bd7b19
comparison
equal deleted inserted replaced
3787:f8a0f3b65371 3788:12317ba98d99
1 #!/usr/bin/env python3
2
3
4 # jp: a SàT command line tool
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 sat.core.i18n import _
24 from sat.tools.common import data_format
25 from sat_frontends.jp import common
26 from sat_frontends.jp.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.blockingList(
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.blockingBlock(
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.confirmOrQuit(
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.blockingUnblock(
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 )