changeset 1876:1088bf7b28e7

jp (blog/edit): metadata file is reused if it already exists
author Goffi <goffi@goffi.org>
date Fri, 04 Mar 2016 10:33:28 +0100 (2016-03-04)
parents c55220fc03e4
children a97db84c048d
files frontends/src/jp/cmd_blog.py
diffstat 1 files changed, 17 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/src/jp/cmd_blog.py	Fri Mar 04 10:00:23 2016 +0100
+++ b/frontends/src/jp/cmd_blog.py	Fri Mar 04 10:33:28 2016 +0100
@@ -117,20 +117,34 @@
             and path to temporary metadata file
         """
         # we first construct metadata from edited item ones and CLI argumments
-        mb_data = {} if mb_data is None else mb_data.copy()
+        # or re-use the existing one if it exists
+        meta_file_path = os.path.splitext(content_file_path)[0] + METADATA_SUFF
+        if os.path.exists(meta_file_path):
+            self.disp(u"Metadata file already exists, we re-use it")
+            try:
+                with open(meta_file_path, 'rb') as f:
+                    mb_data = json.load(f)
+            except (OSError, IOError, ValueError) as e:
+                self.disp(u"Can't read existing metadata file at {path}, aborting: {reason}".format(
+                    path=meta_file_path, reason=e), error=True)
+                self.host.quit(1)
+        else:
+            mb_data = {} if mb_data is None else mb_data.copy()
+
+        # in all cases, we want to remove unwanted keys
         for key in KEY_TO_REMOVE_METADATA:
             try:
                 del mb_data[key]
             except KeyError:
                 pass
+        # and override metadata with command-line arguments
         mb_data['allow_comments'] = C.boolConst(not self.args.no_comment)
         if self.args.tag:
             common.iter2dict('tag', self.args.tag, mb_data)
         if self.args.title is not None:
             mb_data['title'] = self.args.title
 
-        # the we create the file and write metadata there, as JSON dict
-        meta_file_path = os.path.splitext(content_file_path)[0] + METADATA_SUFF
+        # then we create the file and write metadata there, as JSON dict
         # XXX: if we port jp one day on Windows, O_BINARY may need to be added here
         if os.path.exists(meta_file_path):
             self.disp(u"metadata file {} already exists, this should not happen! Cancelling...", error=True)