comparison libervia/cli/cmd_forums.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_forums.py@26b7ed2817da
children 5d056d524298
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 from . import base
22 from libervia.backend.core.i18n import _
23 from libervia.cli.constants import Const as C
24 from libervia.cli import common
25 from libervia.backend.tools.common.ansi import ANSI as A
26 import codecs
27 import json
28
29 __commands__ = ["Forums"]
30
31 FORUMS_TMP_DIR = "forums"
32
33
34 class Edit(base.CommandBase, common.BaseEdit):
35 use_items = False
36
37 def __init__(self, host):
38 base.CommandBase.__init__(
39 self,
40 host,
41 "edit",
42 use_pubsub=True,
43 use_draft=True,
44 use_verbose=True,
45 help=_("edit forums"),
46 )
47 common.BaseEdit.__init__(self, self.host, FORUMS_TMP_DIR)
48
49 def add_parser_options(self):
50 self.parser.add_argument(
51 "-k",
52 "--key",
53 default="",
54 help=_("forum key (DEFAULT: default forums)"),
55 )
56
57 def get_tmp_suff(self):
58 """return suffix used for content file"""
59 return "json"
60
61 async def publish(self, forums_raw):
62 try:
63 await self.host.bridge.forums_set(
64 forums_raw,
65 self.args.service,
66 self.args.node,
67 self.args.key,
68 self.profile,
69 )
70 except Exception as e:
71 self.disp(f"can't set forums: {e}", error=True)
72 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
73 else:
74 self.disp(_("forums have been edited"), 1)
75 self.host.quit()
76
77 async def start(self):
78 try:
79 forums_json = await self.host.bridge.forums_get(
80 self.args.service,
81 self.args.node,
82 self.args.key,
83 self.profile,
84 )
85 except Exception as e:
86 if e.classname == "NotFound":
87 forums_json = ""
88 else:
89 self.disp(f"can't get node configuration: {e}", error=True)
90 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
91
92 content_file_obj, content_file_path = self.get_tmp_file()
93 forums_json = forums_json.strip()
94 if forums_json:
95 # we loads and dumps to have pretty printed json
96 forums = json.loads(forums_json)
97 # cf. https://stackoverflow.com/a/18337754
98 f = codecs.getwriter("utf-8")(content_file_obj)
99 json.dump(forums, f, ensure_ascii=False, indent=4)
100 content_file_obj.seek(0)
101 await self.run_editor("forums_editor_args", content_file_path, content_file_obj)
102
103
104 class Get(base.CommandBase):
105 def __init__(self, host):
106 extra_outputs = {"default": self.default_output}
107 base.CommandBase.__init__(
108 self,
109 host,
110 "get",
111 use_output=C.OUTPUT_COMPLEX,
112 extra_outputs=extra_outputs,
113 use_pubsub=True,
114 use_verbose=True,
115 help=_("get forums structure"),
116 )
117
118 def add_parser_options(self):
119 self.parser.add_argument(
120 "-k",
121 "--key",
122 default="",
123 help=_("forum key (DEFAULT: default forums)"),
124 )
125
126 def default_output(self, forums, level=0):
127 for forum in forums:
128 keys = list(forum.keys())
129 keys.sort()
130 try:
131 keys.remove("title")
132 except ValueError:
133 pass
134 else:
135 keys.insert(0, "title")
136 try:
137 keys.remove("sub-forums")
138 except ValueError:
139 pass
140 else:
141 keys.append("sub-forums")
142
143 for key in keys:
144 value = forum[key]
145 if key == "sub-forums":
146 self.default_output(value, level + 1)
147 else:
148 if self.host.verbosity < 1 and key != "title":
149 continue
150 head_color = C.A_LEVEL_COLORS[level % len(C.A_LEVEL_COLORS)]
151 self.disp(
152 A.color(level * 4 * " ", head_color, key, A.RESET, ": ", value)
153 )
154
155 async def start(self):
156 try:
157 forums_raw = await self.host.bridge.forums_get(
158 self.args.service,
159 self.args.node,
160 self.args.key,
161 self.profile,
162 )
163 except Exception as e:
164 self.disp(f"can't get forums: {e}", error=True)
165 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
166 else:
167 if not forums_raw:
168 self.disp(_("no schema found"), 1)
169 self.host.quit(1)
170 forums = json.loads(forums_raw)
171 await self.output(forums)
172 self.host.quit()
173
174
175 class Forums(base.CommandBase):
176 subcommands = (Get, Edit)
177
178 def __init__(self, host):
179 super(Forums, self).__init__(
180 host, "forums", use_profile=False, help=_("Forums structure edition")
181 )