annotate libervia/cli/cmd_forums.py @ 4206:0f8ea0768a3b

cli (call): implement GUI output: ``call`` commands now handle various output. Beside the original one (now named ``simple``), a new ``gui`` one display a full featured GUI (make with Qt). PyQt 6 or more needs to be installed. rel 427
author Goffi <goffi@goffi.org>
date Sun, 11 Feb 2024 23:20:24 +0100
parents 5d056d524298
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3137
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
1 #!/usr/bin/env python3
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
2
2485
512c443a58ba jp (forums): forums handling commands, first draft
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
3479
be6d91572633 date update
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
2485
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
6
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
11
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
16
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
19
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
20
4191
5d056d524298 core, doc, cli (forums): new `forums set` commands + doc:
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
21 import sys
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
22 from . import base
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
23 from libervia.backend.core.i18n import _
4075
47401850dec6 refactoring: rename `libervia.frontends.jp` to `libervia.cli`
Goffi <goffi@goffi.org>
parents: 4074
diff changeset
24 from libervia.cli.constants import Const as C
47401850dec6 refactoring: rename `libervia.frontends.jp` to `libervia.cli`
Goffi <goffi@goffi.org>
parents: 4074
diff changeset
25 from libervia.cli import common
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
26 from libervia.backend.tools.common.ansi import ANSI as A
4191
5d056d524298 core, doc, cli (forums): new `forums set` commands + doc:
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
27 from libervia.frontends.bridge.bridge_frontend import BridgeException
2608
0883bac573fd jp (forums/edit): fixed unicode when dumping in json
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
28 import codecs
2485
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
29 import json
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
30
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
31 __commands__ = ["Forums"]
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
32
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
33 FORUMS_TMP_DIR = "forums"
2485
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
34
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
35
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
36 class Edit(base.CommandBase, common.BaseEdit):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2608
diff changeset
37 use_items = False
2485
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
38
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
39 def __init__(self, host):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2608
diff changeset
40 base.CommandBase.__init__(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2608
diff changeset
41 self,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2608
diff changeset
42 host,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2608
diff changeset
43 "edit",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2608
diff changeset
44 use_pubsub=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2608
diff changeset
45 use_draft=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2608
diff changeset
46 use_verbose=True,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
47 help=_("edit forums"),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2608
diff changeset
48 )
2485
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
49 common.BaseEdit.__init__(self, self.host, FORUMS_TMP_DIR)
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
50
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
51 def add_parser_options(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2608
diff changeset
52 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2608
diff changeset
53 "-k",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2608
diff changeset
54 "--key",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
55 default="",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
56 help=_("forum key (DEFAULT: default forums)"),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2608
diff changeset
57 )
2485
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
58
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
59 def get_tmp_suff(self):
2485
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
60 """return suffix used for content file"""
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
61 return "json"
2485
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
62
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
63 async def publish(self, forums_raw):
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
64 try:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
65 await self.host.bridge.forums_set(
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
66 forums_raw,
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
67 self.args.service,
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
68 self.args.node,
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
69 self.args.key,
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
70 self.profile,
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
71 )
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
72 except Exception as e:
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
73 self.disp(f"can't set forums: {e}", error=True)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
74 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
75 else:
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
76 self.disp(_("forums have been edited"), 1)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
77 self.host.quit()
2485
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
78
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
79 async def start(self):
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
80 try:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
81 forums_json = await self.host.bridge.forums_get(
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
82 self.args.service,
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
83 self.args.node,
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
84 self.args.key,
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
85 self.profile,
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
86 )
4191
5d056d524298 core, doc, cli (forums): new `forums set` commands + doc:
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
87 except BridgeException as e:
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
88 if e.classname == "NotFound":
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
89 forums_json = ""
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
90 else:
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
91 self.disp(f"can't get node configuration: {e}", error=True)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
92 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
2485
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
93
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
94 content_file_obj, content_file_path = self.get_tmp_file()
2485
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
95 forums_json = forums_json.strip()
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
96 if forums_json:
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
97 # we loads and dumps to have pretty printed json
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
98 forums = json.loads(forums_json)
2608
0883bac573fd jp (forums/edit): fixed unicode when dumping in json
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
99 # cf. https://stackoverflow.com/a/18337754
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2608
diff changeset
100 f = codecs.getwriter("utf-8")(content_file_obj)
2608
0883bac573fd jp (forums/edit): fixed unicode when dumping in json
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
101 json.dump(forums, f, ensure_ascii=False, indent=4)
2485
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
102 content_file_obj.seek(0)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
103 await self.run_editor("forums_editor_args", content_file_path, content_file_obj)
2485
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
104
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
105
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
106 class Get(base.CommandBase):
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
107 def __init__(self, host):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2608
diff changeset
108 extra_outputs = {"default": self.default_output}
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2608
diff changeset
109 base.CommandBase.__init__(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2608
diff changeset
110 self,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2608
diff changeset
111 host,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2608
diff changeset
112 "get",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2608
diff changeset
113 use_output=C.OUTPUT_COMPLEX,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2608
diff changeset
114 extra_outputs=extra_outputs,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2608
diff changeset
115 use_pubsub=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2608
diff changeset
116 use_verbose=True,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
117 help=_("get forums structure"),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2608
diff changeset
118 )
2485
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
119
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
120 def add_parser_options(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2608
diff changeset
121 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2608
diff changeset
122 "-k",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2608
diff changeset
123 "--key",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
124 default="",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
125 help=_("forum key (DEFAULT: default forums)"),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2608
diff changeset
126 )
2485
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
127
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
128 def default_output(self, forums, level=0):
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
129 for forum in forums:
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
130 keys = list(forum.keys())
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
131 keys.sort()
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
132 try:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
133 keys.remove("title")
2485
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
134 except ValueError:
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
135 pass
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
136 else:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
137 keys.insert(0, "title")
2485
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
138 try:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
139 keys.remove("sub-forums")
2485
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
140 except ValueError:
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
141 pass
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
142 else:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
143 keys.append("sub-forums")
2485
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
144
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
145 for key in keys:
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
146 value = forum[key]
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2608
diff changeset
147 if key == "sub-forums":
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2608
diff changeset
148 self.default_output(value, level + 1)
2485
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
149 else:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
150 if self.host.verbosity < 1 and key != "title":
2485
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
151 continue
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
152 head_color = C.A_LEVEL_COLORS[level % len(C.A_LEVEL_COLORS)]
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2608
diff changeset
153 self.disp(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
154 A.color(level * 4 * " ", head_color, key, A.RESET, ": ", value)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2608
diff changeset
155 )
2485
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
156
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
157 async def start(self):
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
158 try:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
159 forums_raw = await self.host.bridge.forums_get(
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
160 self.args.service,
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
161 self.args.node,
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
162 self.args.key,
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
163 self.profile,
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
164 )
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
165 except Exception as e:
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
166 self.disp(f"can't get forums: {e}", error=True)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
167 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
168 else:
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
169 if not forums_raw:
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
170 self.disp(_("no schema found"), 1)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
171 self.host.quit(1)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
172 forums = json.loads(forums_raw)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
173 await self.output(forums)
4191
5d056d524298 core, doc, cli (forums): new `forums set` commands + doc:
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
174
5d056d524298 core, doc, cli (forums): new `forums set` commands + doc:
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
175
5d056d524298 core, doc, cli (forums): new `forums set` commands + doc:
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
176 class Set(base.CommandBase):
5d056d524298 core, doc, cli (forums): new `forums set` commands + doc:
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
177
5d056d524298 core, doc, cli (forums): new `forums set` commands + doc:
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
178 def __init__(self, host):
5d056d524298 core, doc, cli (forums): new `forums set` commands + doc:
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
179 base.CommandBase.__init__(
5d056d524298 core, doc, cli (forums): new `forums set` commands + doc:
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
180 self,
5d056d524298 core, doc, cli (forums): new `forums set` commands + doc:
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
181 host,
5d056d524298 core, doc, cli (forums): new `forums set` commands + doc:
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
182 "set",
5d056d524298 core, doc, cli (forums): new `forums set` commands + doc:
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
183 use_pubsub=True,
5d056d524298 core, doc, cli (forums): new `forums set` commands + doc:
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
184 help=_("set forums"),
5d056d524298 core, doc, cli (forums): new `forums set` commands + doc:
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
185 )
5d056d524298 core, doc, cli (forums): new `forums set` commands + doc:
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
186
5d056d524298 core, doc, cli (forums): new `forums set` commands + doc:
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
187 def add_parser_options(self):
5d056d524298 core, doc, cli (forums): new `forums set` commands + doc:
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
188 self.parser.add_argument(
5d056d524298 core, doc, cli (forums): new `forums set` commands + doc:
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
189 "-k",
5d056d524298 core, doc, cli (forums): new `forums set` commands + doc:
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
190 "--key",
5d056d524298 core, doc, cli (forums): new `forums set` commands + doc:
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
191 default="",
5d056d524298 core, doc, cli (forums): new `forums set` commands + doc:
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
192 help=_("forum key (DEFAULT: default forums)"),
5d056d524298 core, doc, cli (forums): new `forums set` commands + doc:
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
193 )
5d056d524298 core, doc, cli (forums): new `forums set` commands + doc:
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
194
5d056d524298 core, doc, cli (forums): new `forums set` commands + doc:
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
195 async def start(self):
5d056d524298 core, doc, cli (forums): new `forums set` commands + doc:
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
196 forums_raw = sys.stdin.read()
5d056d524298 core, doc, cli (forums): new `forums set` commands + doc:
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
197 try:
5d056d524298 core, doc, cli (forums): new `forums set` commands + doc:
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
198 json.loads(forums_raw)
5d056d524298 core, doc, cli (forums): new `forums set` commands + doc:
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
199 except Exception as e:
5d056d524298 core, doc, cli (forums): new `forums set` commands + doc:
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
200 self.parser.error(f"Invalid JSON, a valid JSON must be used as input: {e}")
5d056d524298 core, doc, cli (forums): new `forums set` commands + doc:
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
201 try:
5d056d524298 core, doc, cli (forums): new `forums set` commands + doc:
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
202 await self.host.bridge.forums_set(
5d056d524298 core, doc, cli (forums): new `forums set` commands + doc:
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
203 forums_raw,
5d056d524298 core, doc, cli (forums): new `forums set` commands + doc:
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
204 self.args.service,
5d056d524298 core, doc, cli (forums): new `forums set` commands + doc:
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
205 self.args.node,
5d056d524298 core, doc, cli (forums): new `forums set` commands + doc:
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
206 self.args.key,
5d056d524298 core, doc, cli (forums): new `forums set` commands + doc:
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
207 self.profile,
5d056d524298 core, doc, cli (forums): new `forums set` commands + doc:
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
208 )
5d056d524298 core, doc, cli (forums): new `forums set` commands + doc:
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
209 except Exception as e:
5d056d524298 core, doc, cli (forums): new `forums set` commands + doc:
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
210 self.disp(f"can't set forums: {e}", error=True)
5d056d524298 core, doc, cli (forums): new `forums set` commands + doc:
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
211 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
5d056d524298 core, doc, cli (forums): new `forums set` commands + doc:
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
212 else:
5d056d524298 core, doc, cli (forums): new `forums set` commands + doc:
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
213 self.disp(_("forums have been set"))
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
214 self.host.quit()
2485
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
215
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
216
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
217 class Forums(base.CommandBase):
4191
5d056d524298 core, doc, cli (forums): new `forums set` commands + doc:
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
218 subcommands = (Get, Set, Edit)
2485
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
219
512c443a58ba jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
220 def __init__(self, host):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2608
diff changeset
221 super(Forums, self).__init__(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
222 host, "forums", use_profile=False, help=_("Forums structure edition")
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2608
diff changeset
223 )