diff 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
line wrap: on
line diff
--- a/frontends/src/jp/cmd_pubsub.py	Tue Jun 27 19:38:22 2017 +0200
+++ b/frontends/src/jp/cmd_pubsub.py	Tue Jun 27 19:38:22 2017 +0200
@@ -21,12 +21,15 @@
 import base
 from sat.core.i18n import _
 from sat_frontends.jp.constants import Const as C
+from sat_frontends.jp import common
 from functools import partial
 from sat.tools.common import uri
 from sat_frontends.tools import jid
 
 __commands__ = ["Pubsub"]
 
+PUBSUB_TMP_DIR = u"pubsub"
+
 
 class NodeInfo(base.CommandBase):
 
@@ -294,6 +297,50 @@
             errback=self.psItemsGetEb)
 
 
+class Edit(base.CommandBase, common.BaseEdit):
+
+    def __init__(self, host):
+        base.CommandBase.__init__(self, host, 'edit', use_verbose=True, use_pubsub_node_req=True, help=_(u'edit an existing or new pubsub item'))
+        common.BaseEdit.__init__(self, self.host, PUBSUB_TMP_DIR)
+
+    def add_parser_options(self):
+        self.parser.add_argument("item", type=base.unicode_decoder, nargs='?', default=u'new', help=_(u"URL of the item to edit, or keyword"))
+        common.BaseEdit.add_parser_options(self)
+
+    def edit(self, content_file_path, content_file_obj):
+        # we launch editor
+        self.runEditor("pubsub_editor_args", content_file_path, content_file_obj)
+
+    def publish(self, content):
+        published_id = self.host.bridge.psItemSend(self.pubsub_service, self.pubsub_node, content, self.pubsub_item or '', {}, self.profile)
+        if published_id:
+            self.disp(u"Item published at {pub_id}".format(pub_id=published_id))
+        else:
+            self.disp(u"Item published")
+
+    def getItemData(self, service, node, item):
+        try:
+            from lxml import etree
+        except ImportError:
+            self.disp(u"lxml module must be installed to use edit, please install it with \"pip install lxml\"", error=True)
+            self.host.quit(1)
+        items = [item] if item is not None else []
+        item_raw =  self.host.bridge.psItemsGet(service, node, 1, items, "", {}, self.profile)[0][0]
+        parser = etree.XMLParser(remove_blank_text=True)
+        item_elt = etree.fromstring(item_raw, parser)
+        try:
+            payload = item_elt[0]
+        except IndexError:
+            self.disp(_(u'Item has not payload'), 1)
+            return  u''
+        return etree.tostring(payload, encoding="unicode", pretty_print=True)
+
+    def start(self):
+        self.pubsub_service, self.pubsub_node, self.pubsub_item, content_file_path, content_file_obj = self.getItemPath(self.args.item)
+
+        self.edit(content_file_path, content_file_obj)
+
+
 class Affiliations(base.CommandBase):
 
     def __init__(self, host):
@@ -360,7 +407,7 @@
 
 
 class Pubsub(base.CommandBase):
-    subcommands = (Get, Node, Affiliations, Uri)
+    subcommands = (Get, Edit, Node, Affiliations, Uri)
 
     def __init__(self, host):
         super(Pubsub, self).__init__(host, 'pubsub', use_profile=False, help=_('PubSub nodes/items management'))