Mercurial > libervia-backend
comparison libervia/cli/cmd_forums.py @ 4191:5d056d524298
core, doc, cli (forums): new `forums set` commands + doc:
- document the fact that if an empty `uri` is used, the forum node is created
automatically
- new `forums/set` CLI commands and its documentation
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 11 Dec 2023 18:10:27 +0100 |
parents | 47401850dec6 |
children |
comparison
equal
deleted
inserted
replaced
4190:92551baea115 | 4191:5d056d524298 |
---|---|
16 | 16 |
17 # You should have received a copy of the GNU Affero General Public License | 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/>. | 18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 | 19 |
20 | 20 |
21 import sys | |
21 from . import base | 22 from . import base |
22 from libervia.backend.core.i18n import _ | 23 from libervia.backend.core.i18n import _ |
23 from libervia.cli.constants import Const as C | 24 from libervia.cli.constants import Const as C |
24 from libervia.cli import common | 25 from libervia.cli import common |
25 from libervia.backend.tools.common.ansi import ANSI as A | 26 from libervia.backend.tools.common.ansi import ANSI as A |
27 from libervia.frontends.bridge.bridge_frontend import BridgeException | |
26 import codecs | 28 import codecs |
27 import json | 29 import json |
28 | 30 |
29 __commands__ = ["Forums"] | 31 __commands__ = ["Forums"] |
30 | 32 |
80 self.args.service, | 82 self.args.service, |
81 self.args.node, | 83 self.args.node, |
82 self.args.key, | 84 self.args.key, |
83 self.profile, | 85 self.profile, |
84 ) | 86 ) |
85 except Exception as e: | 87 except BridgeException as e: |
86 if e.classname == "NotFound": | 88 if e.classname == "NotFound": |
87 forums_json = "" | 89 forums_json = "" |
88 else: | 90 else: |
89 self.disp(f"can't get node configuration: {e}", error=True) | 91 self.disp(f"can't get node configuration: {e}", error=True) |
90 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | 92 self.host.quit(C.EXIT_BRIDGE_ERRBACK) |
167 if not forums_raw: | 169 if not forums_raw: |
168 self.disp(_("no schema found"), 1) | 170 self.disp(_("no schema found"), 1) |
169 self.host.quit(1) | 171 self.host.quit(1) |
170 forums = json.loads(forums_raw) | 172 forums = json.loads(forums_raw) |
171 await self.output(forums) | 173 await self.output(forums) |
174 | |
175 | |
176 class Set(base.CommandBase): | |
177 | |
178 def __init__(self, host): | |
179 base.CommandBase.__init__( | |
180 self, | |
181 host, | |
182 "set", | |
183 use_pubsub=True, | |
184 help=_("set forums"), | |
185 ) | |
186 | |
187 def add_parser_options(self): | |
188 self.parser.add_argument( | |
189 "-k", | |
190 "--key", | |
191 default="", | |
192 help=_("forum key (DEFAULT: default forums)"), | |
193 ) | |
194 | |
195 async def start(self): | |
196 forums_raw = sys.stdin.read() | |
197 try: | |
198 json.loads(forums_raw) | |
199 except Exception as e: | |
200 self.parser.error(f"Invalid JSON, a valid JSON must be used as input: {e}") | |
201 try: | |
202 await self.host.bridge.forums_set( | |
203 forums_raw, | |
204 self.args.service, | |
205 self.args.node, | |
206 self.args.key, | |
207 self.profile, | |
208 ) | |
209 except Exception as e: | |
210 self.disp(f"can't set forums: {e}", error=True) | |
211 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
212 else: | |
213 self.disp(_("forums have been set")) | |
172 self.host.quit() | 214 self.host.quit() |
173 | 215 |
174 | 216 |
175 class Forums(base.CommandBase): | 217 class Forums(base.CommandBase): |
176 subcommands = (Get, Edit) | 218 subcommands = (Get, Set, Edit) |
177 | 219 |
178 def __init__(self, host): | 220 def __init__(self, host): |
179 super(Forums, self).__init__( | 221 super(Forums, self).__init__( |
180 host, "forums", use_profile=False, help=_("Forums structure edition") | 222 host, "forums", use_profile=False, help=_("Forums structure edition") |
181 ) | 223 ) |