comparison frontends/src/jp/cmd_pubsub.py @ 2275:64e99bf0dfa2

jp (pubsub/edit): new edit subcommand, which work in a same way as for blog
author Goffi <goffi@goffi.org>
date Tue, 27 Jun 2017 19:38:22 +0200
parents 27f469d40a83
children 5cd45a79775b
comparison
equal deleted inserted replaced
2274:27f469d40a83 2275:64e99bf0dfa2
19 19
20 20
21 import base 21 import base
22 from sat.core.i18n import _ 22 from sat.core.i18n import _
23 from sat_frontends.jp.constants import Const as C 23 from sat_frontends.jp.constants import Const as C
24 from sat_frontends.jp import common
24 from functools import partial 25 from functools import partial
25 from sat.tools.common import uri 26 from sat.tools.common import uri
26 from sat_frontends.tools import jid 27 from sat_frontends.tools import jid
27 28
28 __commands__ = ["Pubsub"] 29 __commands__ = ["Pubsub"]
30
31 PUBSUB_TMP_DIR = u"pubsub"
29 32
30 33
31 class NodeInfo(base.CommandBase): 34 class NodeInfo(base.CommandBase):
32 35
33 def __init__(self, host): 36 def __init__(self, host):
292 self.profile, 295 self.profile,
293 callback=self.psItemsGetCb, 296 callback=self.psItemsGetCb,
294 errback=self.psItemsGetEb) 297 errback=self.psItemsGetEb)
295 298
296 299
300 class Edit(base.CommandBase, common.BaseEdit):
301
302 def __init__(self, host):
303 base.CommandBase.__init__(self, host, 'edit', use_verbose=True, use_pubsub_node_req=True, help=_(u'edit an existing or new pubsub item'))
304 common.BaseEdit.__init__(self, self.host, PUBSUB_TMP_DIR)
305
306 def add_parser_options(self):
307 self.parser.add_argument("item", type=base.unicode_decoder, nargs='?', default=u'new', help=_(u"URL of the item to edit, or keyword"))
308 common.BaseEdit.add_parser_options(self)
309
310 def edit(self, content_file_path, content_file_obj):
311 # we launch editor
312 self.runEditor("pubsub_editor_args", content_file_path, content_file_obj)
313
314 def publish(self, content):
315 published_id = self.host.bridge.psItemSend(self.pubsub_service, self.pubsub_node, content, self.pubsub_item or '', {}, self.profile)
316 if published_id:
317 self.disp(u"Item published at {pub_id}".format(pub_id=published_id))
318 else:
319 self.disp(u"Item published")
320
321 def getItemData(self, service, node, item):
322 try:
323 from lxml import etree
324 except ImportError:
325 self.disp(u"lxml module must be installed to use edit, please install it with \"pip install lxml\"", error=True)
326 self.host.quit(1)
327 items = [item] if item is not None else []
328 item_raw = self.host.bridge.psItemsGet(service, node, 1, items, "", {}, self.profile)[0][0]
329 parser = etree.XMLParser(remove_blank_text=True)
330 item_elt = etree.fromstring(item_raw, parser)
331 try:
332 payload = item_elt[0]
333 except IndexError:
334 self.disp(_(u'Item has not payload'), 1)
335 return u''
336 return etree.tostring(payload, encoding="unicode", pretty_print=True)
337
338 def start(self):
339 self.pubsub_service, self.pubsub_node, self.pubsub_item, content_file_path, content_file_obj = self.getItemPath(self.args.item)
340
341 self.edit(content_file_path, content_file_obj)
342
343
297 class Affiliations(base.CommandBase): 344 class Affiliations(base.CommandBase):
298 345
299 def __init__(self, host): 346 def __init__(self, host):
300 base.CommandBase.__init__(self, host, 'affiliations', use_output=C.OUTPUT_DICT, use_pubsub=True, help=_(u'retrieve all affiliations on a service')) 347 base.CommandBase.__init__(self, host, 'affiliations', use_output=C.OUTPUT_DICT, use_pubsub=True, help=_(u'retrieve all affiliations on a service'))
301 self.need_loop=True 348 self.need_loop=True
358 else: 405 else:
359 self.display_uri(None) 406 self.display_uri(None)
360 407
361 408
362 class Pubsub(base.CommandBase): 409 class Pubsub(base.CommandBase):
363 subcommands = (Get, Node, Affiliations, Uri) 410 subcommands = (Get, Edit, Node, Affiliations, Uri)
364 411
365 def __init__(self, host): 412 def __init__(self, host):
366 super(Pubsub, self).__init__(host, 'pubsub', use_profile=False, help=_('PubSub nodes/items management')) 413 super(Pubsub, self).__init__(host, 'pubsub', use_profile=False, help=_('PubSub nodes/items management'))