# HG changeset patch # User Goffi # Date 1457084008 -3600 # Node ID 1088bf7b28e7c49618bc822651e6a6949b109767 # Parent c55220fc03e4a6afa330d328acdd4d11ed3fb4b1 jp (blog/edit): metadata file is reused if it already exists diff -r c55220fc03e4 -r 1088bf7b28e7 frontends/src/jp/cmd_blog.py --- 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)