From f9d691fd26078e34be4b81563e393a0bb2cfcebc Mon Sep 17 00:00:00 2001 From: Goffi Date: Fri, 4 Mar 2016 10:33:28 +0100 Subject: [PATCH] jp (blog/edit): metadata file is reused if it already exists --- frontends/src/jp/cmd_blog.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/frontends/src/jp/cmd_blog.py b/frontends/src/jp/cmd_blog.py index 25f44882..294d5d95 100644 --- a/frontends/src/jp/cmd_blog.py +++ b/frontends/src/jp/cmd_blog.py @@ -117,20 +117,34 @@ class Edit(base.CommandBase): 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)