Mercurial > libervia-backend
annotate sat_frontends/jp/cmd_forums.py @ 3849:bc24ce903835
plugin XEP-0060: handle priority in `addManagedNode`:
Priority lets order the callback list when an event is received. This is important in some
use case, notably when a plugin needs to check the former item before it is deleted from
cache or updated.
rel 370
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 14 Jul 2022 12:55:30 +0200 |
parents | be6d91572633 |
children | 524856bd7b19 |
rev | line source |
---|---|
3137 | 1 #!/usr/bin/env python3 |
2 | |
2485
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 # jp: a SàT command line tool |
3479 | 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 |
3028 | 21 from . import base |
2485
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 from sat.core.i18n import _ |
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 from sat_frontends.jp.constants import Const as C |
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 from sat_frontends.jp import common |
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 from sat.tools.common.ansi import ANSI as A |
2608
0883bac573fd
jp (forums/edit): fixed unicode when dumping in json
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
26 import codecs |
2485
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 import json |
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 |
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 __commands__ = ["Forums"] |
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 |
3028 | 31 FORUMS_TMP_DIR = "forums" |
2485
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 |
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 |
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 class Edit(base.CommandBase, common.BaseEdit): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2608
diff
changeset
|
35 use_items = False |
2485
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 |
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 def __init__(self, host): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2608
diff
changeset
|
38 base.CommandBase.__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2608
diff
changeset
|
39 self, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2608
diff
changeset
|
40 host, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2608
diff
changeset
|
41 "edit", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2608
diff
changeset
|
42 use_pubsub=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2608
diff
changeset
|
43 use_draft=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2608
diff
changeset
|
44 use_verbose=True, |
3028 | 45 help=_("edit forums"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2608
diff
changeset
|
46 ) |
2485
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 common.BaseEdit.__init__(self, self.host, FORUMS_TMP_DIR) |
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 |
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 def add_parser_options(self): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2608
diff
changeset
|
50 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2608
diff
changeset
|
51 "-k", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2608
diff
changeset
|
52 "--key", |
3028 | 53 default="", |
54 help=_("forum key (DEFAULT: default forums)"), | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2608
diff
changeset
|
55 ) |
2485
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 |
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 def getTmpSuff(self): |
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 """return suffix used for content file""" |
3028 | 59 return "json" |
2485
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 |
3040 | 61 async def publish(self, forums_raw): |
62 try: | |
63 await self.host.bridge.forumsSet( | |
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() | |
2485
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 |
3040 | 77 async def start(self): |
78 try: | |
79 forums_json = await self.host.bridge.forumsGet( | |
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) | |
2485
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
91 |
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
92 content_file_obj, content_file_path = self.getTmpFile() |
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
93 forums_json = forums_json.strip() |
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
94 if forums_json: |
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
95 # we loads and dumps to have pretty printed json |
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
96 forums = json.loads(forums_json) |
2608
0883bac573fd
jp (forums/edit): fixed unicode when dumping in json
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
97 # cf. https://stackoverflow.com/a/18337754 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2608
diff
changeset
|
98 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
|
99 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
|
100 content_file_obj.seek(0) |
3040 | 101 await self.runEditor("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
|
102 |
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
103 |
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
104 class Get(base.CommandBase): |
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
105 def __init__(self, host): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2608
diff
changeset
|
106 extra_outputs = {"default": self.default_output} |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2608
diff
changeset
|
107 base.CommandBase.__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2608
diff
changeset
|
108 self, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2608
diff
changeset
|
109 host, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2608
diff
changeset
|
110 "get", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2608
diff
changeset
|
111 use_output=C.OUTPUT_COMPLEX, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2608
diff
changeset
|
112 extra_outputs=extra_outputs, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2608
diff
changeset
|
113 use_pubsub=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2608
diff
changeset
|
114 use_verbose=True, |
3028 | 115 help=_("get forums structure"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2608
diff
changeset
|
116 ) |
2485
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
117 |
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
118 def add_parser_options(self): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2608
diff
changeset
|
119 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2608
diff
changeset
|
120 "-k", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2608
diff
changeset
|
121 "--key", |
3028 | 122 default="", |
123 help=_("forum key (DEFAULT: default forums)"), | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2608
diff
changeset
|
124 ) |
2485
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
125 |
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
126 def default_output(self, forums, level=0): |
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
127 for forum in forums: |
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
128 keys = list(forum.keys()) |
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
129 keys.sort() |
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
130 try: |
3028 | 131 keys.remove("title") |
2485
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
132 except ValueError: |
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
133 pass |
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
134 else: |
3028 | 135 keys.insert(0, "title") |
2485
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
136 try: |
3028 | 137 keys.remove("sub-forums") |
2485
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
138 except ValueError: |
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
139 pass |
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
140 else: |
3028 | 141 keys.append("sub-forums") |
2485
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
142 |
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
143 for key in keys: |
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
144 value = forum[key] |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2608
diff
changeset
|
145 if key == "sub-forums": |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2608
diff
changeset
|
146 self.default_output(value, level + 1) |
2485
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
147 else: |
3028 | 148 if self.host.verbosity < 1 and key != "title": |
2485
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
149 continue |
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
150 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
|
151 self.disp( |
3028 | 152 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
|
153 ) |
2485
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
154 |
3040 | 155 async def start(self): |
156 try: | |
157 forums_raw = await self.host.bridge.forumsGet( | |
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() | |
2485
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
173 |
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
174 |
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
175 class Forums(base.CommandBase): |
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
176 subcommands = (Get, Edit) |
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
177 |
512c443a58ba
jp (forums): forums handling commands, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
178 def __init__(self, host): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2608
diff
changeset
|
179 super(Forums, self).__init__( |
3028 | 180 host, "forums", use_profile=False, help=_("Forums structure edition") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2608
diff
changeset
|
181 ) |