comparison sat_frontends/jp/cmd_forums.py @ 2562:26edcf3a30eb

core, setup: huge cleaning: - moved directories from src and frontends/src to sat and sat_frontends, which is the recommanded naming convention - move twisted directory to root - removed all hacks from setup.py, and added missing dependencies, it is now clean - use https URL for website in setup.py - removed "Environment :: X11 Applications :: GTK", as wix is deprecated and removed - renamed sat.sh to sat and fixed its installation - added python_requires to specify Python version needed - replaced glib2reactor which use deprecated code by gtk3reactor sat can now be installed directly from virtualenv without using --system-site-packages anymore \o/
author Goffi <goffi@goffi.org>
date Mon, 02 Apr 2018 19:44:50 +0200
parents frontends/src/jp/cmd_forums.py@772447ec070f
children 0883bac573fd
comparison
equal deleted inserted replaced
2561:bd30dc3ffe5a 2562:26edcf3a30eb
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_draft=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 self.parser.add_argument("-k", "--key", type=base.unicode_decoder, default=u'',
44 help=_(u"forum key (DEFAULT: default forums)"))
45
46 def getTmpSuff(self):
47 """return suffix used for content file"""
48 return u'json'
49
50 def forumsSetCb(self):
51 self.disp(_(u'forums have been edited'), 1)
52 self.host.quit()
53
54 def publish(self, forums_raw):
55 self.host.bridge.forumsSet(
56 forums_raw,
57 self.args.service,
58 self.args.node,
59 self.args.key,
60 self.profile,
61 callback=self.forumsSetCb,
62 errback=partial(self.errback,
63 msg=_(u"can't set forums: {}"),
64 exit_code=C.EXIT_BRIDGE_ERRBACK))
65
66 def forumsGetCb(self, forums_json):
67 content_file_obj, content_file_path = self.getTmpFile()
68 forums_json = forums_json.strip()
69 if forums_json:
70 # we loads and dumps to have pretty printed json
71 forums = json.loads(forums_json)
72 json.dump(forums, content_file_obj, ensure_ascii=False, indent=4)
73 content_file_obj.seek(0)
74 self.runEditor("forums_editor_args", content_file_path, content_file_obj)
75
76 def forumsGetEb(self, failure_):
77 # FIXME: error handling with bridge is broken, need to be properly fixed
78 if failure_.condition == u'item-not-found':
79 self.forumsGetCb(u'')
80 else:
81 self.errback(failure_,
82 msg=_(u"can't get forums structure: {}"),
83 exit_code=C.EXIT_BRIDGE_ERRBACK)
84
85 def start(self):
86 self.host.bridge.forumsGet(
87 self.args.service,
88 self.args.node,
89 self.args.key,
90 self.profile,
91 callback=self.forumsGetCb,
92 errback=self.forumsGetEb)
93
94
95 class Get(base.CommandBase):
96
97 def __init__(self, host):
98 extra_outputs = {'default': self.default_output}
99 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'))
100 self.need_loop=True
101
102 def add_parser_options(self):
103 self.parser.add_argument("-k", "--key", type=base.unicode_decoder, default=u'',
104 help=_(u"forum key (DEFAULT: default forums)"))
105
106 def default_output(self, forums, level=0):
107 for forum in forums:
108 keys = list(forum.keys())
109 keys.sort()
110 try:
111 keys.remove(u'title')
112 except ValueError:
113 pass
114 else:
115 keys.insert(0, u'title')
116 try:
117 keys.remove(u'sub-forums')
118 except ValueError:
119 pass
120 else:
121 keys.append(u'sub-forums')
122
123 for key in keys:
124 value = forum[key]
125 if key == 'sub-forums':
126 self.default_output(value, level+1)
127 else:
128 if self.host.verbosity < 1 and key != u'title':
129 continue
130 head_color = C.A_LEVEL_COLORS[level % len(C.A_LEVEL_COLORS)]
131 self.disp(A.color(level * 4 * u' ',
132 head_color,
133 key,
134 A.RESET,
135 u': ',
136 value))
137
138 def forumsGetCb(self, forums_raw):
139 if not forums_raw:
140 self.disp(_(u'no schema found'), 1)
141 self.host.quit(1)
142 forums = json.loads(forums_raw)
143 self.output(forums)
144 self.host.quit()
145
146 def start(self):
147 self.host.bridge.forumsGet(
148 self.args.service,
149 self.args.node,
150 self.args.key,
151 self.profile,
152 callback=self.forumsGetCb,
153 errback=partial(self.errback,
154 msg=_(u"can't get forums: {}"),
155 exit_code=C.EXIT_BRIDGE_ERRBACK))
156
157
158
159 class Forums(base.CommandBase):
160 subcommands = (Get, Edit)
161
162 def __init__(self, host):
163 super(Forums, self).__init__(host, 'forums', use_profile=False, help=_(u'Forums structure edition'))