diff frontends/src/jp/common.py @ 2270:07caa12be945

jp (blog, common): added --force-item option to avoid magic and use argument as actual item id: - creation of new item with item id has been fixed - --force-item allow to use url as item id, or keyword - pubsub_item can is now returned by getItemPath. If not None, the id is known and must be used
author Goffi <goffi@goffi.org>
date Tue, 27 Jun 2017 17:00:52 +0200
parents 606ff34d30f2
children 5f0dbf42aa9c
line wrap: on
line diff
--- a/frontends/src/jp/common.py	Tue Jun 27 16:23:28 2017 +0200
+++ b/frontends/src/jp/common.py	Tue Jun 27 17:00:52 2017 +0200
@@ -85,6 +85,9 @@
         self.cat_dir = cat_dir.encode('utf-8')
         self.use_metadata = use_metadata
 
+    def add_parser_options(self):
+        self.parser.add_argument("--force-item", action='store_true', help=_(u"don't use magic and take item argument as an actual item"))
+
     def secureUnlink(self, path):
         """Unlink given path after keeping it for a while
 
@@ -299,12 +302,15 @@
                 - file path
                 - item id
         """
+        force_item = self.args.force_item
+        if force_item and not item:
+            self.parser.error(_(u"an item id must be specified if you use --force-item"))
         command = item.lower()
         pubsub_service = self.args.service
         pubsub_node = self.args.node
         pubsub_item = None
 
-        if command not in ('new', 'last', 'current'):
+        if not force_item and command not in ('new', 'last', 'current'):
             # we have probably an URL, we try to parse it
             import urlparse
             url = self.args.item
@@ -351,7 +357,7 @@
                 else:
                     command = 'new'
 
-        if command in ('new', 'last', 'edit'):
+        if not force_item and command in ('new', 'last', 'edit'):
             # we need a temporary file
             tmp_suff = '.' + self.getTmpSuff()
             content_file_obj, content_file_path = self.getTmpFile(tmp_suff)
@@ -374,12 +380,12 @@
         else:
             if self.use_metadata:
                 metadata = None
-            if command == 'current':
+            if not force_item and command == 'current':
                 # user wants to continue current draft
                 content_file_path = self.getCurrentFile(self.profile)
                 self.disp(u'Continuing edition of current draft', 2)
                 content_file_obj = open(content_file_path, 'r+b')
-            elif os.path.isfile(self.args.item):
+            elif not force_item and os.path.isfile(self.args.item):
                 # there is an existing draft that we use
                 content_file_path = os.path.expanduser(self.args.item)
                 content_file_obj = open(content_file_path, 'r+b')
@@ -387,15 +393,27 @@
                 # last chance, it should be an item
                 tmp_suff = '.' + self.getTmpSuff()
                 content_file_obj, content_file_path = self.getTmpFile(tmp_suff)
+                pubsub_item = self.args.item
 
-                if self.use_metadata:
-                    content, metadata = self.getItemData(pubsub_service, pubsub_node, self.args.item)
+                try:
+                    # we try to get existing item
+                    if self.use_metadata:
+                        content, metadata = self.getItemData(pubsub_service, pubsub_node, self.args.item)
+                    else:
+                        content = self.getItemData(pubsub_service, pubsub_node, self.args.item)
+                except Exception as e:
+                    # FIXME: ugly but we have not good may to check errors in bridge
+                    if u'item-not-found' in unicode(e):
+                        # item doesn't exist, we create a new one with requested id
+                        metadata = None
+                        self.disp(_(u'item "{item_id}" not found, we create a new item with this id').format(item_id=pubsub_item), 2)
                 else:
-                    content = self.getItemData(pubsub_service, pubsub_node, self.args.item)
-                content_file_obj.write(content.encode('utf-8'))
-                content_file_obj.seek(0)
+                    # item exists, we write content if content file
+                    content_file_obj.write(content.encode('utf-8'))
+                    content_file_obj.seek(0)
+                    self.disp(_(u'item "{item_id}" found, we edit it').format(item_id=pubsub_item), 2)
 
         if self.use_metadata:
-            return pubsub_service, pubsub_node, content_file_path, content_file_obj, metadata
+            return pubsub_service, pubsub_node, pubsub_item, content_file_path, content_file_obj, metadata
         else:
-            return pubsub_service, pubsub_node, content_file_path, content_file_obj
+            return pubsub_service, pubsub_node, pubsub_item, content_file_path, content_file_obj