comparison sat_frontends/jp/cmd_forums.py @ 2624:56f94936df1e

code style reformatting using black
author Goffi <goffi@goffi.org>
date Wed, 27 Jun 2018 20:14:46 +0200
parents 0883bac573fd
children 003b8b4b56a7
comparison
equal deleted inserted replaced
2623:49533de4540b 2624:56f94936df1e
31 31
32 FORUMS_TMP_DIR = u"forums" 32 FORUMS_TMP_DIR = u"forums"
33 33
34 34
35 class Edit(base.CommandBase, common.BaseEdit): 35 class Edit(base.CommandBase, common.BaseEdit):
36 use_items=False 36 use_items = False
37 37
38 def __init__(self, host): 38 def __init__(self, host):
39 base.CommandBase.__init__(self, host, 'edit', use_pubsub=True, use_draft=True, use_verbose=True, help=_(u'edit forums')) 39 base.CommandBase.__init__(
40 self,
41 host,
42 "edit",
43 use_pubsub=True,
44 use_draft=True,
45 use_verbose=True,
46 help=_(u"edit forums"),
47 )
40 common.BaseEdit.__init__(self, self.host, FORUMS_TMP_DIR) 48 common.BaseEdit.__init__(self, self.host, FORUMS_TMP_DIR)
41 self.need_loop=True 49 self.need_loop = True
42 50
43 def add_parser_options(self): 51 def add_parser_options(self):
44 self.parser.add_argument("-k", "--key", type=base.unicode_decoder, default=u'', 52 self.parser.add_argument(
45 help=_(u"forum key (DEFAULT: default forums)")) 53 "-k",
54 "--key",
55 type=base.unicode_decoder,
56 default=u"",
57 help=_(u"forum key (DEFAULT: default forums)"),
58 )
46 59
47 def getTmpSuff(self): 60 def getTmpSuff(self):
48 """return suffix used for content file""" 61 """return suffix used for content file"""
49 return u'json' 62 return u"json"
50 63
51 def forumsSetCb(self): 64 def forumsSetCb(self):
52 self.disp(_(u'forums have been edited'), 1) 65 self.disp(_(u"forums have been edited"), 1)
53 self.host.quit() 66 self.host.quit()
54 67
55 def publish(self, forums_raw): 68 def publish(self, forums_raw):
56 self.host.bridge.forumsSet( 69 self.host.bridge.forumsSet(
57 forums_raw, 70 forums_raw,
58 self.args.service, 71 self.args.service,
59 self.args.node, 72 self.args.node,
60 self.args.key, 73 self.args.key,
61 self.profile, 74 self.profile,
62 callback=self.forumsSetCb, 75 callback=self.forumsSetCb,
63 errback=partial(self.errback, 76 errback=partial(
64 msg=_(u"can't set forums: {}"), 77 self.errback,
65 exit_code=C.EXIT_BRIDGE_ERRBACK)) 78 msg=_(u"can't set forums: {}"),
79 exit_code=C.EXIT_BRIDGE_ERRBACK,
80 ),
81 )
66 82
67 def forumsGetCb(self, forums_json): 83 def forumsGetCb(self, forums_json):
68 content_file_obj, content_file_path = self.getTmpFile() 84 content_file_obj, content_file_path = self.getTmpFile()
69 forums_json = forums_json.strip() 85 forums_json = forums_json.strip()
70 if forums_json: 86 if forums_json:
71 # we loads and dumps to have pretty printed json 87 # we loads and dumps to have pretty printed json
72 forums = json.loads(forums_json) 88 forums = json.loads(forums_json)
73 # cf. https://stackoverflow.com/a/18337754 89 # cf. https://stackoverflow.com/a/18337754
74 f = codecs.getwriter('utf-8')(content_file_obj) 90 f = codecs.getwriter("utf-8")(content_file_obj)
75 json.dump(forums, f, ensure_ascii=False, indent=4) 91 json.dump(forums, f, ensure_ascii=False, indent=4)
76 content_file_obj.seek(0) 92 content_file_obj.seek(0)
77 self.runEditor("forums_editor_args", content_file_path, content_file_obj) 93 self.runEditor("forums_editor_args", content_file_path, content_file_obj)
78 94
79 def forumsGetEb(self, failure_): 95 def forumsGetEb(self, failure_):
80 # FIXME: error handling with bridge is broken, need to be properly fixed 96 # FIXME: error handling with bridge is broken, need to be properly fixed
81 if failure_.condition == u'item-not-found': 97 if failure_.condition == u"item-not-found":
82 self.forumsGetCb(u'') 98 self.forumsGetCb(u"")
83 else: 99 else:
84 self.errback(failure_, 100 self.errback(
85 msg=_(u"can't get forums structure: {}"), 101 failure_,
86 exit_code=C.EXIT_BRIDGE_ERRBACK) 102 msg=_(u"can't get forums structure: {}"),
103 exit_code=C.EXIT_BRIDGE_ERRBACK,
104 )
87 105
88 def start(self): 106 def start(self):
89 self.host.bridge.forumsGet( 107 self.host.bridge.forumsGet(
90 self.args.service, 108 self.args.service,
91 self.args.node, 109 self.args.node,
92 self.args.key, 110 self.args.key,
93 self.profile, 111 self.profile,
94 callback=self.forumsGetCb, 112 callback=self.forumsGetCb,
95 errback=self.forumsGetEb) 113 errback=self.forumsGetEb,
114 )
96 115
97 116
98 class Get(base.CommandBase): 117 class Get(base.CommandBase):
99
100 def __init__(self, host): 118 def __init__(self, host):
101 extra_outputs = {'default': self.default_output} 119 extra_outputs = {"default": self.default_output}
102 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')) 120 base.CommandBase.__init__(
103 self.need_loop=True 121 self,
122 host,
123 "get",
124 use_output=C.OUTPUT_COMPLEX,
125 extra_outputs=extra_outputs,
126 use_pubsub=True,
127 use_verbose=True,
128 help=_(u"get forums structure"),
129 )
130 self.need_loop = True
104 131
105 def add_parser_options(self): 132 def add_parser_options(self):
106 self.parser.add_argument("-k", "--key", type=base.unicode_decoder, default=u'', 133 self.parser.add_argument(
107 help=_(u"forum key (DEFAULT: default forums)")) 134 "-k",
135 "--key",
136 type=base.unicode_decoder,
137 default=u"",
138 help=_(u"forum key (DEFAULT: default forums)"),
139 )
108 140
109 def default_output(self, forums, level=0): 141 def default_output(self, forums, level=0):
110 for forum in forums: 142 for forum in forums:
111 keys = list(forum.keys()) 143 keys = list(forum.keys())
112 keys.sort() 144 keys.sort()
113 try: 145 try:
114 keys.remove(u'title') 146 keys.remove(u"title")
115 except ValueError: 147 except ValueError:
116 pass 148 pass
117 else: 149 else:
118 keys.insert(0, u'title') 150 keys.insert(0, u"title")
119 try: 151 try:
120 keys.remove(u'sub-forums') 152 keys.remove(u"sub-forums")
121 except ValueError: 153 except ValueError:
122 pass 154 pass
123 else: 155 else:
124 keys.append(u'sub-forums') 156 keys.append(u"sub-forums")
125 157
126 for key in keys: 158 for key in keys:
127 value = forum[key] 159 value = forum[key]
128 if key == 'sub-forums': 160 if key == "sub-forums":
129 self.default_output(value, level+1) 161 self.default_output(value, level + 1)
130 else: 162 else:
131 if self.host.verbosity < 1 and key != u'title': 163 if self.host.verbosity < 1 and key != u"title":
132 continue 164 continue
133 head_color = C.A_LEVEL_COLORS[level % len(C.A_LEVEL_COLORS)] 165 head_color = C.A_LEVEL_COLORS[level % len(C.A_LEVEL_COLORS)]
134 self.disp(A.color(level * 4 * u' ', 166 self.disp(
135 head_color, 167 A.color(level * 4 * u" ", head_color, key, A.RESET, u": ", value)
136 key, 168 )
137 A.RESET,
138 u': ',
139 value))
140 169
141 def forumsGetCb(self, forums_raw): 170 def forumsGetCb(self, forums_raw):
142 if not forums_raw: 171 if not forums_raw:
143 self.disp(_(u'no schema found'), 1) 172 self.disp(_(u"no schema found"), 1)
144 self.host.quit(1) 173 self.host.quit(1)
145 forums = json.loads(forums_raw) 174 forums = json.loads(forums_raw)
146 self.output(forums) 175 self.output(forums)
147 self.host.quit() 176 self.host.quit()
148 177
151 self.args.service, 180 self.args.service,
152 self.args.node, 181 self.args.node,
153 self.args.key, 182 self.args.key,
154 self.profile, 183 self.profile,
155 callback=self.forumsGetCb, 184 callback=self.forumsGetCb,
156 errback=partial(self.errback, 185 errback=partial(
157 msg=_(u"can't get forums: {}"), 186 self.errback,
158 exit_code=C.EXIT_BRIDGE_ERRBACK)) 187 msg=_(u"can't get forums: {}"),
159 188 exit_code=C.EXIT_BRIDGE_ERRBACK,
189 ),
190 )
160 191
161 192
162 class Forums(base.CommandBase): 193 class Forums(base.CommandBase):
163 subcommands = (Get, Edit) 194 subcommands = (Get, Edit)
164 195
165 def __init__(self, host): 196 def __init__(self, host):
166 super(Forums, self).__init__(host, 'forums', use_profile=False, help=_(u'Forums structure edition')) 197 super(Forums, self).__init__(
198 host, "forums", use_profile=False, help=_(u"Forums structure edition")
199 )