comparison frontends/src/jp/cmd_pubsub.py @ 2235:4db836386641

jp: added use_pubsub and use_pubsub_node_req arguments to CommandBase pubsub add --node and --service arguments pubsub_node_req add --service and a positional node argument (i.e. mandatory)
author Goffi <goffi@goffi.org>
date Thu, 27 Apr 2017 01:01:43 +0200
parents 87fcd4a7c7e4
children 17502e74c046
comparison
equal deleted inserted replaced
2234:a41a4729c2d8 2235:4db836386641
26 from sat_frontends.tools import jid 26 from sat_frontends.tools import jid
27 27
28 __commands__ = ["Pubsub"] 28 __commands__ = ["Pubsub"]
29 29
30 30
31 class NodeCommon(object): 31 class NodeInfo(base.CommandBase):
32 node_required = True 32
33 33 def __init__(self, host):
34 def __init__(self, node_required=True): 34 base.CommandBase.__init__(self, host, 'info', use_output=C.OUTPUT_DICT, use_pubsub_node_req=True, help=_(u'retrieve node configuration'))
35 self.node_required = node_required
36
37 def add_parser_options(self):
38 self.parser.add_argument("-s", "--service", type=base.unicode_decoder, default=u'',
39 help=_(u"JID of the PubSub service (default: PEP service)"))
40 if self.node_required:
41 self.parser.add_argument("node", type=base.unicode_decoder, help=_(u"node to request"))
42 else:
43 self.parser.add_argument("-n", "--node", type=base.unicode_decoder, default=u'', help=_(u"node to request"))
44
45
46 class NodeInfo(base.CommandBase, NodeCommon):
47
48 def __init__(self, host):
49 base.CommandBase.__init__(self, host, 'info', use_output=C.OUTPUT_DICT, help=_(u'retrieve node configuration'))
50 self.need_loop=True 35 self.need_loop=True
51 36
52 def add_parser_options(self): 37 def add_parser_options(self):
53 self.parser.add_argument("-k", "--key", type=base.unicode_decoder, action='append', dest='keys', 38 self.parser.add_argument("-k", "--key", type=base.unicode_decoder, action='append', dest='keys',
54 help=_(u"data key to filter")) 39 help=_(u"data key to filter"))
55 NodeCommon.add_parser_options(self)
56 40
57 def removePrefix(self, key): 41 def removePrefix(self, key):
58 return key[7:] if key.startswith(u"pubsub#") else key 42 return key[7:] if key.startswith(u"pubsub#") else key
59 43
60 def filterKey(self, key): 44 def filterKey(self, key):
78 self.profile, 62 self.profile,
79 callback=self.psNodeConfigurationGetCb, 63 callback=self.psNodeConfigurationGetCb,
80 errback=self.psNodeConfigurationGetEb) 64 errback=self.psNodeConfigurationGetEb)
81 65
82 66
83 class NodeCreate(base.CommandBase, NodeCommon): 67 class NodeCreate(base.CommandBase):
84 68
85 def __init__(self, host): 69 def __init__(self, host):
86 NodeCommon.__init__(self, node_required=False) 70 base.CommandBase.__init__(self, host, 'create', use_output=C.OUTPUT_DICT, use_pubsub_node_req=True, use_verbose=True, help=_(u'create a node'))
87 base.CommandBase.__init__(self, host, 'create', use_output=C.OUTPUT_DICT, use_verbose=True, help=_(u'create a node'))
88 self.need_loop=True 71 self.need_loop=True
89 72
90 def add_parser_options(self): 73 def add_parser_options(self):
91 self.parser.add_argument("-f", "--field", type=base.unicode_decoder, action='append', nargs=2, dest='fields', 74 self.parser.add_argument("-f", "--field", type=base.unicode_decoder, action='append', nargs=2, dest='fields',
92 default={}, metavar=(u"KEY", u"VALUE"), help=_(u"configuration field to set (required)")) 75 default={}, metavar=(u"KEY", u"VALUE"), help=_(u"configuration field to set"))
93 self.parser.add_argument("-F", "--full-prefix", action="store_true", help=_(u"don't prepend \"pubsub#\" prefix to field names")) 76 self.parser.add_argument("-F", "--full-prefix", action="store_true", help=_(u"don't prepend \"pubsub#\" prefix to field names"))
94 NodeCommon.add_parser_options(self)
95 77
96 def psNodeCreateCb(self, node_id): 78 def psNodeCreateCb(self, node_id):
97 if self.host.verbosity: 79 if self.host.verbosity:
98 announce = _(u'node created successfully: ') 80 announce = _(u'node created successfully: ')
99 else: 81 else:
120 errback=partial(self.errback, 102 errback=partial(self.errback,
121 msg=_(u"can't create node: {}"), 103 msg=_(u"can't create node: {}"),
122 exit_code=C.EXIT_BRIDGE_ERRBACK)) 104 exit_code=C.EXIT_BRIDGE_ERRBACK))
123 105
124 106
125 class NodeDelete(base.CommandBase, NodeCommon): 107 class NodeDelete(base.CommandBase):
126 108
127 def __init__(self, host): 109 def __init__(self, host):
128 base.CommandBase.__init__(self, host, 'delete', help=_(u'delete a node')) 110 base.CommandBase.__init__(self, host, 'delete', use_pubsub_node_req=True, help=_(u'delete a node'))
129 self.need_loop=True 111 self.need_loop=True
130 112
131 def add_parser_options(self): 113 def add_parser_options(self):
132 self.parser.add_argument('-f', '--force', action='store_true', help=_(u'delete node without confirmation')) 114 self.parser.add_argument('-f', '--force', action='store_true', help=_(u'delete node without confirmation'))
133 NodeCommon.add_parser_options(self)
134 115
135 def psNodeDeleteCb(self): 116 def psNodeDeleteCb(self):
136 self.disp(_(u'node deleted successfully')) 117 self.disp(_(u'node deleted successfully'))
137 self.host.quit() 118 self.host.quit()
138 119
158 errback=partial(self.errback, 139 errback=partial(self.errback,
159 msg=_(u"can't delete node: {}"), 140 msg=_(u"can't delete node: {}"),
160 exit_code=C.EXIT_BRIDGE_ERRBACK)) 141 exit_code=C.EXIT_BRIDGE_ERRBACK))
161 142
162 143
163 class NodeSet(base.CommandBase, NodeCommon): 144 class NodeSet(base.CommandBase):
164 145
165 def __init__(self, host): 146 def __init__(self, host):
166 base.CommandBase.__init__(self, host, 'set', use_output=C.OUTPUT_DICT, use_verbose=True, help=_(u'set node configuration')) 147 base.CommandBase.__init__(self, host, 'set', use_output=C.OUTPUT_DICT, use_pubsub_node_req=True, use_verbose=True, help=_(u'set node configuration'))
167 self.need_loop=True 148 self.need_loop=True
168 149
169 def add_parser_options(self): 150 def add_parser_options(self):
170 self.parser.add_argument("-f", "--field", type=base.unicode_decoder, action='append', nargs=2, dest='fields', 151 self.parser.add_argument("-f", "--field", type=base.unicode_decoder, action='append', nargs=2, dest='fields',
171 required=True, metavar=(u"KEY", u"VALUE"), help=_(u"configuration field to set (required)")) 152 required=True, metavar=(u"KEY", u"VALUE"), help=_(u"configuration field to set (required)"))
172 NodeCommon.add_parser_options(self)
173 153
174 def psNodeConfigurationSetCb(self): 154 def psNodeConfigurationSetCb(self):
175 self.disp(_(u'node configuration successful'), 1) 155 self.disp(_(u'node configuration successful'), 1)
176 self.host.quit() 156 self.host.quit()
177 157
194 self.profile, 174 self.profile,
195 callback=self.psNodeConfigurationSetCb, 175 callback=self.psNodeConfigurationSetCb,
196 errback=self.psNodeConfigurationSetEb) 176 errback=self.psNodeConfigurationSetEb)
197 177
198 178
199 class NodeAffiliationsGet(base.CommandBase, NodeCommon): 179 class NodeAffiliationsGet(base.CommandBase):
200 180
201 def __init__(self, host): 181 def __init__(self, host):
202 base.CommandBase.__init__(self, host, 'get', use_output=C.OUTPUT_DICT, help=_(u'retrieve node affiliations (for node owner)')) 182 base.CommandBase.__init__(self, host, 'get', use_output=C.OUTPUT_DICT, use_pubsub_node_req=True, help=_(u'retrieve node affiliations (for node owner)'))
203 self.need_loop=True 183 self.need_loop=True
204 184
205 def add_parser_options(self): 185 def add_parser_options(self):
206 NodeCommon.add_parser_options(self) 186 pass
207 187
208 def psNodeAffiliationsGetCb(self, affiliations): 188 def psNodeAffiliationsGetCb(self, affiliations):
209 self.output(affiliations) 189 self.output(affiliations)
210 self.host.quit() 190 self.host.quit()
211 191
221 self.profile, 201 self.profile,
222 callback=self.psNodeAffiliationsGetCb, 202 callback=self.psNodeAffiliationsGetCb,
223 errback=self.psNodeAffiliationsGetEb) 203 errback=self.psNodeAffiliationsGetEb)
224 204
225 205
226 class NodeAffiliationsSet(base.CommandBase, NodeCommon): 206 class NodeAffiliationsSet(base.CommandBase):
227 207
228 def __init__(self, host): 208 def __init__(self, host):
229 base.CommandBase.__init__(self, host, 'set', use_verbose=True, help=_(u'set affiliations (for node owner)')) 209 base.CommandBase.__init__(self, host, 'set', use_pubsub_node_req=True, use_verbose=True, help=_(u'set affiliations (for node owner)'))
230 self.need_loop=True 210 self.need_loop=True
231 211
232 def add_parser_options(self): 212 def add_parser_options(self):
233 NodeCommon.add_parser_options(self)
234 # XXX: we use optional argument syntax for a required one because list of list of 2 elements 213 # XXX: we use optional argument syntax for a required one because list of list of 2 elements
235 # (uses to construct dicts) don't work with positional arguments 214 # (uses to construct dicts) don't work with positional arguments
236 self.parser.add_argument("-a", 215 self.parser.add_argument("-a",
237 "--affiliation", 216 "--affiliation",
238 dest="affiliations", 217 dest="affiliations",
275 254
276 def __init__(self, host): 255 def __init__(self, host):
277 super(Node, self).__init__(host, 'node', use_profile=False, help=_('node handling')) 256 super(Node, self).__init__(host, 'node', use_profile=False, help=_('node handling'))
278 257
279 258
280 class Get(base.CommandBase, NodeCommon): 259 class Get(base.CommandBase):
281 260
282 def __init__(self, host): 261 def __init__(self, host):
283 base.CommandBase.__init__(self, host, 'get', use_output=C.OUTPUT_LIST_XML, help=_(u'get pubsub item(s)')) 262 base.CommandBase.__init__(self, host, 'get', use_output=C.OUTPUT_LIST_XML, use_pubsub_node_req=True, help=_(u'get pubsub item(s)'))
284 self.need_loop=True 263 self.need_loop=True
285 264
286 def add_parser_options(self): 265 def add_parser_options(self):
287 self.parser.add_argument("-i", "--item", type=base.unicode_decoder, action='append', default=[], dest='items', 266 self.parser.add_argument("-i", "--item", type=base.unicode_decoder, action='append', default=[], dest='items',
288 help=_(u"item(s) id(s) to get (default: request all items)")) 267 help=_(u"item(s) id(s) to get (default: request all items)"))
313 self.profile, 292 self.profile,
314 callback=self.psItemGetCb, 293 callback=self.psItemGetCb,
315 errback=self.psItemGetEb) 294 errback=self.psItemGetEb)
316 295
317 296
318 class Affiliations(base.CommandBase, NodeCommon): 297 class Affiliations(base.CommandBase):
319 298
320 def __init__(self, host): 299 def __init__(self, host):
321 NodeCommon.__init__(self, node_required=False) 300 base.CommandBase.__init__(self, host, 'affiliations', use_output=C.OUTPUT_DICT, use_pubsub=True, help=_(u'retrieve all affiliations on a service'))
322 base.CommandBase.__init__(self, host, 'affiliations', use_output=C.OUTPUT_DICT, help=_(u'retrieve all affiliations on a service')) 301 self.need_loop=True
323 self.need_loop=True 302
324 303 def add_parser_options(self):
325 def add_parser_options(self): 304 pass
326 NodeCommon.add_parser_options(self)
327 305
328 def psAffiliationsGetCb(self, affiliations): 306 def psAffiliationsGetCb(self, affiliations):
329 self.output(affiliations) 307 self.output(affiliations)
330 self.host.quit() 308 self.host.quit()
331 309
341 self.profile, 319 self.profile,
342 callback=self.psAffiliationsGetCb, 320 callback=self.psAffiliationsGetCb,
343 errback=self.psAffiliationsGetEb) 321 errback=self.psAffiliationsGetEb)
344 322
345 323
346 class Uri(base.CommandBase, NodeCommon): 324 class Uri(base.CommandBase):
347 325
348 def __init__(self, host): 326 def __init__(self, host):
349 base.CommandBase.__init__(self, host, 'uri', use_profile=False, help=_(u'build URI')) 327 base.CommandBase.__init__(self, host, 'uri', use_profile=False, use_pubsub_node_req=True, help=_(u'build URI'))
350 self.need_loop=True 328 self.need_loop=True
351 329
352 def add_parser_options(self): 330 def add_parser_options(self):
353 NodeCommon.add_parser_options(self)
354 self.parser.add_argument("-i", "--item", type=base.unicode_decoder, help=_(u"item to link")) 331 self.parser.add_argument("-i", "--item", type=base.unicode_decoder, help=_(u"item to link"))
355 self.parser.add_argument("-p", "--profile", type=base.unicode_decoder, default=C.PROF_KEY_DEFAULT, help=_(u"profile (used when no server is specified)")) 332 self.parser.add_argument("-p", "--profile", type=base.unicode_decoder, default=C.PROF_KEY_DEFAULT, help=_(u"profile (used when no server is specified)"))
356 333
357 def display_uri(self, jid_): 334 def display_uri(self, jid_):
358 uri_args = {u'type': 'pubsub'} 335 uri_args = {u'type': 'pubsub'}