comparison sat_frontends/jp/cmd_pubsub.py @ 2804:710de41da2f2

jp (pubsub/node): new "import" command, to publish many nodes from an XML file
author Goffi <goffi@goffi.org>
date Fri, 15 Feb 2019 22:13:43 +0100
parents d4a9a60bc650
children dfba1301e61c
comparison
equal deleted inserted replaced
2803:d4a9a60bc650 2804:710de41da2f2
311 {self.getKeyName(k): v for k, v in self.args.fields}, 311 {self.getKeyName(k): v for k, v in self.args.fields},
312 self.profile, 312 self.profile,
313 callback=self.psNodeConfigurationSetCb, 313 callback=self.psNodeConfigurationSetCb,
314 errback=self.psNodeConfigurationSetEb, 314 errback=self.psNodeConfigurationSetEb,
315 ) 315 )
316
317
318 class NodeImport(base.CommandBase):
319
320 def __init__(self, host):
321 super(NodeImport, self).__init__(
322 host,
323 "import",
324 use_pubsub=True,
325 pubsub_flags={C.NODE},
326 help=_(u"import raw XML to a node"),
327 )
328 self.need_loop = True
329
330 def add_parser_options(self):
331 self.parser.add_argument(
332 "--admin",
333 action="store_true",
334 help=_(u"do a pubsub admin request, needed to change publisher"),
335 )
336 self.parser.add_argument(
337 "import_file",
338 type=file,
339 help=_(u"path to the XML file with data to import. The file must contain "
340 u"whole XML of each item to import."),
341 )
342
343 def psItemsSendCb(self, item_ids):
344 self.disp(_(u'items published with id(s) {item_ids}').format(
345 item_ids=u', '.join(item_ids)))
346 self.host.quit()
347
348 def start(self):
349 try:
350 element, etree = xml_tools.etreeParse(self, self.args.import_file,
351 reraise=True)
352 except Exception as e:
353 from lxml.etree import XMLSyntaxError
354 if isinstance(e, XMLSyntaxError) and e.code == 5:
355 # we have extra content, this probaby means that item are not wrapped
356 # so we wrap them here and try again
357 self.args.import_file.seek(0)
358 xml_buf = "<import>" + self.args.import_file.read() + "</import>"
359 element, etree = xml_tools.etreeParse(self, xml_buf)
360
361 # we reverse element as we expect to have most recently published element first
362 # TODO: make this more explicit and add an option
363 element[:] = reversed(element)
364
365 if not all([i.tag == '{http://jabber.org/protocol/pubsub}item' for i in element]):
366 self.disp(
367 _(u"You are not using list of pubsub items, we can't import this file"),
368 error=True)
369 self.host.quit(C.EXIT_DATA_ERROR)
370
371 items = [etree.tostring(i, encoding="utf-8") for i in element]
372 if self.args.admin:
373 self.host.bridge.psAdminItemsSend(
374 self.args.service,
375 self.args.node,
376 items,
377 u"",
378 self.profile,
379 callback=partial(self.psItemsSendCb),
380 errback=partial(
381 self.errback,
382 msg=_(u"can't send item: {}"),
383 exit_code=C.EXIT_BRIDGE_ERRBACK,
384 ),
385 )
386 else:
387 self.disp(_(u"Items are imported without using admin mode, publisher can't "
388 u"be changed"))
389 self.host.bridge.psItemsSend(
390 self.args.service,
391 self.args.node,
392 items,
393 u"",
394 self.profile,
395 callback=partial(self.psItemsSendCb),
396 errback=partial(
397 self.errback,
398 msg=_(u"can't send item: {}"),
399 exit_code=C.EXIT_BRIDGE_ERRBACK,
400 ),
401 )
316 402
317 403
318 class NodeAffiliationsGet(base.CommandBase): 404 class NodeAffiliationsGet(base.CommandBase):
319 def __init__(self, host): 405 def __init__(self, host):
320 base.CommandBase.__init__( 406 base.CommandBase.__init__(
694 NodeInfo, 780 NodeInfo,
695 NodeCreate, 781 NodeCreate,
696 NodePurge, 782 NodePurge,
697 NodeDelete, 783 NodeDelete,
698 NodeSet, 784 NodeSet,
785 NodeImport,
699 NodeAffiliations, 786 NodeAffiliations,
700 NodeSubscriptions, 787 NodeSubscriptions,
701 NodeSchema, 788 NodeSchema,
702 ) 789 )
703 790