Mercurial > libervia-backend
comparison frontends/src/jp/cmd_forums.py @ 2485:512c443a58ba
jp (forums): forums handling commands, first draft
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 30 Jan 2018 08:17:17 +0100 |
parents | |
children | 772447ec070f |
comparison
equal
deleted
inserted
replaced
2484:785b6a1cef0a | 2485:512c443a58ba |
---|---|
1 #!/usr/bin/env python2 | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # jp: a SàT command line tool | |
5 # Copyright (C) 2009-2018 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 base | |
22 from sat.core.i18n import _ | |
23 from sat_frontends.jp.constants import Const as C | |
24 from sat_frontends.jp import common | |
25 from sat.tools.common.ansi import ANSI as A | |
26 from functools import partial | |
27 import json | |
28 | |
29 __commands__ = ["Forums"] | |
30 | |
31 FORUMS_TMP_DIR = u"forums" | |
32 | |
33 | |
34 class Edit(base.CommandBase, common.BaseEdit): | |
35 use_items=False | |
36 | |
37 def __init__(self, host): | |
38 base.CommandBase.__init__(self, host, 'edit', use_pubsub=True, use_verbose=True, help=_(u'edit forums')) | |
39 common.BaseEdit.__init__(self, self.host, FORUMS_TMP_DIR) | |
40 self.need_loop=True | |
41 | |
42 def add_parser_options(self): | |
43 common.BaseEdit.add_parser_options(self) | |
44 self.parser.add_argument("-k", "--key", type=base.unicode_decoder, default=u'', | |
45 help=_(u"forum key (DEFAULT: default forums)")) | |
46 | |
47 def getTmpSuff(self): | |
48 """return suffix used for content file""" | |
49 return u'json' | |
50 | |
51 def forumsSetCb(self): | |
52 self.disp(_(u'forums have been edited'), 1) | |
53 self.host.quit() | |
54 | |
55 def publish(self, forums_raw): | |
56 self.host.bridge.forumsSet( | |
57 forums_raw, | |
58 self.args.service, | |
59 self.args.node, | |
60 self.args.key, | |
61 self.profile, | |
62 callback=self.forumsSetCb, | |
63 errback=partial(self.errback, | |
64 msg=_(u"can't set forums: {}"), | |
65 exit_code=C.EXIT_BRIDGE_ERRBACK)) | |
66 | |
67 def forumsGetCb(self, forums_json): | |
68 content_file_obj, content_file_path = self.getTmpFile() | |
69 forums_json = forums_json.strip() | |
70 if forums_json: | |
71 # we loads and dumps to have pretty printed json | |
72 forums = json.loads(forums_json) | |
73 json.dump(forums, content_file_obj, ensure_ascii=False, indent=4) | |
74 content_file_obj.seek(0) | |
75 self.runEditor("forums_editor_args", content_file_path, content_file_obj) | |
76 | |
77 def forumsGetEb(self, failure_): | |
78 # FIXME: error handling with bridge is broken, need to be properly fixed | |
79 if failure_.condition == u'item-not-found': | |
80 self.forumsGetCb(u'') | |
81 else: | |
82 self.errback(failure_, | |
83 msg=_(u"can't get forums structure: {}"), | |
84 exit_code=C.EXIT_BRIDGE_ERRBACK) | |
85 | |
86 def start(self): | |
87 common.checkURI(self.args) | |
88 self.host.bridge.forumsGet( | |
89 self.args.service, | |
90 self.args.node, | |
91 self.args.key, | |
92 self.profile, | |
93 callback=self.forumsGetCb, | |
94 errback=self.forumsGetEb) | |
95 | |
96 | |
97 class Get(base.CommandBase): | |
98 | |
99 def __init__(self, host): | |
100 extra_outputs = {'default': self.default_output} | |
101 base.CommandBase.__init__(self, host, 'get', use_output=C.OUTPUT_COMPLEX, extra_outputs=extra_outputs, use_pubsub=True, use_verbose=True, help=_(u'get forums structure')) | |
102 self.need_loop=True | |
103 | |
104 def add_parser_options(self): | |
105 self.parser.add_argument("-k", "--key", type=base.unicode_decoder, default=u'', | |
106 help=_(u"forum key (DEFAULT: default forums)")) | |
107 | |
108 def default_output(self, forums, level=0): | |
109 for forum in forums: | |
110 keys = list(forum.keys()) | |
111 keys.sort() | |
112 try: | |
113 keys.remove(u'title') | |
114 except ValueError: | |
115 pass | |
116 else: | |
117 keys.insert(0, u'title') | |
118 try: | |
119 keys.remove(u'sub-forums') | |
120 except ValueError: | |
121 pass | |
122 else: | |
123 keys.append(u'sub-forums') | |
124 | |
125 for key in keys: | |
126 value = forum[key] | |
127 if key == 'sub-forums': | |
128 self.default_output(value, level+1) | |
129 else: | |
130 if self.host.verbosity < 1 and key != u'title': | |
131 continue | |
132 head_color = C.A_LEVEL_COLORS[level % len(C.A_LEVEL_COLORS)] | |
133 self.disp(A.color(level * 4 * u' ', | |
134 head_color, | |
135 key, | |
136 A.RESET, | |
137 u': ', | |
138 value)) | |
139 | |
140 def forumsGetCb(self, forums_raw): | |
141 if not forums_raw: | |
142 self.disp(_(u'no schema found'), 1) | |
143 self.host.quit(1) | |
144 forums = json.loads(forums_raw) | |
145 self.output(forums) | |
146 self.host.quit() | |
147 | |
148 def start(self): | |
149 common.checkURI(self.args) | |
150 self.host.bridge.forumsGet( | |
151 self.args.service, | |
152 self.args.node, | |
153 self.args.key, | |
154 self.profile, | |
155 callback=self.forumsGetCb, | |
156 errback=partial(self.errback, | |
157 msg=_(u"can't get forums: {}"), | |
158 exit_code=C.EXIT_BRIDGE_ERRBACK)) | |
159 | |
160 | |
161 | |
162 class Forums(base.CommandBase): | |
163 subcommands = (Get, Edit) | |
164 | |
165 def __init__(self, host): | |
166 super(Forums, self).__init__(host, 'forums', use_profile=False, help=_(u'Forums structure edition')) |